|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import unittest |
| 19 | +import sys |
| 20 | +sys.path.append("..") |
| 21 | +from op_test import OpTest |
| 22 | +import paddle |
| 23 | +import paddle.fluid as fluid |
| 24 | +from paddle.fluid import core |
| 25 | +import paddle.fluid.framework as framework |
| 26 | + |
| 27 | +paddle.enable_static() |
| 28 | +np.random.seed(10) |
| 29 | + |
| 30 | + |
| 31 | +class TestEyeOp(OpTest): |
| 32 | + def setUp(self): |
| 33 | + ''' |
| 34 | + Test eye op with specified shape |
| 35 | + ''' |
| 36 | + self.set_npu() |
| 37 | + self.place = paddle.NPUPlace(0) |
| 38 | + self.op_type = "eye" |
| 39 | + self.inputs = {} |
| 40 | + |
| 41 | + self.num_rows = 0 |
| 42 | + self.num_columns = 0 |
| 43 | + self.dtype = np.float32 |
| 44 | + |
| 45 | + self.initTestCase() |
| 46 | + |
| 47 | + if self.num_columns == 0: |
| 48 | + self.attrs = { |
| 49 | + 'num_rows': self.num_rows, |
| 50 | + 'dtype': framework.convert_np_dtype_to_dtype_(self.dtype) |
| 51 | + } |
| 52 | + self.outputs = {'Out': np.eye(self.num_rows, dtype=self.dtype)} |
| 53 | + else: |
| 54 | + self.attrs = { |
| 55 | + 'num_rows': self.num_rows, |
| 56 | + 'num_columns': self.num_columns, |
| 57 | + 'dtype': framework.convert_np_dtype_to_dtype_(self.dtype) |
| 58 | + } |
| 59 | + self.outputs = { |
| 60 | + 'Out': np.eye(self.num_rows, self.num_columns, dtype=self.dtype) |
| 61 | + } |
| 62 | + |
| 63 | + def initTestCase(self): |
| 64 | + self.num_rows = 219 |
| 65 | + self.num_columns = 319 |
| 66 | + self.dtype = np.int32 |
| 67 | + |
| 68 | + def set_npu(self): |
| 69 | + self.__class__.use_npu = True |
| 70 | + |
| 71 | + def test_check_output(self): |
| 72 | + self.check_output_with_place(self.place) |
| 73 | + |
| 74 | + |
| 75 | +class TestEyeOp1(TestEyeOp): |
| 76 | + def initTestCase(self): |
| 77 | + self.num_rows = 50 |
| 78 | + |
| 79 | + |
| 80 | +class TestEyeOp2(TestEyeOp): |
| 81 | + def initTestCase(self): |
| 82 | + self.num_rows = 50 |
| 83 | + self.dtype = np.int32 |
| 84 | + |
| 85 | + |
| 86 | +class TestEyeOp3(TestEyeOp): |
| 87 | + def initTestCase(self): |
| 88 | + self.num_rows = 50 |
| 89 | + self.dtype = np.float16 |
| 90 | + |
| 91 | + |
| 92 | +class TestEyeOp4(TestEyeOp): |
| 93 | + def initTestCase(self): |
| 94 | + self.num_rows = 1 |
| 95 | + self.num_columns = 99 |
| 96 | + |
| 97 | + |
| 98 | +class TestEyeOp5(TestEyeOp): |
| 99 | + def initTestCase(self): |
| 100 | + self.num_rows = 100 |
| 101 | + self.num_columns = 100 |
| 102 | + |
| 103 | + |
| 104 | +class TestEyeOp6(TestEyeOp): |
| 105 | + def initTestCase(self): |
| 106 | + self.num_rows = 100 |
| 107 | + self.num_columns = 100 |
| 108 | + self.dtype = np.float32 |
| 109 | + |
| 110 | + |
| 111 | +class API_TestTensorEye(unittest.TestCase): |
| 112 | + def test_out(self): |
| 113 | + with paddle.static.program_guard(paddle.static.Program()): |
| 114 | + data = paddle.eye(10) |
| 115 | + place = paddle.NPUPlace(0) |
| 116 | + exe = paddle.static.Executor(place) |
| 117 | + result, = exe.run(fetch_list=[data]) |
| 118 | + expected_result = np.eye(10, dtype="float32") |
| 119 | + self.assertEqual((result == expected_result).all(), True) |
| 120 | + |
| 121 | + with paddle.static.program_guard(paddle.static.Program()): |
| 122 | + data = paddle.eye(10, num_columns=7, dtype="float16") |
| 123 | + place = paddle.NPUPlace(0) |
| 124 | + exe = paddle.static.Executor(place) |
| 125 | + result, = exe.run(fetch_list=[data]) |
| 126 | + expected_result = np.eye(10, 7, dtype="float16") |
| 127 | + self.assertEqual((result == expected_result).all(), True) |
| 128 | + |
| 129 | + with paddle.static.program_guard(paddle.static.Program()): |
| 130 | + data = paddle.eye(10, dtype="int32") |
| 131 | + place = paddle.NPUPlace(0) |
| 132 | + exe = paddle.static.Executor(place) |
| 133 | + result, = exe.run(fetch_list=[data]) |
| 134 | + expected_result = np.eye(10, dtype="int32") |
| 135 | + self.assertEqual((result == expected_result).all(), True) |
| 136 | + |
| 137 | + paddle.disable_static(paddle.NPUPlace(0)) |
| 138 | + out = paddle.eye(10, dtype="int32") |
| 139 | + expected_result = np.eye(10, dtype="int32") |
| 140 | + paddle.enable_static() |
| 141 | + self.assertEqual((out.numpy() == expected_result).all(), True) |
| 142 | + |
| 143 | + paddle.disable_static(paddle.NPUPlace(0)) |
| 144 | + batch_shape = [2] |
| 145 | + out = fluid.layers.eye(10, 10, dtype="int32", batch_shape=batch_shape) |
| 146 | + result = np.eye(10, dtype="int32") |
| 147 | + expected_result = [] |
| 148 | + for index in reversed(batch_shape): |
| 149 | + tmp_result = [] |
| 150 | + for i in range(index): |
| 151 | + tmp_result.append(result) |
| 152 | + result = tmp_result |
| 153 | + expected_result = np.stack(result, axis=0) |
| 154 | + paddle.enable_static() |
| 155 | + self.assertEqual(out.numpy().shape == np.array(expected_result).shape, |
| 156 | + True) |
| 157 | + self.assertEqual((out.numpy() == expected_result).all(), True) |
| 158 | + |
| 159 | + paddle.disable_static(paddle.NPUPlace(0)) |
| 160 | + batch_shape = [3, 2] |
| 161 | + out = fluid.layers.eye(10, 10, dtype="int32", batch_shape=batch_shape) |
| 162 | + result = np.eye(10, dtype="int32") |
| 163 | + expected_result = [] |
| 164 | + for index in reversed(batch_shape): |
| 165 | + tmp_result = [] |
| 166 | + for i in range(index): |
| 167 | + tmp_result.append(result) |
| 168 | + result = tmp_result |
| 169 | + expected_result = np.stack(result, axis=0) |
| 170 | + paddle.enable_static() |
| 171 | + self.assertEqual(out.numpy().shape == np.array(expected_result).shape, |
| 172 | + True) |
| 173 | + self.assertEqual((out.numpy() == expected_result).all(), True) |
| 174 | + |
| 175 | + def test_errors(self): |
| 176 | + with paddle.static.program_guard(paddle.static.Program()): |
| 177 | + |
| 178 | + def test_num_rows_type_check(): |
| 179 | + paddle.eye(-1, dtype="int64") |
| 180 | + |
| 181 | + self.assertRaises(TypeError, test_num_rows_type_check) |
| 182 | + |
| 183 | + def test_num_columns_type_check(): |
| 184 | + paddle.eye(10, num_columns=5.2, dtype="int64") |
| 185 | + |
| 186 | + self.assertRaises(TypeError, test_num_columns_type_check) |
| 187 | + |
| 188 | + def test_num_columns_type_check1(): |
| 189 | + paddle.eye(10, num_columns=10, dtype="int8") |
| 190 | + |
| 191 | + self.assertRaises(TypeError, test_num_columns_type_check1) |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + unittest.main() |
0 commit comments