|
| 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 | +#include <executorch/kernels/portable/cpu/util/elementwise_util.h> |
| 9 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | +namespace torch { |
| 13 | +namespace executor { |
| 14 | +namespace native { |
| 15 | + |
| 16 | +Tensor& opt_where_out( |
| 17 | + KernelRuntimeContext& ctx, |
| 18 | + const Tensor& cond, |
| 19 | + const Tensor& a, |
| 20 | + const Tensor& b, |
| 21 | + Tensor& out) { |
| 22 | + // Common Dtype |
| 23 | + ScalarType common_type = promoteTypes(a.scalar_type(), b.scalar_type()); |
| 24 | + |
| 25 | + // Check Common Dtype |
| 26 | + ET_KERNEL_CHECK(ctx, common_type == out.scalar_type(), InvalidArgument, out); |
| 27 | + |
| 28 | + // Check Dim Order |
| 29 | + ET_KERNEL_CHECK( |
| 30 | + ctx, tensors_have_same_dim_order(cond, a, b, out), InvalidArgument, out); |
| 31 | + |
| 32 | + // Resize |
| 33 | + ET_KERNEL_CHECK( |
| 34 | + ctx, |
| 35 | + resize_to_broadcast_target_size(a, b, cond, out) == Error::Ok, |
| 36 | + InvalidArgument, |
| 37 | + out); |
| 38 | + |
| 39 | + // Compute Dtype |
| 40 | + ScalarType compute_type = utils::get_compute_type(common_type); |
| 41 | + |
| 42 | + // @lint-ignore CLANGTIDY facebook-hte-CArray |
| 43 | + static constexpr const char op_name[] = "where.self_out"; |
| 44 | + |
| 45 | + if (a.scalar_type() == b.scalar_type() && |
| 46 | + a.scalar_type() == out.scalar_type() && a.scalar_type() == compute_type && |
| 47 | + // Using a Byte tensor for cond has been deprecated for a long time. |
| 48 | + cond.scalar_type() == ScalarType::Bool) { |
| 49 | + auto out_numel = out.numel(); |
| 50 | + ET_SWITCH_REALB_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { |
| 51 | + const bool a_is_broadcasted = !out.sizes().equals(a.sizes()); |
| 52 | + const bool b_is_broadcasted = !out.sizes().equals(b.sizes()); |
| 53 | + const bool cond_is_broadcasted = !out.sizes().equals(cond.sizes()); |
| 54 | + const bool any_is_broadcasted = |
| 55 | + (a_is_broadcasted || b_is_broadcasted || cond_is_broadcasted); |
| 56 | + const CTYPE_COMPUTE* const data_a = a.const_data_ptr<CTYPE_COMPUTE>(); |
| 57 | + const CTYPE_COMPUTE* const data_b = b.const_data_ptr<CTYPE_COMPUTE>(); |
| 58 | + const bool* const data_cond = cond.const_data_ptr<bool>(); |
| 59 | + CTYPE_COMPUTE* const data_out = out.data_ptr<CTYPE_COMPUTE>(); |
| 60 | + if (any_is_broadcasted) { |
| 61 | + for (const auto [out_index, a_index, b_index, cond_index] : |
| 62 | + BroadcastIndexesRange<3>(out, a, b, cond)) { |
| 63 | + data_out[out_index] = |
| 64 | + data_cond[cond_index] ? data_a[a_index] : data_b[b_index]; |
| 65 | + } |
| 66 | + } else { |
| 67 | + for (const auto i : c10::irange(out_numel)) { |
| 68 | + data_out[i] = data_cond[i] ? data_a[i] : data_b[i]; |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + } else { |
| 73 | + // Fall back for mixed dtype to keep code size and compile time |
| 74 | + // reasonable. |
| 75 | + ET_SWITCH_REALB_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { |
| 76 | + utils::apply_tritensor_elementwise_fn<CTYPE_COMPUTE, op_name>( |
| 77 | + [](const CTYPE_COMPUTE val_a, |
| 78 | + const CTYPE_COMPUTE val_b, |
| 79 | + const CTYPE_COMPUTE val_c) { return val_c ? val_a : val_b; }, |
| 80 | + ctx, |
| 81 | + a, |
| 82 | + utils::SupportedTensorDtypes::REALHBBF16, |
| 83 | + b, |
| 84 | + utils::SupportedTensorDtypes::REALHBBF16, |
| 85 | + cond, |
| 86 | + utils::SupportedTensorDtypes::BOOL_OR_BYTE, |
| 87 | + out, |
| 88 | + utils::SupportedTensorDtypes::SAME_AS_COMMON); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + return out; |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace native |
| 96 | +} // namespace executor |
| 97 | +} // namespace torch |
0 commit comments