未验证 提交 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 ...@@ -23,7 +23,7 @@ from codegen_utils import ParseYamlForward, GetInplacedFunctionName
########################### ###########################
## Global Configurations ## ## Global Configurations ##
########################### ###########################
skipped_forward_api_names = set(["scale"]) skipped_forward_api_names = set([])
def SkipAPIGeneration(forward_api_name): 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): ...@@ -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(): if _non_static_mode():
_scale = scale.numpy().item(0) if isinstance(scale, Variable) else scale _scale = scale.numpy().item(0) if isinstance(scale, Variable) else scale
out = _C_ops.scale(x, 'scale', out = _C_ops.scale(x, 'scale',
......
...@@ -27,6 +27,7 @@ from paddle.static import Program, program_guard ...@@ -27,6 +27,7 @@ from paddle.static import Program, program_guard
class TestScaleOp(OpTest): class TestScaleOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = "scale" self.op_type = "scale"
self.python_api = paddle.scale
self.dtype = np.float64 self.dtype = np.float64
self.init_dtype_type() self.init_dtype_type()
self.inputs = {'X': np.random.random((10, 10)).astype(self.dtype)} self.inputs = {'X': np.random.random((10, 10)).astype(self.dtype)}
...@@ -39,15 +40,16 @@ class TestScaleOp(OpTest): ...@@ -39,15 +40,16 @@ class TestScaleOp(OpTest):
pass pass
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_eager=True)
class TestScaleOpScaleVariable(OpTest): class TestScaleOpScaleVariable(OpTest):
def setUp(self): def setUp(self):
self.op_type = "scale" self.op_type = "scale"
self.python_api = paddle.scale
self.dtype = np.float64 self.dtype = np.float64
self.init_dtype_type() self.init_dtype_type()
self.scale = -2.3 self.scale = -2.3
...@@ -62,10 +64,10 @@ class TestScaleOpScaleVariable(OpTest): ...@@ -62,10 +64,10 @@ class TestScaleOpScaleVariable(OpTest):
pass pass
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_eager=True)
class TestScaleOpSelectedRows(unittest.TestCase): class TestScaleOpSelectedRows(unittest.TestCase):
...@@ -144,18 +146,19 @@ class TestScaleFp16Op(TestScaleOp): ...@@ -144,18 +146,19 @@ class TestScaleFp16Op(TestScaleOp):
def test_check_output(self): def test_check_output(self):
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
if core.is_float16_supported(place): 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): def test_check_grad(self):
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
if core.is_float16_supported(place): if core.is_float16_supported(place):
self.check_grad_with_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): class TestScaleBF16Op(OpTest):
def setUp(self): def setUp(self):
self.op_type = "scale" self.op_type = "scale"
self.python_api = paddle.scale
self.dtype = np.uint16 self.dtype = np.uint16
self.attrs = {'scale': -2.3} self.attrs = {'scale': -2.3}
x = np.random.random((10, 10)).astype(np.float32) x = np.random.random((10, 10)).astype(np.float32)
...@@ -164,10 +167,10 @@ class TestScaleBF16Op(OpTest): ...@@ -164,10 +167,10 @@ class TestScaleBF16Op(OpTest):
self.outputs = {'Out': convert_float_to_uint16(out)} self.outputs = {'Out': convert_float_to_uint16(out)}
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_eager=True)
def test_check_grad(self): 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(), @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): ...@@ -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``. Inplace version of ``scale`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_tensor_scale`. Please refer to :ref:`api_tensor_scale`.
""" """
_scale = scale.numpy().item(0) if isinstance(scale, Variable) else scale if in_dygraph_mode():
return _C_ops.scale_(x, 'scale', return _C_ops.final_state_scale_(x, scale, float(bias), bias_after_scale)
float(_scale), 'bias', if _in_legacy_dygraph():
float(bias), 'bias_after_scale', bias_after_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)
def pow(x, y, name=None): def pow(x, y, name=None):
......
...@@ -1345,6 +1345,7 @@ ...@@ -1345,6 +1345,7 @@
kernel : kernel :
func : scale, scale_sr func : scale, scale_sr
inplace : (x -> out) inplace : (x -> out)
backward : scale_grad
- api : scatter - api : scatter
args : (Tensor x, Tensor index, Tensor updates, bool overwrite) args : (Tensor x, Tensor index, Tensor updates, bool overwrite)
......
...@@ -947,7 +947,7 @@ ...@@ -947,7 +947,7 @@
- backward_api : scale_grad - backward_api : scale_grad
forward : scale (Tensor x, Scalar scale, float bias, bool bias_after_scale) -> Tensor(out) 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) output : Tensor(x_grad)
invoke : scale(out_grad, scale, bias, bias_after_scale) 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.
先完成此消息的编辑!
想要评论请 注册