Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
dd74b3d1
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
dd74b3d1
编写于
4月 11, 2023
作者:
X
Xiaoxu Chen
提交者:
GitHub
4月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[prim]use Operator to reconstruct the primitive operator defined in c++ (#51997)
上级
6741dd22
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
129 addition
and
26 deletion
+129
-26
paddle/fluid/prim/utils/static/composite_grad_desc_maker.h
paddle/fluid/prim/utils/static/composite_grad_desc_maker.h
+3
-0
paddle/fluid/pybind/protobuf.cc
paddle/fluid/pybind/protobuf.cc
+2
-1
python/paddle/fluid/backward.py
python/paddle/fluid/backward.py
+51
-18
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+28
-7
python/paddle/fluid/tests/unittests/prim/test_comp_dispensable.py
...addle/fluid/tests/unittests/prim/test_comp_dispensable.py
+45
-0
未找到文件。
paddle/fluid/prim/utils/static/composite_grad_desc_maker.h
浏览文件 @
dd74b3d1
...
...
@@ -575,6 +575,9 @@ class CompositeGradOpMakerBase {
const
std
::
unordered_map
<
std
::
string
,
framework
::
Attribute
>&
RuntimeAttrs
()
const
{
LOG
(
WARNING
)
<<
"CompositeGradOpMaker doesn't support use runtime attrs, "
"but find the op"
<<
fwd_op_
.
Type
()
<<
"use runtime attr."
;
return
fwd_op_
.
GetRuntimeAttrMap
();
}
...
...
paddle/fluid/pybind/protobuf.cc
浏览文件 @
dd74b3d1
...
...
@@ -425,7 +425,8 @@ void BindOpDesc(pybind11::module *m) {
&
pd
::
OpDesc
::
SetDistAttr
,
pybind11
::
return_value_policy
::
reference
)
.
def
(
"inputs"
,
[](
pd
::
OpDesc
&
self
)
{
return
self
.
Inputs
();
})
.
def
(
"outputs"
,
&
pd
::
OpDesc
::
Outputs
);
.
def
(
"outputs"
,
&
pd
::
OpDesc
::
Outputs
)
.
def
(
"get_attr_map"
,
&
pd
::
OpDesc
::
GetAttrMap
);
pybind11
::
class_
<
paddle
::
experimental
::
Scalar
>
scalar
(
*
m
,
"Scalar"
,
""
);
scalar
.
def
(
py
::
init
<
bool
>
())
...
...
python/paddle/fluid/backward.py
浏览文件 @
dd74b3d1
...
...
@@ -1715,35 +1715,68 @@ def _append_backward_vars_(block, start_op_idx, grad_to_var, grad_info_map):
def
infershape_for_composite
(
block
,
grad_op_desc
):
# pruning empty output
# NOTE: why pruning the operator with empty output here ?
# Some backward operator will output emtpy var, which will cause infer
# shape error, such assign with input's stop_gradient=True
if
len
(
grad_op_desc
.
output_arg_names
())
==
0
:
return
# append op to block
op_desc
=
block
.
desc
.
append_op
()
op_desc
.
copy_from
(
grad_op_desc
)
op_desc
.
_set_attr
(
core
.
op_proto_and_checker_maker
.
kOpRoleAttrName
(),
core
.
op_proto_and_checker_maker
.
OpRole
.
Backward
,
)
# create output var
# create output variable
new_vars
=
set
()
# create new gradient variables
for
grad_var_name
in
op_desc
.
output_arg_names
():
for
grad_var_name
in
grad_op_desc
.
output_arg_names
():
if
not
(
block
.
desc
.
has_var_recursive
(
grad_var_name
.
encode
())
or
grad_var_name
==
core
.
empty_var_name
()
):
block
.
desc
.
var
(
grad_var_name
.
encode
())
# NOTE: stop_gradient will be set in append_op
desc
=
block
.
desc
.
var
(
grad_var_name
.
encode
())
block
.
create_var
(
name
=
grad_var_name
,
desc
=
desc
,
type
=
desc
.
type
())
new_vars
.
add
(
grad_var_name
)
# infer shape and infer dthype
op_desc
.
check_attrs
()
op_desc
.
infer_var_type
(
block
.
desc
)
op_desc
.
infer_shape
(
block
.
desc
)
# NOTE For the primitive operator generated by decompositing phi grad kernel,
# we Operator to reconstruct the op_desc for reusing some complex logic, such
# as processing dispensable input, intermediate output, extra attrs, etc...
if
framework
.
OpProtoHolder
.
instance
().
has_op_proto
(
grad_op_desc
.
type
()):
op
=
block
.
append_op
(
type
=
grad_op_desc
.
type
(),
inputs
=
{
name
:
[
block
.
_find_var_recursive
(
arg
)
for
arg
in
args
]
for
name
,
args
in
grad_op_desc
.
inputs
().
items
()
},
outputs
=
{
name
:
[
block
.
_find_var_recursive
(
arg
)
for
arg
in
args
]
for
name
,
args
in
grad_op_desc
.
outputs
().
items
()
},
# NOTE Runtime attr will be ignore as the c++ GetRuntimeAttr
# interface cann't be exported to python. Please note the WARNNING
# message logged in RuntimeAttrs of composite_grad_desc_maker.h
attrs
=
grad_op_desc
.
get_attr_map
(),
)
op
.
desc
.
_set_attr
(
core
.
op_proto_and_checker_maker
.
kOpRoleAttrName
(),
core
.
op_proto_and_checker_maker
.
OpRole
.
Backward
,
)
grad_op_desc
.
copy_from
(
op
.
desc
)
# For the backward operator, we reuse the logic of _append_backward_var
else
:
op_desc
=
block
.
desc
.
append_op
()
op_desc
.
copy_from
(
grad_op_desc
)
op_desc
.
_set_attr
(
core
.
op_proto_and_checker_maker
.
kOpRoleAttrName
(),
core
.
op_proto_and_checker_maker
.
OpRole
.
Backward
,
)
op_desc
.
check_attrs
()
op_desc
.
infer_var_type
(
block
.
desc
)
op_desc
.
infer_shape
(
block
.
desc
)
for
arg
in
op_desc
.
output_arg_names
():
if
arg
in
new_vars
:
_infer_var_data_type_shape_
(
arg
,
block
)
grad_op_desc
.
copy_from
(
op_desc
)
for
arg
in
op_desc
.
output_arg_names
():
# NOTE: Some operator doesn't infer dtype correctly, this patch set the
# grad_var dtype same with corresponding forward variable.
for
arg
in
grad_op_desc
.
output_arg_names
():
if
arg
in
new_vars
:
_infer_var_data_type_shape_
(
arg
,
block
)
...
...
python/paddle/fluid/framework.py
浏览文件 @
dd74b3d1
...
...
@@ -2916,14 +2916,35 @@ class Operator:
for
m
in
proto
.
outputs
:
if
(
m
.
name
not
in
outputs
)
and
m
.
dispensable
:
continue
if
not
((
m
.
name
in
outputs
)
or
m
.
dispensable
):
raise
ValueError
(
(
"Incorrect setting for output(s) of "
"operator
\"
%s
\"
, should set: [%s]."
# FIXME: The outputs of primitive operator currently
# doesn't include intermediate output as it will be dropped
# in operator codegen, such as xshape output of reshape2.
# It will fixed when the operator codegen support
# intermediate output.
if
core
.
_is_bwd_prim_enabled
():
if
not
(
(
m
.
name
in
outputs
)
or
m
.
dispensable
or
m
.
intermediate
):
raise
ValueError
(
(
"Incorrect setting for output(s) of "
"operator
\"
%s
\"
, should set: [%s]."
)
%
(
type
,
m
.
name
)
)
%
(
type
,
m
.
name
)
)
else
:
if
not
((
m
.
name
in
outputs
)
or
m
.
dispensable
):
raise
ValueError
(
(
"Incorrect setting for output(s) of "
"operator
\"
%s
\"
, should set: [%s]."
)
%
(
type
,
m
.
name
)
)
for
out_proto
in
proto
.
outputs
:
if
out_proto
.
name
not
in
outputs
:
continue
...
...
python/paddle/fluid/tests/unittests/prim/test_comp_dispensable.py
0 → 100644
浏览文件 @
dd74b3d1
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
paddle
class
TestDispensable
(
unittest
.
TestCase
):
def
setUp
(
self
):
paddle
.
fluid
.
core
.
_set_prim_all_enabled
(
True
)
def
tearDown
(
self
):
paddle
.
fluid
.
core
.
_set_prim_all_enabled
(
False
)
def
test_dispensable
(
self
):
@
paddle
.
jit
.
to_static
def
f
(
x
):
return
paddle
.
split
(
x
,
num_or_sections
=
2
)
f
=
paddle
.
jit
.
to_static
(
f
)
x
=
paddle
.
rand
((
8
,))
x
.
stop_gradient
=
False
op
=
f
.
get_concrete_program
(
x
)[
1
].
backward_program
.
block
(
0
).
ops
[
-
1
]
self
.
assertEqual
(
op
.
attr
(
'op_role'
),
int
(
paddle
.
fluid
.
core
.
op_proto_and_checker_maker
.
OpRole
.
Backward
),
)
self
.
assertIn
(
'AxisTensor'
,
op
.
input_names
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录