Skip to content

q to s start ops | add dim order sanity check #4332

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

Merged
merged 1 commit into from
Sep 9, 2024
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
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_reflection_pad1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Tensor& reflection_pad1d_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);

Tensor::SizesType target_sizes[kTensorDimensionLimit];
size_t target_ndim = 0;
get_padding_out_target_size(1, in, padding, target_sizes, &target_ndim);
Expand Down
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_reflection_pad2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Tensor& reflection_pad2d_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);

Tensor::SizesType target_sizes[kTensorDimensionLimit];
size_t target_ndim = 0;
get_padding_out_target_size(2, in, padding, target_sizes, &target_ndim);
Expand Down
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_reflection_pad3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Tensor& reflection_pad3d_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);

Tensor::SizesType target_sizes[kTensorDimensionLimit];
size_t target_ndim = 0;
get_padding_out_target_size(3, in, padding, target_sizes, &target_ndim);
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_relu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Tensor& relu_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {

ET_KERNEL_CHECK(ctx, tensor_is_real_type(out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_SWITCH_REAL_TYPES(in.scalar_type(), ctx, "relu.out", CTYPE, [&]() {
apply_unary_map_fn(
[](const CTYPE val_in) {
Expand Down
6 changes: 6 additions & 0 deletions kernels/portable/cpu/op_remainder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Tensor& remainder_Tensor_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, b, out), InvalidArgument, out);

ScalarType a_type = a.scalar_type();
ScalarType b_type = b.scalar_type();
ScalarType common_type = promoteTypes(a_type, b_type);
Expand Down Expand Up @@ -124,6 +127,9 @@ Tensor& remainder_Scalar_out(
out,
"Failed to resize output tensor.");

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, out), InvalidArgument, out);

ScalarType a_type = a.scalar_type();
ScalarType b_type = utils::get_scalar_dtype(b);
ScalarType common_type = utils::promote_type_with_scalar(a_type, b);
Expand Down
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_repeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Tensor& repeat_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(self, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(self), InvalidArgument, out);

// Resize for dynamic shape
ET_KERNEL_CHECK_MSG(
ctx,
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_roll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Tensor& roll_out(
ET_KERNEL_CHECK(
ctx, check_roll_args(in, shifts, dims, out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

if (in.numel() == 0) {
return out;
}
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_round.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Tensor& round_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
ctx, tensors_have_same_shape_and_dtype(in, out), InvalidArgument, out);
ET_KERNEL_CHECK(ctx, tensor_is_real_type(out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

auto in_scalar_type = in.scalar_type();

ET_SWITCH_REAL_TYPES(in.scalar_type(), ctx, "round.out", CTYPE, [&] {
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_rsub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Tensor& rsub_scalar_out(
out,
"Failed to resize output tensor.");

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_realhb_type(out), InvalidArgument, out);

ScalarType a_type = a.scalar_type();
Expand Down
9 changes: 9 additions & 0 deletions kernels/portable/cpu/op_scatter_add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ Tensor& scatter_add_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
context,
tensors_have_same_dim_order(self, src, out),
InvalidArgument,
out);

ET_KERNEL_CHECK(
context, tensor_is_default_dim_order(index), InvalidArgument, out);

if (dim < 0) {
dim += nonzero_dim(self);
}
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_select_scatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Tensor& select_scatter_out(
ET_KERNEL_CHECK(
ctx, resize_tensor(out, in.sizes()) == Error::Ok, InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, src, out), InvalidArgument, out);

// Account for negative indices
if (dim < 0) {
dim += in.dim();
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_sigmoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Tensor& sigmoid_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
ctx, in.scalar_type() != ScalarType::Bool, InvalidArgument, out);
ET_KERNEL_CHECK(ctx, tensor_is_floating_type(out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

// Resize for dynamic shape
ET_KERNEL_CHECK_MSG(
ctx,
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Tensor& sign_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
out,
"Failed to resize output tensor.");

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_shape_and_dtype(in, out), InvalidArgument, out);

Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_slice_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Tensor& slice_copy_Tensor_out(
dim += in.dim();
}

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

// If user do not set value to end_val, set end to in.size(dim) (largest
// value available)
int64_t end = end_val.has_value() ? end_val.value() : in.size(dim);
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_slice_scatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Tensor& slice_scatter_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(input, out), InvalidArgument, out);

if (input.numel() == 0) {
return out;
}
Expand Down
3 changes: 3 additions & 0 deletions kernels/portable/cpu/op_softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Tensor& softmax_out(
ET_KERNEL_CHECK(
ctx, resize_tensor(out, in.sizes()) == Error::Ok, InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

// Adjust for negative dim
dim = dim < 0 ? dim + nonzero_dim(in) : dim;

Expand Down
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_split_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void split_copy_Tensor_out(
check_split_copy_args(input, split_size, dim, out),
InvalidArgument, );

for (size_t i = 0; i < out.size(); ++i) {
ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(input, out[i]), InvalidArgument, );
}

const size_t leading_dims = getLeadingDims(input, dim);
const size_t trailing_dims = getTrailingDims(input, dim);
const size_t step = input.size(dim) * trailing_dims;
Expand Down
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_split_with_sizes_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ void split_with_sizes_copy_out(
check_split_with_sizes_copy_args(in, split_sizes, dim, out),
InvalidArgument, );

for (size_t i = 0; i < out.size(); ++i) {
ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out[i]), InvalidArgument, );
}

// If out is empty, then nothing needs to be done after checking the args.
// Valid args implies that in.size(dim) == 0 and split_sizes is also empty.
if (out.size() == 0) {
Expand Down
10 changes: 10 additions & 0 deletions kernels/portable/cpu/op_squeeze_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Tensor& squeeze_copy_dim_out(
ET_KERNEL_CHECK(
ctx, check_squeeze_copy_dim_args(in, dim, out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);

if (dim < 0) {
dim += nonzero_dim(in);
}
Expand Down Expand Up @@ -62,6 +67,11 @@ Tensor& squeeze_copy_dims_out(
ET_KERNEL_CHECK(
ctx, check_squeeze_copy_dims_args(in, dims, out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);

Tensor::SizesType expected_out_size[kTensorDimensionLimit];
size_t expected_out_dim = 0;
get_squeeze_copy_dims_out_target_size(
Expand Down
10 changes: 10 additions & 0 deletions kernels/portable/cpu/op_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ Tensor& stack_out(
ET_KERNEL_CHECK(
ctx, check_stack_args(tensors, dim, out), InvalidArgument, out);

for (size_t i = 0; i < tensors.size(); ++i) {
ET_KERNEL_CHECK(
ctx,
tensors_have_same_dim_order(tensors[i], out),
InvalidArgument,
out);
}

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(out), InvalidArgument, out);

Tensor::SizesType expected_out_size[kTensorDimensionLimit];
size_t expected_out_dim = 0;
get_stack_out_target_size(tensors, dim, expected_out_size, &expected_out_dim);
Expand Down
6 changes: 6 additions & 0 deletions kernels/portable/cpu/op_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Tensor& sub_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, b, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_realh_type(out), InvalidArgument, out);

ScalarType a_type = a.scalar_type();
Expand Down Expand Up @@ -131,6 +134,9 @@ Tensor& sub_scalar_out(

ET_KERNEL_CHECK(ctx, tensor_is_realh_type(out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, out), InvalidArgument, out);

ScalarType a_type = a.scalar_type();
ScalarType b_type = utils::get_scalar_dtype(b);
ScalarType alpha_type = utils::get_scalar_dtype(alpha);
Expand Down
5 changes: 5 additions & 0 deletions kernels/portable/cpu/op_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Tensor& sum_dim_out(
InvalidArgument,
out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);

ET_SWITCH_REAL_TYPES_AND(
Bool, in.scalar_type(), ctx, "sum.IntList_out", CTYPE_IN, [&] {
ET_SWITCH_REAL_TYPES_AND(
Expand Down
4 changes: 4 additions & 0 deletions kernels/portable/cpu/util/select_copy_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Error select_copy_util(
return Error::InvalidArgument;
}

if (!tensors_have_same_dim_order(in, out)) {
return Error::InvalidArgument;
}

// If the input is a empty tensor, no other operation could be done. We just
// return the output.
if (in.numel() == 0) {
Expand Down
Loading