• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/kernels/portable/cpu/util/slice_util.h>
10 #include <executorch/runtime/kernel/kernel_includes.h>
11 #include <cstring>
12 
13 namespace torch {
14 namespace executor {
15 namespace native {
16 
17 using Tensor = exec_aten::Tensor;
18 
slice_copy_Tensor_out(KernelRuntimeContext & ctx,const Tensor & in,int64_t dim,exec_aten::optional<int64_t> start_val,exec_aten::optional<int64_t> end_val,int64_t step,Tensor & out)19 Tensor& slice_copy_Tensor_out(
20     KernelRuntimeContext& ctx,
21     const Tensor& in,
22     int64_t dim,
23     exec_aten::optional<int64_t> start_val,
24     exec_aten::optional<int64_t> end_val,
25     int64_t step,
26     Tensor& out) {
27   (void)ctx;
28 
29   ET_KERNEL_CHECK(
30       ctx, check_slice_copy_args(in, dim, step, out), InvalidArgument, out);
31 
32   if (dim < 0) {
33     dim += in.dim();
34   }
35 
36   ET_KERNEL_CHECK(
37       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
38 
39   // If user do not set value to end_val, set end to in.size(dim) (largest
40   // value available)
41   int64_t end = end_val.has_value() ? end_val.value() : in.size(dim);
42   // If user do not set value to start_val, set start to 0 (smallest value
43   // available)
44   int64_t start = start_val.has_value() ? start_val.value() : 0;
45 
46   int64_t length = adjust_slice_indices(in.size(dim), &start, &end, step);
47 
48   // @lint-ignore CLANGTIDY facebook-hte-CArray
49   Tensor::SizesType target_sizes[kTensorDimensionLimit];
50   size_t target_ndim = 0;
51   get_slice_copy_out_target_size(in, dim, length, target_sizes, &target_ndim);
52   ET_KERNEL_CHECK(
53       ctx,
54       resize_tensor(out, {target_sizes, target_ndim}) == Error::Ok,
55       InvalidArgument,
56       out);
57 
58   compute_slice(in, dim, start, length, step, out);
59 
60   return out;
61 }
62 
63 } // namespace native
64 } // namespace executor
65 } // namespace torch
66