test_tensor_methods.py 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
import unittest

17
import numpy as np
18

19 20 21 22 23 24 25 26 27 28 29 30
import paddle


@paddle.jit.to_static
def tensor_clone(x):
    x = paddle.to_tensor(x)
    y = x.clone()
    return y


class TestTensorClone(unittest.TestCase):
    def _run(self, to_static):
R
Ryan 已提交
31
        paddle.jit.enable_to_static(to_static)
32 33 34 35 36 37
        x = paddle.ones([1, 2, 3])
        return tensor_clone(x).numpy()

    def test_tensor_clone(self):
        dygraph_res = self._run(to_static=False)
        static_res = self._run(to_static=True)
38
        np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-05)
39 40 41 42 43 44 45 46 47 48 49


@paddle.jit.to_static
def tensor_numpy(x):
    x = paddle.to_tensor(x)
    x.clear_gradient()
    return x


class TestTensorDygraphOnlyMethodError(unittest.TestCase):
    def _run(self, to_static):
R
Ryan 已提交
50
        paddle.jit.enable_to_static(to_static)
51 52 53 54 55 56 57 58 59 60
        x = paddle.zeros([2, 2])
        y = tensor_numpy(x)
        return y.numpy()

    def test_to_static_numpy_report_error(self):
        dygraph_res = self._run(to_static=False)
        with self.assertRaises(AssertionError):
            static_res = self._run(to_static=True)


61 62 63 64 65 66 67 68 69
@paddle.jit.to_static
def tensor_item(x):
    x = paddle.to_tensor(x)
    y = x.clone()
    return y.item()


class TestTensorItem(unittest.TestCase):
    def _run(self, to_static):
R
Ryan 已提交
70
        paddle.jit.enable_to_static(to_static)
71 72 73 74 75 76 77 78 79 80 81
        x = paddle.ones([1])
        if to_static:
            return tensor_item(x).numpy()
        return tensor_item(x)

    def test_tensor_clone(self):
        dygraph_res = self._run(to_static=False)
        static_res = self._run(to_static=True)
        np.testing.assert_allclose(dygraph_res, static_res)


82 83 84 85 86 87 88 89 90 91
@paddle.jit.to_static
def tensor_size(x):
    x = paddle.to_tensor(x)
    x = paddle.reshape(x, paddle.shape(x))  # dynamic shape
    y = x.size
    return y


class TestTensorSize(unittest.TestCase):
    def _run(self, to_static):
R
Ryan 已提交
92
        paddle.jit.enable_to_static(to_static)
93
        x = paddle.ones([1, 2, 3])
94
        if not to_static:
95 96 97 98 99 100 101 102 103
            return tensor_size(x)
        return tensor_size(x).numpy()

    def test_tensor_clone(self):
        dygraph_res = self._run(to_static=False)
        static_res = self._run(to_static=True)
        np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-5)


104 105
if __name__ == '__main__':
    unittest.main()