diff --git a/paddle/fluid/operators/bce_loss_op_npu.cc b/paddle/fluid/operators/bce_loss_op_npu.cc deleted file mode 100644 index ee4aebbea01d8e0951f40f334610a58312fe9f1a..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/bce_loss_op_npu.cc +++ /dev/null @@ -1,77 +0,0 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. - -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 - - http://www.apache.org/licenses/LICENSE-2.0 - -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. */ - -#include "paddle/fluid/operators/bce_loss_op.h" -#include "paddle/fluid/operators/npu_op_runner.h" - -namespace paddle { -namespace operators { - -using Tensor = framework::Tensor; - -template -class BCELossNPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - auto* x = ctx.Input("X"); - auto* labels = ctx.Input("Label"); - auto* out = ctx.Output("Out"); - - out->mutable_data(ctx.GetPlace()); - - auto stream = - ctx.template device_context() - .stream(); - - const auto& runner = NpuOpRunner("BinaryCrossEntropy", {*x, *labels}, - {*out}, {{"reduction", "none"}}); - runner.Run(stream); - } -}; - -template -class BCELossGradNPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - auto* x = ctx.Input("X"); - auto* labels = ctx.Input("Label"); - auto* dout = ctx.Input(framework::GradVarName("Out")); - auto* dx = ctx.Output(framework::GradVarName("X")); - - dx->mutable_data(ctx.GetPlace()); - - auto stream = - ctx.template device_context() - .stream(); - - const auto& runner = - NpuOpRunner("BinaryCrossEntropyGrad", {*x, *labels, *dout}, {*dx}, - {{"reduction", "none"}}); - runner.Run(stream); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -REGISTER_OP_NPU_KERNEL( - bce_loss, ops::BCELossNPUKernel, - ops::BCELossNPUKernel); - -REGISTER_OP_NPU_KERNEL( - bce_loss_grad, ops::BCELossGradNPUKernel, - ops::BCELossGradNPUKernel); diff --git a/paddle/fluid/operators/controlflow/logical_op_npu.cc b/paddle/fluid/operators/controlflow/logical_op_npu.cc index b452bee747232d67389bd253de4802ee02cae811..b9807bfa53e1e116089f5a593d69f5110b0b8f10 100644 --- a/paddle/fluid/operators/controlflow/logical_op_npu.cc +++ b/paddle/fluid/operators/controlflow/logical_op_npu.cc @@ -12,6 +12,10 @@ 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. */ +#ifdef PADDLE_WITH_ASCEND_CL +#include +#include + #include "paddle/fluid/operators/controlflow/logical_op.h" #include "paddle/fluid/operators/npu_op_runner.h" @@ -25,53 +29,18 @@ class LogicalNotNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); - auto* out = ctx.Output("Out"); - - out->mutable_data(ctx.GetPlace()); - - auto stream = - ctx.template device_context() - .stream(); - - const auto& runner = NpuOpRunner("LogicalNot", {*x}, {*out}, {}); - runner.Run(stream); - } -}; - -template -class LogicalOrNPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - auto* x = ctx.Input("X"); - auto* y = ctx.Input("Y"); - auto* out = ctx.Output("Out"); - - out->mutable_data(ctx.GetPlace()); - - auto stream = - ctx.template device_context() - .stream(); - const auto& runner = NpuOpRunner("LogicalOr", {*x, *y}, {*out}, {}); - runner.Run(stream); - } -}; + auto* out = ctx.Output("Out"); -template -class LogicalAndPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - auto* x = ctx.Input("X"); - auto* y = ctx.Input("Y"); - auto* out = ctx.Output("Out"); + auto place = ctx.GetPlace(); - out->mutable_data(ctx.GetPlace()); + out->mutable_data(place); auto stream = ctx.template device_context() .stream(); - const auto& runner = NpuOpRunner("LogicalAnd", {*x, *y}, {*out}, {}); + const auto& runner = NpuOpRunner("LogicalNot", {*x}, {*out}, {}); runner.Run(stream); } }; @@ -80,13 +49,9 @@ class LogicalAndPUKernel : public framework::OpKernel { } // namespace paddle namespace ops = paddle::operators; -namespace plat = paddle::platform; - -REGISTER_OP_NPU_KERNEL(logical_not, - ops::LogicalNotNPUKernel); -REGISTER_OP_NPU_KERNEL(logical_or, - ops::LogicalOrNPUKernel); +REGISTER_OP_NPU_KERNEL( + logical_not, + ops::LogicalNotNPUKernel); -REGISTER_OP_NPU_KERNEL(logical_and, - ops::LogicalAndPUKernel); +#endif diff --git a/python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py b/python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py deleted file mode 100644 index eb00e777fe0eef3811c9749d477677ce794d095d..0000000000000000000000000000000000000000 --- a/python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py +++ /dev/null @@ -1,266 +0,0 @@ -# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -# -# 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 -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# 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, division - -import paddle -import paddle.fluid as fluid -import numpy as np -import unittest -import sys -sys.path.append("..") -from op_test import OpTest - -paddle.enable_static() - - -def test_static_layer(place, - input_np, - label_np, - reduction='mean', - weight_np=None): - prog = paddle.static.Program() - startup_prog = paddle.static.Program() - with paddle.static.program_guard(prog, startup_prog): - input = paddle.fluid.data( - name='input', shape=input_np.shape, dtype='float32') - label = paddle.fluid.data( - name='label', shape=label_np.shape, dtype='float32') - if weight_np is not None: - weight = paddle.fluid.data( - name='weight', shape=weight_np.shape, dtype='float32') - bce_loss = paddle.nn.loss.BCELoss( - weight=weight, reduction=reduction) - else: - bce_loss = paddle.nn.loss.BCELoss(reduction=reduction) - res = bce_loss(input, label) - exe = paddle.static.Executor(place) - static_result = exe.run(prog, - feed={"input": input_np, - "label": label_np} - if weight_np is None else { - "input": input_np, - "label": label_np, - "weight": weight_np - }, - fetch_list=[res]) - return static_result - - -def test_static_functional(place, - input_np, - label_np, - reduction='mean', - weight_np=None): - prog = paddle.static.Program() - startup_prog = paddle.static.Program() - with paddle.static.program_guard(prog, startup_prog): - input = paddle.fluid.data( - name='input', shape=input_np.shape, dtype='float32') - label = paddle.fluid.data( - name='label', shape=label_np.shape, dtype='float32') - if weight_np is not None: - weight = paddle.fluid.data( - name='weight', shape=weight_np.shape, dtype='float32') - res = paddle.nn.functional.binary_cross_entropy( - input, label, weight=weight, reduction=reduction) - else: - res = paddle.nn.functional.binary_cross_entropy( - input, label, reduction=reduction) - exe = paddle.static.Executor(place) - static_result = exe.run(prog, - feed={"input": input_np, - "label": label_np} - if weight_np is None else { - "input": input_np, - "label": label_np, - "weight": weight_np - }, - fetch_list=[res]) - return static_result - - -def test_dygraph_layer(place, - input_np, - label_np, - reduction='mean', - weight_np=None): - paddle.disable_static() - if weight_np is not None: - weight = paddle.to_tensor(weight_np) - bce_loss = paddle.nn.loss.BCELoss(weight=weight, reduction=reduction) - else: - bce_loss = paddle.nn.loss.BCELoss(reduction=reduction) - dy_res = bce_loss(paddle.to_tensor(input_np), paddle.to_tensor(label_np)) - dy_result = dy_res.numpy() - paddle.enable_static() - return dy_result - - -def test_dygraph_functional(place, - input_np, - label_np, - reduction='mean', - weight_np=None): - paddle.disable_static() - input = paddle.to_tensor(input_np) - label = paddle.to_tensor(label_np) - - if weight_np is not None: - weight = paddle.to_tensor(weight_np) - dy_res = paddle.nn.functional.binary_cross_entropy( - input, label, weight=weight, reduction=reduction) - else: - dy_res = paddle.nn.functional.binary_cross_entropy( - input, label, reduction=reduction) - dy_result = dy_res.numpy() - paddle.enable_static() - return dy_result - - -def calc_bceloss(input_np, label_np, reduction='mean', weight_np=None): - if weight_np is None: - expected = -1 * (label_np * np.log(input_np) + - (1. - label_np) * np.log(1. - input_np)) - else: - expected = -1 * weight_np * (label_np * np.log(input_np) + - (1. - label_np) * np.log(1. - input_np)) - - if reduction == 'mean': - expected = np.mean(expected) - elif reduction == 'sum': - expected = np.sum(expected) - else: - expected = expected - - return expected - - -@unittest.skipIf(not paddle.is_compiled_with_npu(), - "core is not compiled with NPU") -class TestBCELoss(unittest.TestCase): - def test_BCELoss(self): - input_np = np.random.uniform(0.1, 0.8, size=(20, 30)).astype(np.float32) - label_np = np.random.randint(0, 2, size=(20, 30)).astype(np.float32) - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_npu(): - places.append(fluid.NPUPlace(0)) - reductions = ['sum', 'mean', 'none'] - for place in places: - for reduction in reductions: - static_result = test_static_layer(place, input_np, label_np, - reduction) - dy_result = test_dygraph_layer(place, input_np, label_np, - reduction) - expected = calc_bceloss(input_np, label_np, reduction) - self.assertTrue(np.allclose(static_result, expected)) - self.assertTrue(np.allclose(static_result, dy_result)) - self.assertTrue(np.allclose(dy_result, expected)) - static_functional = test_static_functional(place, input_np, - label_np, reduction) - dy_functional = test_dygraph_functional(place, input_np, - label_np, reduction) - self.assertTrue(np.allclose(static_functional, expected)) - self.assertTrue(np.allclose(static_functional, dy_functional)) - self.assertTrue(np.allclose(dy_functional, expected)) - - def test_BCELoss_weight(self): - input_np = np.random.uniform( - 0.1, 0.8, size=(2, 3, 4, 10)).astype(np.float32) - label_np = np.random.randint( - 0, 2, size=(2, 3, 4, 10)).astype(np.float32) - weight_np = np.random.random(size=(3, 4, 10)).astype(np.float32) - place = fluid.NPUPlace(0) if fluid.core.is_compiled_with_npu( - ) else fluid.CPUPlace() - for reduction in ['sum', 'mean', 'none']: - static_result = test_static_layer( - place, input_np, label_np, reduction, weight_np=weight_np) - dy_result = test_dygraph_layer( - place, input_np, label_np, reduction, weight_np=weight_np) - expected = calc_bceloss( - input_np, label_np, reduction, weight_np=weight_np) - self.assertTrue(np.allclose(static_result, expected)) - self.assertTrue(np.allclose(static_result, dy_result)) - self.assertTrue(np.allclose(dy_result, expected)) - static_functional = test_static_functional( - place, input_np, label_np, reduction, weight_np=weight_np) - dy_functional = test_dygraph_functional( - place, input_np, label_np, reduction, weight_np=weight_np) - self.assertTrue(np.allclose(static_functional, expected)) - self.assertTrue(np.allclose(static_functional, dy_functional)) - self.assertTrue(np.allclose(dy_functional, expected)) - - def test_BCELoss_error(self): - paddle.disable_static(paddle.NPUPlace(0)) - self.assertRaises( - ValueError, paddle.nn.loss.BCELoss, reduction="unsupport reduction") - input = paddle.to_tensor([[0.1, 0.3]], dtype='float32') - label = paddle.to_tensor([[0.0, 1.0]], dtype='float32') - self.assertRaises( - ValueError, - paddle.nn.functional.binary_cross_entropy, - input=input, - label=label, - reduction="unsupport reduction") - paddle.enable_static() - - -def bce_loss(input, label): - return -1 * (label * np.log(input) + (1. - label) * np.log(1. - input)) - - -@unittest.skipIf(not paddle.is_compiled_with_npu(), - "core is not compiled with NPU") -class TestBceLossOp(OpTest): - def setUp(self): - self.set_npu() - self.init_test_case() - self.op_type = "bce_loss" - input_np = np.random.uniform(0.1, 0.8, self.shape).astype("float32") - label_np = np.random.randint(0, 2, self.shape).astype("float32") - output_np = bce_loss(input_np, label_np) - - self.inputs = {'X': input_np, 'Label': label_np} - self.outputs = {'Out': output_np} - - def set_npu(self): - self.__class__.use_npu = True - self.place = paddle.NPUPlace(0) - - def test_check_output(self): - self.check_output_with_place(self.place) - - def test_check_grad(self): - self.check_grad_with_place(self.place, ['X'], 'Out') - - def init_test_case(self): - self.shape = [10, 10] - - -@unittest.skipIf(not paddle.is_compiled_with_npu(), - "core is not compiled with NPU") -class TestBceLossOpCase1(OpTest): - def init_test_cast(self): - self.shape = [2, 3, 4, 5] - - -@unittest.skipIf(not paddle.is_compiled_with_npu(), - "core is not compiled with NPU") -class TestBceLossOpCase2(OpTest): - def init_test_cast(self): - self.shape = [2, 3, 20] - - -if __name__ == "__main__": - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py index 6d1327f068a528c97b8b3e882bc37f2c07635acb..a2b54be3a1482326f78039e78d4dae026f8445f9 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py @@ -1,10 +1,10 @@ -# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. # # 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 # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -14,240 +14,108 @@ from __future__ import print_function +import numpy as np +import unittest import sys sys.path.append("..") -import op_test -import unittest -import numpy as np +from op_test import OpTest import paddle import paddle.fluid as fluid -from paddle.static import Program, program_guard - -TEST_META_OP_DATA = [{ - 'op_str': 'logical_and', - 'binary_op': True -}, { - 'op_str': 'logical_or', - 'binary_op': True -}, { - 'op_str': 'logical_not', - 'binary_op': False -}] - -TEST_META_SHAPE_DATA = { - 'XDimLargerThanYDim1': { - 'x_shape': [2, 3, 4, 5], - 'y_shape': [4, 5] - }, - 'XDimLargerThanYDim2': { - 'x_shape': [2, 3, 4, 5], - 'y_shape': [4, 1] - }, - 'XDimLargerThanYDim3': { - 'x_shape': [2, 3, 4, 5], - 'y_shape': [1, 4, 1] - }, - 'XDimLargerThanYDim4': { - 'x_shape': [2, 3, 4, 5], - 'y_shape': [3, 4, 1] - }, - 'XDimLargerThanYDim5': { - 'x_shape': [2, 3, 1, 5], - 'y_shape': [3, 1, 1] - }, - 'XDimLessThanYDim1': { - 'x_shape': [4, 1], - 'y_shape': [2, 3, 4, 5] - }, - 'XDimLessThanYDim2': { - 'x_shape': [1, 4, 1], - 'y_shape': [2, 3, 4, 5] - }, - 'XDimLessThanYDim3': { - 'x_shape': [3, 4, 1], - 'y_shape': [2, 3, 4, 5] - }, - 'XDimLessThanYDim4': { - 'x_shape': [3, 1, 1], - 'y_shape': [2, 3, 1, 5] - }, - 'XDimLessThanYDim5': { - 'x_shape': [4, 5], - 'y_shape': [2, 3, 4, 5] - }, - 'Axis1InLargerDim': { - 'x_shape': [1, 4, 5], - 'y_shape': [2, 3, 1, 5] - }, - 'EqualDim1': { - 'x_shape': [10, 7], - 'y_shape': [10, 7] - }, - 'EqualDim2': { - 'x_shape': [1, 1, 4, 5], - 'y_shape': [2, 3, 1, 5] - } -} - -TEST_META_WRONG_SHAPE_DATA = { - 'ErrorDim1': { - 'x_shape': [2, 3, 4, 5], - 'y_shape': [3, 4] - }, - 'ErrorDim2': { - 'x_shape': [2, 3, 4, 5], - 'y_shape': [4, 3] - } -} - - -def run_static(x_np, y_np, op_str, use_npu=False, binary_op=True): - paddle.enable_static() - startup_program = fluid.Program() - main_program = fluid.Program() - place = paddle.CPUPlace() - if use_npu and fluid.core.is_compiled_with_npu(): - place = paddle.NPUPlace(0) - exe = fluid.Executor(place) - with fluid.program_guard(main_program, startup_program): - x = paddle.static.data(name='x', shape=x_np.shape, dtype='bool') - op = getattr(paddle, op_str) - feed_list = {'x': x_np} - if not binary_op: - res = op(x) - else: - y = paddle.static.data(name='y', shape=y_np.shape, dtype='bool') - feed_list['y'] = y_np - res = op(x, y) - exe.run(startup_program) - static_result = exe.run(main_program, feed=feed_list, fetch_list=[res]) - return static_result - - -def run_dygraph(x_np, y_np, op_str, use_npu=False, binary_op=True): - place = paddle.CPUPlace() - if use_npu and fluid.core.is_compiled_with_npu(): - place = paddle.NPUPlace(0) - paddle.disable_static(place) - op = getattr(paddle, op_str) - x = paddle.to_tensor(x_np) - if not binary_op: - dygraph_result = op(x) - else: - y = paddle.to_tensor(y_np) - dygraph_result = op(x, y) - return dygraph_result - - -def np_data_generator(np_shape, *args, **kwargs): - return np.random.choice(a=[True, False], size=np_shape).astype(bool) - - -def test(unit_test, use_npu=False, test_error=False): - for op_data in TEST_META_OP_DATA: - meta_data = dict(op_data) - meta_data['use_npu'] = use_npu - np_op = getattr(np, meta_data['op_str']) - META_DATA = dict(TEST_META_SHAPE_DATA) - if test_error: - META_DATA = dict(TEST_META_WRONG_SHAPE_DATA) - for shape_data in META_DATA.values(): - meta_data['x_np'] = np_data_generator(shape_data['x_shape']) - meta_data['y_np'] = np_data_generator(shape_data['y_shape']) - if meta_data['binary_op'] and test_error: - # catch C++ Exception - unit_test.assertRaises(BaseException, run_static, **meta_data) - unit_test.assertRaises(BaseException, run_dygraph, **meta_data) - continue - static_result = run_static(**meta_data) - dygraph_result = run_dygraph(**meta_data) - if meta_data['binary_op']: - np_result = np_op(meta_data['x_np'], meta_data['y_np']) - else: - np_result = np_op(meta_data['x_np']) - unit_test.assertTrue((static_result == np_result).all()) - unit_test.assertTrue((dygraph_result.numpy() == np_result).all()) - - -def test_type_error(unit_test, use_npu, type_str_map): - def check_type(op_str, x, y, binary_op): - op = getattr(paddle, op_str) - error_type = TypeError - if isinstance(x, np.ndarray): - x = paddle.to_tensor(x) - y = paddle.to_tensor(y) - error_type = BaseException - if binary_op: - if type_str_map['x'] != 'bool' or type_str_map['y'] != 'bool': - unit_test.assertRaises(error_type, op, x=x, y=y) - if not fluid.in_dygraph_mode(): - unit_test.assertRaises(error_type, op, x=x, y=y, out=1) - else: - if type_str_map['x'] != 'bool': - unit_test.assertRaises(error_type, op, x=x) - if not fluid.in_dygraph_mode(): - unit_test.assertRaises(error_type, op, x=x, out=1) - - place = paddle.CPUPlace() - if use_npu and fluid.core.is_compiled_with_npu(): - place = paddle.NPUPlace(0) - for op_data in TEST_META_OP_DATA: - meta_data = dict(op_data) - binary_op = meta_data['binary_op'] - - paddle.disable_static(place) - x = np.random.choice(a=[0, 1], size=[10]).astype(type_str_map['x']) - y = np.random.choice(a=[0, 1], size=[10]).astype(type_str_map['y']) - check_type(meta_data['op_str'], x, y, binary_op) - - paddle.enable_static() - startup_program = paddle.static.Program() - main_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - x = paddle.static.data( - name='x', shape=[10], dtype=type_str_map['x']) - y = paddle.static.data( - name='y', shape=[10], dtype=type_str_map['y']) - check_type(meta_data['op_str'], x, y, binary_op) - - -def type_map_factory(): - x_type_list = ['float32', 'float64', 'int32', 'int64', 'bool'] - y_type_list = ['float32', 'float64', 'int32', 'int64', 'bool'] - return [{ - 'x': x_type, - 'y': y_type - } for x_type in x_type_list for y_type in y_type_list] + +paddle.enable_static() +SEED = 2021 @unittest.skipIf(not paddle.is_compiled_with_npu(), "core is not compiled with NPU") -class TestCPU(unittest.TestCase): - def test(self): - test(self) +class TestLogicalNot(OpTest): + def setUp(self): + self.set_npu() + self.op_type = "logical_not" + self.place = paddle.NPUPlace(4) + + self.init_dtype() + np.random.seed(SEED) + x = np.random.uniform(1, 2, [11, 17]).astype(self.dtype) + out = np.logical_not(x) - def test_error(self): - test(self, False, True) + self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.attrs = {} + self.outputs = {'Out': out} - def test_type_error(self): - type_map_list = type_map_factory() - for type_map in type_map_list: - test_type_error(self, False, type_map) + def set_npu(self): + self.__class__.use_npu = True + + def init_dtype(self): + self.dtype = np.bool + + def test_check_output(self): + self.check_output_with_place(self.place, check_dygraph=False) + + # TODO(ascendrc): Add grad test + # def test_check_grad(self): + # if self.dtype == np.float16: + # return + # self.check_grad(['X'], 'Out') + # @unittest.skipIf(not paddle.is_compiled_with_npu(), "core is not compiled with NPU") -class TestNPU(unittest.TestCase): - def test(self): - test(self, True) +class TestLogcialNotNet(unittest.TestCase): + def _test(self, run_npu=True): + main_prog = paddle.static.Program() + startup_prog = paddle.static.Program() + main_prog.random_seed = SEED + startup_prog.random_seed = SEED + np.random.seed(SEED) + + a_np = np.random.random(size=(32, 32)).astype('bool') + label_np = np.random.randint(2, size=(32, 1)).astype('int64') + + with paddle.static.program_guard(main_prog, startup_prog): + a = paddle.static.data(name="a", shape=[32, 32], dtype='bool') + label = paddle.static.data( + name="label", shape=[32, 1], dtype='int64') + + c = paddle.logical_not(a) + d = paddle.cast(c, dtype="float32") + + fc_1 = fluid.layers.fc(input=d, size=128) + prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax') + + cost = fluid.layers.cross_entropy(input=prediction, label=label) + loss = fluid.layers.reduce_mean(cost) + sgd = fluid.optimizer.SGD(learning_rate=0.01) + sgd.minimize(loss) + + if run_npu: + place = paddle.NPUPlace(4) + else: + place = paddle.CPUPlace() + + exe = paddle.static.Executor(place) + exe.run(startup_prog) + + print("Start run on {}".format(place)) + for epoch in range(100): + + pred_res, loss_res = exe.run(main_prog, + feed={"a": a_np, + "label": label_np}, + fetch_list=[prediction, loss]) + if epoch % 10 == 0: + print("Epoch {} | Prediction[0]: {}, Loss: {}".format( + epoch, pred_res[0], loss_res)) + + return pred_res, loss_res - def test_error(self): - test(self, True, True) + def test_npu(self): + cpu_pred, cpu_loss = self._test(False) + npu_pred, npu_loss = self._test(True) - def test_type_error(self): - type_map_list = type_map_factory() - for type_map in type_map_list: - test_type_error(self, True, type_map) + self.assertTrue(np.allclose(npu_pred, cpu_pred)) + self.assertTrue(np.allclose(npu_loss, cpu_loss)) if __name__ == '__main__':