Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6d7efd09
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
6d7efd09
编写于
8月 02, 2023
作者:
C
cyberslack_lee
提交者:
GitHub
8月 02, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add FP16 & BF16 for erfinv (#55287)
上级
19da5c0c
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
88 addition
and
11 deletion
+88
-11
paddle/phi/kernels/gpu/erfinv_grad_kernel.cu
paddle/phi/kernels/gpu/erfinv_grad_kernel.cu
+8
-2
paddle/phi/kernels/gpu/erfinv_kernel.cu
paddle/phi/kernels/gpu/erfinv_kernel.cu
+22
-1
paddle/phi/kernels/impl/erfinv_grad_kernel_impl.h
paddle/phi/kernels/impl/erfinv_grad_kernel_impl.h
+1
-1
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+4
-2
test/legacy_test/test_erfinv_op.py
test/legacy_test/test_erfinv_op.py
+53
-5
未找到文件。
paddle/phi/kernels/gpu/erfinv_grad_kernel.cu
浏览文件 @
6d7efd09
...
@@ -22,5 +22,11 @@
...
@@ -22,5 +22,11 @@
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/erfinv_grad_kernel_impl.h"
#include "paddle/phi/kernels/impl/erfinv_grad_kernel_impl.h"
PD_REGISTER_KERNEL
(
PD_REGISTER_KERNEL
(
erfinv_grad
,
erfinv_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
ErfinvGradKernel
,
float
,
double
)
{}
GPU
,
ALL_LAYOUT
,
phi
::
ErfinvGradKernel
,
float
,
double
,
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
)
{}
paddle/phi/kernels/gpu/erfinv_kernel.cu
浏览文件 @
6d7efd09
...
@@ -23,7 +23,21 @@ template <typename T>
...
@@ -23,7 +23,21 @@ template <typename T>
struct
ErfinvFunctor
{
struct
ErfinvFunctor
{
HOSTDEVICE
inline
T
operator
()(
const
T
x
)
const
{
return
erfinv
(
x
);
}
HOSTDEVICE
inline
T
operator
()(
const
T
x
)
const
{
return
erfinv
(
x
);
}
};
};
template
<
>
struct
ErfinvFunctor
<
float16
>
{
HOSTDEVICE
inline
float16
operator
()(
const
float16
x
)
const
{
auto
x_
=
static_cast
<
float
>
(
x
);
return
static_cast
<
float16
>
(
erfinv
(
x_
));
}
};
template
<
>
struct
ErfinvFunctor
<
bfloat16
>
{
HOSTDEVICE
inline
bfloat16
operator
()(
const
bfloat16
x
)
const
{
auto
x_
=
static_cast
<
float
>
(
x
);
return
static_cast
<
bfloat16
>
(
erfinv
(
x_
));
}
};
template
<
typename
T
,
typename
Context
>
template
<
typename
T
,
typename
Context
>
void
ErfinvKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
)
{
void
ErfinvKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
)
{
ctx
.
template
Alloc
<
T
>(
out
);
ctx
.
template
Alloc
<
T
>(
out
);
...
@@ -34,4 +48,11 @@ void ErfinvKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out) {
...
@@ -34,4 +48,11 @@ void ErfinvKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out) {
}
// namespace phi
}
// namespace phi
PD_REGISTER_KERNEL
(
erfinv
,
GPU
,
ALL_LAYOUT
,
phi
::
ErfinvKernel
,
float
,
double
)
{}
PD_REGISTER_KERNEL
(
erfinv
,
GPU
,
ALL_LAYOUT
,
phi
::
ErfinvKernel
,
float
,
double
,
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
)
{}
paddle/phi/kernels/impl/erfinv_grad_kernel_impl.h
浏览文件 @
6d7efd09
...
@@ -29,7 +29,7 @@ void ErfinvGradKernel(const Context& ctx,
...
@@ -29,7 +29,7 @@ void ErfinvGradKernel(const Context& ctx,
auto
eigen_dout
=
EigenVector
<
T
>::
Flatten
(
out_grad
);
auto
eigen_dout
=
EigenVector
<
T
>::
Flatten
(
out_grad
);
auto
eigen_dx
=
EigenVector
<
T
>::
Flatten
(
*
x_grad
);
auto
eigen_dx
=
EigenVector
<
T
>::
Flatten
(
*
x_grad
);
auto
&
place
=
*
ctx
.
eigen_device
();
auto
&
place
=
*
ctx
.
eigen_device
();
constexpr
T
half_sqrt_pi
=
static_cast
<
T
>
(
1
/
M_2_SQRTPI
);
T
half_sqrt_pi
=
static_cast
<
T
>
(
1
/
M_2_SQRTPI
);
eigen_dx
.
device
(
place
)
=
half_sqrt_pi
*
eigen_dout
*
eigen_out
.
square
().
exp
();
eigen_dx
.
device
(
place
)
=
half_sqrt_pi
*
eigen_dout
*
eigen_out
.
square
().
exp
();
}
}
...
...
python/paddle/tensor/math.py
浏览文件 @
6d7efd09
...
@@ -4760,7 +4760,7 @@ def erfinv(x, name=None):
...
@@ -4760,7 +4760,7 @@ def erfinv(x, name=None):
erfinv(erf(x)) = x.
erfinv(erf(x)) = x.
Args:
Args:
x (Tensor): An N-D Tensor, the data type is float32, float64.
x (Tensor): An N-D Tensor, the data type is float
16, bfloat16, float
32, float64.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
Returns:
Returns:
...
@@ -4779,7 +4779,9 @@ def erfinv(x, name=None):
...
@@ -4779,7 +4779,9 @@ def erfinv(x, name=None):
if
in_dynamic_mode
():
if
in_dynamic_mode
():
return
_C_ops
.
erfinv
(
x
)
return
_C_ops
.
erfinv
(
x
)
else
:
else
:
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'erfinv'
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
,
'float16'
,
'uint16'
],
'erfinv'
)
helper
=
LayerHelper
(
'erfinv'
,
**
locals
())
helper
=
LayerHelper
(
'erfinv'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
helper
.
append_op
(
type
=
'erfinv'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'erfinv'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
...
...
test/legacy_test/test_erfinv_op.py
浏览文件 @
6d7efd09
...
@@ -15,7 +15,11 @@
...
@@ -15,7 +15,11 @@
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
from
eager_op_test
import
OpTest
from
eager_op_test
import
(
OpTest
,
convert_float_to_uint16
,
convert_uint16_to_float
,
)
from
scipy.special
import
erfinv
from
scipy.special
import
erfinv
import
paddle
import
paddle
...
@@ -25,7 +29,7 @@ paddle.enable_static()
...
@@ -25,7 +29,7 @@ paddle.enable_static()
np
.
random
.
seed
(
0
)
np
.
random
.
seed
(
0
)
class
TestErfinv
(
OpTest
):
class
TestErfinv
Op
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"erfinv"
self
.
op_type
=
"erfinv"
self
.
python_api
=
paddle
.
erfinv
self
.
python_api
=
paddle
.
erfinv
...
@@ -55,12 +59,12 @@ class TestErfinv(OpTest):
...
@@ -55,12 +59,12 @@ class TestErfinv(OpTest):
)
)
class
TestErfinvFP
32
(
TestErfinv
):
class
TestErfinvFP
64Op
(
TestErfinvOp
):
def
init_dtype
(
self
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float
32
self
.
dtype
=
np
.
float
64
class
TestErfinvAPI
(
unittest
.
TestCase
):
class
TestErfinvAPI
Op
(
unittest
.
TestCase
):
def
init_dtype
(
self
):
def
init_dtype
(
self
):
self
.
dtype
=
'float32'
self
.
dtype
=
'float32'
...
@@ -110,5 +114,49 @@ class TestErfinvAPI(unittest.TestCase):
...
@@ -110,5 +114,49 @@ class TestErfinvAPI(unittest.TestCase):
run
(
place
)
run
(
place
)
class
TestErfinvFP16Op
(
TestErfinvOp
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
()
or
not
core
.
is_bfloat16_supported
(
core
.
CUDAPlace
(
0
)),
"core is not complied with CUDA and not support the bfloat16"
,
)
class
TestErfinvBF16Op
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"erfinv"
self
.
public_python_api
=
paddle
.
erfinv
self
.
python_api
=
paddle
.
erfinv
self
.
dtype
=
np
.
uint16
self
.
shape
=
[
11
,
17
]
self
.
datatype
=
np
.
float32
self
.
input_data
=
np
.
random
.
uniform
(
-
1
,
1
,
size
=
self
.
shape
).
astype
(
self
.
datatype
)
self
.
inputs
=
{
'X'
:
convert_float_to_uint16
(
self
.
input_data
)}
self
.
inputs_data
=
convert_uint16_to_float
(
self
.
inputs
[
'X'
])
out_ref
=
erfinv
(
self
.
input_data
)
self
.
grad_out
=
np
.
ones
(
self
.
shape
,
self
.
datatype
)
self
.
gradient
=
(
np
.
sqrt
(
np
.
pi
)
/
2
*
np
.
exp
(
np
.
square
(
out_ref
))
*
self
.
grad_out
)
self
.
outputs
=
{
'Out'
:
convert_float_to_uint16
(
out_ref
)}
def
test_check_output
(
self
):
place
=
core
.
CUDAPlace
(
0
)
self
.
check_output_with_place
(
place
)
def
test_check_grad
(
self
):
place
=
core
.
CUDAPlace
(
0
)
self
.
check_grad_with_place
(
place
,
[
'X'
],
'Out'
,
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录