Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clean fluid] Add inner function _elementwise_op_with_axis #48748

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions python/paddle/fluid/contrib/layers/rnn_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ def get_single_direction_output(
new_hidden = unit_list[i](step_input, pre_hidden)

if mask:
new_hidden = layers.elementwise_mul(
new_hidden = paddle.tensor.math._multiply_with_axis(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要使用内部接口的原因是什么?paddle.multiply是否能满足需求?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle.add/subtract/multiply/divide 这几个 API 没有 axis 参数,然而需要清理的 elementwise.add/sub/mul/div API 有 axis 参数并且在多个地方都使用了这个参数,因此需要封装一个内部接口,专门用来处理带有 axis 参数的四则运算
(注:对于没有用到 axis 参数的 elementwise API,先前已经尽可能用 paddle.add/subtract/multiply/divide 来代替了)

paddle.add/subtract/multiply/divide API doesn't have the parameter axis. However, elementwise.add/sub/mul/div API has axis and the corresponding API is called in several places. Therefore, it is necessary to encapsulate an internal interface, which is specially used to deal with arithmetic operations with axis parameters.

new_hidden, step_mask, axis=0
) - layers.elementwise_mul(
) - paddle.tensor.math._multiply_with_axis(
pre_hidden, (step_mask - 1), axis=0
)
rnn.update_memory(pre_hidden, new_hidden)
Expand Down Expand Up @@ -661,14 +661,14 @@ def get_single_direction_output(
)

if mask:
new_hidden = layers.elementwise_mul(
new_hidden = paddle.tensor.math._multiply_with_axis(
new_hidden, step_mask, axis=0
) - layers.elementwise_mul(
) - paddle.tensor.math._multiply_with_axis(
pre_hidden, (step_mask - 1), axis=0
)
new_cell = layers.elementwise_mul(
new_cell = paddle.tensor.math._multiply_with_axis(
new_cell, step_mask, axis=0
) - layers.elementwise_mul(
) - paddle.tensor.math._multiply_with_axis(
pre_cell, (step_mask - 1), axis=0
)

Expand Down
4 changes: 1 addition & 3 deletions python/paddle/fluid/layer_helper_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ def to_variable(self, value, name=None):
)

def _create_weight_normalize(self, attr, shape, dtype):
from .layers import elementwise_mul

# Remove these ops when LayerHelper and layers support indicating
# program and block.
def __norm_op(
Expand Down Expand Up @@ -272,7 +270,7 @@ def __weight_normalize(g, v, dim):
# Currently, elementwise_mul only support broadcast when the shape
# of y is a subset of the shape of x. Thus, we reshape y to squeeze
# to achieve the subset.
w = elementwise_mul(
w = paddle.tensor.math._multiply_with_axis(
x=v,
y=scale
if dim is None
Expand Down
184 changes: 0 additions & 184 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@
'lod_reset',
'relu',
'elementwise_add',
'elementwise_div',
'elementwise_sub',
'elementwise_mul',
'clip',
'clip_by_norm',
'mean',
Expand Down Expand Up @@ -2839,96 +2837,6 @@ def gen_data():
return _elementwise_op(LayerHelper('elementwise_add', **locals()))


@deprecated(since="2.0.0", update_to="paddle.divide")
def elementwise_div(x, y, axis=-1, act=None, name=None):
"""

Examples:

.. code-block:: python

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
return {
"x": np.array([2, 3, 4]).astype('float32'),
"y": np.array([1, 5, 2]).astype('float32')
}
paddle.enable_static()
x = fluid.data(name="x", shape=[3], dtype='float32')
y = fluid.data(name="y", shape=[3], dtype='float32')
z = fluid.layers.elementwise_div(x, y)
# z = x / y

place = fluid.CPUPlace()
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])

print(z_value) # [2., 0.6, 2.]


.. code-block:: python

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
return {
"x": np.ones((2, 3, 4, 5)).astype('float32'),
"y": np.zeros((3, 4)).astype('float32')
}
paddle.enable_static()
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[3,4], dtype='float32')
z = fluid.layers.elementwise_div(x, y, axis=1)
# z = x / y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])

print(z_value) # z.shape=[2,3,4,5]


.. code-block:: python

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
return {
"x": np.random.randint(1, 5, size=[2, 3, 4, 5]).astype('float32'),
"y": np.random.randint(1, 5, size=[5]).astype('float32')
}
paddle.enable_static()
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[5], dtype='float32')
z = fluid.layers.elementwise_div(x, y, axis=3)
# z = x / y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])
print(z_value) # z.shape=[2,3,4,5]

"""
if _non_static_mode():
return _elementwise_op_in_dygraph(
x, y, axis=axis, act=act, op_name='elementwise_div'
)

return _elementwise_op(LayerHelper('elementwise_div', **locals()))


def elementwise_sub(x, y, axis=-1, act=None, name=None):
"""

Expand Down Expand Up @@ -3018,101 +2926,9 @@ def gen_data():
return _elementwise_op(LayerHelper('elementwise_sub', **locals()))


@deprecated(since="2.0.0", update_to="paddle.multiply")
def elementwise_mul(x, y, axis=-1, act=None, name=None):
"""

Examples:

.. code-block:: python

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
return {
"x": np.array([2, 3, 4]).astype('float32'),
"y": np.array([1, 5, 2]).astype('float32')
}
paddle.enable_static()
x = fluid.data(name="x", shape=[3], dtype='float32')
y = fluid.data(name="y", shape=[3], dtype='float32')
z = fluid.layers.elementwise_mul(x, y)
# z = x * y

place = fluid.CPUPlace()
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])

print(z_value) # [2., 15., 8.]


.. code-block:: python

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
return {
"x": np.ones((2, 3, 4, 5)).astype('float32'),
"y": np.zeros((3, 4)).astype('float32')
}
paddle.enable_static()
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[3,4], dtype='float32')
z = fluid.layers.elementwise_mul(x, y, axis=1)
# z = x * y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])

print(z_value) # z.shape=[2,3,4,5]


.. code-block:: python

import paddle.fluid as fluid
import numpy as np
import paddle

def gen_data():
return {
"x": np.random.randint(1, 5, size=[2, 3, 4, 5]).astype('float32'),
"y": np.random.randint(1, 5, size=[5]).astype('float32')
}
paddle.enable_static()
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
y = fluid.data(name="y", shape=[5], dtype='float32')
z = fluid.layers.elementwise_mul(x, y, axis=3)
# z = x * y

place = fluid.CPUPlace()
exe = fluid.Executor(place)

z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])
print(z_value) # z.shape=[2,3,4,5]

"""
if _non_static_mode():
return _elementwise_op_in_dygraph(
x, y, axis=axis, act=act, op_name='elementwise_mul'
)

return _elementwise_op(LayerHelper('elementwise_mul', **locals()))


for func in [
elementwise_add,
elementwise_div,
elementwise_sub,
elementwise_mul,
]:
op_proto = OpProtoHolder.instance().get_op_proto(func.__name__)

Expand Down
16 changes: 10 additions & 6 deletions python/paddle/fluid/layers/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ def __getitem__(self, item):

def _maybe_copy(state, new_state, step_mask):
"""update rnn state or just pass the old state through"""
new_state = nn.elementwise_mul(
new_state = paddle.tensor.math._multiply_with_axis(
new_state, step_mask, axis=0
) + nn.elementwise_mul(state, (1 - step_mask), axis=0)
) + paddle.tensor.math._multiply_with_axis(state, (1 - step_mask), axis=0)
return new_state


Expand Down Expand Up @@ -839,9 +839,11 @@ def _maybe_copy(state, new_state, step_mask):
# otherwise, renamed bool gradients of would be summed up leading
# to sum(bool) error.
step_mask.stop_gradient = True
new_state = nn.elementwise_mul(
new_state = paddle.tensor.math._multiply_with_axis(
state, step_mask, axis=0
) - nn.elementwise_mul(new_state, (step_mask - 1), axis=0)
) - paddle.tensor.math._multiply_with_axis(
new_state, (step_mask - 1), axis=0
)
if convert_dtype(state_dtype) in ["bool"]:
new_state = tensor.cast(new_state, dtype=state_dtype)
return new_state
Expand Down Expand Up @@ -994,9 +996,11 @@ def _maybe_copy(state, new_state, step_mask):
# otherwise, renamed bool gradients of would be summed up leading
# to sum(bool) error.
step_mask.stop_gradient = True
new_state = nn.elementwise_mul(
new_state = paddle.tensor.math._multiply_with_axis(
state, step_mask, axis=0
) - nn.elementwise_mul(new_state, (step_mask - 1), axis=0)
) - paddle.tensor.math._multiply_with_axis(
new_state, (step_mask - 1), axis=0
)
if convert_dtype(state_dtype) in ["bool"]:
new_state = tensor.cast(new_state, dtype=state_dtype)
return new_state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def forward(self, input):
y = paddle.nn.functional.relu(y)
y = self._excitation(y)
y = paddle.nn.functional.sigmoid(y)
y = fluid.layers.elementwise_mul(x=input, y=y, axis=0)
y = paddle.tensor.math._multiply_with_axis(x=input, y=y, axis=0)
return y


Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/tests/unittests/dist_se_resnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ def squeeze_excitation(self, input, num_channels, reduction_ratio):
),
act='sigmoid',
)
scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)
scale = paddle.tensor.math._multiply_with_axis(
x=input, y=excitation, axis=0
)
return scale


Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/dist_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ def beam_search():
}
for cache in caches
]
pre_pos = layers.elementwise_mul(
pre_pos = paddle.tensor.math._multiply_with_axis(
x=layers.fill_constant_batch_size_like(
input=pre_enc_output, # can't use pre_ids here since it has lod
value=1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ def _expand_to_beam_size(self, x):
return x

def _real_state(self, state, new_state, step_mask):
new_state = fluid.layers.elementwise_mul(
new_state = paddle.tensor.math._multiply_with_axis(
new_state, step_mask, axis=0
) - fluid.layers.elementwise_mul(state, (step_mask - 1), axis=0)
) - paddle.tensor.math._multiply_with_axis(
state, (step_mask - 1), axis=0
)
return new_state

def _gather(self, x, indices, batch_pos):
Expand Down Expand Up @@ -450,7 +452,7 @@ def beam_search(self, inputs):
[-1, -1, self.tar_vocab_size],
),
noend_mask_tensor,
) - fluid.layers.elementwise_mul(
) - paddle.tensor.math._multiply_with_axis(
step_log_probs, (beam_finished - 1), axis=0
)
log_probs = fluid.layers.elementwise_add(
Expand Down Expand Up @@ -685,9 +687,11 @@ def _expand_to_beam_size(self, x):
return x

def _real_state(self, state, new_state, step_mask):
new_state = fluid.layers.elementwise_mul(
new_state = paddle.tensor.math._multiply_with_axis(
new_state, step_mask, axis=0
) - fluid.layers.elementwise_mul(state, (step_mask - 1), axis=0)
) - paddle.tensor.math._multiply_with_axis(
state, (step_mask - 1), axis=0
)
return new_state

def _gather(self, x, indices, batch_pos):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def forward(self, input):
y = paddle.nn.functional.relu(y)
y = self._excitation(y)
y = paddle.nn.functional.sigmoid(y)
y = fluid.layers.elementwise_mul(x=input, y=y, axis=0)
y = paddle.tensor.math._multiply_with_axis(x=input, y=y, axis=0)
return y


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,9 @@ def mask_probs(probs, finished, noend_mask_tensor):
[-1, -1, self.trg_vocab_size],
),
noend_mask_tensor,
) - layers.elementwise_mul(probs, (finished - 1), axis=0)
) - paddle.tensor.math._multiply_with_axis(
probs, (finished - 1), axis=0
)
return probs

def gather(input, indices, batch_pos):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def fp16_enabled(self):
return True

def set_test_op(self):
self.op = paddle.fluid.layers.elementwise_mul
self.op = paddle.tensor.math._multiply_with_axis

def set_feed_attr(self):
self.feed_shape = [x.shape for x in self.feed_fp32.values()]
Expand Down
Loading