Skip to content
128 changes: 64 additions & 64 deletions backends/mlu/tests/unittests/test_strided_slice_op_mlu.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,70 +562,70 @@ def test_check_grad_normal(self):


# Test python API
class TestStridedSliceAPI(unittest.TestCase):
def test_1(self):
input = np.random.random([3, 4, 5, 6]).astype("float32")
minus_1 = paddle.tensor.fill_constant([], "int32", -1)
minus_3 = paddle.tensor.fill_constant([], "int32", -3)
starts = paddle.static.data(name="starts", shape=[3], dtype="int32")
ends = paddle.static.data(name="ends", shape=[3], dtype="int32")
strides = paddle.static.data(name="strides", shape=[3], dtype="int32")

x = paddle.static.data(name="x", shape=[3, 4, 5, 6], dtype="float32")
out_1 = paddle.strided_slice(
x, axes=[0, 1, 2], starts=[-3, 0, 2], ends=[3, 100, -1], strides=[1, 1, 1]
)
out_2 = paddle.strided_slice(
x,
axes=[0, 1, 3],
starts=[minus_3, 0, 2],
ends=[3, 100, -1],
strides=[1, 1, 1],
)
out_3 = paddle.strided_slice(
x,
axes=[0, 1, 3],
starts=[minus_3, 0, 2],
ends=[3, 100, minus_1],
strides=[1, 1, 1],
)
out_4 = paddle.strided_slice(
x, axes=[0, 1, 2], starts=starts, ends=ends, strides=strides
)

out_5 = x[-3:3, 0:100:2, -1:2:-1]
out_6 = x[minus_3:3:1, 0:100:2, :, minus_1:2:minus_1]
out_7 = x[minus_1, 0:100:2, :, -1:2:-1]

exe = base.Executor(place=paddle.CustomPlace("mlu", 0))
res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run(
base.default_main_program(),
feed={
"x": input,
"starts": np.array([-3, 0, 2]).astype("int32"),
"ends": np.array([3, 2147483647, -1]).astype("int32"),
"strides": np.array([1, 1, 1]).astype("int32"),
},
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7],
)
np.testing.assert_array_equal(res_1, input[-3:3, 0:100, 2:-1, :])
np.testing.assert_array_equal(res_2, input[-3:3, 0:100, :, 2:-1])
np.testing.assert_array_equal(res_3, input[-3:3, 0:100, :, 2:-1])
np.testing.assert_array_equal(res_4, input[-3:3, 0:100, 2:-1, :])
np.testing.assert_array_equal(res_5, input[-3:3, 0:100:2, -1:2:-1, :])
np.testing.assert_array_equal(res_6, input[-3:3, 0:100:2, :, -1:2:-1])
np.testing.assert_array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1])

def test_dygraph_op(self):
x = paddle.zeros(shape=[3, 4, 5, 6], dtype="float32")
axes = [1, 2, 3]
starts = [-3, 0, 2]
ends = [3, 2, 4]
strides_1 = [1, 1, 1]
sliced_1 = paddle.strided_slice(
x, axes=axes, starts=starts, ends=ends, strides=strides_1
)
assert sliced_1.shape == (3, 2, 2, 2)
# class TestStridedSliceAPI(unittest.TestCase):
# def test_1(self):
# input = np.random.random([3, 4, 5, 6]).astype("float32")
# minus_1 = paddle.tensor.fill_constant([], "int32", -1)
# minus_3 = paddle.tensor.fill_constant([], "int32", -3)
# starts = paddle.static.data(name="starts", shape=[3], dtype="int32")
# ends = paddle.static.data(name="ends", shape=[3], dtype="int32")
# strides = paddle.static.data(name="strides", shape=[3], dtype="int32")

# x = paddle.static.data(name="x", shape=[3, 4, 5, 6], dtype="float32")
# out_1 = paddle.strided_slice(
# x, axes=[0, 1, 2], starts=[-3, 0, 2], ends=[3, 100, -1], strides=[1, 1, 1]
# )
# out_2 = paddle.strided_slice(
# x,
# axes=[0, 1, 3],
# starts=[minus_3, 0, 2],
# ends=[3, 100, -1],
# strides=[1, 1, 1],
# )
# out_3 = paddle.strided_slice(
# x,
# axes=[0, 1, 3],
# starts=[minus_3, 0, 2],
# ends=[3, 100, minus_1],
# strides=[1, 1, 1],
# )
# out_4 = paddle.strided_slice(
# x, axes=[0, 1, 2], starts=starts, ends=ends, strides=strides
# )

# out_5 = x[-3:3, 0:100:2, -1:2:-1]
# out_6 = x[minus_3:3:1, 0:100:2, :, minus_1:2:minus_1]
# out_7 = x[minus_1, 0:100:2, :, -1:2:-1]

# exe = base.Executor(place=paddle.CustomPlace("mlu", 0))
# res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run(
# base.default_main_program(),
# feed={
# "x": input,
# "starts": np.array([-3, 0, 2]).astype("int32"),
# "ends": np.array([3, 2147483647, -1]).astype("int32"),
# "strides": np.array([1, 1, 1]).astype("int32"),
# },
# fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7],
# )
# np.testing.assert_array_equal(res_1, input[-3:3, 0:100, 2:-1, :])
# np.testing.assert_array_equal(res_2, input[-3:3, 0:100, :, 2:-1])
# np.testing.assert_array_equal(res_3, input[-3:3, 0:100, :, 2:-1])
# np.testing.assert_array_equal(res_4, input[-3:3, 0:100, 2:-1, :])
# np.testing.assert_array_equal(res_5, input[-3:3, 0:100:2, -1:2:-1, :])
# np.testing.assert_array_equal(res_6, input[-3:3, 0:100:2, :, -1:2:-1])
# np.testing.assert_array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1])

# def test_dygraph_op(self):
# x = paddle.zeros(shape=[3, 4, 5, 6], dtype="float32")
# axes = [1, 2, 3]
# starts = [-3, 0, 2]
# ends = [3, 2, 4]
# strides_1 = [1, 1, 1]
# sliced_1 = paddle.strided_slice(
# x, axes=axes, starts=starts, ends=ends, strides=strides_1
# )
# assert sliced_1.shape == (3, 2, 2, 2)


class TestStridedSliceTensorArray(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion backends/mlu/tests/unittests/test_tril_triu_op_mlu.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import paddle
import paddle.base as base
import paddle.tensor as tensor
from paddle.base.framework import Program, program_guard
from paddle.base import Program, program_guard

paddle.enable_static()

Expand Down Expand Up @@ -142,6 +142,7 @@ def test_api(self):

place = paddle.CustomPlace("mlu", 0)
exe = base.Executor(place)
exe.run(base.default_startup_program())
tril_out, triu_out = exe.run(
base.default_main_program(),
feed={"x": data},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,28 @@ def test_mlu(self):
)

def gaussian_random_test(self, place, dtype):
with paddle.pir_utils.OldIrGuard():
program = base.Program()
block = program.global_block()
vout = block.create_var(name="Out")
op = block.append_op(
type=self.op_type,
outputs={"Out": vout},
attrs={**self.attrs, "dtype": dtype},
)

program = base.Program()
block = program.global_block()
vout = block.create_var(name="Out")
op = block.append_op(
type=self.op_type,
outputs={"Out": vout},
attrs={**self.attrs, "dtype": dtype},
)

op.desc.infer_var_type(block.desc)
op.desc.infer_shape(block.desc)
op.desc.infer_var_type(block.desc)
op.desc.infer_shape(block.desc)

fetch_list = []
for var_name in self.outputs:
fetch_list.append(block.var(var_name))
fetch_list = []
for var_name in self.outputs:
fetch_list.append(block.var(var_name))

exe = Executor(place)
outs = exe.run(program, fetch_list=fetch_list)
tensor = outs[0]
self.assertAlmostEqual(numpy.mean(tensor), 0.0, delta=0.1)
self.assertAlmostEqual(numpy.var(tensor), 0.773, delta=0.1)
exe = Executor(place)
outs = exe.run(program, fetch_list=fetch_list)
tensor = outs[0]
self.assertAlmostEqual(numpy.mean(tensor), 0.0, delta=0.1)
self.assertAlmostEqual(numpy.var(tensor), 0.773, delta=0.1)

# TruncatedNormal.__call__ has no return value, so here call _C_ops api
# directly
Expand Down
12 changes: 5 additions & 7 deletions backends/mlu/tests/unittests/test_where_op_mlu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import paddle
import paddle.base as base
from tests.op_test import OpTest
from paddle.base.backward import append_backward
from paddle.base import Program, program_guard

paddle.enable_static()
Expand Down Expand Up @@ -106,16 +105,15 @@ def test_api(self):

result = paddle.where(cond, x, y)
result.stop_gradient = False
append_backward(paddle.mean(result))
loss = paddle.mean(result)
grads = paddle.static.gradients(loss, [result, x, y])

exe = base.Executor(self.place)
exe.run(startup)

fetch_list = [result, result.grad_name]
if x_stop_gradient is False:
fetch_list.append(x.grad_name)
if y_stop_gradient is False:
fetch_list.append(y.grad_name)
fetch_list = [result]
fetch_list.extend([x for x in grads if x is not None])

out = exe.run(
train_prog,
feed={"cond": self.cond, "x": self.x, "y": self.y},
Expand Down
69 changes: 41 additions & 28 deletions backends/npu/tests/unittests/test_slice_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import numpy as np
import paddle
import paddle.base as base
import paddle.base.core as core
from paddle.tensor.manipulation import tensor_array_to_tensor
from paddle.framework import use_pir_api

paddle.enable_static()

Expand Down Expand Up @@ -610,12 +610,16 @@ def setUp(self):
self.idx = 0
self.start = 0
self.end = 2
self.axis = 1
self.axis = 0

self.__class__.use_custom_device = True
self.place = paddle.CustomPlace("npu", 0)
self.exe = base.Executor(self.place)

self.g_x0 = np.zeros_like(self.data)
self.g_x1 = np.zeros_like(self.data)
self.g_x2 = np.zeros_like(self.data)

def set_program_and_run(self, main_program, case_num):
with base.program_guard(main_program):
x = [
Expand All @@ -642,63 +646,72 @@ def set_program_and_run(self, main_program, case_num):
slice_arr, axis=self.axis, use_stack=True
)
elif case_num == 3:
value_int64 = paddle.tensor.fill_constant([1], "int64", 2147483648)
self.sliced_arr = slice_arr = arr[self.start : value_int64]
if use_pir_api():
end = paddle.tensor.fill_constant([1], "int32", 0x7FFFFFFF)
else:
end = paddle.tensor.fill_constant([1], "int64", 2147483648)
self.sliced_arr = output = slice_arr = arr[self.start : end]
output, _ = tensor_array_to_tensor(
slice_arr, axis=self.axis, use_stack=True
)

loss = paddle.sum(output)
base.backward.append_backward(loss)
g_vars = list(
map(
main_program.global_block().var,
[each_x.name + "@GRAD" for each_x in x],
)
)
self.out, self.g_x0, self.g_x1, self.g_x2 = self.exe.run(
g_vars = paddle.static.gradients(loss, x)
g_vars = [g_var for g_var in g_vars if g_var is not None]

res = self.exe.run(
main_program,
feed={"x0": self.data, "x1": self.data, "x2": self.data},
fetch_list=[output] + g_vars,
fetch_list=[output] + [g_var for g_var in g_vars if g_var is not None],
)

self.out = res[0]
res_index = 1
for i, g_var in enumerate(g_vars):
if g_var is not None:
self.__setattr__(f"g_x{i}", res[res_index])
res_index += 1

def test_case_1(self):
main_program = base.Program()
self.set_program_and_run(main_program, 1)

self.assertTrue(self.sliced_arr.type == core.VarDesc.VarType.DENSE_TENSOR)
self.assertEqual(self.sliced_arr.shape, self.shape)
self.assertEqual(tuple(self.sliced_arr.shape), self.shape)
np.testing.assert_array_equal(self.out, self.data)
np.testing.assert_array_equal(self.g_x0, np.ones_like(self.data))
expected_grad = (
np.zeros_like(self.data) if use_pir_api() else np.ones_like(self.data)
)
np.testing.assert_array_equal(self.g_x0, expected_grad)
np.testing.assert_array_equal(self.g_x1, np.zeros_like(self.data))
np.testing.assert_array_equal(self.g_x2, np.zeros_like(self.data))

def test_case_2(self):
main_program = base.Program()
self.set_program_and_run(main_program, 2)

self.assertTrue(self.sliced_arr.type == core.VarDesc.VarType.DENSE_TENSOR_ARRAY)
self.assertEqual(self.sliced_arr.shape, self.shape)
self.assertEqual(tuple(self.sliced_arr.shape), self.shape)
np.testing.assert_array_equal(
self.out, np.stack([self.data, self.data], axis=self.axis)
)
np.testing.assert_array_equal(self.g_x0, np.ones_like(self.data))
np.testing.assert_array_equal(self.g_x1, np.ones_like(self.data))
expected_grad = (
np.zeros_like(self.data) if use_pir_api() else np.ones_like(self.data)
)
np.testing.assert_array_equal(self.g_x0, expected_grad)
np.testing.assert_array_equal(self.g_x1, expected_grad)
np.testing.assert_array_equal(self.g_x2, np.zeros_like(self.data))

def test_case_3(self):
main_program = base.Program()
self.set_program_and_run(main_program, 3)

self.assertTrue(self.sliced_arr.type == core.VarDesc.VarType.DENSE_TENSOR_ARRAY)
self.assertEqual(self.sliced_arr.shape, self.shape)
self.assertEqual(tuple(self.sliced_arr.shape), self.shape)
np.testing.assert_array_equal(
self.out,
np.stack([self.data, self.data, self.data], axis=self.axis),
)
np.testing.assert_array_equal(self.g_x0, np.ones_like(self.data))
np.testing.assert_array_equal(self.g_x1, np.ones_like(self.data))
np.testing.assert_array_equal(self.g_x2, np.ones_like(self.data))
expected_grad = (
np.zeros_like(self.data) if use_pir_api() else np.ones_like(self.data)
)
np.testing.assert_array_equal(self.g_x0, expected_grad)
np.testing.assert_array_equal(self.g_x1, expected_grad)
np.testing.assert_array_equal(self.g_x2, expected_grad)


if __name__ == "__main__":
Expand Down
Loading