Skip to content

[0-size Tensor No.98、246、247、329] Add 0-size Tensor support for renorm/kron/repeat_interleave #73003

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

Closed
wants to merge 6 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
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/repeat_interleave_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ void RepeatInterleaveGradKernel(const Context& ctx,
int repeats,
int dim,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
ctx.template Alloc<T>(x_grad);
return;
}
auto input_dim = x_grad->dims();
if (dim < 0) {
dim += input_dim.size();
Expand Down
13 changes: 12 additions & 1 deletion paddle/phi/kernels/impl/kron_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#pragma once

#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/impl/kron_kernel_impl.h"
#include "paddle/phi/kernels/reduce_sum_kernel.h"

namespace phi {

template <typename T>
Expand Down Expand Up @@ -266,6 +266,17 @@ void KronGradKernel(const Context &ctx,
const DenseTensor &out_grad,
DenseTensor *x_grad,
DenseTensor *y_grad) {
if (out_grad.numel() == 0) {
if (x_grad) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(x_grad->dims())), 0, x_grad);
}
if (y_grad) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(y_grad->dims())), 0, y_grad);
}
return;
}
if (x_grad) {
ctx.template Alloc<T>(x_grad);
}
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/impl/kron_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ void KronKernel(const Context &ctx,
const DenseTensor &y,
DenseTensor *out) {
ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}

int ndims = out->dims().size();
DenseTensor xx = UnsqueezeTo(x, ndims);
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/impl/renorm_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void RenormGradKernel(const Context& dev_ctx,
auto dimension_each = input_dims[dim];
dx->Resize(x.dims());
dev_ctx.template Alloc<T>(dx);
if (dx && dx->numel() == 0) {
return;
}
phi::funcs::RenormGradFunc(dev_ctx,
x_data,
dout_data,
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/renorm_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// 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.
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
Expand All @@ -28,6 +29,9 @@ void RenormKernel(const Context& dev_ctx,
DenseTensor* out) {
out->Resize(x.dims());
dev_ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}
auto x_ptr = x.template data<T>();
auto numel = x.numel();
int dim = axis;
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/repeat_interleave_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ void RepeatInterleaveGradKernel(const Context& ctx,
int repeats,
int dim,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
ctx.template Alloc<T>(x_grad);
return;
}
auto input_dim = x_grad->dims();
if (dim < 0) {
dim += input_dim.size();
Expand Down
5 changes: 4 additions & 1 deletion paddle/phi/kernels/impl/repeat_interleave_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ void RepeatInterleaveKernel(const Context& ctx,
0,
common::errors::InvalidArgument(
"repeats must grater than 0, but got %d", repeats));

if (out && out->numel() == 0) {
ctx.template Alloc<T>(out);
return;
}
auto place = ctx.GetPlace();
auto cpu_place = phi::CPUPlace();

Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/xpu/repeat_interleave_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ void RepeatInterleaveKernel(const Context& ctx,
0,
common::errors::InvalidArgument(
"repeats must grater than 0, but got %d", repeats));
if (out && out->numel() == 0) {
ctx.template Alloc<T>(out);
return;
}
using XPUType = typename XPUTypeTrait<T>::Type;

auto input_dim = x.dims();
Expand Down
27 changes: 27 additions & 0 deletions test/legacy_test/test_kron_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ def _init_dtype(self):
return "float16"


class TestKronOp_ZeroSize(OpTest):
def setUp(self):
self.op_type = "kron"
self.prim_op_type = "prim"
self.python_api = paddle.kron
self.public_python_api = paddle.kron
self.dtype = self._init_dtype()
x = np.random.uniform(size=(10, 0, 2)).astype(self.dtype)
y = np.random.uniform(size=(2, 3, 4, 3, 2)).astype(self.dtype)
out_ref = np.kron(x, y)
self.inputs = {'X': x, 'Y': y}
self.outputs = {'Out': out_ref}

def _init_dtype(self):
return "float64"

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

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


@unittest.skipIf(
not core.is_compiled_with_cuda()
or not core.is_bfloat16_supported(core.CUDAPlace(0)),
Expand Down
35 changes: 35 additions & 0 deletions test/legacy_test/test_renorm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,41 @@ def test_dygraph_api(self):
np.testing.assert_allclose(expected, np.array(y), rtol=1e-05)


class TestRenormAPI_ZeroSize(unittest.TestCase):
def input_data(self):
self.shape = [2, 0, 3]
self.data_x = np.random.random(self.shape).astype('float64')
self.p = 1.0
self.dim = 2
self.max_norm = 2.05

def test_renorm_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=self.shape, dtype='float64')
z = paddle.renorm(x, self.p, self.dim, self.max_norm)
exe = base.Executor(base.CPUPlace())
(res,) = exe.run(
feed={"x": self.data_x}, fetch_list=[z], return_numpy=False
)
np.testing.assert_allclose(np.array(res).shape, self.shape)

def test_dygraph_api(self):
self.input_data()
with base.dygraph.guard(base.CPUPlace()):
x = paddle.to_tensor(self.data_x, stop_gradient=False)
y = paddle.renorm(x, 1.0, 2, 2.05)
np.testing.assert_allclose(np.array(y).shape, self.shape)
z = paddle.mean(y)
z.backward(retain_graph=True)
np.testing.assert_allclose(x.grad.shape, x.shape)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
8 changes: 8 additions & 0 deletions test/legacy_test/test_repeat_interleave_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def test_check_grad_normal(self):
self.check_grad(['X'], 'Out', check_pir=True)


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')
Expand Down
Loading