From c4ae56ef42c57077fe6242a6a3bb58f82add9029 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 31 May 2018 17:43:27 +0900 Subject: [PATCH] [Pure CL] Introduce Const Tensor View (#1474) This commit introduces ConstView class to make it easy to write generic const operand updates. Signed-off-by: Jonghyun Park --- .../src/internal/nnapi/tensor/ConstView.h | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 runtimes/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h b/runtimes/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h new file mode 100644 index 0000000..085f715 --- /dev/null +++ b/runtimes/pure_arm_compute/src/internal/nnapi/tensor/ConstView.h @@ -0,0 +1,69 @@ +#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 +{ + +template class ConstView +{ +public: + ConstView(const ::nnfw::util::tensor::Shape &shape, const uint8_t *ptr, size_t len) + : _shape{shape}, _ptr{ptr}, _len{len} + { + // DO NOTHING + } + +public: + const nnfw::util::tensor::Shape &shape(void) const { return _shape; } + +private: + // TODO Make this as a helper function, and share it for both View and ConstView + uint32_t offset_of(const nnfw::util::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: + T at(const nnfw::util::tensor::Index &index) const + { + const auto offset = offset_of(index); + + const T *arr = reinterpret_cast(_ptr); + + return arr[offset]; + } + +private: + const nnfw::util::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__ -- 2.7.4