|
| 1 | +# Copyright (c) 2022 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 | +from tests.op_test import OpTest |
| 21 | +import paddle |
| 22 | +import paddle.fluid as fluid |
| 23 | +from paddle.fluid import core |
| 24 | + |
| 25 | +paddle.enable_static() |
| 26 | +SEED = 2021 |
| 27 | + |
| 28 | + |
| 29 | +class TestSoftmax(OpTest): |
| 30 | + def setUp(self): |
| 31 | + self.set_npu() |
| 32 | + self.place = paddle.CustomPlace('ascend', 0) |
| 33 | + self.op_type = "softmax" |
| 34 | + self.init_dtype() |
| 35 | + |
| 36 | + x = np.random.random([3, 3]).astype(self.dtype) |
| 37 | + np_out = np.exp(x) / np.sum(np.exp(x), axis=1, keepdims=True) |
| 38 | + self.inputs = {'X': x} |
| 39 | + |
| 40 | + self.attrs = {} |
| 41 | + self.outputs = {'Out': np_out} |
| 42 | + |
| 43 | + def set_npu(self): |
| 44 | + self.__class__.use_custom_device = True |
| 45 | + self.__class__.no_need_check_grad = True |
| 46 | + |
| 47 | + def init_dtype(self): |
| 48 | + self.dtype = np.float32 |
| 49 | + |
| 50 | + def test_check_output(self): |
| 51 | + self.check_output_with_place(self.place) |
| 52 | + |
| 53 | + |
| 54 | +class TestSoftmaxNet(unittest.TestCase): |
| 55 | + def _test(self, run_npu=True): |
| 56 | + main_prog = paddle.static.Program() |
| 57 | + startup_prog = paddle.static.Program() |
| 58 | + main_prog.random_seed = SEED |
| 59 | + startup_prog.random_seed = SEED |
| 60 | + np.random.seed(SEED) |
| 61 | + |
| 62 | + a_np = np.random.random(size=(4, 32)).astype('float32') |
| 63 | + b_np = np.random.random(size=(4, 32)).astype('float32') |
| 64 | + label_np = np.random.randint(2, size=(4, 1)).astype('int64') |
| 65 | + |
| 66 | + with paddle.static.program_guard(main_prog, startup_prog): |
| 67 | + a = paddle.static.data(name="a", shape=[4, 32], dtype='float32') |
| 68 | + b = paddle.static.data(name="b", shape=[4, 32], dtype='float32') |
| 69 | + label = paddle.static.data( |
| 70 | + name="label", shape=[4, 1], dtype='int64') |
| 71 | + |
| 72 | + c = paddle.multiply(a, b) |
| 73 | + d = paddle.sqrt(c) |
| 74 | + |
| 75 | + # 4 x 128 |
| 76 | + fc_1 = fluid.layers.fc(input=d, size=128) |
| 77 | + # 4 x 2 |
| 78 | + prediction = fluid.layers.fc(input=fc_1, size=2) |
| 79 | + |
| 80 | + # 4 x 2 |
| 81 | + prob = fluid.layers.softmax(prediction, axis=1) |
| 82 | + |
| 83 | + cost = fluid.layers.cross_entropy(input=prob, label=label) |
| 84 | + loss = fluid.layers.mean(cost) |
| 85 | + sgd = fluid.optimizer.SGD(learning_rate=0.01) |
| 86 | + sgd.minimize(loss) |
| 87 | + |
| 88 | + if run_npu: |
| 89 | + place = paddle.CustomPlace('ascend', 0) |
| 90 | + else: |
| 91 | + place = paddle.CPUPlace() |
| 92 | + |
| 93 | + exe = paddle.static.Executor(place) |
| 94 | + exe.run(startup_prog) |
| 95 | + |
| 96 | + print("Start run on {}".format(place)) |
| 97 | + for epoch in range(100): |
| 98 | + |
| 99 | + pred_res, loss_res = exe.run( |
| 100 | + main_prog, |
| 101 | + feed={"a": a_np, |
| 102 | + "b": b_np, |
| 103 | + "label": label_np}, |
| 104 | + fetch_list=[prediction, loss]) |
| 105 | + if epoch % 10 == 0: |
| 106 | + print("Epoch {} | Prediction[0]: {}, Loss: {}".format( |
| 107 | + epoch, pred_res[0], loss_res)) |
| 108 | + |
| 109 | + return pred_res, loss_res |
| 110 | + |
| 111 | + def test_npu(self): |
| 112 | + cpu_pred, cpu_loss = self._test(False) |
| 113 | + npu_pred, npu_loss = self._test(True) |
| 114 | + |
| 115 | + self.assertTrue(np.allclose(npu_pred, cpu_pred, rtol=1e-2)) |
| 116 | + self.assertTrue(np.allclose(npu_loss, cpu_loss, rtol=1e-2)) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + unittest.main() |
0 commit comments