The Wayback Machine - https://web.archive.org/web/20210821172017/https://github.com/uber/neuropod/issues/351
Skip to content
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

Add at method to accessors #351

Open
VivekPanyam opened this issue May 18, 2020 · 2 comments
Open

Add at method to accessors #351

VivekPanyam opened this issue May 18, 2020 · 2 comments

Comments

@VivekPanyam
Copy link
Collaborator

@VivekPanyam VivekPanyam commented May 18, 2020

Bounds check and call [] operator

@VivekPanyam
Copy link
Collaborator Author

@VivekPanyam VivekPanyam commented May 26, 2021

More context

Add an at method to TensorAccessor that does a bounds check and then calls the [] operator.

The relevant code is

// 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
Copy link

@ArkaprabhaChakraborty ArkaprabhaChakraborty commented May 27, 2021

Hi can I get some guidance to work on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants