Skip to content
Merged
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
1 change: 1 addition & 0 deletions paddle/phi/kernels/xpu/p_norm_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void PNormGradKernel(const Context& dev_ctx,
DenseTensor* x_grad) {
using XPUType = typename XPUTypeTrait<T>::Type;
dev_ctx.template Alloc<T>(x_grad);
if (x.numel() == 0) return;
auto xdim = x.dims();
axis = axis < 0 ? xdim.size() + axis : axis;
int m, t, n;
Expand Down
7 changes: 6 additions & 1 deletion paddle/phi/kernels/xpu/p_norm_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "paddle/phi/kernels/p_norm_kernel.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/kernels/full_kernel.h"
namespace phi {

inline void GetDims(const phi::DDim& dim,
Expand Down Expand Up @@ -50,6 +50,11 @@ void PNormKernel(const Context& dev_ctx,
DenseTensor* out) {
using XPUType = typename XPUTypeTrait<T>::Type;
dev_ctx.template Alloc<T>(out);
if (x.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
return;
}
auto xdim = x.dims();
if (axis < 0) axis = xdim.size() + axis;
std::vector<int64_t> r_dim;
Expand Down
67 changes: 67 additions & 0 deletions test/legacy_test/test_norm_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,73 @@ def err_dtype(p, shape_x, xdtype, out=None):
)


class API_NormTest_ZeroSize(unittest.TestCase):
def check_linalg_norm_dygraph_and_grad(
self, p, axis, shape_x, dtype, keep_dim, check_dim=False
):
x_numpy = (np.random.random(shape_x) + 1.0).astype(dtype)
expected_result = np_linalg_norm(
x_numpy, porder=p, axis=axis, keepdims=keep_dim
)
x_paddle = paddle.to_tensor(x_numpy)
x_paddle.stop_gradient = False
result1 = paddle.linalg.norm(
x=x_paddle, p=p, axis=axis, keepdim=keep_dim
)
result = result1.numpy()
np.testing.assert_allclose(
result, expected_result, rtol=1e-6, atol=1e-8
)
if keep_dim and check_dim:
np.testing.assert_equal(result.shape, expected_result.shape)
loss = paddle.sum(result1)
loss.backward()
np.testing.assert_equal(x_paddle.grad.shape, x_paddle.shape)

def check_linalg_vector_dygraph_and_grad(
self, p, axis, shape_x, dtype, keep_dim, check_dim=False
):
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
)
x_paddle = paddle.to_tensor(x_numpy)
x_paddle.stop_gradient = False
result1 = paddle.linalg.vector_norm(
x=x_paddle, p=p, axis=axis, keepdim=keep_dim
)
result = result1.numpy()
np.testing.assert_allclose(
result, expected_result, rtol=1e-6, atol=1e-8
)
if keep_dim and check_dim:
np.testing.assert_equal(result.shape, expected_result.shape)
loss = paddle.sum(result1)
loss.backward()
np.testing.assert_equal(x_paddle.grad.shape, x_paddle.shape)

def test_dygraph(self):
paddle.disable_static()
keep_dims = {False, True}
for keep in keep_dims:
self.check_linalg_norm_dygraph_and_grad(
p=1,
axis=[0, 1],
shape_x=[0, 3, 4],
dtype="float64",
keep_dim=keep,
check_dim=True,
)

self.check_linalg_vector_dygraph_and_grad(
p=np.inf,
axis=[1],
shape_x=[0, 3, 4],
dtype="float32",
keep_dim=keep,
)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
42 changes: 42 additions & 0 deletions test/legacy_test/test_pairwise_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,47 @@ def test_pairwise_distance_fp16(self):
static_ret = test_static(place, x_np, y_np)


class TestPairwiseDistance_ZeroSize(unittest.TestCase):
def test_pairwise_distance(self):
epsilon = 1e-6
all_shape = [[0], [100, 0]]
dtype = 'float32'
p = 0
places = []
if (
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
in ['1', 'true', 'on']
or not paddle.device.is_compiled_with_cuda()
):
places.append(paddle.CPUPlace())
if paddle.device.is_compiled_with_cuda():
places.append(paddle.CUDAPlace(0))
keeps = [False, True]
for place in places:
for shape in all_shape:
for keepdim in keeps:
x_np = np.random.random(shape).astype(dtype)
y_np = np.random.random(shape).astype(dtype)

excepted_value = np_pairwise_distance(
x_np, y_np, p, epsilon=epsilon, keepdim=keepdim
)
paddle.disable_static(place)
x = paddle.to_tensor(x_np)
x.stop_gradient = False
y = paddle.to_tensor(y_np)
ret = call_pairwise_distance_functional(
x=x, y=y, p=p, epsilon=epsilon, keepdim=keepdim
)
np.testing.assert_allclose(
ret.numpy(),
excepted_value,
rtol=1e-05,
)
loss = paddle.sum(ret)
loss.backward()
np.testing.assert_allclose(x.grad.shape, x.shape)


if __name__ == "__main__":
unittest.main()
Loading