751 lines
28 KiB
Python
751 lines
28 KiB
Python
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import unittest
|
|
|
|
import numpy as np
|
|
from op_test import OpTest
|
|
|
|
import paddle
|
|
from paddle import base
|
|
|
|
|
|
class TestRepeatInterleaveOp(OpTest):
|
|
def setUp(self):
|
|
self.op_type = "repeat_interleave"
|
|
self.python_api = paddle.repeat_interleave
|
|
self.init_dtype_type()
|
|
index_np = np.random.randint(
|
|
low=0, high=3, size=self.index_size
|
|
).astype(self.index_type)
|
|
x_np = np.random.random(self.x_shape).astype(self.x_type)
|
|
|
|
self.inputs = {'X': x_np, 'RepeatsTensor': index_np}
|
|
self.attrs = {'dim': self.dim, 'output_size': -1}
|
|
|
|
outer_loop = np.prod(self.x_shape[: self.dim])
|
|
x_reshape = [outer_loop, *self.x_shape[self.dim :]]
|
|
x_np_reshape = np.reshape(x_np, tuple(x_reshape))
|
|
out_list = []
|
|
for i in range(outer_loop):
|
|
for j in range(self.index_size):
|
|
for k in range(index_np[j]):
|
|
out_list.append(x_np_reshape[i, j])
|
|
self.out_shape = list(self.x_shape)
|
|
self.out_shape[self.dim] = np.sum(index_np)
|
|
self.out_shape = tuple(self.out_shape)
|
|
|
|
out = np.reshape(out_list, self.out_shape)
|
|
self.outputs = {'Out': out}
|
|
|
|
def init_dtype_type(self):
|
|
self.dim = 1
|
|
self.x_type = np.float64
|
|
self.index_type = np.int64
|
|
self.x_shape = (8, 4, 5)
|
|
self.index_size = self.x_shape[self.dim]
|
|
|
|
def test_check_output(self):
|
|
self.check_output(check_pir=True, check_symbol_infer=False)
|
|
|
|
def test_check_grad_normal(self):
|
|
self.check_grad(['X'], 'Out', check_pir=True)
|
|
|
|
|
|
class TestRepeatInterleaveOp2(OpTest):
|
|
def setUp(self):
|
|
self.op_type = "repeat_interleave"
|
|
self.python_api = paddle.repeat_interleave
|
|
self.init_dtype_type()
|
|
index_np = 2
|
|
x_np = np.random.random(self.x_shape).astype(self.x_type)
|
|
self.inputs = {'X': x_np} # , 'RepeatsTensor': None}
|
|
self.attrs = {'dim': self.dim, 'Repeats': index_np, 'output_size': -1}
|
|
|
|
outer_loop = np.prod(self.x_shape[: self.dim])
|
|
x_reshape = [outer_loop, *self.x_shape[self.dim :]]
|
|
x_np_reshape = np.reshape(x_np, tuple(x_reshape))
|
|
out_list = []
|
|
for i in range(outer_loop):
|
|
for j in range(self.index_size):
|
|
for k in range(index_np):
|
|
out_list.append(x_np_reshape[i, j])
|
|
self.out_shape = list(self.x_shape)
|
|
self.out_shape[self.dim] = index_np * self.index_size
|
|
self.out_shape = tuple(self.out_shape)
|
|
|
|
out = np.reshape(out_list, self.out_shape)
|
|
self.outputs = {'Out': out}
|
|
|
|
def init_dtype_type(self):
|
|
self.dim = 1
|
|
self.x_type = np.float64
|
|
self.x_shape = (8, 4, 5)
|
|
self.index_size = self.x_shape[self.dim]
|
|
|
|
def test_check_output(self):
|
|
self.check_output(check_pir=True)
|
|
|
|
def test_check_grad_normal(self):
|
|
self.check_grad(['X'], 'Out', check_pir=True)
|
|
|
|
|
|
class TestRepeatInterleaveOpWithOutputSize1(TestRepeatInterleaveOp):
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.attrs['output_size'] = self.out_shape[self.dim]
|
|
|
|
|
|
class TestRepeatInterleaveOpWithOutputSize2(TestRepeatInterleaveOp):
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.attrs['output_size'] = -1
|
|
|
|
|
|
class TestRepeatInterleaveOp2WithOutputSize1(TestRepeatInterleaveOp2):
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.attrs['output_size'] = self.out_shape[self.dim]
|
|
|
|
|
|
class TestRepeatInterleaveOp2WithOutputSize2(TestRepeatInterleaveOp2):
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.attrs['output_size'] = -1
|
|
|
|
|
|
class TestRepeatInterleaveOp_ZeroSize(TestRepeatInterleaveOp2):
|
|
def init_dtype_type(self):
|
|
self.dim = 1
|
|
self.x_type = np.float64
|
|
self.x_shape = (8, 0, 5)
|
|
self.index_size = self.x_shape[self.dim]
|
|
|
|
|
|
class TestIndexSelectAPI(unittest.TestCase):
|
|
def input_data(self):
|
|
self.data_zero_dim_x = np.array(0.5).astype('float32')
|
|
self.data_x = np.array(
|
|
[
|
|
[1.0, 2.0, 3.0, 4.0],
|
|
[5.0, 6.0, 7.0, 8.0],
|
|
[9.0, 10.0, 11.0, 12.0],
|
|
]
|
|
).astype('float32')
|
|
self.data_zero_dim_index = np.array(2)
|
|
self.data_index = np.array([0, 1, 2, 1]).astype('int32')
|
|
self.data_index_output_size = np.array([2, 1, 3]).astype('int32')
|
|
|
|
def test_repeat_interleave_api(self):
|
|
paddle.enable_static()
|
|
self.input_data()
|
|
|
|
# case 1:
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_',
|
|
shape=[4],
|
|
dtype='int32',
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
x.stop_gradient = False
|
|
index.stop_gradient = False
|
|
z = paddle.repeat_interleave(x, index, axis=1)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res,) = exe.run(
|
|
feed={'x': self.data_x, 'repeats_': self.data_index},
|
|
fetch_list=[z],
|
|
return_numpy=False,
|
|
)
|
|
expect_out = np.repeat(self.data_x, self.data_index, axis=1)
|
|
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)
|
|
|
|
# case 2:
|
|
repeats = np.array([1, 2, 1]).astype('int32')
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 4], dtype="float32")
|
|
index = paddle.static.data(
|
|
name='repeats_',
|
|
shape=[3],
|
|
dtype='int32',
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, index, axis=0)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res,) = exe.run(
|
|
feed={
|
|
'x': self.data_x,
|
|
'repeats_': repeats,
|
|
},
|
|
fetch_list=[z],
|
|
return_numpy=False,
|
|
)
|
|
expect_out = np.repeat(self.data_x, repeats, axis=0)
|
|
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)
|
|
|
|
repeats = 2
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32')
|
|
z = paddle.repeat_interleave(x, repeats, axis=0)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res,) = exe.run(
|
|
feed={'x': self.data_x}, fetch_list=[z], return_numpy=False
|
|
)
|
|
expect_out = np.repeat(self.data_x, repeats, axis=0)
|
|
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)
|
|
|
|
# case 3 zero_dim:
|
|
if not paddle.framework.in_pir_mode():
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1], dtype="float32")
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, repeats)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res,) = exe.run(
|
|
feed={'x': self.data_zero_dim_x},
|
|
fetch_list=[z],
|
|
return_numpy=False,
|
|
)
|
|
expect_out = np.repeat(self.data_zero_dim_x, repeats)
|
|
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)
|
|
|
|
# case 4 negative axis:
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_',
|
|
shape=[4],
|
|
dtype='int32',
|
|
)
|
|
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, index, axis=-1)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res,) = exe.run(
|
|
feed={'x': self.data_x, 'repeats_': self.data_index},
|
|
fetch_list=[z],
|
|
return_numpy=False,
|
|
)
|
|
expect_out = np.repeat(self.data_x, self.data_index, axis=-1)
|
|
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)
|
|
|
|
# case 5 output_size:
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_',
|
|
shape=[3],
|
|
dtype='int32',
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=6)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res,) = exe.run(
|
|
feed={
|
|
'x': self.data_x[:, :3],
|
|
'repeats_': self.data_index_output_size,
|
|
},
|
|
fetch_list=[z],
|
|
)
|
|
|
|
expect_out = np.repeat(
|
|
self.data_x[:, :3], self.data_index_output_size, axis=1
|
|
)
|
|
np.testing.assert_allclose(expect_out, res, rtol=1e-05)
|
|
|
|
# case 6 output_size = -1
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_',
|
|
shape=[3],
|
|
dtype='int32',
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
|
|
z2 = paddle.repeat_interleave(x, index, axis=1, output_size=-1)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res2,) = exe.run(
|
|
feed={
|
|
'x': self.data_x[:, :3],
|
|
'repeats_': self.data_index_output_size,
|
|
},
|
|
fetch_list=[z2],
|
|
)
|
|
np.testing.assert_allclose(expect_out, res2, rtol=1e-05)
|
|
|
|
# case 7 output_size error
|
|
with (
|
|
self.assertRaises(ValueError),
|
|
paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
),
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_',
|
|
shape=[3],
|
|
dtype='int32',
|
|
)
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=5)
|
|
exe = base.Executor(base.CPUPlace())
|
|
exe.run(
|
|
feed={
|
|
'x': self.data_x[:, :3],
|
|
'repeats_': self.data_index_output_size,
|
|
},
|
|
fetch_list=[z],
|
|
)
|
|
|
|
# case 8 repeats is int, output_size provided and correct
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32')
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, 2, axis=1, output_size=6)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res3,) = exe.run(
|
|
feed={'x': self.data_x[:, :3]},
|
|
fetch_list=[z],
|
|
)
|
|
expect_out3 = np.repeat(self.data_x[:, :3], 2, axis=1)
|
|
np.testing.assert_allclose(expect_out3, res3, rtol=1e-05)
|
|
|
|
# case 9: x.numel = 0, repeats is tensor, output_size = -1
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[0, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_', shape=[3], dtype='int32'
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=-1)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res4,) = exe.run(
|
|
feed={
|
|
'x': np.zeros((0, 3), dtype='float32'),
|
|
'repeats_': self.data_index_output_size,
|
|
},
|
|
fetch_list=[z],
|
|
)
|
|
expect_out4 = np.repeat(
|
|
np.zeros((0, 3), dtype='float32'),
|
|
self.data_index_output_size,
|
|
axis=1,
|
|
)
|
|
np.testing.assert_allclose(expect_out4, res4, rtol=1e-05)
|
|
|
|
# case 10: x.numel = 0, repeats is tensor, output_size = actual value
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[0, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_', shape=[3], dtype='int32'
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
output_size_actual = int(self.data_index_output_size.sum())
|
|
z = paddle.repeat_interleave(
|
|
x, index, axis=1, output_size=output_size_actual
|
|
)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res4b,) = exe.run(
|
|
feed={
|
|
'x': np.zeros((0, 3), dtype='float32'),
|
|
'repeats_': self.data_index_output_size,
|
|
},
|
|
fetch_list=[z],
|
|
)
|
|
expect_out4b = np.repeat(
|
|
np.zeros((0, 3), dtype='float32'),
|
|
self.data_index_output_size,
|
|
axis=1,
|
|
)
|
|
np.testing.assert_allclose(expect_out4b, res4b, rtol=1e-05)
|
|
|
|
# case 11: repeats tensor dtype = int64, output_size = -1
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_', shape=[3], dtype='int64'
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=-1)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res5,) = exe.run(
|
|
feed={
|
|
'x': self.data_x[:, :3],
|
|
'repeats_': self.data_index_output_size.astype('int64'),
|
|
},
|
|
fetch_list=[z],
|
|
)
|
|
expect_out5 = np.repeat(
|
|
self.data_x[:, :3], self.data_index_output_size, axis=1
|
|
)
|
|
np.testing.assert_allclose(expect_out5, res5, rtol=1e-05)
|
|
|
|
# case 11: repeats tensor dtype = int64, output_size = actual value
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32')
|
|
index = paddle.static.data(
|
|
name='repeats_', shape=[3], dtype='int64'
|
|
)
|
|
if not paddle.framework.in_pir_mode():
|
|
x.desc.set_need_check_feed(False)
|
|
index.desc.set_need_check_feed(False)
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=6)
|
|
exe = base.Executor(base.CPUPlace())
|
|
(res6,) = exe.run(
|
|
feed={
|
|
'x': self.data_x[:, :3],
|
|
'repeats_': self.data_index_output_size.astype('int64'),
|
|
},
|
|
fetch_list=[z],
|
|
)
|
|
np.testing.assert_allclose(expect_out5, res6, rtol=1e-05)
|
|
|
|
def test_dygraph_api(self):
|
|
self.input_data()
|
|
# case axis none
|
|
input_x = np.array([[1, 2, 1], [1, 2, 3]]).astype('int32')
|
|
index_x = np.array([1, 1, 2, 1, 2, 2]).astype('int32')
|
|
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(input_x)
|
|
index = paddle.to_tensor(index_x)
|
|
z = paddle.repeat_interleave(x, index, None)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(input_x, index_x, axis=None)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case repeats int
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(input_x)
|
|
index = 2
|
|
z = paddle.repeat_interleave(x, index, None)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(input_x, index, axis=None)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case input dtype is bfloat16
|
|
input_x = np.array([[1, 2, 1], [1, 2, 3]]).astype('uint16')
|
|
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(input_x)
|
|
index = paddle.to_tensor(index_x)
|
|
z = paddle.repeat_interleave(x, index, None)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(input_x, index_x, axis=None)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(input_x)
|
|
index = 2
|
|
z = paddle.repeat_interleave(x, index, None)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(input_x, index, axis=None)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case 1:
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_x)
|
|
index = paddle.to_tensor(self.data_index)
|
|
z = paddle.repeat_interleave(x, index, -1)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(self.data_x, self.data_index, axis=-1)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_x)
|
|
index = paddle.to_tensor(self.data_index)
|
|
z = paddle.repeat_interleave(x, index, 1)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(self.data_x, self.data_index, axis=1)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case 2:
|
|
index_x = np.array([1, 2, 1]).astype('int32')
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_x)
|
|
index = paddle.to_tensor(index_x)
|
|
z = paddle.repeat_interleave(x, index, axis=0)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(self.data_x, index, axis=0)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case 3 zero_dim:
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_zero_dim_x)
|
|
index = 2
|
|
z = paddle.repeat_interleave(x, index, None)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(self.data_zero_dim_x, index, axis=None)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case 4 zero_dim_index
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_zero_dim_x)
|
|
index = paddle.to_tensor(self.data_zero_dim_index)
|
|
z = paddle.repeat_interleave(x, index, None)
|
|
np_z = z.numpy()
|
|
expect_out = np.repeat(
|
|
self.data_zero_dim_x, self.data_zero_dim_index, axis=None
|
|
)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
# case 5 repeat_interleave_with_tensor_double_grad
|
|
with base.dygraph.guard():
|
|
x_pd = paddle.randn([40, 50])
|
|
x_pd.stop_gradient = False
|
|
axis = 1
|
|
repeats_pd = paddle.randint(1, 50, [x_pd.shape[axis]])
|
|
|
|
y_pd = paddle.repeat_interleave(x_pd, repeats_pd, axis)
|
|
dy_pd = paddle.randn_like(y_pd)
|
|
dy_pd.stop_gradient = False
|
|
g_pd = paddle.grad(y_pd, x_pd, dy_pd, create_graph=True)[0]
|
|
|
|
ddx_pd = paddle.randn_like(x_pd)
|
|
gg_pd = paddle.grad(g_pd, dy_pd, ddx_pd)[0]
|
|
|
|
np.testing.assert_allclose(
|
|
gg_pd.numpy(),
|
|
paddle.repeat_interleave(ddx_pd, repeats_pd, axis).numpy(),
|
|
1e-5,
|
|
1e-5,
|
|
)
|
|
|
|
# case 6 repeat_interleave_double_grad
|
|
with base.dygraph.guard():
|
|
x_pd = paddle.randn([40, 50])
|
|
x_pd.stop_gradient = False
|
|
axis = 1
|
|
repeats_pd = 4
|
|
|
|
y_pd = paddle.repeat_interleave(x_pd, repeats_pd, axis)
|
|
dy_pd = paddle.randn_like(y_pd)
|
|
dy_pd.stop_gradient = False
|
|
g_pd = paddle.grad(y_pd, x_pd, dy_pd, create_graph=True)[0]
|
|
|
|
ddx_pd = paddle.randn_like(x_pd)
|
|
gg_pd = paddle.grad(g_pd, dy_pd, ddx_pd)[0]
|
|
|
|
np.testing.assert_allclose(
|
|
gg_pd.numpy(),
|
|
paddle.repeat_interleave(ddx_pd, repeats_pd, axis).numpy(),
|
|
1e-5,
|
|
1e-5,
|
|
)
|
|
|
|
# case 7 repeat_interleave_with_i64_tensor_double_grad
|
|
with base.dygraph.guard():
|
|
x_pd = paddle.randn([40, 50])
|
|
x_pd.stop_gradient = False
|
|
axis = 1
|
|
repeats_pd = paddle.randint(
|
|
1, 50, [x_pd.shape[axis]], dtype="int64"
|
|
)
|
|
|
|
y_pd = paddle.repeat_interleave(x_pd, repeats_pd, axis)
|
|
dy_pd = paddle.randn_like(y_pd)
|
|
dy_pd.stop_gradient = False
|
|
g_pd = paddle.grad(y_pd, x_pd, dy_pd, create_graph=True)[0]
|
|
|
|
ddx_pd = paddle.randn_like(x_pd)
|
|
gg_pd = paddle.grad(g_pd, dy_pd, ddx_pd)[0]
|
|
|
|
np.testing.assert_allclose(
|
|
gg_pd.numpy(),
|
|
paddle.repeat_interleave(ddx_pd, repeats_pd, axis).numpy(),
|
|
1e-5,
|
|
1e-5,
|
|
)
|
|
|
|
# case 8 0-size_repeat_interleave_with_i64_tensor_double_grad
|
|
with base.dygraph.guard():
|
|
x_pd = paddle.randn([0, 50])
|
|
x_pd.stop_gradient = False
|
|
axis = 1
|
|
repeats_pd = paddle.randint(
|
|
1, 50, [x_pd.shape[axis]], dtype="int64"
|
|
)
|
|
|
|
y_pd = paddle.repeat_interleave(x_pd, repeats_pd, axis)
|
|
dy_pd = paddle.randn_like(y_pd)
|
|
dy_pd.stop_gradient = False
|
|
g_pd = paddle.grad(y_pd, x_pd, dy_pd, create_graph=True)[0]
|
|
|
|
ddx_pd = paddle.randn_like(x_pd)
|
|
gg_pd = paddle.grad(g_pd, dy_pd, ddx_pd)[0]
|
|
|
|
np.testing.assert_allclose(
|
|
gg_pd.numpy(),
|
|
paddle.repeat_interleave(ddx_pd, repeats_pd, axis).numpy(),
|
|
1e-5,
|
|
1e-5,
|
|
)
|
|
# case 9 0-size_repeat_interleave_with_i32_tensor_double_grad
|
|
with base.dygraph.guard():
|
|
x_pd = paddle.randn([0, 50])
|
|
x_pd.stop_gradient = False
|
|
axis = 1
|
|
repeats_pd = paddle.randint(
|
|
1, 50, [x_pd.shape[axis]], dtype="int32"
|
|
)
|
|
|
|
y_pd = paddle.repeat_interleave(x_pd, repeats_pd, axis)
|
|
dy_pd = paddle.randn_like(y_pd)
|
|
dy_pd.stop_gradient = False
|
|
g_pd = paddle.grad(y_pd, x_pd, dy_pd, create_graph=True)[0]
|
|
|
|
ddx_pd = paddle.randn_like(x_pd)
|
|
gg_pd = paddle.grad(g_pd, dy_pd, ddx_pd)[0]
|
|
|
|
np.testing.assert_allclose(
|
|
gg_pd.numpy(),
|
|
paddle.repeat_interleave(ddx_pd, repeats_pd, axis).numpy(),
|
|
1e-5,
|
|
1e-5,
|
|
)
|
|
|
|
# case 10 output_size:
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_x[:, :3])
|
|
index = paddle.to_tensor(self.data_index_output_size)
|
|
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=6)
|
|
np_z = z.numpy()
|
|
|
|
expect_out = np.repeat(
|
|
self.data_x[:, :3], self.data_index_output_size, axis=1
|
|
)
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_x[:, :3])
|
|
index = paddle.to_tensor(self.data_index_output_size)
|
|
|
|
z = x.repeat_interleave(index, axis=1, output_size=6)
|
|
np_z = z.numpy()
|
|
|
|
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)
|
|
|
|
with base.dygraph.guard():
|
|
x_np = np.array([[1.0, 2.0], [3.0, 4.0]]).astype('float32')
|
|
index_np = np.array([2, 1]).astype('int32')
|
|
|
|
x = paddle.to_tensor(x_np, stop_gradient=False)
|
|
index = paddle.to_tensor(index_np)
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=3)
|
|
|
|
z.backward()
|
|
|
|
expected_grad = np.array([[2.0, 1.0], [2.0, 1.0]])
|
|
np.testing.assert_allclose(
|
|
x.grad.numpy(), expected_grad, rtol=1e-05
|
|
)
|
|
|
|
x = paddle.to_tensor(x_np, stop_gradient=False)
|
|
z = x.repeat_interleave(index, axis=1, output_size=3)
|
|
|
|
z.backward()
|
|
|
|
np.testing.assert_allclose(
|
|
x.grad.numpy(), expected_grad, rtol=1e-05
|
|
)
|
|
|
|
with base.dygraph.guard():
|
|
x = paddle.to_tensor(self.data_x[:, :3])
|
|
index = paddle.to_tensor(self.data_index_output_size)
|
|
|
|
with self.assertRaises(ValueError):
|
|
z = paddle.repeat_interleave(x, index, axis=1, output_size=5)
|
|
|
|
|
|
class TestRepeatInterleave_ZeroSizeRepeatsTensor(unittest.TestCase):
|
|
"""Cover the `repeats.dims()[0] == 0` branch added to
|
|
paddle/phi/kernels/funcs/repeat_tensor2index_tensor.{cc,cu}.
|
|
|
|
The functor short-circuits when the repeats tensor itself is 0-sized
|
|
(i.e. x.shape[axis] == 0 with a Tensor-typed repeats argument).
|
|
"""
|
|
|
|
def _check(self, repeats_dtype):
|
|
# InferMeta only allows 0-size repeats tensor when x is 1-D shape [0].
|
|
x_np = np.zeros([0], dtype="float32")
|
|
repeats_np = np.zeros([0], dtype=repeats_dtype)
|
|
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(x_np)
|
|
x.stop_gradient = False
|
|
repeats = paddle.to_tensor(repeats_np)
|
|
out = paddle.repeat_interleave(x, repeats, axis=0)
|
|
np.testing.assert_equal(out.numpy().shape, (0,))
|
|
# Also exercise the backward path through the functor.
|
|
loss = paddle.sum(out)
|
|
loss.backward()
|
|
np.testing.assert_equal(x.grad.shape, [0])
|
|
|
|
def test_zero_size_repeats_int32(self):
|
|
self._check(repeats_dtype="int32")
|
|
|
|
def test_zero_size_repeats_int64(self):
|
|
self._check(repeats_dtype="int64")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|