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 18
# 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.

from __future__ import print_function
import unittest
import numpy as np
from op_test import OpTest
19
import paddle
20
import paddle.compat as cpt
21 22 23 24
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
25
from paddle.fluid.framework import _test_eager_guard
26 27


28
class TestZerosOpError(unittest.TestCase):
29

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

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

40

41
class ApiZerosTest(unittest.TestCase):
42

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

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

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

87 88

class ApiZerosError(unittest.TestCase):
89

90
    def test_errors(self):
91

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

        self.assertRaises(TypeError, test_error1)

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

        self.assertRaises(TypeError, test_error2)

104 105 106 107 108 109 110 111 112
    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

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

118

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