xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/op_roll.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <executorch/runtime/kernel/kernel_includes.h>
10 #include <cstddef>
11 
12 namespace torch {
13 namespace executor {
14 namespace native {
15 namespace {
16 
check_roll_args(const Tensor & in,IntArrayRef shifts,IntArrayRef dims,const Tensor & out)17 bool check_roll_args(
18     const Tensor& in,
19     IntArrayRef shifts,
20     IntArrayRef dims,
21     const Tensor& out) {
22   ET_LOG_AND_RETURN_IF_FALSE(tensor_has_rank_greater_or_equal_to(in, 1));
23   if (in.numel() > 0) {
24     for (const auto& d : dims) {
25       ET_LOG_AND_RETURN_IF_FALSE(dim_is_valid(d, in.dim()));
26     }
27   }
28   ET_LOG_AND_RETURN_IF_FALSE(!shifts.empty());
29   ET_LOG_AND_RETURN_IF_FALSE(shifts.size() == dims.size());
30   ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));
31   return true;
32 }
33 
unshift_flat_ix(size_t ix,const Tensor & in,IntArrayRef dim_shifts)34 size_t unshift_flat_ix(size_t ix, const Tensor& in, IntArrayRef dim_shifts) {
35   size_t ix_coord[kTensorDimensionLimit];
36   indexToCoordinate(in, ix, ix_coord);
37 
38   size_t shifted_coord[kTensorDimensionLimit];
39   for (size_t d = 0; d < in.dim(); d++) {
40     shifted_coord[d] =
41         (ix_coord[d] + in.size(d) - dim_shifts[d] % in.size(d)) % in.size(d);
42   }
43 
44   return coordinateToIndex(in, shifted_coord);
45 }
46 
47 } // namespace
48 
roll_out(KernelRuntimeContext & ctx,const Tensor & in,IntArrayRef shifts,IntArrayRef dims,Tensor & out)49 Tensor& roll_out(
50     KernelRuntimeContext& ctx,
51     const Tensor& in,
52     IntArrayRef shifts,
53     IntArrayRef dims,
54     Tensor& out) {
55   (void)ctx;
56 
57   ET_KERNEL_CHECK(
58       ctx, resize_tensor(out, in.sizes()) == Error::Ok, InvalidArgument, out);
59 
60   ET_KERNEL_CHECK(
61       ctx, check_roll_args(in, shifts, dims, out), InvalidArgument, out);
62 
63   ET_KERNEL_CHECK(
64       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
65 
66   if (in.numel() == 0) {
67     return out;
68   }
69 
70   int64_t dim_shift_array[kTensorDimensionLimit];
71   for (size_t i = 0; i < in.dim(); i++) {
72     dim_shift_array[i] = 0;
73   }
74   for (size_t i = 0; i < dims.size(); i++) {
75     const auto d = dims[i] < 0 ? dims[i] + in.dim() : dims[i];
76     dim_shift_array[d] += shifts[i];
77   }
78 
79   size_t dim_shift_array_length = static_cast<size_t>(in.dim()); // NOLINT
80   IntArrayRef dim_shifts(dim_shift_array, dim_shift_array_length);
81 
82   constexpr auto name = "roll.out";
83 
84   ET_SWITCH_REALHB_TYPES(in.scalar_type(), ctx, name, CTYPE, [&] {
85     const CTYPE* in_data = in.const_data_ptr<CTYPE>();
86     CTYPE* out_data = out.mutable_data_ptr<CTYPE>();
87 
88     for (size_t ix = 0; ix < out.numel(); ++ix) {
89       out_data[ix] = in_data[unshift_flat_ix(ix, in, dim_shifts)];
90     }
91   });
92 
93   return out;
94 }
95 
96 } // namespace native
97 } // namespace executor
98 } // namespace torch
99