From f4a0e68481952219d4d5e18ac758247428a03cfa Mon Sep 17 00:00:00 2001 From: baojun <32073718+baojun-nervana@users.noreply.github.com> Date: Mon, 11 Feb 2019 04:02:32 -0800 Subject: [PATCH] Fix ngraph compile WITH_DISTRIBUTE=ON (#15636) * fix compile issue with_distribute test=develop * simplified logic test=develop * use ngraph dependency test=develop * set cpu only test=develop * update test and eliminate fp16 test test=develop --- paddle/fluid/framework/CMakeLists.txt | 21 +++--- .../fluid/operators/ngraph/ngraph_engine_op.h | 2 +- .../ngraph/test_accuracy_ngraph_op.py | 31 +++++++-- .../unittests/ngraph/test_conv2d_ngraph_op.py | 26 ++++++- .../ngraph/test_elementwise_add_ngraph_op.py | 67 ++----------------- .../unittests/ngraph/test_mean_ngraph_op.py | 8 +-- .../unittests/ngraph/test_mul_ngraph_op.py | 39 +++++++---- .../unittests/ngraph/test_pool2d_ngraph_op.py | 26 ++++++- .../unittests/ngraph/test_scale_ngraph_op.py | 18 +++-- .../unittests/ngraph/test_top_k_ngraph_op.py | 4 ++ 10 files changed, 133 insertions(+), 109 deletions(-) diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 910318a49c..7ddf1ab44f 100644 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -158,18 +158,19 @@ cc_library(variable_helper SRCS variable_helper.cc DEPS lod_tensor) cc_library(naive_executor SRCS naive_executor.cc DEPS op_registry device_context scope framework_proto glog lod_rank_table feed_fetch_method graph_to_program_pass variable_helper) -if(WITH_DISTRIBUTE) - cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto glog - lod_rank_table feed_fetch_method sendrecvop_rpc ${GLOB_DISTRIBUTE_DEPS} graph_to_program_pass variable_helper) +if(WITH_NGRAPH) + set(NGRAPH_EXE_DEPS ngraph_engine) +else() + set(NGRAPH_EXE_DEPS) +endif() - set(DISTRIBUTE_COMPILE_FLAGS "-Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor") - set_source_files_properties(executor.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) +if(WITH_DISTRIBUTE) + cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto glog + lod_rank_table feed_fetch_method sendrecvop_rpc ${GLOB_DISTRIBUTE_DEPS} graph_to_program_pass variable_helper ${NGRAPH_EXE_DEPS}) + set(DISTRIBUTE_COMPILE_FLAGS "-Wno-non-virtual-dtor -Wno-error=non-virtual-dtor -Wno-error=delete-non-virtual-dtor") + set_source_files_properties(executor.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) else() - if (WITH_NGRAPH) - cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto glog lod_rank_table feed_fetch_method graph_to_program_pass variable_helper ngraph_engine) - else () - cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto glog lod_rank_table feed_fetch_method graph_to_program_pass variable_helper) - endif() + cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto glog lod_rank_table feed_fetch_method graph_to_program_pass variable_helper ${NGRAPH_EXE_DEPS}) cc_test(test_naive_executor SRCS naive_executor_test.cc DEPS naive_executor elementwise_add_op) endif() diff --git a/paddle/fluid/operators/ngraph/ngraph_engine_op.h b/paddle/fluid/operators/ngraph/ngraph_engine_op.h index d2974298b0..2f194a9b87 100644 --- a/paddle/fluid/operators/ngraph/ngraph_engine_op.h +++ b/paddle/fluid/operators/ngraph/ngraph_engine_op.h @@ -35,7 +35,7 @@ class NgraphEngineOp : public framework::OperatorWithKernel { framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { framework::OpKernelType kt = framework::OpKernelType( - framework::proto::VarType::FP32, ctx.GetPlace()); + framework::proto::VarType::FP32, platform::CPUPlace()); return kt; } }; diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_accuracy_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_accuracy_ngraph_op.py index 13a33e2047..84b9198dbf 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_accuracy_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_accuracy_ngraph_op.py @@ -16,14 +16,37 @@ from __future__ import print_function import unittest import numpy as np -import paddle.fluid.core as core from paddle.fluid.tests.unittests.op_test import OpTest -from paddle.fluid.tests.unittests.test_accuracy_op import TestAccuracyOp -class TestNGRAPHAccuracyOp(TestAccuracyOp): +class TestNGRAPHAccuracyOp(OpTest): def setUp(self): - super(TestNGRAPHAccuracyOp, self).setUp() + self.op_type = "accuracy" + self.dtype = np.float32 + self.init_dtype() + n = 128 + infer = np.random.random((n, 1)).astype(self.dtype) + indices = np.random.randint(0, 2, (n, 1)) + label = np.random.randint(0, 2, (n, 1)) + self.inputs = {'Out': infer, 'Indices': indices, "Label": label} + num_correct = 0 + for rowid in range(n): + for ele in indices[rowid]: + if ele == label[rowid]: + num_correct += 1 + break + self.outputs = { + 'Accuracy': np.array([num_correct / float(n)]).astype(self.dtype), + 'Correct': np.array([num_correct]).astype("int64"), + 'Total': np.array([n]).astype("int64") + } + self._cpu_only = True + + def init_dtype(self): + pass + + def test_check_output(self): + self.check_output() if __name__ == '__main__': diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py index e5424e8a6e..dbc8557b4e 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py @@ -15,35 +15,59 @@ from __future__ import print_function import unittest -from paddle.fluid.tests.unittests.test_conv2d_op import * +from paddle.fluid.tests.unittests.test_conv2d_op import TestConv2dOp, TestWithPad, TestWithStride, TestWithGroup, TestWith1x1, TestWithInput1x1Filter1x1 class TestNGRAPH(TestConv2dOp): + def setUp(self): + super(TestNGRAPH, self).setUp() + self._cpu_only = True + def init_kernel_type(self): super(TestNGRAPH, self).init_kernel_type() class TestNGRAPHWithPad(TestWithPad): + def setUp(self): + super(TestNGRAPHWithPad, self).setUp() + self._cpu_only = True + def init_kernel_type(self): super(TestNGRAPHWithPad, self).init_kernel_type() class TestNGRAPHWithStride(TestWithStride): + def setUp(self): + super(TestNGRAPHWithStride, self).setUp() + self._cpu_only = True + def init_kernel_type(self): super(TestNGRAPHWithStride, self).init_kernel_type() class TestNGRAPHWithGroup(TestWithGroup): + def setUp(self): + super(TestNGRAPHWithGroup, self).setUp() + self._cpu_only = True + def init_kernel_type(self): super(TestNGRAPHWithGroup, self).init_kernel_type() class TestNGRAPHWith1x1(TestWith1x1): + def setUp(self): + super(TestNGRAPHWith1x1, self).setUp() + self._cpu_only = True + def init_kernel_type(self): super(TestNGRAPHWith1x1, self).init_kernel_type() class TestNGRAPHWithInput1x1Filter1x1(TestWithInput1x1Filter1x1): + def setUp(self): + super(TestNGRAPHWithInput1x1Filter1x1, self).setUp() + self._cpu_only = True + def init_kernel_type(self): super(TestNGRAPHWithInput1x1Filter1x1, self).init_kernel_type() diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_elementwise_add_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_elementwise_add_ngraph_op.py index 67722db89b..67f749bfee 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_elementwise_add_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_elementwise_add_ngraph_op.py @@ -14,73 +14,16 @@ from __future__ import print_function import unittest -from paddle.fluid.tests.unittests.test_elementwise_add_op import * +from paddle.fluid.tests.unittests.test_elementwise_add_op import TestElementwiseAddOp class TestNGRAPHElementwiseAddOp(TestElementwiseAddOp): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_scalar(TestElementwiseAddOp_scalar): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_scalar, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_scalar2(TestElementwiseAddOp_scalar2): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_scalar2, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_Vector(TestElementwiseAddOp_Vector): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_Vector, self).init_input_output() - - -class TesNGRAPHtElementwiseAddOp_broadcast_0(TestElementwiseAddOp_broadcast_0): - def init_input_output(self): - super(TesNGRAPHtElementwiseAddOp_broadcast_0, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_broadcast_1(TestElementwiseAddOp_broadcast_1): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_broadcast_1, self).init_input_output() + def setUp(self): + super(TestNGRAPHElementwiseAddOp, self).setUp() + self._cpu_only = True - -class TestNGRAPHElementwiseAddOp_broadcast_2(TestElementwiseAddOp_broadcast_2): def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_broadcast_2, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_broadcast_3(TestElementwiseAddOp_broadcast_3): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_broadcast_3, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_broadcast_4(TestElementwiseAddOp_broadcast_4): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_broadcast_4, self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_rowwise_add_0( - TestElementwiseAddOp_rowwise_add_0): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_rowwise_add_0, - self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_rowwise_add_1( - TestElementwiseAddOp_rowwise_add_1): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_rowwise_add_1, - self).init_input_output() - - -class TestNGRAPHElementwiseAddOp_channelwise_add( - TestElementwiseAddOp_channelwise_add): - def init_input_output(self): - super(TestNGRAPHElementwiseAddOp_channelwise_add, - self).init_input_output() + super(TestNGRAPHElementwiseAddOp, self).init_input_output() if __name__ == '__main__': diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_mean_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_mean_ngraph_op.py index 5535427ea8..11881ac6e5 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_mean_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_mean_ngraph_op.py @@ -14,17 +14,13 @@ from __future__ import print_function import unittest -from paddle.fluid.tests.unittests.test_mean_op import TestMeanOp, TestFP16MeanOp +from paddle.fluid.tests.unittests.test_mean_op import TestMeanOp class TestNGRAPHMeanOp(TestMeanOp): def setUp(self): super(TestNGRAPHMeanOp, self).setUp() - - -class TestNGRAPHFP16MeanOp(TestFP16MeanOp): - def setUp(self): - super(TestNGRAPHFP16MeanOp, self).setUp() + self._cpu_only = True if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_mul_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_mul_ngraph_op.py index 6aba62f7c0..a916c8d450 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_mul_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_mul_ngraph_op.py @@ -15,27 +15,38 @@ from __future__ import print_function import unittest -from paddle.fluid.tests.unittests.test_mul_op import TestMulOp, TestMulOp2, TestFP16MulOp1, TestFP16MulOp2 +import numpy as np +from paddle.fluid.tests.unittests.op_test import OpTest + + +class TestNGRAPHMulOp(OpTest): + def setUp(self): + self.op_type = "mul" + self.dtype = np.float32 + self.init_dtype_type() + self.inputs = { + 'X': np.random.random((2, 4)).astype(self.dtype), + 'Y': np.random.random((4, 4)).astype(self.dtype) + } + self.outputs = {'Out': np.dot(self.inputs['X'], self.inputs['Y'])} + self._cpu_only = True - -class TestNGRAPHMulOp(TestMulOp): def init_dtype_type(self): pass + def test_check_output(self): + self.check_output() -class TestNGRAPHMulOp2(TestMulOp2): - def init_dtype_type(self): - pass + def test_check_grad_normal(self): + self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.5) + def test_check_grad_ingore_x(self): + self.check_grad( + ['Y'], 'Out', max_relative_error=0.5, no_grad_set=set("X")) -class TestNGRAPHFP16MulOp1(TestFP16MulOp1): - def init_dtype_type(self): - pass - - -class TestNGRAPHFP16MulOp2(TestFP16MulOp2): - def init_dtype_type(self): - pass + def test_check_grad_ingore_y(self): + self.check_grad( + ['X'], 'Out', max_relative_error=0.5, no_grad_set=set('Y')) if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py index 95e592e8ec..96a2b72d8a 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py @@ -14,35 +14,59 @@ from __future__ import print_function -from paddle.fluid.tests.unittests.test_pool2d_op import * +from paddle.fluid.tests.unittests.test_pool2d_op import TestPool2D_Op, TestCase1, TestCase2, TestCase3, TestCase4, TestCase5 class TestNGRAPHPool2D_Op(TestPool2D_Op): + def setUp(self): + super(TestNGRAPHPool2D_Op, self).setUp() + self._cpu_only = True + def init_test_case(self): super(TestNGRAPHPool2D_Op, self).init_test_case() class TestNGRAPHCase1(TestCase1): + def setUp(self): + super(TestNGRAPHCase1, self).setUp() + self._cpu_only = True + def init_test_case(self): super(TestNGRAPHCase1, self).init_test_case() class TestNGRAPHCase2(TestCase2): + def setUp(self): + super(TestNGRAPHCase2, self).setUp() + self._cpu_only = True + def init_test_case(self): super(TestNGRAPHCase2, self).init_test_case() class TestNGRAPHCase3(TestCase3): + def setUp(self): + super(TestNGRAPHCase3, self).setUp() + self._cpu_only = True + def init_pool_type(self): super(TestNGRAPHCase3, self).init_pool_type() class TestNGRAPHCase4(TestCase4): + def setUp(self): + super(TestNGRAPHCase4, self).setUp() + self._cpu_only = True + def init_pool_type(self): super(TestNGRAPHCase4, self).init_pool_type() class TestNGRAPHCase5(TestCase5): + def setUp(self): + super(TestNGRAPHCase5, self).setUp() + self._cpu_only = True + def init_pool_type(self): super(TestNGRAPHCase5, self).init_pool_type() diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_scale_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_scale_ngraph_op.py index b42a1f73fa..4da5ca4583 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_scale_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_scale_ngraph_op.py @@ -13,25 +13,23 @@ # limitations under the License. from __future__ import print_function import unittest -from paddle.fluid.tests.unittests.test_scale_op import TestScaleOp, TestScaleOpSelectedRows, TestScaleFp16Op, TestScaleFp16OpSelectedRows +from paddle.fluid.tests.unittests.test_scale_op import TestScaleOp, TestScaleOpSelectedRows class TestNGRAPHScaleOp(TestScaleOp): - def init_dtype_type(self): - pass + def setUp(self): + super(TestNGRAPHScaleOp, self).setUp() + self._cpu_only = True - -class TestNGRAPHScaleOpSelectedRows(TestScaleOpSelectedRows): def init_dtype_type(self): pass -class TestNGRAPHScaleFp16Op(TestScaleFp16Op): - def init_dtype_type(self): - pass - +class TestNGRAPHScaleOpSelectedRows(TestScaleOpSelectedRows): + def setUp(self): + super(TestNGRAPHScaleOpSelectedRows, self).setUp() + self._cpu_only = True -class TestNGRAPHScaleFp16OpSelectedRows(TestScaleFp16OpSelectedRows): def init_dtype_type(self): pass diff --git a/python/paddle/fluid/tests/unittests/ngraph/test_top_k_ngraph_op.py b/python/paddle/fluid/tests/unittests/ngraph/test_top_k_ngraph_op.py index 3a0171087d..fa68df1adf 100644 --- a/python/paddle/fluid/tests/unittests/ngraph/test_top_k_ngraph_op.py +++ b/python/paddle/fluid/tests/unittests/ngraph/test_top_k_ngraph_op.py @@ -20,21 +20,25 @@ from paddle.fluid.tests.unittests.test_top_k_op import TestTopkOp, TestTopkOp3d, class TestNGRAPHTopkOp(TestTopkOp): def setUp(self): super(TestNGRAPHTopkOp, self).setUp() + self._cpu_only = True class TestNGRAPHTopkOp2(TestTopkOp2): def setUp(self): super(TestNGRAPHTopkOp2, self).setUp() + self._cpu_only = True class TestNGRAPHTopkOp3(TestTopkOp3): def setUp(self): super(TestNGRAPHTopkOp3, self).setUp() + self._cpu_only = True class TestNGRAPHTopkOp4(TestTopkOp4): def setUp(self): super(TestNGRAPHTopkOp4, self).setUp() + self._cpu_only = True if __name__ == "__main__": -- GitLab