diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py b/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py index 463c50658cd328e5f7c5b05df80397ae87e00d55..8075b65b1945b932a9366f84713d2eade2f36929 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py @@ -23,7 +23,7 @@ from codegen_utils import ParseYamlForward, GetInplacedFunctionName ########################### ## Global Configurations ## ########################### -skipped_forward_api_names = set(["scale"]) +skipped_forward_api_names = set([]) def SkipAPIGeneration(forward_api_name): diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 0dcc8ee517fb1806afa81c04f5b95b1a3da38130..d7ec3276d8b7905cb60d004fe30fb8b5ed655f01 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -11779,6 +11779,9 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): """ + if in_dygraph_mode(): + out = _C_ops.final_state_scale(x, scale, float(bias), bias_after_scale) + return dygraph_utils._append_activation_in_dygraph(out) if _non_static_mode(): _scale = scale.numpy().item(0) if isinstance(scale, Variable) else scale out = _C_ops.scale(x, 'scale', diff --git a/python/paddle/fluid/tests/unittests/test_scale_op.py b/python/paddle/fluid/tests/unittests/test_scale_op.py index d432b8057f624831f40b8cd48a0ede694f8d0a55..04ddb5a788d6f4ba18248957470768423da98e89 100644 --- a/python/paddle/fluid/tests/unittests/test_scale_op.py +++ b/python/paddle/fluid/tests/unittests/test_scale_op.py @@ -27,6 +27,7 @@ from paddle.static import Program, program_guard class TestScaleOp(OpTest): def setUp(self): self.op_type = "scale" + self.python_api = paddle.scale self.dtype = np.float64 self.init_dtype_type() self.inputs = {'X': np.random.random((10, 10)).astype(self.dtype)} @@ -39,15 +40,16 @@ class TestScaleOp(OpTest): pass def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_eager=True) class TestScaleOpScaleVariable(OpTest): def setUp(self): self.op_type = "scale" + self.python_api = paddle.scale self.dtype = np.float64 self.init_dtype_type() self.scale = -2.3 @@ -62,10 +64,10 @@ class TestScaleOpScaleVariable(OpTest): pass def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_eager=True) class TestScaleOpSelectedRows(unittest.TestCase): @@ -144,18 +146,19 @@ class TestScaleFp16Op(TestScaleOp): def test_check_output(self): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_output_with_place(place, atol=0.002) + self.check_output_with_place(place, atol=0.002, check_eager=True) def test_check_grad(self): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_grad_with_place( - place, ["X"], "Out", max_relative_error=0.05) + place, ["X"], "Out", max_relative_error=0.05, check_eager=True) class TestScaleBF16Op(OpTest): def setUp(self): self.op_type = "scale" + self.python_api = paddle.scale self.dtype = np.uint16 self.attrs = {'scale': -2.3} x = np.random.random((10, 10)).astype(np.float32) @@ -164,10 +167,10 @@ class TestScaleBF16Op(OpTest): self.outputs = {'Out': convert_float_to_uint16(out)} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', numeric_grad_delta=0.8) + self.check_grad(['X'], 'Out', numeric_grad_delta=0.8, check_eager=True) @unittest.skipIf(not core.is_compiled_with_cuda(), diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index adca732dfdaa0b409ee1675ee5a7464d5e41f703..c552fb4c09ca535475504439a2ee7f5427a32cdd 100755 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -98,10 +98,13 @@ def scale_(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): Inplace version of ``scale`` API, the output Tensor will be inplaced with input ``x``. Please refer to :ref:`api_tensor_scale`. """ - _scale = scale.numpy().item(0) if isinstance(scale, Variable) else scale - return _C_ops.scale_(x, 'scale', - float(_scale), 'bias', - float(bias), 'bias_after_scale', bias_after_scale) + if in_dygraph_mode(): + return _C_ops.final_state_scale_(x, scale, float(bias), bias_after_scale) + if _in_legacy_dygraph(): + _scale = scale.numpy().item(0) if isinstance(scale, Variable) else scale + return _C_ops.scale_(x, 'scale', + float(_scale), 'bias', + float(bias), 'bias_after_scale', bias_after_scale) def pow(x, y, name=None): diff --git a/python/paddle/utils/code_gen/api.yaml b/python/paddle/utils/code_gen/api.yaml index 2a0026fb509332a0f2b6837f904950db957547a6..507f8b3f36097e7ccb81cb0c095bb65d9321c976 100644 --- a/python/paddle/utils/code_gen/api.yaml +++ b/python/paddle/utils/code_gen/api.yaml @@ -1345,6 +1345,7 @@ kernel : func : scale, scale_sr inplace : (x -> out) + backward : scale_grad - api : scatter args : (Tensor x, Tensor index, Tensor updates, bool overwrite) diff --git a/python/paddle/utils/code_gen/backward.yaml b/python/paddle/utils/code_gen/backward.yaml index 80ec2d9b84e54c5440495d1178cd9f1e44e1c8b4..cb72040aa4ea52bf0ef3055138654cd4e3bbe5ab 100644 --- a/python/paddle/utils/code_gen/backward.yaml +++ b/python/paddle/utils/code_gen/backward.yaml @@ -947,7 +947,7 @@ - backward_api : scale_grad forward : scale (Tensor x, Scalar scale, float bias, bool bias_after_scale) -> Tensor(out) - args : (Tensor out_grad, Scalar scale, float bias=0.0, bool bias_after_scale=true) + args : (Tensor out_grad, Scalar scale=1.0, float bias=0.0, bool bias_after_scale=true) output : Tensor(x_grad) invoke : scale(out_grad, scale, bias, bias_after_scale)