Add `at` method to accessors
VivekPanyam opened this issue · 2 comments
VivekPanyam commented
Bounds check and call []
operator
VivekPanyam commented
More context
Add an at
method to TensorAccessor
that does a bounds check and then calls the []
operator.
The relevant code is
neuropod/source/neuropod/internal/tensor_accessor.hh
Lines 99 to 140 in fab5a86
// Indexing into a N dimensional accessor returns an N - 1 dimensional accessor | |
const TensorAccessor<Container, N - 1> operator[](int64_t i) const | |
{ | |
// This operator returns a TensorAccessor that accesses an `N - 1` dimensional tensor at index `i` | |
// of this TensorAccessor. To do this, we compute the correct offsets into `data_` and pass along the last | |
// `N - 1` elements in `dims_` and `strides_` to the new accessor. For example: | |
// | |
// auto tensor = allocator->allocate_tensor<float>({3, 5}); | |
// | |
// // Not using `auto` for clarity on types | |
// TensorAccessor<float *, 2> accessor = tensor->accessor(); | |
// | |
// // In this accessor: | |
// // `data_` points to the tensor's underlying buffer | |
// // `dims_` points to an array containing 3, 5 | |
// // `strides_` points to an array containing 5, 1 | |
// // `offset_` is 0 | |
// // This means that any indexing into this accessor indexes into the underlying buffer | |
// // starting at `0` with a stride of `5` | |
// | |
// // Since `N` (in the template args) is > 1, this accessor returns another accessor when indexed into | |
// TensorAccessor<float *, 1> subaccessor = accessor[2]; | |
// | |
// // In this accessor: | |
// // `data_` points to the tensor's underlying buffer | |
// // `dims_` points to an array containing 5 | |
// // `strides_` points to an array containing 1 | |
// // `offset_` is 10 | |
// // This means that any indexing into this accessor indexes into the underlying buffer | |
// // starting at `10` with a stride of `1` | |
// | |
// // This is equivalent to an index of 11 in the underlying buffer (or the 12th item) | |
// float item = subaccessor[1]; | |
// | |
// // Same as this | |
// float same_item = accessor[2][1]; | |
// | |
// // Same as this | |
// float also_same = *(tensor->get_raw_data_ptr() + 11); | |
// | |
return TensorAccessor<Container, N - 1>(data_, dims_ + 1, strides_ + 1, offset_ + strides_[0] * i); | |
} |
and
auto operator[](int64_t i) const -> decltype(data_[offset_ + i]) { return data_[offset_ + i]; } |
ArkaprabhaChakraborty commented
Hi can I get some guidance to work on this issue?