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

[Prim] fix loss of composite rule #52120

Merged
merged 17 commits into from
Mar 30, 2023

Conversation

cyber-pioneer
Copy link
Contributor

@cyber-pioneer cyber-pioneer commented Mar 24, 2023

PR types

Others

PR changes

Others

Describe

Pcard-66969
Fix bug: when op has been attached with prim vjp rule, recursive call of composite rule will be stopped (case:_to_prim(whole_program.blocks, whitelist=self.custom_vjps) ). For example, reduce_mean is non primitive operator, but it will not be processed in composite rule of batch_norm.
Support: to_prim support processing particular part of block.

@paddle-bot
Copy link

paddle-bot bot commented Mar 24, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@cyber-pioneer cyber-pioneer changed the title [Prim] fix_prim [Prim] fix loss of composite rule Mar 26, 2023
Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

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

Two comments, can be fixed in next pr

comp_flag = (lookup_fn(op_name) is not None) and filter_(op)
# Attr op_role will be set after grad op has been attached to origin op.
# Currently non primitive ops in prim vjp rule will not be processed here.
if op.desc.attr("op_role") == 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

comment to show what is op_role == 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deleted

@@ -1478,7 +1478,7 @@ def _out_grad_names(program_desc, fwd_end_op_index, out_size):
min(fwd_end_op_index + out_size, program_desc.block(0).op_size()),
):
op = program_desc.block(0).op(i)
if op.type() == 'fill_any_like':
if op.type() in ('fill_any_like', 'fill_constant'):
Copy link
Contributor

Choose a reason for hiding this comment

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

Add comment to show why we can need fill_constant here, better to add flag to show only in prim we need this.

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

@cyber-pioneer
Copy link
Contributor Author

Two comments, can be fixed in next pr

thanks

Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

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

some comments

block,
filter_: typing.Callable[[framework.Operator], bool] = lambda x: True,
start_idx=0,
backward_length=0,
Copy link
Contributor

Choose a reason for hiding this comment

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

Default value should be -1 or None

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

assert (
0 <= start_idx <= length
), f'expect 0 <= start_idx <= {length}, but got start_idx: {start_idx}'
assert not (
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

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 lookup_fn(op.type) is not None and filter_(op):

op_name = op.type
comp_flag = (
Copy link
Contributor

Choose a reason for hiding this comment

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

bad name, boolean should show its value

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

@@ -1264,8 +1264,10 @@ def before_append_backward(self, forward_program):
def after_append_backward(self, whole_program, backward_start_idx):
backward_length = len(whole_program.block(0).ops) - backward_start_idx
if core._is_fwd_prim_enabled() and len(self.custom_vjps) != 0:
_to_prim(whole_program.blocks, whitelist=self.custom_vjps)
_to_prim(whole_program.blocks, backward_length=backward_length)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add comments what does it means..

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

backward_length > 0 and start_idx > 0
), f'got start_idx: {start_idx} and backward_length: {backward_length}'
if backward_length > 0:
idx_list = range(length - backward_length)
Copy link
Contributor

Choose a reason for hiding this comment

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

comments

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 _to_prim(
blocks,
blacklist=frozenset(),
whitelist=frozenset(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid circular dependency. And this will be deleted later.

@cyber-pioneer cyber-pioneer force-pushed the fix_prim branch 2 times, most recently from 47d3eb5 to 2ba2af2 Compare March 30, 2023 03:14
@cyber-pioneer cyber-pioneer merged commit a4e0f66 into PaddlePaddle:develop Mar 30, 2023
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.

3 participants