Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【New IR】backward code of new ir #55957

Merged
merged 62 commits into from
Aug 9, 2023

Conversation

xiaoguoguo626807
Copy link
Contributor

@xiaoguoguo626807 xiaoguoguo626807 commented Aug 3, 2023

PR types

New features

PR changes

Others

Description

修改block.get_ops() 函数调用接口为 block.ops
新ir 自动微分模块初步开发:
完成x -> tanh -> mean -> out 子图的反向构建过程。初步验证检查逻辑;反向组网逻辑。

接口:
临时调用接口paddle.ir.grad 待验证完备后合并至paddle.grad接口

TODO:

  1. 待 add_n python api 及 add 反向vjp 可调用时验证梯度聚合相关逻辑
  2. 待concat, split python api 及 对应反向vjp 可调用时开发验证多输入/多输出反向添加流程:
    buildin combine + concat op. 对应concat vjp = concat gradop + split
    split op + buildin split. 对应split vjp = combine + split gradop
    3.待 验证剪枝逻辑 + no_grad_set用法

Others

pcard-67164

@xiaoguoguo626807 xiaoguoguo626807 changed the title backward origin code 【new ir】backward code of new ir Aug 8, 2023
@xiaoguoguo626807 xiaoguoguo626807 changed the title 【new ir】backward code of new ir 【New IR】backward code of new ir Aug 8, 2023
@@ -40,4 +41,7 @@
'OpResult',
'Type',
'translate_to_new_ir',
'grad',
'calc_gradient',
'calc_gradient_helper',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些接口应该不需要公开

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

from collections.abc import Sequence

import paddle.ir
from paddle.fluid.core import call_vjp, has_vjp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用paddle.framework.core 吧,fluid core逻辑已经迁移到 framework下了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return complete_outputs, complete_gradoutputs, backward_ops


def some_in_set(value_list, value_set):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该算法语义貌似本质上是判断两个集合交集是否为空,用集合操作可能更直观一些

 if set(operand2value(src_operand) ) & set(operand2value(dst_operand)):
    return True 
return False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion

def inverse_sort_op(ops):
'''
if topo graph is op1 -> op2 -> op3
return [ops, op2, op1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

拼写错误嘛,ops -> op3 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return sorted_list


def append_backward_ops(
Copy link
Contributor

@cxxly cxxly Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数建议有个比较详细的注释,说明函数主体逻辑

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@cxxly
Copy link
Contributor

cxxly commented Aug 9, 2023

backward.py相关逻辑放到autograd目录下是否更合适?个人认为,自动微分和IR不是强绑定关系

for input in inputs:
if input.get_defining_op().get_parent_block() != block:
raise ValueError(
"all inputts must be in the same block with outputs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"all inputts must be in the same block with outputs"
"all inputs must be in the same block with outputs"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

)


def update_no_grad_set_stopgradient(block, no_grad_set):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def update_no_grad_set_stopgradient(block, no_grad_set):
def update_no_grad_set(block, no_grad_set):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are two update_no_grad_set function, so add postfix to distinguish details

no_grad_set.add(value)


def update_all_structure(backward_ops, op_to_opgrad_list, gradop):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grad_op 是不是比gradop 更可读一些?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

):
"""
if grad_outputs is none, add fill_1 op to create grad_outputs
else check if outputs shape and dtype is same to grad_outputs, else raise error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else check if outputs shape and dtype is same to grad_outputs, else raise error
else check whether outputs shape and dtype is same to grad_outputs, otherwise raise error


if len(grad_outputs) != len(outputs):
raise ValueError(
"Should have the same number of grad_outputs as outputs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Should have the same number of grad_outputs as outputs"
"grad_outputs should have the same length as outputs."

from outputs to inputs add value not in the path to no_grad_set,
'''
inputs_set = set(inputs)
if inputs_set != []:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if inputs_set != []:
if inputs_set:

no_grad_set.add(value)

outputs_set = set(outputs)
no_grad_set_tmp = set()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要单独定义一个tmp,for 循环里直接 no_grad_set.add 就可以?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no_grad_set 在循环中会被用到,不能边修改边使用

add grad_op in order of topological sort
'''
for op in effective_forward_op:
if has_vjp(op):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个函数体不易过大,可以把if-else分支的逻辑封装成单独的函数

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处逻辑会配合多输入多输出算子做调整,后续统一整理

0.0,
dtype=value.dtype,
)
fillop = block.ops[len(block.ops) - 1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

if state.value_to_valuegrad[output] != []:
inputs_set.add(state.value_to_valuegrad[output][0][0])

no_gradvar_set = set() # no_grad_set 中前向对应的反向变量
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释应该都是英文~~

Copy link
Contributor

@Aurelius84 Aurelius84 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xiaoguoguo626807 xiaoguoguo626807 merged commit 0399b39 into PaddlePaddle:develop Aug 9, 2023
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants