Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
urhero
Paddle
提交
77051cc9
P
Paddle
项目概览
urhero
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
77051cc9
编写于
1月 12, 2021
作者:
F
furnace
提交者:
GitHub
1月 12, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add fp16 support for tril_triu op (#30186)
上级
efa54629
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
69 addition
and
35 deletion
+69
-35
paddle/fluid/operators/tril_triu_op.cc
paddle/fluid/operators/tril_triu_op.cc
+6
-2
paddle/fluid/operators/tril_triu_op.cu
paddle/fluid/operators/tril_triu_op.cu
+6
-2
paddle/fluid/operators/tril_triu_op.h
paddle/fluid/operators/tril_triu_op.h
+1
-0
python/paddle/fluid/tests/unittests/test_tril_triu_op.py
python/paddle/fluid/tests/unittests/test_tril_triu_op.py
+54
-29
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+2
-2
未找到文件。
paddle/fluid/operators/tril_triu_op.cc
浏览文件 @
77051cc9
...
...
@@ -99,6 +99,7 @@ class TrilTriuGradOpMaker : public framework::SingleGradOpMaker<T> {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OPERATOR
(
tril_triu
,
ops
::
TrilTriuOp
,
ops
::
TrilTriuOpMaker
,
ops
::
TrilTriuGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
TrilTriuGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
...
...
@@ -107,10 +108,13 @@ REGISTER_OP_CPU_KERNEL(
tril_triu
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
plat
::
float16
>
);
REGISTER_OP_CPU_KERNEL
(
tril_triu_grad
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
plat
::
float16
>
);
paddle/fluid/operators/tril_triu_op.cu
浏览文件 @
77051cc9
...
...
@@ -15,16 +15,20 @@ limitations under the License. */
#include "paddle/fluid/operators/tril_triu_op.h"
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_CUDA_KERNEL
(
tril_triu
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
);
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
,
ops
::
TrilTriuOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
plat
::
float16
>
);
REGISTER_OP_CUDA_KERNEL
(
tril_triu_grad
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
);
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
,
ops
::
TrilTriuGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
plat
::
float16
>
);
paddle/fluid/operators/tril_triu_op.h
浏览文件 @
77051cc9
...
...
@@ -15,6 +15,7 @@ limitations under the License. */
#pragma once
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/fluid/platform/for_range.h"
namespace
paddle
{
...
...
python/paddle/fluid/tests/unittests/test_tril_triu_op.py
浏览文件 @
77051cc9
...
...
@@ -16,8 +16,10 @@ from __future__ import print_function
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle
import
paddle.fluid
as
fluid
import
paddle.tensor
as
tensor
from
paddle.fluid.framework
import
Program
,
program_guard
class
TrilTriuOpDefaultTest
(
OpTest
):
...
...
@@ -68,6 +70,8 @@ def case_generator(op_type, Xshape, diagonal, expected):
class
FailureCase
(
unittest
.
TestCase
):
def
test_failure
(
self
):
paddle
.
enable_static
()
data
=
fluid
.
data
(
shape
=
Xshape
,
dtype
=
'float64'
,
name
=
cls_name
)
with
self
.
assertRaisesRegexp
(
eval
(
expected
.
split
(
':'
)[
-
1
]),
errmsg
[
expected
]):
...
...
@@ -75,6 +79,8 @@ def case_generator(op_type, Xshape, diagonal, expected):
class
SuccessCase
(
TrilTriuOpDefaultTest
):
def
initTestCase
(
self
):
paddle
.
enable_static
()
self
.
real_op_type
=
op_type
self
.
diagonal
=
diagonal
self
.
X
=
np
.
random
.
random
(
Xshape
).
astype
(
"float64"
)
...
...
@@ -120,8 +126,15 @@ class TestTrilTriuOpAPI(unittest.TestCase):
"""
def
test_api
(
self
):
data
=
np
.
random
.
random
([
1
,
9
,
9
,
4
]).
astype
(
'float32'
)
x
=
fluid
.
data
(
shape
=
[
1
,
9
,
-
1
,
4
],
dtype
=
'float32'
,
name
=
'x'
)
paddle
.
enable_static
()
dtypes
=
[
'float16'
,
'float32'
]
for
dtype
in
dtypes
:
prog
=
Program
()
startup_prog
=
Program
()
with
program_guard
(
prog
,
startup_prog
):
data
=
np
.
random
.
random
([
1
,
9
,
9
,
4
]).
astype
(
dtype
)
x
=
fluid
.
data
(
shape
=
[
1
,
9
,
-
1
,
4
],
dtype
=
dtype
,
name
=
'x'
)
tril_out
,
triu_out
=
tensor
.
tril
(
x
),
tensor
.
triu
(
x
)
place
=
fluid
.
CUDAPlace
(
0
)
if
fluid
.
core
.
is_compiled_with_cuda
(
...
...
@@ -135,16 +148,28 @@ class TestTrilTriuOpAPI(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
triu_out
,
np
.
triu
(
data
)))
def
test_api_with_dygraph
(
self
):
paddle
.
disable_static
()
dtypes
=
[
'float16'
,
'float32'
]
for
dtype
in
dtypes
:
with
fluid
.
dygraph
.
guard
():
data
=
np
.
random
.
random
([
1
,
9
,
9
,
4
]).
astype
(
'float32'
)
data
=
np
.
random
.
random
([
1
,
9
,
9
,
4
]).
astype
(
dtype
)
x
=
fluid
.
dygraph
.
to_variable
(
data
)
tril_out
,
triu_out
=
tensor
.
tril
(
x
).
numpy
(),
tensor
.
triu
(
x
).
numpy
()
tril_out
,
triu_out
=
tensor
.
tril
(
x
).
numpy
(),
tensor
.
triu
(
x
).
numpy
()
self
.
assertTrue
(
np
.
allclose
(
tril_out
,
np
.
tril
(
data
)))
self
.
assertTrue
(
np
.
allclose
(
triu_out
,
np
.
triu
(
data
)))
def
test_fluid_api
(
self
):
data
=
np
.
random
.
random
([
1
,
9
,
9
,
4
]).
astype
(
'float32'
)
x
=
fluid
.
data
(
shape
=
[
1
,
9
,
-
1
,
4
],
dtype
=
'float32'
,
name
=
'x'
)
paddle
.
enable_static
()
dtypes
=
[
'float16'
,
'float32'
]
for
dtype
in
dtypes
:
prog
=
Program
()
startup_prog
=
Program
()
with
program_guard
(
prog
,
startup_prog
):
data
=
np
.
random
.
random
([
1
,
9
,
9
,
4
]).
astype
(
dtype
)
x
=
fluid
.
data
(
shape
=
[
1
,
9
,
-
1
,
4
],
dtype
=
dtype
,
name
=
'x'
)
triu_out
=
fluid
.
layers
.
triu
(
x
)
place
=
fluid
.
CUDAPlace
(
0
)
if
fluid
.
core
.
is_compiled_with_cuda
(
...
...
python/paddle/tensor/creation.py
浏览文件 @
77051cc9
...
...
@@ -558,8 +558,8 @@ def _tril_triu_op(helper):
x
=
helper
.
kwargs
.
get
(
'x'
,
None
)
assert
x
is
not
None
,
'x cannot be None in {}'
.
format
(
op_type
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
op_type
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
op_type
)
if
len
(
x
.
shape
)
<
2
:
raise
ValueError
(
"x shape in {} must be at least 2-D"
.
format
(
op_type
))
diagonal
=
helper
.
kwargs
.
get
(
'diagonal'
,
0
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录