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

[Zero-Dim] add 0D support for trace #53208

Merged
merged 3 commits into from
Apr 24, 2023
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: 0 additions & 1 deletion paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4402,7 +4402,6 @@ void TraceInferMeta(
auto sizes = vectorize(x_dims);
if (x_dims.size() == 2) {
sizes.clear();
sizes.push_back(1);
} else {
sizes.erase(sizes.begin() + std::max(dim1_, dim2_));
sizes.erase(sizes.begin() + std::min(dim1_, dim2_));
Expand Down
5 changes: 4 additions & 1 deletion paddle/phi/kernels/gpu/trace_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ void TraceKernel(const Context& ctx,
auto diag = funcs::Diagonal<T, Context>(ctx, &x, offset, axis1, axis2);
if (diag.numel() > 0) {
std::vector<int> reduce_dims;
reduce_dims.push_back(out->dims().size());
// Adapt to 0D output
auto out_dim_size = out->dims().size();
if (out_dim_size == 0) out_dim_size = 1;
reduce_dims.push_back(out_dim_size);
funcs::ReduceKernel<T, T, kps::AddFunctor, kps::IdentityFunctor<T>>(
ctx, diag, out, kps::IdentityFunctor<T>(), reduce_dims);
} else {
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/impl/trace_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ void TraceGradKernel(const Context& ctx,
auto input_dims = in_grad->dims();
auto input_stride = phi::stride(input_dims);
auto output_dims = out_grad.dims();
auto output_stride = phi::stride(output_dims);
auto output_stride = output_dims.size() == 0 ? phi::DDim(output_dims)
: phi::stride(output_dims);

auto* out_data = out_grad.data<T>();
T* x_data = ctx.template Alloc<T>(in_grad);
Expand Down
24 changes: 24 additions & 0 deletions python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,16 @@ def test_multi_dot(self):
self.assertEqual(b.grad.shape, [4, 5])
self.assertEqual(c.grad.shape, [5])

def test_trace(self):
x = paddle.to_tensor([[3, 2], [1, 9]], dtype="float32")
x.stop_gradient = False
out = paddle.trace(x)
out.backward()

self.assertEqual(out.shape, [])
np.testing.assert_allclose(out, np.array(12))
self.assertEqual(x.grad.shape, [2, 2])


class TestSundryAPIStatic(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -4399,6 +4409,20 @@ def test_multi_dot(self):
self.assertEqual(res[2].shape, (4, 5))
self.assertEqual(res[3].shape, (5,))

@prog_scope()
def test_trace(self):
x = paddle.to_tensor([[3, 2], [1, 9]], dtype="float32")
x.stop_gradient = False
out = paddle.trace(x)
paddle.static.append_backward(out)

prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out, x.grad_name])

self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, (2, 2))
np.testing.assert_allclose(res[0], np.array(12))


# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
class TestNoBackwardAPI(unittest.TestCase):
Expand Down