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

【易用性提升No.21】新增paddle.nn.functional.group_norm #60275

Closed
wants to merge 2 commits 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: 2 additions & 0 deletions python/paddle/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
layer_norm,
local_response_norm,
normalize,
group_norm,
)
from .pooling import (
adaptive_avg_pool1d,
Expand Down Expand Up @@ -262,6 +263,7 @@
'batch_norm',
'layer_norm',
'instance_norm',
'group_norm',
'class_center_sample',
'sparse_attention',
'fold',
Expand Down
55 changes: 55 additions & 0 deletions python/paddle/nn/functional/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,58 @@ def local_response_norm(
div = paddle.pow(div, beta)
res = paddle.divide(x, div, name=name)
return res


def group_norm(
x,
num_groups,
weight=None,
bias=None,
epsilon=1e-05,
data_format="NCHW",
name=None,
):
if data_format not in ['NCHW', 'NHWC']:
Copy link
Contributor

Choose a reason for hiding this comment

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

这个不能用于1d、3d吗

raise ValueError("unsupported data layout:" + data_format)
if in_dynamic_or_pir_mode():
return _C_ops.group_norm(
x,
weight,
bias,
epsilon,
num_groups,
data_format,
)
helper = LayerHelper('group_norm', **locals())
mean_out = helper.create_variable_for_type_inference(
dtype=x.dtype, stop_gradient=True
)
variance_out = helper.create_variable_for_type_inference(
dtype=x.dtype, stop_gradient=True
)

inputs = {'X': x}
if bias is not None:
inputs['Bias'] = bias
if weight is not None:
inputs['Scale'] = weight

# create output
group_norm_out = helper.create_variable_for_type_inference(dtype=x.dtype)

helper.append_op(
type="group_norm",
inputs=inputs,
outputs={
"Y": group_norm_out,
"Mean": mean_out,
"Variance": variance_out,
},
attrs={
"epsilon": epsilon,
"groups": num_groups,
"data_layout": data_format,
},
)

return helper.append_activation(group_norm_out)
16 changes: 14 additions & 2 deletions test/legacy_test/test_group_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def group_norm_wrapper(
):
if data_format == "AnyLayout":
data_format = "NCDHW"
return paddle._C_ops.group_norm(
input, weight, bias, epsilon, num_groups, data_format
return paddle.nn.functional.group_norm(
input, num_groups, weight, bias, epsilon, data_format
)


Expand Down Expand Up @@ -525,6 +525,18 @@ def attr_data_format():

self.assertRaises(ValueError, attr_data_format)

def test_func_dataformat_exception(self):
paddle.disable_static()
data = paddle.rand([1, 3, 3, 4], dtype="float64")

def func_data_format():
out = paddle.nn.functional.group_norm(
data, 2, None, None, 1e-5, data_format="NDHW"
)

self.assertRaises(ValueError, func_data_format)
paddle.enable_static()


class TestGroupNormEager(unittest.TestCase):
def test_dygraph_api(self):
Expand Down