From 4a386ec330c733f7ce5ebc314900006f1ed6a645 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9E=A5=EC=A7=80=EC=84=AD/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Tue, 28 Aug 2018 10:30:03 +0900 Subject: [PATCH] Reduce unnecessary casting from template class for data (#2473) This commit reduces unnecessary casting from template class for data. Signed-off-by: jiseob.jang --- include/util/tensor/Shape.h | 11 ++++++++++ runtimes/pure_arm_compute/src/compilation.cc | 12 +++++++---- .../pure_arm_compute/src/internal/FeatureSink.h | 2 +- .../pure_arm_compute/src/internal/FeatureSource.h | 6 +----- runtimes/pure_arm_compute/src/internal/Sinks.h | 4 +--- .../pure_arm_compute/src/internal/TensorSource.h | 6 +----- .../src/internal/nnapi/feature/Reader.h | 14 ++++++------ .../src/internal/nnapi/feature/View.h | 25 +++++++--------------- .../src/internal/nnapi/kernel/Reader.h | 14 ++++++------ .../src/internal/nnapi/matrix/Reader.h | 14 ++++++------ .../src/internal/nnapi/tensor/Reader.h | 13 ++++++----- .../src/internal/nnapi/tensor/View.h | 17 ++++++--------- 12 files changed, 61 insertions(+), 77 deletions(-) diff --git a/include/util/tensor/Shape.h b/include/util/tensor/Shape.h index acca7a4..a4401c1 100644 --- a/include/util/tensor/Shape.h +++ b/include/util/tensor/Shape.h @@ -55,6 +55,17 @@ public: int32_t dim(size_t n) const { return _dimensions.at(n); } int32_t &dim(size_t n) { return _dimensions.at(n); } +public: + size_t element_nums() const + { + size_t nums = 1; + for (auto d : _dimensions) + { + nums *= d; + } + return nums; + } + private: std::deque _dimensions; diff --git a/runtimes/pure_arm_compute/src/compilation.cc b/runtimes/pure_arm_compute/src/compilation.cc index af35074..3b77ea7 100644 --- a/runtimes/pure_arm_compute/src/compilation.cc +++ b/runtimes/pure_arm_compute/src/compilation.cc @@ -179,7 +179,8 @@ static void initFeatureTensor(::arm_compute::ITensor &tensor, const nnfw::util::feature::Shape &feature_shape, const uint8_t *feature_base, const size_t feature_size) { - const ::internal::nnapi::feature::Reader from{feature_shape, feature_base, feature_size}; + const ::internal::nnapi::feature::Reader from{ + feature_shape, reinterpret_cast(feature_base), feature_size}; ::internal::arm_compute::feature::View into{&tensor}; ::nnfw::util::feature::iterate(feature_shape) @@ -211,7 +212,8 @@ static void initMatrixTensor(::arm_compute::ITensor &tensor, const nnfw::util::matrix::Shape &matrix_shape, const uint8_t *matrix_base, const size_t matrix_size) { - const ::internal::nnapi::matrix::Reader from{matrix_shape, matrix_base, matrix_size}; + const ::internal::nnapi::matrix::Reader from{ + matrix_shape, reinterpret_cast(matrix_base), matrix_size}; ::internal::arm_compute::matrix::View into{&tensor}; ::nnfw::util::matrix::iterate(matrix_shape) << [&](uint32_t row, uint32_t col) { @@ -242,7 +244,8 @@ static void initKernelTensor(::arm_compute::ITensor &tensor, const nnfw::util::kernel::Shape &kernel_shape, const uint8_t *kernel_base, const size_t kernel_size) { - const ::internal::nnapi::kernel::Reader from{kernel_shape, kernel_base, kernel_size}; + const ::internal::nnapi::kernel::Reader from{ + kernel_shape, reinterpret_cast(kernel_base), kernel_size}; ::internal::arm_compute::kernel::View into{&tensor}; ::nnfw::util::kernel::iterate(kernel_shape) @@ -2043,7 +2046,8 @@ void Planner::visit(const ::internal::tflite::op::FullyConnected::Node &node) auto initializer = [num_output, N, C, H, W, weight_base, weight_size](::arm_compute::ITensor &tensor) { const ::nnfw::util::kernel::Shape ker_shape{N, C, H, W}; - const ::internal::nnapi::kernel::Reader from{ker_shape, weight_base, weight_size}; + const ::internal::nnapi::kernel::Reader from{ + ker_shape, reinterpret_cast(weight_base), weight_size}; ::nnfw::util::kernel::iterate(ker_shape) << [&](uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) { const auto value = from.at(nth, ch, row, col); diff --git a/runtimes/pure_arm_compute/src/internal/FeatureSink.h b/runtimes/pure_arm_compute/src/internal/FeatureSink.h index 5586f1a..da52f04 100644 --- a/runtimes/pure_arm_compute/src/internal/FeatureSink.h +++ b/runtimes/pure_arm_compute/src/internal/FeatureSink.h @@ -26,7 +26,7 @@ public: const ::internal::arm_compute::feature::View from{&tensor}; // TODO Should remove casting. // Inevitably casting must be done. - ::internal::nnapi::feature::View into{_shape, reinterpret_cast(_base), _size}; + ::internal::nnapi::feature::View into{_shape, _base, _size}; ::nnfw::util::feature::iterate(_shape) << [&](uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) { diff --git a/runtimes/pure_arm_compute/src/internal/FeatureSource.h b/runtimes/pure_arm_compute/src/internal/FeatureSource.h index 50c0b19..e4228d5 100644 --- a/runtimes/pure_arm_compute/src/internal/FeatureSource.h +++ b/runtimes/pure_arm_compute/src/internal/FeatureSource.h @@ -19,11 +19,7 @@ public: public: void push(::arm_compute::ITensor &tensor) const override { - // TODO Should replace the Construct parameter of Reader and View from uint8_t * with typename - // T. - // Inevitably casting must be done. - const ::internal::nnapi::feature::Reader from{ - _shape, reinterpret_cast(_base), _size}; + const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; ::internal::arm_compute::feature::View into{&tensor}; ::nnfw::util::feature::iterate(_shape) diff --git a/runtimes/pure_arm_compute/src/internal/Sinks.h b/runtimes/pure_arm_compute/src/internal/Sinks.h index affe866..e81db84 100644 --- a/runtimes/pure_arm_compute/src/internal/Sinks.h +++ b/runtimes/pure_arm_compute/src/internal/Sinks.h @@ -27,9 +27,7 @@ public: void pull(::arm_compute::ITensor &tensor) const override { const ::internal::arm_compute::tensor::View from{&tensor}; - // TODO Should remove casting. - // Inevitably casting must be done. - ::internal::nnapi::tensor::View into{_shape, reinterpret_cast(_base), _size}; + ::internal::nnapi::tensor::View into{_shape, _base, _size}; using ::nnfw::util::tensor::iterate; using ::nnfw::util::tensor::Index; diff --git a/runtimes/pure_arm_compute/src/internal/TensorSource.h b/runtimes/pure_arm_compute/src/internal/TensorSource.h index dc479db..ab5b8f2 100644 --- a/runtimes/pure_arm_compute/src/internal/TensorSource.h +++ b/runtimes/pure_arm_compute/src/internal/TensorSource.h @@ -20,11 +20,7 @@ public: public: void push(::arm_compute::ITensor &tensor) const override { - // TODO Should replace the Construct parameter of Reader and View from uint8_t * with typename - // T. - // Inevitably casting must be done. - const ::internal::nnapi::tensor::Reader from{ - _shape, reinterpret_cast(_base), _size}; + const ::internal::nnapi::tensor::Reader from{_shape, _base, _size}; ::internal::arm_compute::tensor::View into{&tensor}; ::nnfw::util::tensor::iterate(_shape) << [&](const nnfw::util::tensor::Index &index_nnapi) { diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/feature/Reader.h b/runtimes/pure_arm_compute/src/internal/nnapi/feature/Reader.h index c026271..30a68b8 100644 --- a/runtimes/pure_arm_compute/src/internal/nnapi/feature/Reader.h +++ b/runtimes/pure_arm_compute/src/internal/nnapi/feature/Reader.h @@ -15,10 +15,11 @@ namespace feature template class Reader final : public nnfw::util::feature::Reader { public: - Reader(const ::nnfw::util::feature::Shape &shape, const uint8_t *ptr, size_t len) - : _shape{shape}, _ptr{ptr}, _len{len} + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::util::feature::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} { - // DO NOTHING + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); } public: @@ -38,17 +39,14 @@ public: { uint32_t index = index_of(_shape, batch, ch, row, col); - const auto arr = reinterpret_cast(_ptr); - - return arr[index]; + return _ptr[index]; } private: nnfw::util::feature::Shape _shape; private: - const uint8_t *_ptr; - const size_t _len; + const T *_ptr; }; } // namespace feature diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/feature/View.h b/runtimes/pure_arm_compute/src/internal/nnapi/feature/View.h index 93695e8..15126fe 100644 --- a/runtimes/pure_arm_compute/src/internal/nnapi/feature/View.h +++ b/runtimes/pure_arm_compute/src/internal/nnapi/feature/View.h @@ -15,10 +15,10 @@ namespace feature template class View final : public nnfw::util::feature::Reader { public: - View(const ::nnfw::util::feature::Shape &shape, uint8_t *ptr, size_t len) - : _shape{shape}, _ptr{ptr}, _len{len} + // NOTE The parameter len denotes the number of bytes. + View(const ::nnfw::util::feature::Shape &shape, T *ptr, size_t len) : _shape{shape}, _ptr{ptr} { - // DO NOTHING + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); } public: @@ -29,42 +29,33 @@ public: { uint32_t index = index_of(_shape, ch, row, col); - T *arr = reinterpret_cast(_ptr); - - return arr[index]; + return _ptr[index]; } 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); - T *arr = reinterpret_cast(_ptr); - - return arr[index]; + return _ptr[index]; } T &at(uint32_t ch, uint32_t row, uint32_t col) { uint32_t index = index_of(_shape, ch, row, col); - T *arr = reinterpret_cast(_ptr); - - return arr[index]; + return _ptr[index]; } T &at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) { uint32_t index = index_of(_shape, batch, ch, row, col); - T *arr = reinterpret_cast(_ptr); - - return arr[index]; + return _ptr[index]; } private: nnfw::util::feature::Shape _shape; private: - uint8_t *_ptr; - const size_t _len; + T *_ptr; }; } // namespace feature diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/kernel/Reader.h b/runtimes/pure_arm_compute/src/internal/nnapi/kernel/Reader.h index a0a0c29..cd0c8b1 100644 --- a/runtimes/pure_arm_compute/src/internal/nnapi/kernel/Reader.h +++ b/runtimes/pure_arm_compute/src/internal/nnapi/kernel/Reader.h @@ -14,10 +14,11 @@ namespace kernel template class Reader final : public nnfw::util::kernel::Reader { public: - Reader(const ::nnfw::util::kernel::Shape &shape, const uint8_t *base, size_t size) - : _shape{shape}, _base{base}, _size{size} + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::util::kernel::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} { - // DO NOTHING + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); } public: @@ -34,17 +35,14 @@ public: index += col * _shape.C; index += ch; - const T *ptr = reinterpret_cast(_base); - - return ptr[index]; + return _ptr[index]; } private: nnfw::util::kernel::Shape _shape; private: - const uint8_t *_base; - const size_t _size; + const T *_ptr; }; } // namespace kernel diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/matrix/Reader.h b/runtimes/pure_arm_compute/src/internal/nnapi/matrix/Reader.h index 20e12bf..518962c 100644 --- a/runtimes/pure_arm_compute/src/internal/nnapi/matrix/Reader.h +++ b/runtimes/pure_arm_compute/src/internal/nnapi/matrix/Reader.h @@ -14,10 +14,11 @@ namespace matrix template class Reader final : public nnfw::util::matrix::Reader { public: - Reader(const ::nnfw::util::matrix::Shape &shape, const uint8_t *base, size_t size) - : _shape{shape}, _base{base}, _size{size} + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::util::matrix::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} { - // DO NOTHING + assert(shape.H * shape.W * sizeof(T) == len); } public: @@ -32,17 +33,14 @@ public: index += row * _shape.W; index += col; - const T *ptr = reinterpret_cast(_base); - - return ptr[index]; + return _ptr[index]; } private: nnfw::util::matrix::Shape _shape; private: - const uint8_t *_base; - const size_t _size; + const T *_ptr; }; } // namespace matrix diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/tensor/Reader.h b/runtimes/pure_arm_compute/src/internal/nnapi/tensor/Reader.h index e3308b3..e8b24d9 100644 --- a/runtimes/pure_arm_compute/src/internal/nnapi/tensor/Reader.h +++ b/runtimes/pure_arm_compute/src/internal/nnapi/tensor/Reader.h @@ -14,9 +14,11 @@ namespace tensor template class Reader final : public nnfw::util::tensor::Reader { public: - Reader(const ::nnfw::util::tensor::Shape &shape, const uint8_t *ptr, size_t len) - : _shape{shape}, _ptr{ptr}, _len{len} + // NOTE The parameter len denotes the number of bytes. + Reader(const ::nnfw::util::tensor::Shape &shape, const T *ptr, size_t len) + : _shape{shape}, _ptr{ptr} { + assert(shape.element_nums() * sizeof(T) == len); initialize(); } @@ -31,9 +33,7 @@ public: for (int i = 0; i < _shape.rank(); i++) offset += index_nnapi.at(i) * _stridess.at(i); - const auto arr = reinterpret_cast(_ptr); - - return arr[offset]; + return _ptr[offset]; } private: @@ -63,8 +63,7 @@ private: nnfw::util::tensor::Shape _shape; private: - const uint8_t *_ptr; - const size_t _len; + const T *_ptr; std::vector _stridess; }; diff --git a/runtimes/pure_arm_compute/src/internal/nnapi/tensor/View.h b/runtimes/pure_arm_compute/src/internal/nnapi/tensor/View.h index e521088..4afb215 100644 --- a/runtimes/pure_arm_compute/src/internal/nnapi/tensor/View.h +++ b/runtimes/pure_arm_compute/src/internal/nnapi/tensor/View.h @@ -14,10 +14,10 @@ namespace tensor template class View { public: - View(const ::nnfw::util::tensor::Shape &shape, uint8_t *ptr, size_t len) - : _shape{shape}, _ptr{ptr}, _len{len} + // NOTE The parameter len denotes the number of bytes. + View(const ::nnfw::util::tensor::Shape &shape, T *ptr, size_t len) : _shape{shape}, _ptr{ptr} { - // DO NOTHING + assert(shape.element_nums() * sizeof(T) == len); } public: @@ -48,26 +48,21 @@ public: { const auto offset = offset_of(index); - T *arr = reinterpret_cast(_ptr); - - return arr[offset]; + return _ptr[offset]; } T &at(const nnfw::util::tensor::Index &index) { const auto offset = offset_of(index); - T *arr = reinterpret_cast(_ptr); - - return arr[offset]; + return _ptr[offset]; } private: nnfw::util::tensor::Shape _shape; private: - uint8_t *_ptr; - const size_t _len; + T *_ptr; }; } // namespace tensor -- 2.7.4