• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/kernels/portable/cpu/util/broadcast_util.h>
11 #include <executorch/kernels/portable/cpu/util/kernel_ops_util.h>
12 #include <executorch/runtime/kernel/kernel_includes.h>
13 
14 namespace torch {
15 namespace executor {
16 namespace native {
17 
18 using Tensor = exec_aten::Tensor;
19 using ScalarType = exec_aten::ScalarType;
20 using Scalar = exec_aten::Scalar;
21 
masked_fill_scalar_out(KernelRuntimeContext & ctx,const Tensor & in,const Tensor & mask,const Scalar & value,Tensor & out)22 Tensor& masked_fill_scalar_out(
23     KernelRuntimeContext& ctx,
24     const Tensor& in,
25     const Tensor& mask,
26     const Scalar& value,
27     Tensor& out) {
28   (void)ctx;
29 
30   ET_KERNEL_CHECK(
31       ctx, check_masked_fill_args(in, mask, value, out), InvalidArgument, out);
32 
33   ScalarType in_type = in.scalar_type();
34   ScalarType val_type = utils::get_scalar_dtype(value);
35 
36   ET_KERNEL_CHECK(
37       ctx,
38       resize_to_broadcast_target_size(in, mask, out) == Error::Ok,
39       InvalidArgument,
40       out);
41 
42   ET_KERNEL_CHECK(
43       ctx, tensors_have_same_dim_order(in, mask, out), InvalidArgument, out);
44 
45   ET_SWITCH_REAL_TYPES_AND(
46       Bool, in_type, ctx, "masked_fill.Scalar_out", CTYPE, [&]() {
47         ET_SWITCH_REAL_TYPES_AND(
48             Bool, val_type, ctx, "masked_fill.Scalar_out", CTYPE_VAL, [&]() {
49               CTYPE_VAL value_v;
50               utils::extract_scalar(value, &value_v);
51               CTYPE val = static_cast<CTYPE>(value_v);
52 
53               apply_binary_elementwise_fn<CTYPE, bool, CTYPE>(
54                   [val](const CTYPE val_in, const bool val_mask) {
55                     return val_mask ? val : val_in;
56                   },
57                   in,
58                   mask,
59                   out);
60             });
61       });
62 
63   return out;
64 }
65 
66 } // namespace native
67 } // namespace executor
68 } // namespace torch
69