Skip to content
Merged
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
17 changes: 17 additions & 0 deletions python/paddle/pir/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ def clone(self):
"""
return paddle.assign(self)

def append(self, var):
"""
**Notes**:
**The type OpResult must be LoD Tensor Array.

"""
if not self.is_dense_tensor_array_type():
raise TypeError(
"Only OpResult with pd_op.tensor_array support `append` method, but received type: {}".format(
self.type()
)
)
from paddle.tensor.array import array_length, array_write
Copy link
Contributor

Choose a reason for hiding this comment

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

在文件前面import是会有循环import 的问题吗


array_write(x=var, i=array_length(self), array=self)

import paddle

opresult_methods = [
Expand All @@ -367,6 +383,7 @@ def clone(self):
('astype', astype),
('size', _size_),
('clone', clone),
('append', append),
(
'__add__',
_binary_creator_('__add__', paddle.tensor.add, False, _scalar_add_),
Expand Down
19 changes: 18 additions & 1 deletion test/legacy_test/test_math_op_patch_pir.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def new_program():
# TODO(gouzil): Optimize program code
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
place = base.CPUPlace()
place = paddle.CPUPlace()
exe = base.Executor(place)
return (
main_program,
Expand Down Expand Up @@ -444,6 +444,23 @@ def test_clone(self):
np.testing.assert_array_equal(x_np, a_np)
self.assertNotEqual(id(x), id(a))

def test_append(self):
with paddle.pir_utils.IrGuard():
_, _, program_guard = new_program()
with program_guard:
x = paddle.static.data(name='x', shape=[-1, 1], dtype="float32")
init_data = [
np.random.random(shape).astype('float32')
for shape in [[10, 4], [8, 12], [1]]
]

array = paddle.tensor.create_array(
'int64', [paddle.to_tensor(x) for x in init_data]
)
array.append(x)
with self.assertRaises(TypeError):
x.append(array)

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