• 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 <cstring>
10 
11 #include <executorch/runtime/kernel/kernel_includes.h>
12 
13 namespace torch {
14 namespace executor {
15 namespace native {
16 
17 using Tensor = exec_aten::Tensor;
18 
19 Tensor&
alias_copy_out(KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)20 alias_copy_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
21   (void)ctx;
22 
23   // Resize for dynamic shape
24   ET_KERNEL_CHECK_MSG(
25       ctx,
26       resize_tensor(out, in.sizes()) == Error::Ok,
27       InvalidArgument,
28       out,
29       "Failed to resize output tensor.");
30 
31   ET_KERNEL_CHECK(ctx, tensors_have_same_dtype(in, out), InvalidArgument, out);
32   ET_KERNEL_CHECK(
33       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
34 
35   if (in.nbytes() > 0) {
36     // Note that this check is important. It's valid for a tensor with numel 0
37     // to have a null data pointer, but in some environments it's invalid to
38     // pass a null pointer to memcpy() even when the size is zero.
39     memcpy(out.mutable_data_ptr(), in.const_data_ptr(), in.nbytes());
40   }
41   return out;
42 }
43 
44 } // namespace native
45 } // namespace executor
46 } // namespace torch
47