xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/op_full.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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/scalar_utils.h>
10 #include <executorch/runtime/kernel/kernel_includes.h>
11 
12 namespace torch {
13 namespace executor {
14 namespace native {
15 
16 using Tensor = exec_aten::Tensor;
17 using ScalarType = exec_aten::ScalarType;
18 
full_out(KernelRuntimeContext & ctx,const IntArrayRef sizes,const Scalar & fill_value,Tensor & out)19 Tensor& full_out(
20     KernelRuntimeContext& ctx,
21     const IntArrayRef sizes,
22     const Scalar& fill_value,
23     Tensor& out) {
24   (void)ctx;
25 
26   ScalarType val_type = utils::get_scalar_dtype(fill_value);
27   ScalarType out_type = out.scalar_type();
28 
29   // Resize for dynamic shape
30   ET_KERNEL_CHECK_MSG(
31       ctx,
32       resize_tensor(out, sizes) == Error::Ok,
33       InvalidArgument,
34       out,
35       "Failed to resize output tensor.");
36 
37   constexpr auto name = "full.out";
38 
39   ET_SWITCH_SCALAR_OBJ_TYPES(val_type, ctx, name, CTYPE_VAL, [&] {
40     CTYPE_VAL val;
41     utils::extract_scalar(fill_value, &val);
42 
43     ET_SWITCH_REALHB_TYPES(out_type, ctx, name, CTYPE_OUT, [&] {
44       CTYPE_OUT val_casted = static_cast<CTYPE_OUT>(val);
45       auto data_out = out.mutable_data_ptr<CTYPE_OUT>();
46       for (size_t i = 0; i < out.numel(); ++i) {
47         data_out[i] = val_casted;
48       }
49     });
50   });
51 
52   return out;
53 }
54 
55 } // namespace native
56 } // namespace executor
57 } // namespace torch
58