未验证 提交 bcb663cc 编写于 作者: C Chen Weihang 提交者: GitHub

[Phi] Support scale dygraph final state (#41321)

* support scale final state

* fix inplace error

* pass arg directly

* pass arg directly for inplace api

* fix type
上级 c5285cc5
......@@ -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):
......
......@@ -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',
......
......@@ -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(),
......
......@@ -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):
......
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册