|
| 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/runtime/kernel/kernel_includes.h> |
| 10 | + |
| 11 | +namespace torch { |
| 12 | +namespace executor { |
| 13 | +namespace native { |
| 14 | +namespace { |
| 15 | + |
| 16 | +bool check_repeat_interleave_args( |
| 17 | + const Tensor& repeats, |
| 18 | + int64_t output_size_value, |
| 19 | + int64_t repeats_sum, |
| 20 | + Tensor& out) { |
| 21 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 22 | + repeats.scalar_type() == ScalarType::Int || |
| 23 | + repeats.scalar_type() == ScalarType::Long, |
| 24 | + "repeats must be int or long"); |
| 25 | + ET_LOG_MSG_AND_RETURN_IF_FALSE(repeats.dim() == 1, "repeats must be 1D"); |
| 26 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 27 | + output_size_value == repeats_sum, |
| 28 | + "output_size, if provided, must be equal to repeats.sum()"); |
| 29 | + ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(repeats, out)); |
| 30 | + |
| 31 | + if (repeats.scalar_type() == ScalarType::Long) { |
| 32 | + const int64_t* const repeats_data = repeats.const_data_ptr<int64_t>(); |
| 33 | + for (size_t i = 0; i < repeats.numel(); ++i) { |
| 34 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 35 | + repeats_data[i] >= 0, "repeats cannot be negative"); |
| 36 | + } |
| 37 | + } else { |
| 38 | + const int32_t* const repeats_data = repeats.const_data_ptr<int32_t>(); |
| 39 | + for (size_t i = 0; i < repeats.numel(); ++i) { |
| 40 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 41 | + repeats_data[i] >= 0, "repeats cannot be negative"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +} // namespace |
| 49 | + |
| 50 | +using Tensor = exec_aten::Tensor; |
| 51 | + |
| 52 | +Tensor& repeat_interleave_Tensor_out( |
| 53 | + KernelRuntimeContext& ctx, |
| 54 | + const Tensor& repeats, |
| 55 | + exec_aten::optional<int64_t> output_size, |
| 56 | + Tensor& out) { |
| 57 | + (void)ctx; |
| 58 | + |
| 59 | + int64_t repeats_sum = 0; |
| 60 | + |
| 61 | + constexpr auto name = "repeat_interleave.Tensor_out"; |
| 62 | + |
| 63 | + ET_SWITCH_TWO_TYPES(Int, Long, repeats.scalar_type(), ctx, name, CTYPE, [&] { |
| 64 | + const CTYPE* repeats_data = repeats.const_data_ptr<CTYPE>(); |
| 65 | + for (size_t ix = 0; ix < repeats.numel(); ++ix) { |
| 66 | + repeats_sum += static_cast<int64_t>(repeats_data[ix]); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + int64_t output_size_value = |
| 71 | + output_size.has_value() ? output_size.value() : repeats_sum; |
| 72 | + |
| 73 | + ET_KERNEL_CHECK( |
| 74 | + ctx, |
| 75 | + check_repeat_interleave_args( |
| 76 | + repeats, output_size_value, repeats_sum, out), |
| 77 | + InvalidArgument, |
| 78 | + out); |
| 79 | + |
| 80 | + ET_KERNEL_CHECK( |
| 81 | + ctx, tensors_have_same_dim_order(repeats, out), InvalidArgument, out); |
| 82 | + |
| 83 | + ET_KERNEL_CHECK( |
| 84 | + ctx, tensor_is_default_dim_order(repeats), InvalidArgument, out); |
| 85 | + |
| 86 | + ET_KERNEL_CHECK_MSG( |
| 87 | + ctx, |
| 88 | + resize_tensor( |
| 89 | + out, {static_cast<exec_aten::SizesType>(output_size_value)}) == |
| 90 | + Error::Ok, |
| 91 | + InvalidArgument, |
| 92 | + out, |
| 93 | + "Failed to resize output tensor."); |
| 94 | + |
| 95 | + ET_SWITCH_TWO_TYPES(Int, Long, repeats.scalar_type(), ctx, name, CTYPE, [&] { |
| 96 | + const CTYPE* repeats_data = repeats.const_data_ptr<CTYPE>(); |
| 97 | + CTYPE* out_data = out.mutable_data_ptr<CTYPE>(); |
| 98 | + size_t out_ix = 0; |
| 99 | + for (size_t ix = 0; ix < repeats.numel(); ix++) { |
| 100 | + for (CTYPE i = 0; i < repeats_data[ix]; i++, out_ix++) { |
| 101 | + out_data[out_ix] = static_cast<CTYPE>(ix); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + return out; |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace native |
| 110 | +} // namespace executor |
| 111 | +} // namespace torch |
0 commit comments