test_zeros_op.py 4.2 KB
Newer Older
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15 16 17
# 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.

import unittest
import numpy as np
from op_test import OpTest
18
import paddle
19
import paddle.compat as cpt
20 21 22 23
import paddle.fluid.core as core
from paddle.fluid.op import Operator
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
24
from paddle.fluid.framework import _test_eager_guard
25 26


27
class TestZerosOpError(unittest.TestCase):
28

29 30
    def test_errors(self):
        with program_guard(Program(), Program()):
31
            shape = [4]
32
            dtype = 'int8'
33
            self.assertRaises(TypeError, fluid.layers.zeros, shape, dtype)
34

35 36 37 38
    def test_eager(self):
        with _test_eager_guard():
            self.test_errors()

39

40
class ApiZerosTest(unittest.TestCase):
41

42
    def test_out(self):
43
        with program_guard(Program()):
44
            zeros = paddle.zeros(shape=[10], dtype='float64')
45
            place = paddle.CPUPlace()
46
            exe = paddle.static.Executor(place)
47 48
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='float64')
49
        self.assertEqual((result == expected_result).all(), True)
50
        with paddle.static.program_guard(Program()):
51
            zeros = paddle.zeros(shape=[10], dtype='int64')
52
            place = paddle.CPUPlace()
53
            exe = paddle.static.Executor(place)
54 55
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='int64')
56
        self.assertEqual((result == expected_result).all(), True)
57
        with program_guard(Program()):
58
            zeros = paddle.zeros(shape=[10], dtype='int64')
59
            place = paddle.CPUPlace()
60
            exe = paddle.static.Executor(place)
61 62
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='int64')
63 64
        self.assertEqual((result == expected_result).all(), True)
        with program_guard(Program()):
65 66
            out_np = np.zeros(shape=1, dtype='float32')
            out = paddle.zeros(shape=[1], dtype='float32')
67
            place = paddle.CPUPlace()
68
            exe = paddle.static.Executor(place)
69 70 71 72 73
            result = exe.run(fetch_list=[out])
            self.assertEqual((result == out_np).all(), True)

    def test_fluid_out(self):
        with program_guard(Program()):
74
            zeros = fluid.layers.zeros(shape=[10], dtype='int64')
75
            place = paddle.CPUPlace()
76
            exe = paddle.static.Executor(place)
77 78
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='int64')
79 80
        self.assertEqual((result == expected_result).all(), True)

81 82 83 84 85
    def test_eager(self):
        with _test_eager_guard():
            self.test_out()
            self.test_fluid_out()

86 87

class ApiZerosError(unittest.TestCase):
88

89
    def test_errors(self):
90

91
        def test_error1():
92
            with paddle.static.program_guard(fluid.Program()):
93
                ones = fluid.layers.zeros(shape=10, dtype='int64')
94 95 96 97

        self.assertRaises(TypeError, test_error1)

        def test_error2():
98
            with paddle.static.program_guard(fluid.Program()):
99
                ones = fluid.layers.zeros(shape=[10], dtype='int8')
100 101 102

        self.assertRaises(TypeError, test_error2)

103 104 105 106 107 108 109 110 111
    def test_shape_errors(self):
        with fluid.dygraph.guard():
            try:
                shape = [-1, 5]
                out = paddle.zeros(shape)
            except Exception as e:
                error_msg = cpt.get_exception_message(e)
                assert error_msg.find("expected to be no less than 0") > 0

112 113 114
    def test_eager(self):
        with _test_eager_guard():
            self.test_errors()
115
            self.test_shape_errors()
116

117

118
if (__name__ == '__main__'):
119
    unittest.main()