Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion test/legacy_test/test_complex_view_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

import numpy as np
from op_test import OpTest

import paddle
from paddle import static
from paddle.base import dygraph
from paddle.base import core, dygraph

paddle.enable_static()

Expand Down Expand Up @@ -121,5 +122,33 @@ def test_static(self):
np.testing.assert_allclose(self.out, out_np, rtol=1e-05)


class TestViewAsRealAPI_ZeroSize(unittest.TestCase):
def get_places(self):
places = []
if (
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
in ['1', 'true', 'on']
or not core.is_compiled_with_cuda()
):
places.append(core.CPUPlace())
if core.is_compiled_with_cuda():
places.append(core.CUDAPlace(0))
return places

def setUp(self):
self.x = np.random.randn(10, 0) + 1j * np.random.randn(10, 0)
self.out = ref_view_as_real(self.x)

def test_dygraph(self):
for place in self.get_places():
with dygraph.guard(place):
x_tensor = paddle.to_tensor(self.x)
x_tensor.stop_gradient = False
out = paddle.as_real(x_tensor)
np.testing.assert_allclose(self.out, out.numpy(), rtol=1e-05)
out.sum().backward()
np.testing.assert_allclose(x_tensor.grad.shape, x_tensor.shape)


if __name__ == "__main__":
unittest.main()
24 changes: 24 additions & 0 deletions test/legacy_test/test_frac_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,29 @@ def test_dygraph_error(self):
self.assertRaises(TypeError, paddle.frac, x)


class TestFracAPI_ZeroSize(unittest.TestCase):
def set_dtype(self):
self.dtype = 'float64'

def setUp(self):
self.set_dtype()
self.x_np = np.random.random([0, 3]).astype(self.dtype)
self.place = (
paddle.CUDAPlace(0)
if core.is_compiled_with_cuda()
else paddle.CPUPlace()
)

def test_api_dygraph(self):
paddle.disable_static(self.place)
x = paddle.to_tensor(self.x_np)
x.stop_gradient = False
out = paddle.frac(x)
out_ref = ref_frac(self.x_np)
np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05)
out.sum().backward()
np.testing.assert_allclose(x.grad.shape, x.shape)


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