• 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 <cstdint>
10 #include <cstring>
11 
12 #include <executorch/kernels/portable/cpu/util/copy_ops_util.h>
13 #include <executorch/runtime/kernel/kernel_includes.h>
14 
15 namespace torch {
16 namespace executor {
17 namespace native {
18 
19 using Tensor = exec_aten::Tensor;
20 
21 // view_copy.out(Tensor self, int[] size, *, Tensor(a!) out) -> Tensor(a!)
view_copy_out(KernelRuntimeContext & ctx,const Tensor & self,exec_aten::ArrayRef<int64_t> size_int64_t,Tensor & out)22 Tensor& view_copy_out(
23     KernelRuntimeContext& ctx,
24     const Tensor& self,
25     exec_aten::ArrayRef<int64_t> size_int64_t,
26     Tensor& out) {
27   (void)ctx;
28 
29   Tensor::SizesType expected_output_size[16];
30   ET_KERNEL_CHECK(
31       ctx,
32       get_view_copy_target_size(
33           self, size_int64_t, out.dim(), expected_output_size),
34       InvalidArgument,
35       out);
36 
37   // Resize for dynamic shape
38   ET_KERNEL_CHECK_MSG(
39       ctx,
40       resize_tensor(
41           out, {expected_output_size, static_cast<size_t>(out.dim())}) ==
42           Error::Ok,
43       InvalidArgument,
44       out,
45       "Failed to resize output tensor.");
46 
47   ET_KERNEL_CHECK(
48       ctx, tensors_have_same_dim_order(self, out), InvalidArgument, out);
49 
50   ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(self), InvalidArgument, out);
51 
52   ET_KERNEL_CHECK(
53       ctx, check_view_copy_args(self, size_int64_t, out), InvalidArgument, out);
54 
55   if (self.nbytes() > 0) {
56     memcpy(out.mutable_data_ptr(), self.const_data_ptr(), self.nbytes());
57   }
58   return out;
59 }
60 
61 } // namespace native
62 } // namespace executor
63 } // namespace torch
64