Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions kernels/portable/cpu/op_arange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include <executorch/kernels/portable/cpu/scalar_utils.h>
#include <executorch/kernels/portable/cpu/util/arange_util.h>
#include <executorch/kernels/portable/cpu/util/kernel_ops_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <executorch/runtime/platform/assert.h>
Expand All @@ -29,22 +30,15 @@ Tensor& arange_out(KernelRuntimeContext& ctx, const Scalar& end, Tensor& out) {

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(out), InvalidArgument, out);

size_t size = static_cast<size_t>(std::ceil(end_val));

Tensor::SizesType out_length = static_cast<Tensor::SizesType>(size);
Tensor::SizesType out_length = compute_arange_out_size(0.0, end_val, 1.0);

ET_KERNEL_CHECK(
ctx,
resize_tensor(out, {&out_length, 1}) == Error::Ok,
InvalidArgument,
out);

ET_SWITCH_REALHBF16_TYPES(out.scalar_type(), ctx, "arange.out", CTYPE, [&]() {
auto out_data = out.mutable_data_ptr<CTYPE>();
for (size_t i = 0; i < size; i++) {
out_data[i] = static_cast<CTYPE>(i);
}
});
arange_out_impl(ctx, end_val, out);

return out;
}
Expand Down Expand Up @@ -77,24 +71,16 @@ Tensor& arange_start_out(

ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(out), InvalidArgument, out);

double size_d = (d_end - d_start) / d_step;
size_t size = static_cast<size_t>(std::ceil(size_d));

Tensor::SizesType out_length = static_cast<Tensor::SizesType>(size);
Tensor::SizesType out_length =
compute_arange_out_size(d_start, d_end, d_step);

ET_KERNEL_CHECK(
ctx,
resize_tensor(out, {&out_length, 1}) == Error::Ok,
InvalidArgument,
out);

ET_SWITCH_REALHBF16_TYPES(
out.scalar_type(), ctx, "arange.start_out", CTYPE, [&]() {
auto out_data = out.mutable_data_ptr<CTYPE>();
for (size_t i = 0; i < size; i++) {
out_data[i] = convert<CTYPE, double>(d_start + i * d_step);
}
});
arange_out_impl(ctx, d_start, d_end, d_step, out);

return out;
}
Expand Down
44 changes: 44 additions & 0 deletions kernels/portable/cpu/util/arange_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/kernels/portable/cpu/util/arange_util.h>

namespace torch::executor::native {
#define ET_ARANGE_IMPL(ctx, start, numel, step, out, op_name) \
ET_SWITCH_REALHBF16_TYPES(out.scalar_type(), ctx, op_name, CTYPE, [&]() { \
auto out_data = out.mutable_data_ptr<CTYPE>(); \
for (size_t i = 0; i < numel; ++i) { \
out_data[i] = static_cast<CTYPE>(start + i * step); \
} \
})

Tensor::SizesType
compute_arange_out_size(double start, double end, double step) {
ET_CHECK_MSG(
end > start, "end (%f) must be greater than start (%f)", end, start);
ET_CHECK_MSG(step > 0, "step must be positive, got %f", step);
Tensor::SizesType numel =
static_cast<Tensor::SizesType>(std::ceil((end - start) / step));
return numel;
}

void arange_out_impl(
KernelRuntimeContext& ctx,
double start,
double end,
double step,
Tensor& out) {
Tensor::SizesType numel = compute_arange_out_size(start, end, step);
ET_ARANGE_IMPL(ctx, start, numel, step, out, "arange.start_out");
}

void arange_out_impl(KernelRuntimeContext& ctx, double end, Tensor& out) {
ET_ARANGE_IMPL(ctx, 0.0, end, 1.0, out, "arange.out");
}

} // namespace torch::executor::native
41 changes: 41 additions & 0 deletions kernels/portable/cpu/util/arange_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <executorch/runtime/kernel/kernel_includes.h>

namespace torch::executor::native {

Tensor::SizesType
compute_arange_out_size(double start, double end, double step);

inline Tensor::SizesType compute_arange_out_size(double end) {
return compute_arange_out_size(0.0, end, 1.0);
}

void arange_out_impl(
KernelRuntimeContext& ctx,
double start,
double end,
double step,
Tensor& out);

void arange_out_impl(KernelRuntimeContext& ctx, double end, Tensor& out);

inline void
arange_out_impl(double start, double end, double step, Tensor& out) {
KernelRuntimeContext ctx;
arange_out_impl(ctx, start, end, step, out);
}

inline void arange_out_impl(double end, Tensor& out) {
KernelRuntimeContext ctx;
arange_out_impl(ctx, 0.0, end, 1.0, out);
}
} // namespace torch::executor::native
14 changes: 14 additions & 0 deletions kernels/portable/cpu/util/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def define_common_targets():
name = "all_deps",
exported_deps = [
"//executorch/extension/threadpool:threadpool",
"//executorch/kernels/portable/cpu/util:arange_util",
"//executorch/kernels/portable/cpu/util:functional_util",
"//executorch/kernels/portable/cpu/util:broadcast_util",
"//executorch/kernels/portable/cpu/util:kernel_ops_util",
Expand Down Expand Up @@ -294,6 +295,19 @@ def define_common_targets():
visibility = ["//executorch/kernels/portable/cpu/..."],
)

runtime.cxx_library(
name = "arange_util",
srcs = ["arange_util.cpp"],
exported_headers = ["arange_util.h"],
deps = [
"//executorch/runtime/kernel:kernel_includes",
],
visibility = [
"//executorch/kernels/portable/cpu/...",
"//executorch/extension/llm/...",
],
)

runtime.cxx_library(
name = "broadcast_indexes_range",
exported_headers = ["broadcast_indexes_range.h"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ ATEN_OPS = (
op_target(
name = "op_arange",
deps = [
"//executorch/kernels/portable/cpu/util:arange_util",
"//executorch/kernels/portable/cpu/util:kernel_ops_util",
"//executorch/runtime/core/exec_aten/util:scalar_type_util",
"//executorch/runtime/core/exec_aten/util:tensor_util",
Expand Down
Loading