|
| 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 | +#pragma once |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <array> |
| 13 | +#include <cstdint> |
| 14 | +#include <iterator> |
| 15 | + |
| 16 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 17 | +#include <executorch/runtime/core/exec_aten/util/tensor_dimension_limit.h> |
| 18 | + |
| 19 | +namespace torch::executor { |
| 20 | + |
| 21 | +namespace internal { |
| 22 | +class DelinearizedIndexesIterator { |
| 23 | + public: |
| 24 | + using difference_type = ssize_t; |
| 25 | + using value_type = std::array<std::size_t, ::executorch::runtime::kTensorDimensionLimit>; |
| 26 | + using reference = const value_type&; |
| 27 | + using pointer = const value_type*; |
| 28 | + using iterator_category = std::forward_iterator_tag; |
| 29 | + |
| 30 | + DelinearizedIndexesIterator() = default; |
| 31 | + |
| 32 | + explicit DelinearizedIndexesIterator(const Tensor& t) |
| 33 | + : idx_(0), dim_(t.dim()), shape_(t.sizes()) { |
| 34 | + } |
| 35 | + |
| 36 | + struct make_end_t { |
| 37 | + explicit constexpr make_end_t() = default; |
| 38 | + }; |
| 39 | + |
| 40 | + DelinearizedIndexesIterator(make_end_t, const Tensor& t) |
| 41 | + : idx_(t.numel()) {} |
| 42 | + |
| 43 | + bool operator==(const DelinearizedIndexesIterator& rhs) const { |
| 44 | + return idx_ == rhs.idx_; |
| 45 | + } |
| 46 | + |
| 47 | + bool operator!=(const DelinearizedIndexesIterator& rhs) const { |
| 48 | + return !operator==(rhs); |
| 49 | + } |
| 50 | + |
| 51 | + reference operator*() const { |
| 52 | + return repr_; |
| 53 | + } |
| 54 | + |
| 55 | + pointer operator->() const { |
| 56 | + return &repr_; |
| 57 | + } |
| 58 | + |
| 59 | + DelinearizedIndexesIterator& operator++() { |
| 60 | + idx_++; |
| 61 | + for (auto ii = dim_ - 1; ii >= 0; --ii) { |
| 62 | + repr_[ii]++; |
| 63 | + ET_DCHECK(repr_[ii] <= shape_[ii]); |
| 64 | + if ET_LIKELY (repr_[ii] < shape_[ii]) { |
| 65 | + break; |
| 66 | + } else { |
| 67 | + repr_[ii] = 0; |
| 68 | + } |
| 69 | + } |
| 70 | + return *this; |
| 71 | + } |
| 72 | + |
| 73 | + DelinearizedIndexesIterator operator++(int) { |
| 74 | + auto it = *this; |
| 75 | + operator++(); |
| 76 | + return it; |
| 77 | + } |
| 78 | + |
| 79 | + difference_type operator-(const DelinearizedIndexesIterator& rhs) const { |
| 80 | + return difference_type(idx_ - rhs.idx_); |
| 81 | + } |
| 82 | + |
| 83 | + private: |
| 84 | + std::size_t idx_ = 0; |
| 85 | + value_type repr_ = {0,}; |
| 86 | + ssize_t dim_; |
| 87 | + ArrayRef<exec_aten::SizesType> shape_; |
| 88 | +}; |
| 89 | +} // namespace internal |
| 90 | + |
| 91 | +class DelinearizedIndexesRange { |
| 92 | + public: |
| 93 | + using iterator = internal::DelinearizedIndexesIterator; |
| 94 | + |
| 95 | + DelinearizedIndexesRange(const Tensor& t) : |
| 96 | + tensor_(t) {} |
| 97 | + |
| 98 | + iterator begin() const { |
| 99 | + return iterator(tensor_); |
| 100 | + } |
| 101 | + |
| 102 | + iterator end() { |
| 103 | + return iterator(iterator::make_end_t(), tensor_); |
| 104 | + } |
| 105 | + private: |
| 106 | + const Tensor& tensor_; |
| 107 | +}; |
| 108 | +} // namespace torch::executor |
0 commit comments