diff options
Diffstat (limited to 'runtime/contrib/pure_arm_compute/src/internal/nnapi')
8 files changed, 851 insertions, 0 deletions
diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/Reader.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/Reader.h new file mode 100644 index 000000000..ac25692a1 --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/Reader.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file Reader.h + * @ingroup COM_AI_RUNTIME + * @brief This file defines internal::nnapi::feature::Reader + */ +#ifndef __INTERNAL_NNAPI_FEATURE_READER_H__ +#define __INTERNAL_NNAPI_FEATURE_READER_H__ + +#include "internal/nnapi/feature/Utils.h" + +#include "misc/feature/Reader.h" + +namespace internal +{ +namespace nnapi +{ +namespace feature +{ + +/** + * @brief Class to support reading element in feature(3D, 4D) + */ +template <typename T> class Reader final : public nnfw::misc::feature::Reader<T> +{ +public: + /** + * @brief Construct a new Reader object + * @param[in] shape Shape of feature + * @param[in] ptr Pointer to feature data + * @param[in] len Size of tensor (byte) + */ + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::misc::feature::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} + { + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); + } + +public: + /** + * @brief Get shape of feature + * @return Shape of feature + */ + const nnfw::misc::feature::Shape &shape(void) const { return _shape; } + +public: + /** + * @brief Get value of element using channel, row, and column index for 3D feature + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Value of element + */ + T at(uint32_t ch, uint32_t row, uint32_t col) const override + { + uint32_t index = index_of(_shape, ch, row, col); + + const auto arr = reinterpret_cast<const T *>(_ptr); + + return arr[index]; + } + + /** + * @brief Get value of element using batch, channel, row, and column index for 4D feature + * @param[in] batch Batch index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Value of element + */ + T at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const override + { + uint32_t index = index_of(_shape, batch, ch, row, col); + + return _ptr[index]; + } + +private: + nnfw::misc::feature::Shape _shape; + +private: + const T *_ptr; +}; + +} // namespace feature +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_FEATURE_READER_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/Utils.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/Utils.h new file mode 100644 index 000000000..ee59d217e --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/Utils.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file Utils.h + * @ingroup COM_AI_RUNTIME + * @brief This file defines utility functions used in internal::nnapi::feature namespace + */ +#ifndef __INTERNAL_NNAPI_FEATURE_UTILS_H__ +#define __INTERNAL_NNAPI_FEATURE_UTILS_H__ + +#include "misc/feature/Shape.h" + +namespace internal +{ +namespace nnapi +{ +namespace feature +{ + +/** + * @brief Get position of element using channel, row, and column for 3D feature + * @param[in] shape Shape of feature + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Position of element + */ +inline uint32_t index_of(const ::nnfw::misc::feature::Shape &shape, uint32_t ch, uint32_t row, + uint32_t col) +{ + uint32_t res = 0; + + // NNAPI uses NHWC ordering + res += row * shape.W * shape.C; + res += col * shape.C; + res += ch; + + return res; +} + +/** + * @brief Get position of element using batch, channel, row, and column for 4D feature + * @param[in] shape Shape of feature + * @param[in] batch Batch index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Position of element + */ +inline uint32_t index_of(const ::nnfw::misc::feature::Shape &shape, uint32_t batch, uint32_t ch, + uint32_t row, uint32_t col) +{ + uint32_t res = 0; + + // NNAPI uses NHWC ordering + res += batch * shape.H * shape.W * shape.C; + res += row * shape.W * shape.C; + res += col * shape.C; + res += ch; + + return res; +} + +} // namespace feature +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_FEATURE_UTILS_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/View.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/View.h new file mode 100644 index 000000000..965e42f1c --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/feature/View.h @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file View.h + * @ingroup COM_AI_RUNTIME + * @brief This file defines internal::nnapi::feature::View class + */ +#ifndef __INTERNAL_NNAPI_FEATURE_VIEW_H__ +#define __INTERNAL_NNAPI_FEATURE_VIEW_H__ + +#include "internal/nnapi/feature/Utils.h" + +#include "misc/feature/Reader.h" + +namespace internal +{ +namespace nnapi +{ +namespace feature +{ + +/** + * @brief Class to access feature's element information using index + */ +template <typename T> class View final : public nnfw::misc::feature::Reader<T> +{ +public: + /** + * @brief Construct a new View object + * @param[in] shape Shape of feature + * @param[in] ptr Pointer to feature data + * @param[in] len Size of feature (byte) + * @return + */ + // NOTE The parameter len denotes the number of bytes. + View(const ::nnfw::misc::feature::Shape &shape, T *ptr, size_t len) : _shape{shape}, _ptr{ptr} + { + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); + } + +public: + /** + * @brief Get shape of feature + * @return Shape of feature + */ + const nnfw::misc::feature::Shape &shape(void) const { return _shape; } + +public: + /** + * @brief Get value of element in 3D feature using channel, row, and column index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Value of element + */ + T at(uint32_t ch, uint32_t row, uint32_t col) const override + { + uint32_t index = index_of(_shape, ch, row, col); + + return _ptr[index]; + } + + /** + * @brief Get value of element in 4D feature using batch, channel, row and column index + * @param[in] batch Batch index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Value of element + */ + T at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const override + { + uint32_t index = index_of(_shape, batch, ch, row, col); + + return _ptr[index]; + } + + /** + * @brief Get reference of element in 3D feature using channel, row, and column index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Reference of element + */ + T &at(uint32_t ch, uint32_t row, uint32_t col) + { + uint32_t index = index_of(_shape, ch, row, col); + + return _ptr[index]; + } + + /** + * @brief Get reference of element in 4D feature using batch, channel, row and column index + * @param[in] batch Batch index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Reference of element + */ + T &at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) + { + uint32_t index = index_of(_shape, batch, ch, row, col); + + return _ptr[index]; + } + +private: + nnfw::misc::feature::Shape _shape; + +private: + T *_ptr; +}; + +} // namespace feature +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_FEATURE_VIEW_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/kernel/Reader.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/kernel/Reader.h new file mode 100644 index 000000000..ae964f74c --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/kernel/Reader.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file Reader.h + * @ingroup COM_AI_RUNTIME + * @brief This file defines internal::nnapi::kernel::Reader class + */ +#ifndef __INTERNAL_NNAPI_KERNEL_READER_H__ +#define __INTERNAL_NNAPI_KERNEL_READER_H__ + +#include "misc/kernel/Shape.h" +#include "misc/kernel/Reader.h" + +namespace internal +{ +namespace nnapi +{ +namespace kernel +{ + +/** + * @brief Class to support reading element in kernel + */ +template <typename T> class Reader final : public nnfw::misc::kernel::Reader<T> +{ +public: + /** + * @brief Construct a new Reader object + * @param[in] shape Shape of kernel + * @param[in] ptr Pointer to kernel data + * @param[in] len Size of kernel (byte) + */ + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::misc::kernel::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} + { + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); + } + +public: + /** + * @brief Get shape of kernel + * @return Shape of kernel + */ + const nnfw::misc::kernel::Shape &shape(void) const { return _shape; } + +public: + /** + * @brief Get value of element for kernel + * @param[in] nth Kernel index + * @param[in] ch Channel index + * @param[in] row Row index + * @param[in] col Column index + * @return Value of element + */ + T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override + { + // NNAPI uses NHWC ordering + uint32_t index = 0; + + index += nth * _shape.H * _shape.W * _shape.C; + index += row * _shape.W * _shape.C; + index += col * _shape.C; + index += ch; + + return _ptr[index]; + } + +private: + nnfw::misc::kernel::Shape _shape; + +private: + const T *_ptr; +}; + +} // namespace kernel +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_KERNEL_READER_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/matrix/Reader.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/matrix/Reader.h new file mode 100644 index 000000000..f03a4be31 --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/matrix/Reader.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file Reader.h + * @ingroup COM_AI_RUNTIME + * @brief This file defines internal::nnapi::matrix::Reader class + */ +#ifndef __INTERNAL_NNAPI_MATRIX_READER_H__ +#define __INTERNAL_NNAPI_MATRIX_READER_H__ + +#include "misc/matrix/Shape.h" +#include "misc/matrix/Reader.h" + +namespace internal +{ +namespace nnapi +{ +namespace matrix +{ + +/** + * @brief Class to support reading element in matrix + */ +template <typename T> class Reader final : public nnfw::misc::matrix::Reader<T> +{ +public: + /** + * @brief Construct a new Reader object + * @param[in] shape Shape of matrix + * @param[in] ptr Pointer to matrix data + * @param[in] len Size of matrix (byte) + */ + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::misc::matrix::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} + { + assert(shape.H * shape.W * sizeof(T) == len); + } + +public: + /** + * @brief Get shape of matrix + * @return Shape of matrix + */ + const nnfw::misc::matrix::Shape &shape(void) const { return _shape; } + +public: + /** + * @brief Get value of element for matrix + * @param[in] row Row index + * @param[in] col Column index + * @return Value of element + */ + T at(uint32_t row, uint32_t col) const override + { + // NNAPI uses NHWC ordering + uint32_t index = 0; + + index += row * _shape.W; + index += col; + + return _ptr[index]; + } + +private: + nnfw::misc::matrix::Shape _shape; + +private: + const T *_ptr; +}; + +} // namespace matrix +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_MATRIX_READER_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h new file mode 100644 index 000000000..6a3fff646 --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file ConstView.h + * @brief This file contains ConstView class + * @ingroup COM_AI_RUNTIME + */ + +#ifndef __INTERNAL_NNAPI_TENSOR_CONST_VIEW_H__ +#define __INTERNAL_NNAPI_TENSOR_CONST_VIEW_H__ + +#include "util/tensor/Shape.h" +#include "util/tensor/Index.h" + +namespace internal +{ +namespace nnapi +{ +namespace tensor +{ + +/** + * @brief Wrapper class to read tensor values + * @tparam T The tensor element type + */ +template <typename T> class ConstView +{ +public: + /** + * @brief Construct a ConstView class + * @param[in] shape Tensor shape + * @param[in] ptr The base pointer of actual data + * @param[in] len The number of bytes + */ + ConstView(const ::nnfw::misc::tensor::Shape &shape, const uint8_t *ptr, size_t len) + : _shape{shape}, _ptr{ptr}, _len{len} + { + // DO NOTHING + } + +public: + const nnfw::misc::tensor::Shape &shape(void) const { return _shape; } + +private: + // TODO Make this as a helper function, and share it for both View<T> and ConstView<T> + /** + * @brief Calculate offset for the given tensor index + * @param[in] index Tensor index + * @return The calculated offset + */ + uint32_t offset_of(const nnfw::misc::tensor::Index &index) const + { + if (_shape.rank() == 0) + { + return 0; + } + + uint32_t offset = index.at(0); + + // Stride decreases as axis increases in NNAPI + for (uint32_t axis = 1; axis < _shape.rank(); ++axis) + { + offset *= _shape.dim(axis); + offset += index.at(axis); + } + + return offset; + } + +public: + /** + * @brief Get the value on the given index + * @param[in] index Flattened tensor index + * @return The value on the given index + */ + T at(const nnfw::misc::tensor::Index &index) const + { + const auto offset = offset_of(index); + + const T *arr = reinterpret_cast<const T *>(_ptr); + + return arr[offset]; + } + +private: + const nnfw::misc::tensor::Shape _shape; + +private: + const uint8_t *const _ptr; + const size_t _len; +}; + +} // namespace tensor +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_TENSOR_CONST_VIEW_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/Reader.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/Reader.h new file mode 100644 index 000000000..fc6d490da --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/Reader.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file Reader.h + * @brief This file contains Reader class + * @ingroup COM_AI_RUNTIME + */ + +#ifndef __INTERNAL_NNAPI_TENSOR_READER_H__ +#define __INTERNAL_NNAPI_TENSOR_READER_H__ + +#include <vector> +#include "misc/tensor/Reader.h" + +namespace internal +{ +namespace nnapi +{ +namespace tensor +{ + +/** + * @brief Wrapper class to read tensor values + * @tparam T The tensor element type + */ +template <typename T> class Reader final : public nnfw::misc::tensor::Reader<T> +{ +public: + /** + * @brief Construct a Reader class + * @param[in] shape Tensor shape + * @param[in] ptr The base pointer of actual data + * @param[in] len The number of bytes + */ + Reader(const ::nnfw::misc::tensor::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} + { + assert(shape.num_elements() * sizeof(T) == len); + initialize(); + } + +public: + /** + * @brief Get shape object + * @return The shape as const reference + */ + const nnfw::misc::tensor::Shape &shape(void) const { return _shape; } + +public: + /** + * @brief Get the value on the given index + * @param[in] index_nnapi Flattened tensor index + * @return The value on the given index + */ + T at(const nnfw::misc::tensor::Index &index_nnapi) const override + { + uint32_t offset = 0; + + for (int i = 0; i < _shape.rank(); i++) + offset += index_nnapi.at(i) * _stridess.at(i); + + return _ptr[offset]; + } + +private: + /** + * @brief Initializes @c _stridess + * @return N/A + * @note Assuming that shape is [d4, .. , d1] and data is stored at a pointer ptr, + we need to calculate the offset of index [i4, .. i1] as follows: + offset = i4 * (d3 * d2 * d1) + + i3 * (d2 * d1) + + i2 * (d1) + + i1 + So (d4 * d3 * d2 * d1) or (d3 * d2 * d1) or (d2 * d1) happens whenever offset is + calculate. To minimize this repetitive calculation, + _stridess[n] contains _spape[n-1]*_spape[n-2]*_spape[0] + */ + void initialize(void) + { + for (int r = 0; r < _shape.rank(); r++) + { + int elem_count = 1; + for (int k = r + 1; k < _shape.rank(); k++) + elem_count *= _shape.dim(k); + _stridess.emplace_back(elem_count); + } + } + +private: + nnfw::misc::tensor::Shape _shape; + +private: + const T *_ptr; + std::vector<int32_t> _stridess; +}; + +} // namespace tensor +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_TENSOR_READER_H__ diff --git a/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/View.h b/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/View.h new file mode 100644 index 000000000..4766851b9 --- /dev/null +++ b/runtime/contrib/pure_arm_compute/src/internal/nnapi/tensor/View.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file View.h + * @ingroup COM_AI_RUNTIME + * @brief This file defines internal::nnapi::tensor::View class + */ +#ifndef __INTERNAL_NNAPI_TENSOR_VIEW_H__ +#define __INTERNAL_NNAPI_TENSOR_VIEW_H__ + +#include "misc/tensor/Shape.h" +#include "misc/tensor/Index.h" + +namespace internal +{ +namespace nnapi +{ +namespace tensor +{ + +/** + * @brief Class to access tensor's element information using index + */ +template <typename T> class View +{ +public: + /** + * @brief Construct a new View object + * @param[in] shape Shape of tensor + * @param[in] ptr Pointer to tensor data + * @param[in] len Size of tensor (byte) + */ + // NOTE The parameter len denotes the number of bytes. + View(const ::nnfw::misc::tensor::Shape &shape, T *ptr, size_t len) : _shape{shape}, _ptr{ptr} + { + assert(shape.num_elements() * sizeof(T) == len); + } + +public: + /** + * @brief Get shape of tensor + * @return Shape of tensor + */ + const nnfw::misc::tensor::Shape &shape(void) const { return _shape; } + +private: + /** + * @brief Get position of element using index in tensor + * @param[in] index Index of element + * @return Position of element + */ + uint32_t offset_of(const nnfw::misc::tensor::Index &index) const + { + if (_shape.rank() == 0) + { + return 0; + } + + uint32_t offset = index.at(0); + + // Stride decreases as axis increases in NNAPI + for (uint32_t axis = 1; axis < _shape.rank(); ++axis) + { + offset *= _shape.dim(axis); + offset += index.at(axis); + } + + return offset; + } + +public: + /** + * @brief Get value of element at index + * @param[in] index Index of element + * @return Value of element at index + */ + T at(const nnfw::misc::tensor::Index &index) const + { + const auto offset = offset_of(index); + + return _ptr[offset]; + } + + /** + * @brief Get reference of element at index + * @param[in] index Index of element + * @return Reference of element at index + */ + T &at(const nnfw::misc::tensor::Index &index) + { + const auto offset = offset_of(index); + + return _ptr[offset]; + } + +private: + nnfw::misc::tensor::Shape _shape; + +private: + T *_ptr; +}; + +} // namespace tensor +} // namespace nnapi +} // namespace internal + +#endif // __INTERNAL_NNAPI_TENSOR_VIEW_H__ |