Skip to content

Commit

Permalink
[Zero-Dim] support input 0D Tensor for std/var (#49735)
Browse files Browse the repository at this point in the history
* add test_std

* add test_var

* fix std/var assertequal

* fix std/var assertequal

* fix std/var assertequal

* -madd api name to reduce_api

* fix

* fix var

* fix

* fix

* fix stat

* fix unitest

* fix stat/var

* fix stat/var, unittest

* fix stat/std, unittest

* add unittest of var,std, fix stat/var,std

* fix stat/var, unittest

* fix

* fix unittest

* fix

* fix

* fix

* fix unittest
  • Loading branch information
mhy-666 authored Feb 10, 2023
1 parent efef303 commit 86cc694
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
81 changes: 81 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 @@ -622,6 +622,45 @@ def test_median(self):
self.assertEqual(x.grad.shape, [])
np.testing.assert_allclose(x.grad, 3.0)

def test_std(self):
x = paddle.rand([])
x.stop_gradient = False
out1 = paddle.std(x)
out2 = paddle.std(x, [])
out1.backward()
out2.backward()

# checkout shape of out
self.assertEqual(out1.shape, [])
self.assertEqual(out2.shape, [])

# checkout value of out
self.assertEqual(out1, 0)
self.assertEqual(out2, 0)

# checkout backward
self.assertEqual(x.grad.shape, [])

def test_var(self):
x = paddle.rand([])
x.stop_gradient = False
out1 = paddle.var(x)
out2 = paddle.var(x, [])
out1.backward()
out2.backward()

# checkout shape of out
self.assertEqual(out1.shape, [])
self.assertEqual(out2.shape, [])

# checkout value of out
self.assertEqual(out1, 0)
self.assertEqual(out2, 0)

# checkout backward
self.assertEqual(x.grad.shape, [])
np.testing.assert_allclose(x.grad, 0)

def test_quantile(self):
# 1) x is 0D
x = paddle.rand([])
Expand Down Expand Up @@ -1708,6 +1747,48 @@ def test_median(self):
self.assertEqual(res[2].shape, ())
np.testing.assert_allclose(res[2], 1.0)

@prog_scope()
def test_std(self):
x = paddle.rand([])
out1 = paddle.std(x)
out2 = paddle.std(x, [])
paddle.static.append_backward(out1)
paddle.static.append_backward(out2)

prog = paddle.static.default_main_program()
res = self.exe.run(
prog,
fetch_list=[
x,
out1,
out2,
],
)
self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, ())
self.assertEqual(res[2].shape, ())

@prog_scope()
def test_var(self):
x = paddle.rand([])
out1 = paddle.var(x)
out2 = paddle.var(x, [])
paddle.static.append_backward(out1)
paddle.static.append_backward(out2)

prog = paddle.static.default_main_program()
res = self.exe.run(
prog,
fetch_list=[
x,
out1,
out2,
],
)
self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, ())
self.assertEqual(res[2].shape, ())

@prog_scope()
def test_quantile(self):
x1 = paddle.rand([])
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/tensor/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None):
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'var')

u = mean(x, axis, True, name)
out = paddle.sum((x - u) ** 2, axis, keepdim=keepdim, name=name)
out = paddle.sum(paddle.pow((x - u), 2), axis, keepdim=keepdim, name=name)

dtype = x.dtype
n = paddle.cast(paddle.numel(x), paddle.int64) / paddle.cast(
Expand Down Expand Up @@ -212,7 +212,6 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None):
"""
if not in_dygraph_mode():
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'std')

out = var(**locals())
return paddle.sqrt(out)

Expand Down

0 comments on commit 86cc694

Please sign in to comment.