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 Tensor&
scalar_tensor_out(KernelRuntimeContext & ctx,const Scalar & s,Tensor & out)17 scalar_tensor_out(KernelRuntimeContext& ctx, const Scalar& s, Tensor& out) {
18 (void)ctx;
19
20 ET_KERNEL_CHECK(
21 ctx, resize_tensor(out, {}) == Error::Ok, InvalidArgument, out);
22
23 ScalarType s_type = utils::get_scalar_dtype(s);
24 ScalarType out_type = out.scalar_type();
25
26 constexpr auto name = "scalar_tensor.out";
27
28 ET_SWITCH_REAL_TYPES_AND3(
29 Half, Bool, BFloat16, out_type, ctx, name, CTYPE, [&]() {
30 ET_SWITCH_SCALAR_OBJ_TYPES(s_type, ctx, name, CTYPE_S, [&]() {
31 CTYPE_S val_s;
32 utils::extract_scalar(s, &val_s);
33 out.mutable_data_ptr<CTYPE>()[0] = convert<CTYPE, CTYPE_S>(val_s);
34 });
35 });
36
37 return out;
38 }
39
40 } // namespace native
41 } // namespace executor
42 } // namespace torch
43