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

【BUPT】[Paddle Tensor 第二期 API 鲁棒性增强] paddle.linalg.vector_norm API 鲁棒性增强 #70499

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Fix bug of paddle.linalg.vector_norm and add test
  • Loading branch information
nizne9 committed Dec 26, 2024
commit 3280d85b9dd53b9123166fabb205ee70565307d4
13 changes: 2 additions & 11 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3320,17 +3320,8 @@ void PNormInferMeta(const MetaTensor& x,
auto x_dim = x.dims();
auto x_rank = x_dim.size();

PADDLE_ENFORCE_GE(axis,
-x_rank,
errors::InvalidArgument(
"Attr(axis) value should be in range [-R, R-1], R is "
"the rank of Input(X). But received axis: %d, R: %d. "
"Current Input(X)'s shape is=[%s].",
axis,
x_rank,
x_dim));
PADDLE_ENFORCE_LT(axis,
x_rank,
PADDLE_ENFORCE_EQ((axis >= -x_rank && axis < x_rank) || x_rank == 0,
true,
errors::InvalidArgument(
"Attr(axis) value should be in range [-R, R-1], R is "
"the rank of Input(X). But received axis: %d, R: %d. "
Expand Down
17 changes: 13 additions & 4 deletions python/paddle/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,15 @@ def vector_norm_axis_int(
if isinstance(axis, list) and len(axis) == 1:
axis = axis[0]

if paddle.is_complex(x):
abs_x = paddle.abs(x)
else:
abs_x = x

# when len(axis) == 1, use the original op to calculate
if isinstance(axis, int):
return vector_norm_axis_int(
x,
abs_x,
axis=axis,
porder=p,
keepdim=keepdim,
Expand All @@ -686,12 +691,16 @@ def vector_norm_axis_int(
# when len(axis) >= 1, calculate by combining other Python apis
elif isinstance(axis, list):
if p == np.inf or p == -np.inf:
return inf_norm(x, porder=p, axis=axis, keepdim=keepdim, name=name)
return inf_norm(
abs_x, porder=p, axis=axis, keepdim=keepdim, name=name
)
elif p == 0:
return zero_norm(x, porder=p, axis=axis, keepdim=keepdim, name=name)
return zero_norm(
abs_x, porder=p, axis=axis, keepdim=keepdim, name=name
)
else:
return vector_norm_axis_tuple(
x, porder=p, axis=axis, keepdim=keepdim, name=name
abs_x, porder=p, axis=axis, keepdim=keepdim, name=name
)


Expand Down
58 changes: 56 additions & 2 deletions test/legacy_test/test_norm_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def check_linalg_vector_static(
)
place = base.CPUPlace()
exe = base.Executor(place)
np_input = (np.random.rand(*shape_x) + 1.0).astype(dtype)
np_input = np.array(np.random.rand(*shape_x) + 1.0).astype(dtype)
expected_result = np_linalg_vector_norm(
np_input, porder=p, axis=axis, keepdims=keep_dim
).astype(dtype)
Expand All @@ -616,7 +616,7 @@ def check_linalg_vector_static(
def check_linalg_vector_dygraph(
self, p, axis, shape_x, dtype, keep_dim, check_dim=False
):
x_numpy = (np.random.random(shape_x) + 1.0).astype(dtype)
x_numpy = np.array(np.random.random(shape_x) + 1.0).astype(dtype)
expected_result = np_linalg_vector_norm(
x_numpy, porder=p, axis=axis, keepdims=keep_dim
)
Expand Down Expand Up @@ -909,6 +909,33 @@ def test_basic(self):
keep_dim=keep,
check_dim=True,
)
check_linalg_vector_static(
self,
p=2,
axis=None,
shape_x=[],
dtype="float64",
keep_dim=keep,
check_dim=True,
)
check_linalg_vector_static(
self,
p=np.inf,
axis=None,
shape_x=[],
dtype="complex64",
keep_dim=keep,
check_dim=True,
)
check_linalg_vector_static(
self,
p=-np.inf,
axis=[0, 1, 2, 3],
shape_x=[1, 14, 5, 14],
dtype="complex128",
keep_dim=keep,
check_dim=True,
)
check_linalg_matrix_static(
self,
p=-np.inf,
Expand Down Expand Up @@ -1237,6 +1264,33 @@ def test_dygraph(self):
keep_dim=keep,
check_dim=True,
)
check_linalg_vector_dygraph(
self,
p=2,
axis=None,
shape_x=(),
dtype="float64",
keep_dim=keep,
check_dim=True,
)
check_linalg_vector_dygraph(
self,
p=np.inf,
axis=None,
shape_x=[],
dtype="complex64",
keep_dim=keep,
check_dim=True,
)
check_linalg_vector_dygraph(
self,
p=-np.inf,
axis=[0, 1, 2, 3],
shape_x=[1, 14, 5, 14],
dtype="complex128",
keep_dim=keep,
check_dim=True,
)
check_linalg_matrix_dygraph(
self,
p=-np.inf,
Expand Down
Loading