test_zeros_op.py 4.2 KB
Newer Older
1 2
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
# 
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
    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 41
class ApiZerosTest(unittest.TestCase):
    def test_out(self):
42
        with program_guard(Program()):
43
            zeros = paddle.zeros(shape=[10], dtype='float64')
44
            place = paddle.CPUPlace()
45
            exe = paddle.static.Executor(place)
46 47
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='float64')
48
        self.assertEqual((result == expected_result).all(), True)
49
        with paddle.static.program_guard(Program()):
50
            zeros = paddle.zeros(shape=[10], dtype='int64')
51
            place = paddle.CPUPlace()
52
            exe = paddle.static.Executor(place)
53 54
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='int64')
55
        self.assertEqual((result == expected_result).all(), True)
56
        with program_guard(Program()):
57
            zeros = paddle.zeros(shape=[10], dtype='int64')
58
            place = paddle.CPUPlace()
59
            exe = paddle.static.Executor(place)
60 61
            (result, ) = exe.run(fetch_list=[zeros])
            expected_result = np.zeros(10, dtype='int64')
62 63
        self.assertEqual((result == expected_result).all(), True)
        with program_guard(Program()):
64 65
            out_np = np.zeros(shape=1, dtype='float32')
            out = paddle.zeros(shape=[1], dtype='float32')
66
            place = paddle.CPUPlace()
67
            exe = paddle.static.Executor(place)
68 69 70 71 72
            result = exe.run(fetch_list=[out])
            self.assertEqual((result == out_np).all(), True)

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

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

85 86 87 88

class ApiZerosError(unittest.TestCase):
    def test_errors(self):
        def test_error1():
89
            with paddle.static.program_guard(fluid.Program()):
90
                ones = fluid.layers.zeros(shape=10, dtype='int64')
91 92 93 94

        self.assertRaises(TypeError, test_error1)

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

        self.assertRaises(TypeError, test_error2)

100 101 102 103 104 105 106 107 108
    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

109 110 111
    def test_eager(self):
        with _test_eager_guard():
            self.test_errors()
112
            self.test_shape_errors()
113

114

115
if (__name__ == '__main__'):
116
    unittest.main()