Skip to content

Commit

Permalink
【Hackathon 5th No.3】为 Paddle 新增 masked_fill API (PaddlePaddle#57355)
Browse files Browse the repository at this point in the history
* add masked_fill for paddle

* update doc

* update some test case

* remove full_like

* update test codes

* update test cases

* recover codes

* update test codes

* fix gradients error

* update test codes

* fix

* add bf16 test cases

* update code-block

* update code-block

* update test codes

* Update __init__.py

* fix

* fix code style and recover third_party

* add v grad check

* add scalar value case

* fix test case

* use logical_not

* fix doc style

* Update manipulation.py
  • Loading branch information
AndSonder authored Nov 2, 2023
1 parent 0ab3175 commit fcbb519
Show file tree
Hide file tree
Showing 5 changed files with 472 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@
view,
view_as,
unfold,
masked_fill,
masked_fill_,
)

from .tensor.math import ( # noqa: F401
Expand Down Expand Up @@ -907,6 +909,8 @@
'i1e',
'polygamma',
'polygamma_',
'masked_fill',
'masked_fill_',
'hypot',
'hypot_',
]
4 changes: 4 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
from .manipulation import view # noqa: F401
from .manipulation import view_as # noqa: F401
from .manipulation import unfold # noqa: F401
from .manipulation import masked_fill # noqa: F401
from .manipulation import masked_fill_ # noqa: F401
from .math import abs # noqa: F401
from .math import abs_ # noqa: F401
from .math import acos # noqa: F401
Expand Down Expand Up @@ -695,6 +697,8 @@
'i1e',
'polygamma',
'polygamma_',
'masked_fill',
'masked_fill_',
'diag_embed',
'atan2',
'diagflat',
Expand Down
70 changes: 70 additions & 0 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4722,6 +4722,76 @@ def moveaxis(x, source, destination, name=None):
return out


def masked_fill(x, mask, value, name=None):
"""
Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.
Args:
x (Tensor) : The Destination Tensor. Supported data types are float,
double, int, int64_t,float16 and bfloat16.
mask (Tensor): The boolean tensor indicate the position to be filled.
The data type of mask must be bool.
value (Scalar or 0-D Tensor): The value used to fill the target tensor.
Supported data types are float, double, int, int64_t,float16 and bfloat16.
name(str, optional): The default value is None. Normally there is no
need for user to set this property. For more information, please
refer to :ref:`api_guide_Name`.
Returns:
Tensor, same dimention and dtype with x.
Examples:
.. code-block:: python
>>> # doctest: +REQUIRES(env:GPU)
>>> import paddle
>>> x = paddle.ones((3, 3), dtype="float32")
>>> mask = paddle.to_tensor([[True, True, False]])
>>> print(mask)
Tensor(shape=[1, 3], dtype=bool, place=Place(gpu:0), stop_gradient=True,
[[True , True , False]])
>>> out = paddle.masked_fill(x, mask, 2)
>>> print(out)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[2., 2., 1.],
[2., 2., 1.],
[2., 2., 1.]])
"""
if np.isscalar(value):
value = paddle.full([], value, x.dtype)

mask = paddle.logical_not(mask)
out = paddle.where(mask, x, value)
return out


@inplace_apis_in_dygraph_only
def masked_fill_(x, mask, value, name=None):
"""
Inplace version of ``masked_fill`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_paddle_masked_fill`.
Examples:
.. code-block:: python
>>> # doctest: +REQUIRES(env:GPU)
>>> import paddle
>>> x = paddle.ones((3, 3), dtype="float32")
>>> mask = paddle.to_tensor([[True, False, False]])
>>> out = paddle.masked_fill_(x, mask, 2)
>>> print(out)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[2., 1., 1.],
[2., 1., 1.],
[2., 1., 1.]])
"""
if np.isscalar(value):
value = paddle.full([], value, x.dtype)

mask = paddle.logical_not(mask)
out = paddle.where_(mask, x, value)
return out


def non_negative_axis(arr, axis):
ndim = len(arr.shape)
if axis >= 0:
Expand Down
66 changes: 66 additions & 0 deletions test/legacy_test/test_inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,72 @@ def test_backward_success_2(self):
np.testing.assert_array_equal(grad_var_a_inplace, grad_var_a)


class TestDygraphInplaceMaskedFill(TestDygraphInplace):
def non_inplace_api_processing(self, var):
return paddle.masked_fill(var, self.mask, self.value)

def inplace_api_processing(self, var):
return paddle.masked_fill_(var, self.mask, self.value)

def init_data(self):
self.dtype = "float32"
self.input_var_numpy = np.random.uniform(-5, 5, [30, 3])
self.value = np.random.uniform(-10, 10)
self.value = paddle.to_tensor(self.value, dtype=self.dtype)
self.mask = np.random.randint(0, 2, [30, 3]).astype('bool')
self.mask = paddle.to_tensor(self.mask, dtype='bool')

def test_forward_version(self):
with paddle.base.dygraph.guard():
var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype)
self.assertEqual(var.inplace_version, 0)

inplace_var = self.inplace_api_processing(var)
self.assertEqual(var.inplace_version, 2)

inplace_var[0] = 2
self.assertEqual(var.inplace_version, 3)

inplace_var = self.inplace_api_processing(inplace_var)
self.assertEqual(var.inplace_version, 5)

def test_backward_error(self):
# It raises an error because the inplace operator will result
# in incorrect gradient computation.
with paddle.base.dygraph.guard():
var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype)
var_a.stop_gradient = False

var_b = var_a**2

# Here, the gradient computation will use the value of var_b
var_c = var_b**2
self.inplace_api_processing(var_b)

loss = paddle.nn.functional.relu(var_c)
with self.assertRaisesRegex(
RuntimeError,
f"received tensor_version:{2} != wrapper_version_snapshot:{0}",
):
loss.backward()


class TestDygraphInplaceMaskedFill2(TestDygraphInplaceMaskedFill):
def non_inplace_api_processing(self, var):
return paddle.masked_fill(var, self.mask, self.value)

def inplace_api_processing(self, var):
return paddle.masked_fill_(var, self.mask, self.value)

def init_data(self):
self.dtype = "float32"
self.input_var_numpy = np.random.uniform(-5, 5, [30, 3])
self.value = np.random.uniform(-10, 10)
self.value = paddle.to_tensor(self.value, dtype=self.dtype)
self.mask = np.random.randint(0, 2, [30, 1]).astype('bool')
self.mask = paddle.to_tensor(self.mask, dtype='bool')


class TestDygraphInplaceWithContinuous(TestDygraphInplace):
def init_data(self):
self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1])
Expand Down
Loading

0 comments on commit fcbb519

Please sign in to comment.