Skip to content

Commit 2fe5382

Browse files
authored
[0-size Tensor Job2 No.68、92] Add 0-size Tensor support for pow (#73204)
* Fix * Fix
1 parent c9cc49e commit 2fe5382

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/legacy_test/test_pow.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,41 @@ def x_dtype_error():
232232
self.assertRaises(TypeError, x_dtype_error)
233233

234234

235+
class TestPowerAPI_ZeroSize(unittest.TestCase):
236+
"""TestPowerAPI."""
237+
238+
def setUp(self):
239+
self.places = []
240+
if (
241+
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
242+
in ['1', 'true', 'on']
243+
or not core.is_compiled_with_cuda()
244+
):
245+
self.places.append('cpu')
246+
if core.is_compiled_with_cuda():
247+
self.places.append('gpu')
248+
249+
def _test_power(self, shape):
250+
np.random.seed(7)
251+
for place in self.places:
252+
dims = shape
253+
x = (np.random.rand(*dims) * 10).astype(np.float64)
254+
y = np.random.rand() * 10
255+
paddle.disable_static()
256+
paddle.set_device(place)
257+
x_ = paddle.to_tensor(x)
258+
x_.stop_gradient = False
259+
y_ = y
260+
res = paddle.pow(x_, y_)
261+
np.testing.assert_allclose(res, np.power(x, y), rtol=1e-05)
262+
loss = paddle.sum(res)
263+
loss.backward()
264+
np.testing.assert_allclose(x_.grad.shape, x_.shape)
265+
266+
def test_power(self):
267+
self._test_power((0, 2))
268+
self._test_power((0, 0))
269+
270+
235271
if __name__ == '__main__':
236272
unittest.main()

0 commit comments

Comments
 (0)