Skip to content
Open
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
2 changes: 1 addition & 1 deletion backends/cadence/fusion_g3/operators/op_slice_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Tensor& slice_copy_Tensor_out(
InvalidArgument,
out);

torch::executor::compute_slice(in, dim, start, length, step, out);
torch::executor::compute_slice(ctx, in, dim, start, length, step, out);
}

return out;
Expand Down
2 changes: 1 addition & 1 deletion backends/cadence/hifi/operators/op_slice_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Tensor& slice_copy_Tensor_out(
InvalidArgument,
out);

compute_slice(in, dim, start, length, step, out);
compute_slice(ctx, in, dim, start, length, step, out);

return out;
}
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_narrow_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Tensor& narrow_copy_out(
out);

if (length != 0) {
compute_slice(in, dim, start, length, 1, out);
compute_slice(ctx, in, dim, start, length, 1, out);
}

return out;
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_slice_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Tensor& slice_copy_Tensor_out(
InvalidArgument,
out);

compute_slice(in, dim, start, length, step, out);
compute_slice(ctx, in, dim, start, length, step, out);

return out;
}
Expand Down
24 changes: 24 additions & 0 deletions kernels/portable/cpu/util/slice_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,31 @@ int64_t adjust_slice_indices(
}

void compute_slice(
KernelRuntimeContext& ctx,
const Tensor& in,
int64_t dim,
int64_t start,
int64_t length,
int64_t step,
Tensor& out) {
// No slicing requested.
if (length <= 0) {
return;
}

ET_KERNEL_CHECK_MSG(
ctx,
dim < in.dim(),
InvalidArgument,
/* void */,
"Requested dim is larger than input tensor dim");
size_t dim_length = in.size(dim);
ET_KERNEL_CHECK_MSG(
ctx,
(start + (length - 1) * step) < dim_length,
InvalidArgument,
/* void */,
"Requested slice is larger than the dim size");

size_t leading_dims = getLeadingDims(in, dim);
size_t trailing_dims = getTrailingDims(in, dim);
Expand All @@ -170,6 +188,12 @@ void compute_slice(
const char* input_data = in.const_data_ptr<char>();
char* dest = out.mutable_data_ptr<char>();

ET_KERNEL_CHECK_MSG(
ctx,
out.nbytes() >= (length * leading_dims * length_per_step),
InvalidArgument,
/* void */,
"out.nbytes() is smaller than the expected slice size.");
for (const auto i : c10::irange(leading_dims)) {
const char* src = input_data + (i * dim_length + start) * length_per_step;
for ([[maybe_unused]] const auto j : c10::irange(length)) {
Expand Down
1 change: 1 addition & 0 deletions kernels/portable/cpu/util/slice_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int64_t adjust_slice_indices(
int64_t step);

void compute_slice(
KernelRuntimeContext& ctx,
const Tensor& in,
int64_t dim,
int64_t start,
Expand Down
Loading