Skip to content

Commit

Permalink
speed up paddle.add paddle.nn.Linear (#32125)
Browse files Browse the repository at this point in the history
* modify API nn.Bilinear's doc, test=develop

* speed up paddle.add paddle.nn.Linear, test=develop

* fix bug, test=develop
  • Loading branch information
wanghuancoder authored May 27, 2021
1 parent 6c399d9 commit 481ee79
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
11 changes: 6 additions & 5 deletions python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,11 +1446,12 @@ def linear(x, weight, bias=None, name=None):
# [2.1077576 2.1077576 2.1077576 2.1077576 ]]
"""
if in_dygraph_mode():
pre_bias = _varbase_creator(dtype=x.dtype)
core.ops.matmul(x, weight, pre_bias, 'transpose_X', False,
'transpose_Y', False, "alpha", 1)
return dygraph_utils._append_bias_in_dygraph(
pre_bias, bias, axis=len(x.shape) - 1)
pre_bias = core.ops.matmul_v2(x, weight)

if bias is None:
return pre_bias

return core.ops.elementwise_add(pre_bias, bias)
else:
helper = LayerHelper('linear', **locals())
dtype = x.dtype
Expand Down
8 changes: 3 additions & 5 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,11 @@ def add(x, y, name=None):
print(z) # [3., 8., 6. ]
"""
op_type = 'elementwise_add'
axis = -1

if in_dygraph_mode():
return _elementwise_op_in_dygraph(
x, y, axis=axis, op_name=op_type)
return core.ops.elementwise_add(x, y)

return _elementwise_op(LayerHelper(op_type, **locals()))
return _elementwise_op(LayerHelper('elementwise_add', **locals()))


@inplace_apis_in_dygraph_only
Expand Down

0 comments on commit 481ee79

Please sign in to comment.