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/copy_ops_util.h>
10 #include <executorch/runtime/core/exec_aten/util/tensor_util.h>
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 template <typename SELF_CTYPE, typename OUT_CTYPE>
_to_impl(const Tensor & self,Tensor & out)20 void _to_impl(const Tensor& self, Tensor& out) {
21 auto self_data = self.mutable_data_ptr<SELF_CTYPE>();
22 auto out_data = out.mutable_data_ptr<OUT_CTYPE>();
23
24 for (int i = 0; i < self.numel(); i++) {
25 out_data[i] = static_cast<OUT_CTYPE>(self_data[i]);
26 }
27 }
28
29 // to_copy.out(Tensor self, *, bool non_blocking=False, MemoryFormat?
30 // memory_format=None, Tensor(a!) out) -> Tensor(a!)
to_copy_out(KernelRuntimeContext & ctx,const Tensor & self,bool non_blocking,exec_aten::optional<exec_aten::MemoryFormat> memory_format,Tensor & out)31 Tensor& to_copy_out(
32 KernelRuntimeContext& ctx,
33 const Tensor& self,
34 bool non_blocking,
35 exec_aten::optional<exec_aten::MemoryFormat> memory_format,
36 Tensor& out) {
37 ET_KERNEL_CHECK(
38 ctx,
39 check_to_copy_args(self, non_blocking, memory_format, out),
40 InvalidArgument,
41 out);
42
43 ET_KERNEL_CHECK(
44 ctx,
45 resize_tensor(out, self.sizes()) == torch::executor::Error::Ok,
46 InvalidArgument,
47 out);
48
49 ET_KERNEL_CHECK(
50 ctx, tensors_have_same_dim_order(self, out), InvalidArgument, out);
51
52 ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(self), InvalidArgument, out);
53
54 ET_SWITCH_REALHBBF16_TYPES(self.scalar_type(), ctx, "to_copy", CTYPE_IN, [&] {
55 ET_SWITCH_REALHBBF16_TYPES(
56 out.scalar_type(), ctx, "to_copy", CTYPE_OUT, [&] {
57 _to_impl<CTYPE_IN, CTYPE_OUT>(self, out);
58 });
59 });
60
61 return out;
62 }
63
64 } // namespace native
65 } // namespace executor
66 } // namespace torch
67