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

【PIR API adaptor No.145】masked_select #58929

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 10 additions & 12 deletions python/paddle/tensor/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,21 +913,19 @@ def masked_select(x, mask, name=None):
>>> print(out.numpy())
[1. 5. 6. 9.]
"""

if in_dynamic_mode():
check_variable_and_dtype(
x,
'x',
['float16', 'float32', 'float64', 'int32', 'int64', 'uint16'],
'paddle.tensor.search.mask_select',
)
check_variable_and_dtype(
mask, 'mask', ['bool'], 'paddle.tensor.search.masked_select'
)
0x45f marked this conversation as resolved.
Show resolved Hide resolved
if in_dynamic_or_pir_mode():
return _C_ops.masked_select(x, mask)

else:
helper = LayerHelper("masked_select", **locals())
check_variable_and_dtype(
x,
'x',
['float16', 'float32', 'float64', 'int32', 'int64', 'uint16'],
'paddle.tensor.search.mask_select',
)
check_variable_and_dtype(
mask, 'mask', ['bool'], 'paddle.tensor.search.masked_select'
)
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(
type='masked_select',
Expand Down
17 changes: 11 additions & 6 deletions test/legacy_test/test_masked_select_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import paddle
from paddle.base import core
from paddle.pir_utils import test_with_pir_api


def np_masked_select(x, mask):
Expand All @@ -42,10 +43,10 @@ def setUp(self):
self.outputs = {'Y': out}

def test_check_output(self):
self.check_output()
self.check_output(check_pir=True)

def test_check_grad(self):
self.check_grad(['X'], 'Y')
self.check_grad(['X'], 'Y', check_pir=True)

def init(self):
self.shape = (50, 3)
Expand Down Expand Up @@ -77,10 +78,10 @@ def setUp(self):
self.outputs = {'Y': out}

def test_check_output(self):
self.check_output()
self.check_output(check_pir=True)

def test_check_grad(self):
self.check_grad(['X'], 'Y')
self.check_grad(['X'], 'Y', check_pir=True)

def init(self):
self.shape = (50, 3)
Expand Down Expand Up @@ -114,10 +115,12 @@ def setUp(self):
self.outputs = {'Y': convert_float_to_uint16(out)}

def test_check_output(self):
self.check_output_with_place(core.CUDAPlace(0))
self.check_output_with_place(core.CUDAPlace(0), check_pir=True)

def test_check_grad(self):
self.check_grad_with_place(core.CUDAPlace(0), ['X'], 'Y')
self.check_grad_with_place(
core.CUDAPlace(0), ['X'], 'Y', check_pir=True
)

def init(self):
self.shape = (50, 3)
Expand Down Expand Up @@ -146,6 +149,7 @@ def test_imperative_mode(self):
np.testing.assert_allclose(out.numpy(), np_out, rtol=1e-05)
paddle.enable_static()

@test_with_pir_api
def test_static_mode(self):
shape = [8, 9, 6]
x = paddle.static.data(shape=shape, dtype='float32', name='x')
Expand All @@ -170,6 +174,7 @@ class TestMaskedSelectError(unittest.TestCase):
def setUp(self):
paddle.enable_static()

@test_with_pir_api
def test_error(self):
0x45f marked this conversation as resolved.
Show resolved Hide resolved
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
Expand Down