xref: /aosp_15_r20/external/executorch/extension/llm/custom_ops/op_fallback.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Qualcomm Innovation Center, Inc.
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 #include <executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h>
9 #include <executorch/extension/llm/custom_ops/op_fallback.h>
10 #include <cstring>
11 
12 namespace torch {
13 namespace executor {
14 
15 namespace native {
16 
17 // Copy from op_clone.cpp
fallback_out(KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)18 Tensor& fallback_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
19   (void)ctx;
20 
21   ET_KERNEL_CHECK(
22       ctx,
23       resize_tensor(out, in.sizes()) == torch::executor::Error::Ok,
24       InvalidArgument,
25       out);
26 
27   // The input and out shall share same dtype and size
28   ET_KERNEL_CHECK(
29       ctx, tensors_have_same_shape_and_dtype(in, out), InvalidArgument, out);
30 
31   if (in.nbytes() > 0) {
32     // Note that this check is important. It's valid for a tensor with numel 0
33     // to have a null data pointer, but in some environments it's invalid to
34     // pass a null pointer to memcpy() even when the size is zero.
35     memcpy(out.mutable_data_ptr(), in.const_data_ptr(), in.nbytes());
36   }
37 
38   return out;
39 }
40 
41 } // namespace native
42 } // namespace executor
43 } // namespace torch
44 
45 EXECUTORCH_LIBRARY(
46     llama,
47     "fallback.out",
48     torch::executor::native::fallback_out);
49