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

[PIR]Using inplace batch norm in PIR #59752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/fluid/pir/dialect/op_generator/ops_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'generate_sequence_xpu',
'layer_norm_act_xpu',
'memcpy',
'batch_norm_',
'multi_encoder_xpu',
'multihead_matmul',
'squeeze_excitation_block',
Expand All @@ -104,7 +105,6 @@
'add_n_',
'add_n_with_kernel',
'assign_value',
'batch_norm_',
'c_allgather',
'c_allreduce_max',
'c_allreduce_sum',
Expand Down
33 changes: 25 additions & 8 deletions python/paddle/nn/layer/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
_global_flags,
get_default_dtype,
in_dynamic_or_pir_mode,
in_pir_mode,
no_grad,
)
from .. import functional as F
Expand Down Expand Up @@ -1056,7 +1057,7 @@ def __init__(
self._trainable_statistics = trainable_statistics

def forward(self, input):
if in_dynamic_or_pir_mode():
if in_dynamic_mode():
batch_norm_out, t1, t2, t3, t4, _ = _C_ops.batch_norm(
input,
self._mean,
Expand All @@ -1072,13 +1073,29 @@ def forward(self, input):
)
if self._act is None:
return batch_norm_out
if in_dynamic_mode():
return dygraph_utils._append_activation_in_dygraph(
batch_norm_out, act=self._act, use_mkldnn=self._use_mkldnn
)
else:
act_op = getattr(_C_ops, self._act)
return act_op(batch_norm_out)

return dygraph_utils._append_activation_in_dygraph(
batch_norm_out, act=self._act, use_mkldnn=self._use_mkldnn
)
elif in_pir_mode():
batch_norm_out, t1, t2, t3, t4, _ = _C_ops.batch_norm_(
input,
self._mean,
self._variance,
self.weight,
self.bias,
not self.training,
self._momentum,
self._epsilon,
self._data_layout,
self._use_global_stats,
self._trainable_statistics,
)
if self._act is None:
return batch_norm_out

act_op = getattr(_C_ops, self._act)
return act_op(batch_norm_out)
else:
# create output
# mean and mean_out share the same memory
Expand Down