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

[WIP][Hackathon 5th No.49][pir] add OpResult.__setitem__ - Part 5 #58739

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions paddle/fluid/pir/dialect/op_generator/ops_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
'layer_norm_act_xpu',
'multi_encoder_xpu',
'multihead_matmul',
'set_value',
'set_value_',
'set_value_with_tensor',
'set_value_with_tensor_',
'squeeze_excitation_block',
'yolo_box_xpu',
'fusion_gru',
Expand Down Expand Up @@ -115,10 +119,6 @@
'rnn_',
'seed',
'send_v2',
'set_value',
'set_value_',
'set_value_with_tensor',
'set_value_with_tensor_',
'shadow_feed',
'sparse_momentum',
]
Expand Down
5 changes: 3 additions & 2 deletions python/paddle/pir/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,10 @@ def __impl__(self, other_var):
if impl:
setattr(OpResult, magic_method, impl)

# Handling __getitem__
from ..base.variable_index import _getitem_static
# Handling __getitem__ and __setitem__
from ..base.variable_index import _getitem_static, _setitem_static

OpResult.__getitem__ = _getitem_static
OpResult.__setitem__ = _setitem_static

_already_patch_opresult = True
18 changes: 18 additions & 0 deletions test/legacy_test/test_math_op_patch_pir.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,24 @@ def test_some_dim(self):
self.assertEqual(x.ndimension(), 3)
self.assertEqual(x.ndim, 3)

def test_setitem(self):
paddle.disable_static()
x_np = np.ones([3, 2, 1]).astype("float32")
res_np_b = x_np.copy()
res_np_b[0] = 4
paddle.enable_static()
with paddle.pir_utils.IrGuard():
main_program, exe, program_guard = new_program()
with program_guard:
x = paddle.static.data(name='x', shape=[3, 2, 1])
b = paddle.static.setitem(x, (0), 4)
(b_np,) = exe.run(
main_program,
feed={"x": x_np},
fetch_list=[b],
)
np.testing.assert_array_equal(res_np_b, b_np)

def test_math_exists(self):
with paddle.pir_utils.IrGuard():
a = paddle.static.data(name='a', shape=[1], dtype='float32')
Expand Down