diff --git a/paddle/fluid/eager/grad_tensor_holder.cc b/paddle/fluid/eager/grad_tensor_holder.cc index b15d9b892f8109ff1eb41d12cc5023bbebbf708a..2dacb588ff847e63f8ccebc33faa74f63fef59b9 100644 --- a/paddle/fluid/eager/grad_tensor_holder.cc +++ b/paddle/fluid/eager/grad_tensor_holder.cc @@ -64,8 +64,9 @@ void GradTensorHolder::CopyValueFromTensor( } else { // Create new tensor->impl and fill it with 1.0 if (t.defined()) { - // Fill 1.0 - buffer_[slot_id][rank] = paddle::experimental::ones_like(t, t.dtype()); + // Fill 1.0, use full to support complex, one_like don't support it. + buffer_[slot_id][rank] = + paddle::experimental::full(t.shape(), 1, t.dtype(), t.inner_place()); } } } diff --git a/paddle/fluid/pybind/op_function_generator.h b/paddle/fluid/pybind/op_function_generator.h index 1e501a0c9e024ddef5d74566ffa96983f5e798e2..b8202fe8c51fd822cecf298a9a27c45793e67739 100644 --- a/paddle/fluid/pybind/op_function_generator.h +++ b/paddle/fluid/pybind/op_function_generator.h @@ -52,6 +52,7 @@ std::map> op_ins_map = { {"fake_quantize_dequantize_moving_average_abs_max", {"X", "InScale", "InAccum", "InState"}}, {"nll_loss", {"X", "Label", "Weight"}}, + {"smooth_l1_loss", {"X", "Y", "InsideWeight", "OutsideWeight"}}, {"bilinear_tensor_product", {"X", "Y", "Weight", "Bias"}}, {"gather", {"X", "Index", "Axis"}}, {"repeat_interleave", {"X", "RepeatsTensor"}}, diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index dc1f82d235e31c4c98cf7249e41483e2496d37b0..20c441f36414531056f7f4f2276bdde35e9e4bd1 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -106,14 +106,35 @@ _global_flags_ = core.globals() # to make sure in most case, we find new dygraph mode first with only one if statement. +def _update_monkey_methods(is_eager): + """ + Update monkey methods of VarBase or eager.Tensor while + switching eager mode and legacy mode. + """ + from paddle import _C_ops + from .dygraph.varbase_patch_methods import monkey_patch_varbase + from .dygraph import monkey_patch_math_varbase + + assert isinstance(is_eager, bool) + if is_eager: + _C_ops.switch_to_eager_ops() + else: + _C_ops.switch_to_core_ops() + + monkey_patch_varbase() + monkey_patch_math_varbase() + + def _enable_legacy_dygraph(): global _in_eager_mode_ _in_eager_mode_ = False + _update_monkey_methods(is_eager=False) def _disable_legacy_dygraph(): global _in_eager_mode_ _in_eager_mode_ = True + _update_monkey_methods(is_eager=True) def _in_eager_without_dygraph_check(): diff --git a/python/paddle/fluid/tests/unittests/op_test.py b/python/paddle/fluid/tests/unittests/op_test.py index be883d243f79585ce697e01d2170a7bdf804e1d2..60064340b198a2be672c99918b6f2cadfaccf02a 100644 --- a/python/paddle/fluid/tests/unittests/op_test.py +++ b/python/paddle/fluid/tests/unittests/op_test.py @@ -1937,6 +1937,9 @@ class OpTest(unittest.TestCase): "Gradient Check On %s" % str(place)) if check_dygraph: + # ensure switch into legacy dygraph + g_enable_legacy_dygraph() + dygraph_grad = self._get_dygraph_grad( inputs_to_check, place, output_names, user_defined_grad_outputs, no_grad_set, False) @@ -1950,6 +1953,8 @@ class OpTest(unittest.TestCase): self._assert_is_close(numeric_grads, dygraph_grad, inputs_to_check, max_relative_error, "Gradient Check On %s" % str(place)) + # ensure switch back eager dygraph + g_disable_legacy_dygraph() if check_eager: with fluid.dygraph.base.guard(place): @@ -2087,7 +2092,6 @@ class OpTest(unittest.TestCase): inputs={"X": loss_sum}, outputs={"Out": loss}, attrs={'scale': 1.0 / float(len(avg_sum))}) - loss.backward() fetch_list_grad = [] diff --git a/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py b/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py index f3f3431c9fb3ea799a465fa3456f4d2782bccefc..8d7dd0d81180ec126a0efd08ba7f924f01432e71 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py @@ -127,6 +127,9 @@ class TestBicubicInterpOp(OpTest): self.data_layout = 'NCHW' self.init_test_case() self.op_type = "bicubic_interp" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_eager while found them in Inputs. + self.check_eager = True input_np = np.random.random(self.input_shape).astype("float64") if self.data_layout == "NCHW": @@ -149,8 +152,10 @@ class TestBicubicInterpOp(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False if self.actual_shape is not None: self.inputs['OutSize'] = self.actual_shape + self.check_eager = False self.attrs = { 'out_h': self.out_h, @@ -163,10 +168,11 @@ class TestBicubicInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'bicubic' @@ -442,4 +448,5 @@ class TestBicubicOpError(unittest.TestCase): if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py index 43e418addf2bfd561c98171be6b1128e92396c5c..d5c3aee2f43722bde7a638a6ea4ec6242766c917 100644 --- a/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py @@ -16,7 +16,7 @@ from __future__ import print_function import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, _in_eager_without_dygraph_check import paddle.fluid.core as core import paddle.fluid as fluid import paddle @@ -135,6 +135,10 @@ class TestBicubicInterpOp(OpTest): self.data_layout = 'NCHW' self.init_test_case() self.op_type = "bicubic_interp_v2" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_eager while found them in Inputs. + # TODO(dev): add self.python_api + self.check_eager = False input_np = np.random.random(self.input_shape).astype("float64") scale_h = 0 scale_w = 0 @@ -166,8 +170,10 @@ class TestBicubicInterpOp(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False if self.actual_shape is not None: self.inputs['OutSize'] = self.actual_shape + self.check_eager = False self.attrs = { 'out_h': self.out_h, @@ -186,10 +192,11 @@ class TestBicubicInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'bicubic' @@ -543,4 +550,5 @@ class TestBicubicOpError(unittest.TestCase): if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py b/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py index 083b671c283a0f5fe0302837d60a414f3061632a..1817ef160c70a0fce0999ca124a4d9904bab2d76 100755 --- a/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py @@ -102,6 +102,9 @@ class TestBilinearInterpOp(OpTest): self.data_layout = 'NCHW' self.init_test_case() self.op_type = "bilinear_interp" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_eager while found them in Inputs. + self.check_eager = True input_np = np.random.random(self.input_shape).astype("float64") if self.data_layout == "NCHW": @@ -124,8 +127,10 @@ class TestBilinearInterpOp(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False if self.actual_shape is not None: self.inputs['OutSize'] = self.actual_shape + self.check_eager = False self.attrs = { 'out_h': self.out_h, @@ -139,10 +144,11 @@ class TestBilinearInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'bilinear' @@ -266,6 +272,7 @@ class TestBilinearInterpOpUint8(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "bilinear_interp" + self.check_eager = True input_np = np.random.randint( low=0, high=256, size=self.input_shape).astype("uint8") @@ -282,6 +289,7 @@ class TestBilinearInterpOpUint8(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False self.attrs = { 'out_h': self.out_h, @@ -294,7 +302,8 @@ class TestBilinearInterpOpUint8(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output_with_place(place=core.CPUPlace(), atol=1) + self.check_output_with_place( + place=core.CPUPlace(), atol=1, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'bilinear' @@ -397,6 +406,7 @@ class TestBilinearInterpOp_attr_tensor(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "bilinear_interp" + self.check_eager = True self.shape_by_1Dtensor = False self.scale_by_1Dtensor = False self.attrs = { @@ -419,12 +429,14 @@ class TestBilinearInterpOp_attr_tensor(OpTest): if self.shape_by_1Dtensor: self.inputs['OutSize'] = self.out_size + self.check_eager = False elif self.out_size is not None: size_tensor = [] for index, ele in enumerate(self.out_size): size_tensor.append(("x" + str(index), np.ones( (1)).astype('int32') * ele)) self.inputs['SizeTensor'] = size_tensor + self.check_eager = False self.attrs['out_h'] = self.out_h self.attrs['out_w'] = self.out_w @@ -433,10 +445,11 @@ class TestBilinearInterpOp_attr_tensor(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'bilinear' diff --git a/python/paddle/fluid/tests/unittests/test_crop_op.py b/python/paddle/fluid/tests/unittests/test_crop_op.py index b08648b99f123519032d1ac999327bff7e3b2689..acb652ad6f9e8f1b30e28537c53db2b3f3796bb2 100644 --- a/python/paddle/fluid/tests/unittests/test_crop_op.py +++ b/python/paddle/fluid/tests/unittests/test_crop_op.py @@ -71,10 +71,10 @@ class TestCropOp(OpTest): self.offsets = [1, 2] def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_eager=True) class TestCase1(TestCropOp): @@ -125,4 +125,6 @@ class TestCase6(TestCropOp): if __name__ == '__main__': + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_crop_tensor_op.py b/python/paddle/fluid/tests/unittests/test_crop_tensor_op.py index 0808f99ff1a94dfb9e7639646eeeffd129b2760c..a4552c8f5ddbbd3a5fa93b6aec42d9eaca6fcfc0 100644 --- a/python/paddle/fluid/tests/unittests/test_crop_tensor_op.py +++ b/python/paddle/fluid/tests/unittests/test_crop_tensor_op.py @@ -77,10 +77,10 @@ class TestCropTensorOp(OpTest): self.offsets = [1, 2] def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_eager=True) class TestCase1(TestCropTensorOp): @@ -175,10 +175,10 @@ class TestCropTensorOpTensorAttr(OpTest): self.shape_attr = [0, 0] def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): - self.check_grad(["X"], "Out") + self.check_grad(["X"], "Out", check_eager=True) class TestCropTensorOpTensorAttrCase1(TestCropTensorOpTensorAttr): @@ -262,4 +262,6 @@ class TestCropTensorException(unittest.TestCase): if __name__ == '__main__': + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_decayed_adagrad_op.py b/python/paddle/fluid/tests/unittests/test_decayed_adagrad_op.py index a664a1529f4de1f372241319b57fad6b0ba8b8a2..e2f6d17cc96a8ae1ff8caa0f66400f00d08c1f44 100644 --- a/python/paddle/fluid/tests/unittests/test_decayed_adagrad_op.py +++ b/python/paddle/fluid/tests/unittests/test_decayed_adagrad_op.py @@ -48,7 +48,7 @@ class TestDecayedAdagradOp1(OpTest): self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) class TestDecayedAdagradOp2(OpTest): @@ -80,8 +80,10 @@ class TestDecayedAdagradOp2(OpTest): self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) if __name__ == "__main__": + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_dpsgd_op.py b/python/paddle/fluid/tests/unittests/test_dpsgd_op.py index 48bf786e139dd493fcb3ed6122b4d617a5f5bf2f..35a922b78205ffa7d9f78facd9f49ffe44ab7954 100644 --- a/python/paddle/fluid/tests/unittests/test_dpsgd_op.py +++ b/python/paddle/fluid/tests/unittests/test_dpsgd_op.py @@ -45,7 +45,7 @@ class TestDpsgdOp(OpTest): self.outputs = {'ParamOut': param_out} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def dpsgd_step(inputs, attributes): @@ -70,4 +70,6 @@ def dpsgd_step(inputs, attributes): if __name__ == "__main__": + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_ftrl_op.py b/python/paddle/fluid/tests/unittests/test_ftrl_op.py index f58672a7a1e89c26868be5ee652cf128cc8d3e3c..1826fdc3c060465a58acc0e5a405f3246fbcd545 100644 --- a/python/paddle/fluid/tests/unittests/test_ftrl_op.py +++ b/python/paddle/fluid/tests/unittests/test_ftrl_op.py @@ -101,7 +101,7 @@ class TestFTRLOp(OpTest): } def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) class TestSparseFTRLOp(unittest.TestCase): @@ -201,4 +201,6 @@ class TestSparseFTRLOp2(TestSparseFTRLOp): if __name__ == "__main__": + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_mean_iou.py b/python/paddle/fluid/tests/unittests/test_mean_iou.py index 4e89a9034a341777f09958d9709b64a12020ec28..b392a328494b3a16eca062a1f38f8fced5d77cc7 100644 --- a/python/paddle/fluid/tests/unittests/test_mean_iou.py +++ b/python/paddle/fluid/tests/unittests/test_mean_iou.py @@ -15,6 +15,7 @@ from __future__ import print_function from __future__ import division + import unittest import numpy as np from op_test import OpTest @@ -113,6 +114,11 @@ class TestCase1(TestMeanIOUOp): self.in_correct_num = 2 self.in_mean_iou_num = 2 + # NOTE(dev): Skip check_dygraph becuase Python API doesn't expose + # in_wrong_num/in_correct_num/in_mean_iou_num argument + def test_check_output(self): + self.check_output(check_dygraph=False, check_eager=False) + class TestMeanIOUOpError(unittest.TestCase): def test_errors(self): @@ -130,5 +136,4 @@ class TestMeanIOUOpError(unittest.TestCase): if __name__ == '__main__': - paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py b/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py index eda530f30df26a80ec9b0f46d876b71f2818859f..5df085d4febacfdf65f4fe2461df67b122b2a9ad 100755 --- a/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_nearest_interp_op.py @@ -79,6 +79,7 @@ class TestNearestInterpOp(OpTest): self.data_layout = 'NCHW' self.init_test_case() self.op_type = "nearest_interp" + self.check_eager = True input_np = np.random.random(self.input_shape).astype("float64") if self.data_layout == "NCHW": @@ -101,8 +102,10 @@ class TestNearestInterpOp(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False if self.actual_shape is not None: self.inputs['OutSize'] = self.actual_shape + self.check_eager = False self.attrs = { 'out_h': self.out_h, 'out_w': self.out_w, @@ -114,10 +117,11 @@ class TestNearestInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'nearest' @@ -231,6 +235,7 @@ class TestNearestInterpOpUint8(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "nearest_interp" + self.check_eager = True input_np = np.random.randint( low=0, high=256, size=self.input_shape).astype("uint8") @@ -247,6 +252,7 @@ class TestNearestInterpOpUint8(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False self.attrs = { 'out_h': self.out_h, 'out_w': self.out_w, @@ -257,7 +263,8 @@ class TestNearestInterpOpUint8(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output_with_place(place=core.CPUPlace(), atol=1) + self.check_output_with_place( + place=core.CPUPlace(), atol=1, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'nearest' @@ -339,6 +346,9 @@ class TestNearestInterpOp_attr_tensor(OpTest): 'interp_method': self.interp_method, 'align_corners': self.align_corners, } + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_eager while found them in Inputs. + self.check_eager = True input_np = np.random.random(self.input_shape).astype("float64") self.inputs = {'X': input_np} @@ -355,12 +365,14 @@ class TestNearestInterpOp_attr_tensor(OpTest): if self.shape_by_1Dtensor: self.inputs['OutSize'] = self.out_size + self.check_eager = False elif self.out_size is not None: size_tensor = [] for index, ele in enumerate(self.out_size): size_tensor.append(("x" + str(index), np.ones( (1)).astype('int32') * ele)) self.inputs['SizeTensor'] = size_tensor + self.check_eager = False self.attrs['out_h'] = self.out_h self.attrs['out_w'] = self.out_w @@ -370,10 +382,11 @@ class TestNearestInterpOp_attr_tensor(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'nearest' @@ -495,4 +508,6 @@ class TestNearestInterpException(unittest.TestCase): if __name__ == "__main__": + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_prroi_pool_op.py b/python/paddle/fluid/tests/unittests/test_prroi_pool_op.py index efb5e05bdebca1cdfd7b13118418318e0dc06452..8e5ba7c3363a1081a05c16a6fbe1189a10463f20 100644 --- a/python/paddle/fluid/tests/unittests/test_prroi_pool_op.py +++ b/python/paddle/fluid/tests/unittests/test_prroi_pool_op.py @@ -80,14 +80,14 @@ class TestPRROIPoolOp(OpTest): self.set_data() def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_backward(self): places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) for place in places: - self.check_grad_with_place(place, ['X'], 'Out') + self.check_grad_with_place(place, ['X'], 'Out', check_eager=True) def run_net(self, place): with program_guard(Program(), Program()): @@ -197,14 +197,14 @@ class TestPRROIPoolOpTensorRoIs(OpTest): self.set_data() def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_backward(self): places = [fluid.CPUPlace()] if fluid.core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) for place in places: - self.check_grad_with_place(place, ['X'], 'Out') + self.check_grad_with_place(place, ['X'], 'Out', check_eager=True) def run_net(self, place): with program_guard(Program(), Program()): @@ -280,4 +280,6 @@ class TestPRROIPoolOpTensorRoIs(OpTest): if __name__ == '__main__': + import paddle + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_smooth_l1_loss_op.py b/python/paddle/fluid/tests/unittests/test_smooth_l1_loss_op.py index 3c825c08e8c3fc00db440a1a43b5de87b01d0c97..63e8568048d13a8dd97d983ffe55626bed5e3bfc 100644 --- a/python/paddle/fluid/tests/unittests/test_smooth_l1_loss_op.py +++ b/python/paddle/fluid/tests/unittests/test_smooth_l1_loss_op.py @@ -48,18 +48,27 @@ class TestSmoothL1LossOp1(OpTest): } def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): - self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.02) + self.check_grad( + ['X', 'Y'], 'Out', max_relative_error=0.02, check_eager=True) def test_check_grad_ingore_x(self): self.check_grad( - ['Y'], 'Out', max_relative_error=0.03, no_grad_set=set("X")) + ['Y'], + 'Out', + max_relative_error=0.03, + no_grad_set=set("X"), + check_eager=True) def test_check_grad_ingore_y(self): self.check_grad( - ['X'], 'Out', max_relative_error=0.03, no_grad_set=set('Y')) + ['X'], + 'Out', + max_relative_error=0.03, + no_grad_set=set('Y'), + check_eager=True) class TestSmoothL1LossOp2(OpTest): @@ -86,24 +95,27 @@ class TestSmoothL1LossOp2(OpTest): } def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): - self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.03) + self.check_grad( + ['X', 'Y'], 'Out', max_relative_error=0.03, check_eager=True) def test_check_grad_ingore_x(self): self.check_grad( ['Y'], 'Out', max_relative_error=0.03, - no_grad_set=set(['X', 'InsideWeight', 'OutsideWeight'])) + no_grad_set=set(['X', 'InsideWeight', 'OutsideWeight']), + check_eager=True) def test_check_grad_ingore_y(self): self.check_grad( ['X'], 'Out', max_relative_error=0.03, - no_grad_set=set(['Y', 'InsideWeight', 'OutsideWeight'])) + no_grad_set=set(['Y', 'InsideWeight', 'OutsideWeight']), + check_eager=True) class TestSmoothL1LossOpError(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_sparse_momentum_op.py b/python/paddle/fluid/tests/unittests/test_sparse_momentum_op.py index e36cb72efc725347cf4d4d480e740768775e4052..033dbd250ed611752f430be269b7057400373f7d 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_momentum_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_momentum_op.py @@ -163,7 +163,8 @@ class TestSparseMomentumOp(OpTest): pass def test_check_output(self): - self.check_output(atol=5e-3 if self.multi_precision else 1e-5) + self.check_output( + atol=5e-3 if self.multi_precision else 1e-5, check_eager=True) class TestSparseMomentumOpDtype1(TestSparseMomentumOp): @@ -240,3 +241,7 @@ class TestSparseMomentumOpMultiPrecision3(TestSparseMomentumOp): def init_use_nesterov(self): self.use_nesterov = False + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_stft_op.py b/python/paddle/fluid/tests/unittests/test_stft_op.py index f228c148d6e177085f9b684b046f6a20c48ee7b8..41e950606b3dbe9f1fe4ed0e5322b20afd629d21 100644 --- a/python/paddle/fluid/tests/unittests/test_stft_op.py +++ b/python/paddle/fluid/tests/unittests/test_stft_op.py @@ -77,12 +77,12 @@ class TestStftOp(OpTest): def test_check_output(self): paddle.enable_static() - self.check_output() + self.check_output(check_eager=True) paddle.disable_static() def test_check_grad_normal(self): paddle.enable_static() - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_eager=True) paddle.disable_static() diff --git a/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py b/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py index 2778fa0c6ace4a7f2eaedb04ba3b27a7b3e8e8e1..49699b8fafd0356bbca8e5573e01bc3d687c5ff3 100755 --- a/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py +++ b/python/paddle/fluid/tests/unittests/test_trilinear_interp_op.py @@ -131,6 +131,9 @@ class TestTrilinearInterpOp(OpTest): self.data_layout = 'NCDHW' self.init_test_case() self.op_type = "trilinear_interp" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_eager while found them in Inputs. + self.check_eager = True input_np = np.random.random(self.input_shape).astype("float32") if self.data_layout == "NCDHW": @@ -157,8 +160,10 @@ class TestTrilinearInterpOp(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False if self.actual_shape is not None: self.inputs['OutSize'] = self.actual_shape + self.check_eager = False # c++ end treat NCDHW the same way as NCHW if self.data_layout == 'NCDHW': data_layout = 'NCHW' @@ -177,10 +182,11 @@ class TestTrilinearInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'trilinear' @@ -326,6 +332,7 @@ class TestTrilinearInterpOpUint8(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "trilinear_interp" + self.check_eager = True input_np = np.random.randint( low=0, high=256, size=self.input_shape).astype("uint8") @@ -344,6 +351,7 @@ class TestTrilinearInterpOpUint8(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False self.attrs = { 'out_d': self.out_d, @@ -357,7 +365,8 @@ class TestTrilinearInterpOpUint8(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output_with_place(place=core.CPUPlace(), atol=1) + self.check_output_with_place( + place=core.CPUPlace(), atol=1, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'trilinear' @@ -467,6 +476,7 @@ class TestTrilinearInterpOp_attr_tensor(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "trilinear_interp" + self.check_eager = True self.shape_by_1Dtensor = False self.scale_by_1Dtensor = False self.attrs = { @@ -492,12 +502,14 @@ class TestTrilinearInterpOp_attr_tensor(OpTest): if self.shape_by_1Dtensor: self.inputs['OutSize'] = self.out_size + self.check_eager = False elif self.out_size is not None: size_tensor = [] for index, ele in enumerate(self.out_size): size_tensor.append(("x" + str(index), np.ones( (1)).astype('int32') * ele)) self.inputs['SizeTensor'] = size_tensor + self.check_eager = False self.attrs['out_d'] = self.out_d self.attrs['out_h'] = self.out_h @@ -508,10 +520,11 @@ class TestTrilinearInterpOp_attr_tensor(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'trilinear' diff --git a/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py b/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py index 9f46b539a04b6104bb9a363bed5d74af3fa27f00..6d072e3c377fef21ee7381708b12d1b1aa5c27b4 100755 --- a/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_trilinear_interp_v2_op.py @@ -145,6 +145,10 @@ class TestTrilinearInterpOp(OpTest): self.data_layout = 'NCDHW' self.init_test_case() self.op_type = "trilinear_interp_v2" + # NOTE(dev): some AsDispensible input is not used under imperative mode. + # Skip check_eager while found them in Inputs. + # TODO(dev): add self.python_api + self.check_eager = False input_np = np.random.random(self.input_shape).astype("float32") scale_w = 0 @@ -183,8 +187,10 @@ class TestTrilinearInterpOp(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False if self.actual_shape is not None: self.inputs['OutSize'] = self.actual_shape + self.check_eager = False # c++ end treat NCDHW the same way as NCHW if self.data_layout == 'NCDHW': data_layout = 'NCHW' @@ -208,10 +214,11 @@ class TestTrilinearInterpOp(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'trilinear' @@ -357,6 +364,8 @@ class TestTrilinearInterpOpUint8(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "trilinear_interp_v2" + # TODO(dev): add self.python_api + self.check_eager = False input_np = np.random.randint( low=0, high=256, size=self.input_shape).astype("uint8") @@ -383,6 +392,7 @@ class TestTrilinearInterpOpUint8(OpTest): self.inputs = {'X': input_np} if self.out_size is not None: self.inputs['OutSize'] = self.out_size + self.check_eager = False self.attrs = { 'out_d': self.out_d, @@ -401,7 +411,8 @@ class TestTrilinearInterpOpUint8(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output_with_place(place=core.CPUPlace(), atol=1) + self.check_output_with_place( + place=core.CPUPlace(), atol=1, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'trilinear' @@ -511,6 +522,8 @@ class TestTrilinearInterpOp_attr_tensor(OpTest): self.actual_shape = None self.init_test_case() self.op_type = "trilinear_interp_v2" + # TODO(dev): add self.python_api + self.check_eager = False self.shape_by_1Dtensor = False self.scale_by_1Dtensor = False self.attrs = { @@ -543,12 +556,14 @@ class TestTrilinearInterpOp_attr_tensor(OpTest): if self.shape_by_1Dtensor: self.inputs['OutSize'] = self.out_size + self.check_eager = False elif self.out_size is not None: size_tensor = [] for index, ele in enumerate(self.out_size): size_tensor.append(("x" + str(index), np.ones( (1)).astype('int32') * ele)) self.inputs['SizeTensor'] = size_tensor + self.check_eager = False self.attrs['out_d'] = self.out_d self.attrs['out_h'] = self.out_h @@ -565,10 +580,11 @@ class TestTrilinearInterpOp_attr_tensor(OpTest): self.outputs = {'Out': output_np} def test_check_output(self): - self.check_output() + self.check_output(check_eager=self.check_eager) def test_check_grad(self): - self.check_grad(['X'], 'Out', in_place=True) + self.check_grad( + ['X'], 'Out', in_place=True, check_eager=self.check_eager) def init_test_case(self): self.interp_method = 'trilinear'