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 <cstring>
11
12 namespace torch {
13 namespace executor {
14 namespace native {
15
16 using Tensor = exec_aten::Tensor;
17 using ScalarType = exec_aten::ScalarType;
18
19 namespace {} // namespace
20
21 /**
22 * Copy the tener `self` to `out`, assume `self` and `out` have same type and
23 * shape
24 */
25 Tensor&
detach_copy_out(KernelRuntimeContext & ctx,const Tensor & self,Tensor & out)26 detach_copy_out(KernelRuntimeContext& ctx, const Tensor& self, Tensor& out) {
27 (void)ctx;
28
29 // Resize for dynamic shape
30 ET_KERNEL_CHECK_MSG(
31 ctx,
32 resize_tensor(out, self.sizes()) == Error::Ok,
33 InvalidArgument,
34 out,
35 "Failed to resize output tensor.");
36
37 ET_KERNEL_CHECK(
38 ctx, tensors_have_same_dim_order(self, out), InvalidArgument, out);
39
40 ET_KERNEL_CHECK(
41 ctx, tensors_have_same_shape_and_dtype(self, out), InvalidArgument, out);
42
43 if (self.nbytes() > 0) {
44 // Note that this check is important. It's valid for a tensor with numel 0
45 // to have a null data pointer, but in some environments it's invalid to
46 // pass a null pointer to memcpy() even when the size is zero.
47 memcpy(out.mutable_data_ptr(), self.const_data_ptr(), self.nbytes());
48 }
49
50 return out;
51 }
52
53 } // namespace native
54 } // namespace executor
55 } // namespace torch
56