|
| 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 | + |
| 25 | +paddle.enable_static() |
| 26 | +SEED = 2021 |
| 27 | + |
| 28 | + |
| 29 | +@unittest.skipIf(not paddle.is_compiled_with_npu(), |
| 30 | + "core is not compiled with NPU") |
| 31 | +class TestReshape2(OpTest): |
| 32 | + def setUp(self): |
| 33 | + self.set_npu() |
| 34 | + self.op_type = "reshape2" |
| 35 | + self.place = paddle.NPUPlace(0) |
| 36 | + |
| 37 | + self.init_data() |
| 38 | + self.inputs = {"X": np.random.random(self.ori_shape).astype("float32")} |
| 39 | + self.attrs = {"shape": self.new_shape} |
| 40 | + self.outputs = { |
| 41 | + "Out": self.inputs["X"].reshape(self.infered_shape), |
| 42 | + 'XShape': np.random.random(self.ori_shape).astype("float32") |
| 43 | + } |
| 44 | + |
| 45 | + def set_npu(self): |
| 46 | + self.__class__.use_npu = True |
| 47 | + |
| 48 | + def init_data(self): |
| 49 | + self.ori_shape = (2, 60) |
| 50 | + self.new_shape = (12, 10) |
| 51 | + self.infered_shape = (12, 10) |
| 52 | + |
| 53 | + def test_check_output(self): |
| 54 | + self.check_output( |
| 55 | + self.place, check_dygraph=False, no_check_set=['XShape']) |
| 56 | + |
| 57 | + |
| 58 | +class TestReshape2_case2(TestReshape2): |
| 59 | + def init_data(self): |
| 60 | + self.ori_shape = (2, 60) |
| 61 | + self.new_shape = (-1, 10) |
| 62 | + self.infered_shape = (12, 10) |
| 63 | + |
| 64 | + |
| 65 | +class TestReshape2_case3(TestReshape2): |
| 66 | + def init_data(self): |
| 67 | + self.ori_shape = (2, 5, 6) |
| 68 | + self.new_shape = (-1, 0, 3) |
| 69 | + self.infered_shape = (4, 5, 3) |
| 70 | + |
| 71 | + |
| 72 | + # TODO(ascendrc): Add grad test |
| 73 | + # def test_check_grad(self): |
| 74 | + # if self.dtype == np.float16: |
| 75 | + # return |
| 76 | + # self.check_grad(['X'], 'Out') |
| 77 | + # |
| 78 | +@unittest.skipIf(not paddle.is_compiled_with_npu(), |
| 79 | + "core is not compiled with NPU") |
| 80 | +class TestReshapeNet(unittest.TestCase): |
| 81 | + def _test(self, run_npu=True): |
| 82 | + main_prog = paddle.static.Program() |
| 83 | + startup_prog = paddle.static.Program() |
| 84 | + main_prog.random_seed = SEED |
| 85 | + startup_prog.random_seed = SEED |
| 86 | + np.random.seed(SEED) |
| 87 | + |
| 88 | + a_np = np.random.random(size=(32, 32)).astype('float32') |
| 89 | + b_np = np.random.random(size=(32, 32)).astype('float32') |
| 90 | + label_np = np.random.randint(2, size=(32, 1)).astype('int64') |
| 91 | + |
| 92 | + with paddle.static.program_guard(main_prog, startup_prog): |
| 93 | + a = paddle.static.data(name="a", shape=[32, 32], dtype='float32') |
| 94 | + b = paddle.static.data(name="b", shape=[32, 32], dtype='float32') |
| 95 | + label = paddle.static.data( |
| 96 | + name="label", shape=[32, 1], dtype='int64') |
| 97 | + |
| 98 | + sum = paddle.add(a, b) |
| 99 | + z = paddle.reshape(sum, shape=[32, 32]) |
| 100 | + |
| 101 | + fc_1 = fluid.layers.fc(input=z, size=128) |
| 102 | + prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax') |
| 103 | + |
| 104 | + cost = fluid.layers.cross_entropy(input=prediction, label=label) |
| 105 | + loss = fluid.layers.reduce_mean(cost) |
| 106 | + sgd = fluid.optimizer.SGD(learning_rate=0.01) |
| 107 | + sgd.minimize(loss) |
| 108 | + |
| 109 | + if run_npu: |
| 110 | + place = paddle.NPUPlace(0) |
| 111 | + else: |
| 112 | + place = paddle.CPUPlace() |
| 113 | + |
| 114 | + exe = paddle.static.Executor(place) |
| 115 | + exe.run(startup_prog) |
| 116 | + |
| 117 | + print("Start run on {}".format(place)) |
| 118 | + for epoch in range(100): |
| 119 | + |
| 120 | + pred_res, loss_res = exe.run( |
| 121 | + main_prog, |
| 122 | + feed={"a": a_np, |
| 123 | + "b": b_np, |
| 124 | + "label": label_np}, |
| 125 | + fetch_list=[prediction, loss]) |
| 126 | + if epoch % 10 == 0: |
| 127 | + print("Epoch {} | Prediction[0]: {}, Loss: {}".format( |
| 128 | + epoch, pred_res[0], loss_res)) |
| 129 | + |
| 130 | + return pred_res, loss_res |
| 131 | + |
| 132 | + def test_npu(self): |
| 133 | + cpu_pred, cpu_loss = self._test(False) |
| 134 | + npu_pred, npu_loss = self._test(True) |
| 135 | + |
| 136 | + self.assertTrue(np.allclose(npu_pred, cpu_pred)) |
| 137 | + self.assertTrue(np.allclose(npu_loss, cpu_loss)) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == '__main__': |
| 141 | + unittest.main() |
0 commit comments