Skip to content

Sync constexpr irange patch from PyTorch core #8610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion runtime/core/array_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <array>
#include <cstdint>

#include <c10/util/irange.h>
#include <executorch/runtime/platform/assert.h>

namespace executorch {
Expand Down Expand Up @@ -149,7 +150,7 @@ class ArrayRef final {
if (Length != RHS.Length) {
return false;
}
for (size_t i = 0; i < this->Length; i++) {
for (const auto i : c10::irange(this->Length)) {
if (Data[i] != RHS.Data[i]) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/core/hierarchical_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <c10/util/irange.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/core/span.h>
Expand Down Expand Up @@ -96,7 +97,7 @@ class HierarchicalAllocator final {
"n_allocators %" PRIu32 " > %zu",
n_allocators,
kSpanArraySize);
for (uint32_t i = 0; i < n_allocators; ++i) {
for (const auto i : c10::irange(n_allocators)) {
span_array_[i] =
Span<uint8_t>(allocators[i].base_address(), allocators[i].size());
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/core/portable_type/c10/c10/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def define_common_targets():
"-DC10_USING_CUSTOM_GENERATED_MACROS",
],
visibility = [
"//executorch/runtime/core/portable_type/...",
"//executorch/...",
],
deps = select({
"DEFAULT": [],
Expand Down
22 changes: 11 additions & 11 deletions runtime/core/portable_type/c10/c10/util/irange.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ struct integer_iterator {
using pointer = I*;
using reference = I&;

explicit integer_iterator(I value) : value(value) {}
explicit constexpr integer_iterator(I value) : value(value) {}

I operator*() const {
constexpr I operator*() const {
return value;
}

I const* operator->() const {
constexpr I const* operator->() const {
return &value;
}

integer_iterator& operator++() {
constexpr integer_iterator& operator++() {
++value;
return *this;
}

integer_iterator operator++(int) {
constexpr integer_iterator operator++(int) {
const auto copy = *this;
++*this;
return copy;
}

bool operator==(const integer_iterator& other) const {
constexpr bool operator==(const integer_iterator& other) const {
if constexpr (one_sided) {
// Range-for loops' end test is `begin != end`, not `begin <
// end`. To handle `c10::irange(n)` where n < 0 (which should be
Expand All @@ -64,7 +64,7 @@ struct integer_iterator {
return false; // Horrible hack
}

bool operator!=(const integer_iterator& other) const {
constexpr bool operator!=(const integer_iterator& other) const {
return !(*this == other);
}

Expand All @@ -80,12 +80,12 @@ template <
std::enable_if_t<std::is_integral_v<I>, bool> = true>
struct integer_range {
public:
integer_range(I begin, I end) : begin_(begin), end_(end) {}
constexpr integer_range(I begin, I end) : begin_(begin), end_(end) {}
using iterator = detail::integer_iterator<I, one_sided>;
iterator begin() const {
constexpr iterator begin() const {
return begin_;
}
iterator end() const {
constexpr iterator end() const {
return end_;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ integer_range<Integer2> irange(Integer1 begin, Integer2 end) {
template <
typename Integer,
std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
integer_range<Integer, true> irange(Integer end) {
constexpr integer_range<Integer, true> irange(Integer end) {
return {Integer(), end};
}

Expand Down
9 changes: 5 additions & 4 deletions runtime/core/portable_type/test/bfloat16_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include <c10/util/irange.h>
#include <executorch/runtime/core/portable_type/bfloat16.h>

#include <gtest/gtest.h>
Expand Down Expand Up @@ -41,7 +42,7 @@ uint16_t bits_from_f32(float src) {
TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
float in[100];
for (int i = 0; i < 100; ++i) {
for (const auto i : c10::irange(100)) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers)
in[i] = i + 1.25;
}
Expand All @@ -51,7 +52,7 @@ TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
float out[100];

for (int i = 0; i < 100; ++i) {
for (const auto i : c10::irange(100)) {
bfloats[i].x = bits_from_f32(in[i]);
out[i] = f32_from_bits(bfloats[i].x);

Expand All @@ -64,7 +65,7 @@ TEST(BFloat16Conversion, FloatToBFloat16AndBack) {
TEST(BFloat16Conversion, FloatToBFloat16RNEAndBack) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
float in[100];
for (int i = 0; i < 100; ++i) {
for (const auto i : c10::irange(100)) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers)
in[i] = i + 1.25;
}
Expand All @@ -74,7 +75,7 @@ TEST(BFloat16Conversion, FloatToBFloat16RNEAndBack) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers,modernize-avoid-c-arrays)
float out[100];

for (int i = 0; i < 100; ++i) {
for (const auto i : c10::irange(100)) {
bfloats[i].x = round_to_nearest_even(in[i]);
out[i] = f32_from_bits(bfloats[i].x);

Expand Down
2 changes: 2 additions & 0 deletions runtime/core/portable_type/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def define_common_targets():
srcs = ["bfloat16_test.cpp"],
deps = [
"//executorch/runtime/core/portable_type:portable_type",
"//executorch/runtime/core/portable_type/c10/c10:c10",
],
)

Expand Down Expand Up @@ -52,5 +53,6 @@ def define_common_targets():
deps = [
"//executorch/runtime/core/exec_aten/util:tensor_util",
"//executorch/runtime/core/portable_type:portable_type",
"//executorch/runtime/core/portable_type/c10/c10:c10",
],
)
3 changes: 2 additions & 1 deletion runtime/core/portable_type/test/tensor_impl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include <c10/util/irange.h>
#include <executorch/runtime/core/portable_type/tensor_impl.h>

#include <gtest/gtest.h>
Expand Down Expand Up @@ -76,7 +77,7 @@ TEST_F(TensorImplTest, TestSetSizesContigContract) {

SizesType new_sizes[RANK] = {0, 0, 0, 0, 0};
// assign random sizes between 1 and 100
for (int i = 0; i < RANK; i++) {
for (const auto i : c10::irange(RANK)) {
new_sizes[i] = distribution(generator);
}
Error err = resize_tensor_impl(&t, {new_sizes, RANK});
Expand Down
9 changes: 7 additions & 2 deletions runtime/core/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def define_common_targets():
],
exported_preprocessor_flags = get_core_flags(),
exported_deps = [
"//executorch/runtime/core/portable_type/c10/c10:c10",
"//executorch/runtime/platform:platform",
],
)
Expand All @@ -73,6 +74,7 @@ def define_common_targets():
],
exported_deps = [
":core",
"//executorch/runtime/core/portable_type/c10/c10:c10",
],
visibility = [
"//executorch/...",
Expand Down Expand Up @@ -145,13 +147,16 @@ def define_common_targets():
":tensor_layout",
],
)

runtime.cxx_library(
name = "tensor_layout",
srcs = ["tensor_layout.cpp"],
exported_headers = ["tensor_layout.h"],
deps = [
"//executorch/runtime/core/portable_type/c10/c10:c10",
],
exported_deps = [
":core",
":core",
"//executorch/runtime/core/exec_aten:lib",
],
visibility = ["//executorch/..."],
Expand Down
3 changes: 2 additions & 1 deletion runtime/core/tensor_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include <c10/util/irange.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
#include <executorch/runtime/core/span.h>
Expand Down Expand Up @@ -43,7 +44,7 @@ Result<const TensorLayout> TensorLayout::create(
return Error::InvalidArgument;
}

for (size_t i = 0; i < dim_order.size(); i++) {
for (const auto i : c10::irange(dim_order.size())) {
if (dim_order[i] >= sizes.size()) {
return Error::InvalidArgument;
}
Expand Down
5 changes: 3 additions & 2 deletions runtime/core/test/event_tracer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <gtest/gtest.h>

#include <c10/util/irange.h>
#include <executorch/runtime/core/array_ref.h>
#include <executorch/runtime/core/evalue.h>
#include <executorch/runtime/core/event_tracer.h>
Expand Down Expand Up @@ -207,7 +208,7 @@ TEST(TestEventTracer, SimpleEventTracerTest) {
// and also with a null pointer (to test that the null case works).
DummyEventTracer dummy;
std::vector<DummyEventTracer*> dummy_event_tracer_arr = {&dummy, nullptr};
for (size_t i = 0; i < dummy_event_tracer_arr.size(); i++) {
for (const auto i : c10::irange(dummy_event_tracer_arr.size())) {
RunSimpleTracerTest(&dummy);
RunSimpleTracerTest(nullptr);
}
Expand All @@ -234,7 +235,7 @@ TEST(TestEventTracer, SimpleEventTracerTestDelegate) {
// and also with a null pointer (to test that the null case works).
DummyEventTracer dummy;
std::vector<DummyEventTracer*> dummy_event_tracer_arr = {&dummy, nullptr};
for (size_t i = 0; i < dummy_event_tracer_arr.size(); i++) {
for (const auto i : c10::irange(dummy_event_tracer_arr.size())) {
RunSimpleTracerTestDelegate(&dummy);
RunSimpleTracerTestDelegate(nullptr);
}
Expand Down
7 changes: 4 additions & 3 deletions runtime/core/test/memory_allocator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <array>
#include <vector>

#include <c10/util/irange.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/platform/runtime.h>
#include <executorch/test/utils/alignment.h>
Expand Down Expand Up @@ -62,12 +63,12 @@ TEST_F(MemoryAllocatorTest, MemoryAllocatorAlignment) {
128,
2};

for (int i = 0; i < arr_size; i++) {
for (const auto i : c10::irange(arr_size)) {
auto align_size = alignment[i];
constexpr size_t mem_size = 1000;
uint8_t mem_pool[mem_size];
MemoryAllocator allocator = MemoryAllocator(mem_size, mem_pool);
for (int j = 0; j < arr_size; j++) {
for (const auto j : c10::irange(arr_size)) {
auto size = allocation[j];
void* start = allocator.allocate(size, align_size);
EXPECT_ALIGNED(start, align_size);
Expand All @@ -81,7 +82,7 @@ TEST_F(MemoryAllocatorTest, MemoryAllocatorNonPowerOfTwoAlignment) {
MemoryAllocator allocator(mem_size, mem_pool);

size_t alignment[5] = {0, 5, 6, 12, 34};
for (int i = 0; i < 5; i++) {
for (const auto i : c10::irange(5)) {
ASSERT_EQ(nullptr, allocator.allocate(8, alignment[i]));
}
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/core/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def define_common_targets():
],
deps = [
"//executorch/runtime/core:event_tracer",
"//executorch/runtime/core/portable_type/c10/c10:c10",
],
)

Expand Down Expand Up @@ -68,6 +69,7 @@ def define_common_targets():
],
deps = [
"//executorch/runtime/core:memory_allocator",
"//executorch/runtime/core/portable_type/c10/c10:c10",
],
)

Expand Down
Loading