未验证 提交 116305ea 编写于 作者: L Leo Chen 提交者: GitHub

Improve performance of elementwise_add grad op (#29187)

* pass stop_gradient for cast op

* improve performance of elementwise_add grad

* use tensor copy async

* dygraph branch

* fix dygraph branch

* add ut
上级 07c67d5a
...@@ -144,7 +144,20 @@ class ElementwiseAddGradKernel : public ElemwiseGradKernel<T> { ...@@ -144,7 +144,20 @@ class ElementwiseAddGradKernel : public ElemwiseGradKernel<T> {
// skip out // skip out
auto *out = dout; auto *out = dout;
if (dx != nullptr && dy != nullptr && (dx->dims() == dy->dims())) { // Special case when dy is not needed and dx doesn't reduce
if (dx != nullptr && dy == nullptr && dx->dims() == dout->dims()) {
VLOG(4) << "Special case when dy is not needed and dx doesn't "
"reduce";
framework::TensorCopy(
*dout, ctx.GetPlace(),
ctx.template device_context<platform::DeviceContext>(), dx);
} else if (dx == nullptr && dy != nullptr && dy->dims() == dout->dims()) {
VLOG(4) << "Special case when dx is not needed and dy doesn't "
"reduce";
framework::TensorCopy(
*dout, ctx.GetPlace(),
ctx.template device_context<platform::DeviceContext>(), dy);
} else if (dx != nullptr && dy != nullptr && (dx->dims() == dy->dims())) {
elementwise_add_grad<DeviceContext, T>(ctx, x, y, out, dout, dx, dy); elementwise_add_grad<DeviceContext, T>(ctx, x, y, out, dout, dx, dy);
} else { } else {
default_elementwise_add_grad<DeviceContext, T>(ctx, x, y, out, dout, dx, default_elementwise_add_grad<DeviceContext, T>(ctx, x, y, out, dout, dx,
......
...@@ -179,6 +179,7 @@ def monkey_patch_variable(): ...@@ -179,6 +179,7 @@ def monkey_patch_variable():
outputs={"Out": [out]}, outputs={"Out": [out]},
attrs={"in_dtype": self.dtype, attrs={"in_dtype": self.dtype,
"out_dtype": out.dtype}) "out_dtype": out.dtype})
out.stop_gradient = self.stop_gradient
return out return out
def _scalar_op_(var, scale, bias): def _scalar_op_(var, scale, bias):
......
...@@ -224,6 +224,11 @@ def cast(x, dtype): ...@@ -224,6 +224,11 @@ def cast(x, dtype):
x = paddle.to_tensor([2, 3, 4], 'float64') x = paddle.to_tensor([2, 3, 4], 'float64')
y = paddle.cast(x, 'uint8') y = paddle.cast(x, 'uint8')
""" """
if in_dygraph_mode():
if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)
out = core.ops.cast(x, 'in_dtype', x.dtype, 'out_dtype', dtype)
check_variable_and_dtype( check_variable_and_dtype(
x, 'x', x, 'x',
['bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'uint8'], ['bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'uint8'],
...@@ -234,7 +239,8 @@ def cast(x, dtype): ...@@ -234,7 +239,8 @@ def cast(x, dtype):
], 'cast') ], 'cast')
helper = LayerHelper('cast', **locals()) helper = LayerHelper('cast', **locals())
out = helper.create_variable_for_type_inference(dtype=dtype) out = helper.create_variable_for_type_inference(
dtype=dtype, stop_gradient=x.stop_gradient)
helper.append_op( helper.append_op(
type='cast', type='cast',
inputs={'X': [x]}, inputs={'X': [x]},
......
...@@ -257,6 +257,19 @@ class TestMathOpPatches(unittest.TestCase): ...@@ -257,6 +257,19 @@ class TestMathOpPatches(unittest.TestCase):
fetch_list=[b]) fetch_list=[b])
self.assertTrue(numpy.allclose(-a_np, b_np)) self.assertTrue(numpy.allclose(-a_np, b_np))
@prog_scope()
def test_astype(self):
a = fluid.layers.data(name="a", shape=[10, 1])
b = a.astype('float32')
place = fluid.CPUPlace()
exe = fluid.Executor(place)
a_np = numpy.random.uniform(-1, 1, size=[10, 1]).astype('float64')
b_np = exe.run(fluid.default_main_program(),
feed={"a": a_np},
fetch_list=[b])
self.assertTrue(numpy.allclose(a_np.astype('float32'), b_np))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册