* @brief Represents the shape of a tensor. * * The rank of the tensor is the * number of dimensions in the shape. The data array stores the size of each * dimension. For now, we limit the rank to 8 to avoid dynamic allocation. * * @code * Shape shape = {256, 256}; * @endcode */
| 53 | * @endcode |
| 54 | */ |
| 55 | struct Shape { |
| 56 | static constexpr size_t kMaxRank = 8; // Maximum rank of a tensor, avoids |
| 57 | // dynamic allocation for shape data |
| 58 | std::array<size_t, kMaxRank> data = {0}; |
| 59 | size_t rank = 0; |
| 60 | inline Shape() = default; |
| 61 | inline Shape(std::initializer_list<size_t> dims) { |
| 62 | assert(dims.size() <= kMaxRank); |
| 63 | std::copy(dims.begin(), dims.end(), data.begin()); |
| 64 | rank = dims.size(); |
| 65 | } |
| 66 | inline size_t &operator[](size_t index) { |
| 67 | assert(index < rank); |
| 68 | return data[index]; |
| 69 | } |
| 70 | inline const size_t &operator[](size_t index) const { |
| 71 | assert(index < rank); |
| 72 | return data[index]; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * @brief Returns the number of elements in a tensor with the given shape, |
nothing calls this directly
no outgoing calls
no test coverage detected