From 47baecd85a0dcdf5fbcfaf90244c2e343590b6e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/=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: Fri, 3 Aug 2018 11:16:01 +0900 Subject: [PATCH] [neurun] Split execution NNAPI implementation into frontend (#2152) Split exeution NNAPI implementation into frontend Move Sink and Source class into internal Signed-off-by: Hyeongseok Oh --- runtimes/neurun/src/execution.h | 16 +-- runtimes/neurun/src/{ => frontend}/execution.cc | 161 +----------------------- runtimes/neurun/src/internal/Sink.h | 96 ++++++++++++++ runtimes/neurun/src/internal/Source.h | 99 +++++++++++++++ 4 files changed, 198 insertions(+), 174 deletions(-) rename runtimes/neurun/src/{ => frontend}/execution.cc (52%) create mode 100644 runtimes/neurun/src/internal/Sink.h create mode 100644 runtimes/neurun/src/internal/Source.h diff --git a/runtimes/neurun/src/execution.h b/runtimes/neurun/src/execution.h index 59fc52f..ec1d364 100644 --- a/runtimes/neurun/src/execution.h +++ b/runtimes/neurun/src/execution.h @@ -2,20 +2,8 @@ #define __EXECUTION_H__ #include "internal/arm_compute.h" - -struct Source -{ - virtual ~Source() = default; - - virtual void push(::arm_compute::ITensor &tensor) const = 0; -}; - -struct Sink -{ - virtual ~Sink() = default; - - virtual void pull(::arm_compute::ITensor &tensor) const = 0; -}; +#include "internal/Source.h" +#include "internal/Sink.h" struct ANeuralNetworksExecution { diff --git a/runtimes/neurun/src/execution.cc b/runtimes/neurun/src/frontend/execution.cc similarity index 52% rename from runtimes/neurun/src/execution.cc rename to runtimes/neurun/src/frontend/execution.cc index 9e5c57a..4862938 100644 --- a/runtimes/neurun/src/execution.cc +++ b/runtimes/neurun/src/frontend/execution.cc @@ -3,166 +3,7 @@ #include "compilation.h" #include "execution.h" -#include "internal/nnapi/feature/Reader.h" -#include "internal/nnapi/feature/View.h" - -#include "internal/cpu.h" -#include - -#include "backend/acl_cl/feature/View.h" - -#include "util/feature/IndexIterator.h" - -#include - -// -// VectorSource -// -class VectorSource final : public Source -{ -public: - VectorSource(const int32_t vlen, const uint8_t *base, const size_t size) - : _vlen{vlen}, _base{base} - { - assert(size >= _vlen * sizeof(float)); - } - -public: - void push(::arm_compute::ITensor &tensor) const override - { - auto base = reinterpret_cast(_base); - - for (int32_t n = 0; n < _vlen; ++n) - { - auto from = base + n; - auto into = reinterpret_cast(tensor.ptr_to_element(::arm_compute::Coordinates{n})); - - *into = *from; - } - } - -private: - const int32_t _vlen; - const uint8_t *const _base; -}; - -// -// FeatureSource -// -class FeatureSource final : public Source -{ -public: - FeatureSource(const nnfw::util::feature::Shape &shape, const uint8_t *base, const size_t size) - : _shape{shape}, _base{base}, _size{size} - { - // DO NOTHING - } - -public: - void push(::arm_compute::ITensor &tensor) const override - { - // TODO: This is just workaround codes, It needs to refactor. - if (typeid(tensor) == typeid(::internal::cpu::Tensor)) - { - const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; - ::internal::nnapi::feature::View into{_shape, tensor.buffer(), _size}; - - ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(ch, row, col); - into.at(ch, row, col) = value; - }; - } - else if (typeid(tensor) == typeid(::arm_compute::CLTensor)) - { - const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; - ::internal::arm_compute::feature::View into{&tensor}; - - ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(ch, row, col); - into.at(ch, row, col) = value; - }; - } - } - -private: - const nnfw::util::feature::Shape _shape; - const uint8_t *const _base; - const size_t _size; -}; - -// -// VectorSink -// -class VectorSink final : public Sink -{ -public: - VectorSink(const int32_t vlen, uint8_t *base, const size_t size) : _vlen{vlen}, _base{base} - { - assert(size >= _vlen * sizeof(float)); - } - -public: - void pull(::arm_compute::ITensor &tensor) const override - { - float *base = reinterpret_cast(_base); - - for (int32_t n = 0; n < _vlen; ++n) - { - auto from = reinterpret_cast(tensor.ptr_to_element(::arm_compute::Coordinates{n})); - auto into = base + n; - - *into = *from; - } - } - -private: - const int32_t _vlen; - uint8_t *const _base; -}; - -// -// FeatureSink -// -class FeatureSink final : public Sink -{ -public: - FeatureSink(const nnfw::util::feature::Shape &shape, uint8_t *base, const size_t size) - : _shape{shape}, _base{base}, _size{size} - { - // DO NOTHING - } - -public: - void pull(::arm_compute::ITensor &tensor) const override - { - // TODO: This is just workaround codes, It needs to refactor. - if (typeid(tensor) == typeid(::internal::cpu::Tensor)) - { - const ::internal::nnapi::feature::Reader from{_shape, tensor.buffer(), _size}; - ::internal::nnapi::feature::View into{_shape, _base, _size}; - - ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(ch, row, col); - into.at(ch, row, col) = value; - }; - } - else if (typeid(tensor) == typeid(::arm_compute::CLTensor)) - { - const ::internal::arm_compute::feature::View from{&tensor}; - ::internal::nnapi::feature::View into{_shape, _base, _size}; - - ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { - const auto value = from.at(ch, row, col); - into.at(ch, row, col) = value; - }; - } - } - -private: - const nnfw::util::feature::Shape _shape; - uint8_t *const _base; - const size_t _size; -}; +#include "internal/Source.h" // // NNAPI Implementation diff --git a/runtimes/neurun/src/internal/Sink.h b/runtimes/neurun/src/internal/Sink.h new file mode 100644 index 0000000..59dedf4 --- /dev/null +++ b/runtimes/neurun/src/internal/Sink.h @@ -0,0 +1,96 @@ +#ifndef __INTERNAL_SINK_H__ +#define __INTERNAL_SINK_H__ + +#include + +#include + +#include +#include + +#include "internal/cpu.h" +#include "internal/nnapi/feature/View.h" +#include "internal/nnapi/feature/Reader.h" + +struct Sink +{ + virtual ~Sink() = default; + + virtual void pull(::arm_compute::ITensor &tensor) const = 0; +}; + +// +// VectorSink +// +class VectorSink final : public Sink +{ +public: + VectorSink(const int32_t vlen, uint8_t *base, const size_t size) : _vlen{vlen}, _base{base} + { + assert(size >= _vlen * sizeof(float)); + } + +public: + void pull(::arm_compute::ITensor &tensor) const override + { + float *base = reinterpret_cast(_base); + + for (int32_t n = 0; n < _vlen; ++n) + { + auto from = reinterpret_cast(tensor.ptr_to_element(::arm_compute::Coordinates{n})); + auto into = base + n; + + *into = *from; + } + } + +private: + const int32_t _vlen; + uint8_t *const _base; +}; + +// +// FeatureSink +// +class FeatureSink final : public Sink +{ +public: + FeatureSink(const nnfw::util::feature::Shape &shape, uint8_t *base, const size_t size) + : _shape{shape}, _base{base}, _size{size} + { + // DO NOTHING + } + +public: + void pull(::arm_compute::ITensor &tensor) const override + { + // TODO: This is just workaround codes, It needs to refactor. + if (typeid(tensor) == typeid(::internal::cpu::Tensor)) + { + const ::internal::nnapi::feature::Reader from{_shape, tensor.buffer(), _size}; + ::internal::nnapi::feature::View into{_shape, _base, _size}; + + ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(ch, row, col); + into.at(ch, row, col) = value; + }; + } + else if (typeid(tensor) == typeid(::arm_compute::CLTensor)) + { + const ::internal::arm_compute::feature::View from{&tensor}; + ::internal::nnapi::feature::View into{_shape, _base, _size}; + + ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(ch, row, col); + into.at(ch, row, col) = value; + }; + } + } + +private: + const nnfw::util::feature::Shape _shape; + uint8_t *const _base; + const size_t _size; +}; + +#endif // __INTERNAL_SINK_H__ diff --git a/runtimes/neurun/src/internal/Source.h b/runtimes/neurun/src/internal/Source.h new file mode 100644 index 0000000..e539d29 --- /dev/null +++ b/runtimes/neurun/src/internal/Source.h @@ -0,0 +1,99 @@ +#ifndef __INTERNAL_SOURCE_H__ +#define __INTERNAL_SOURCE_H__ + +#include + +#include + +#include +#include + +#include "internal/cpu.h" +#include "internal/nnapi/feature/Reader.h" +#include "internal/nnapi/feature/View.h" + +#include "backend/acl_cl/feature/View.h" + +struct Source +{ + virtual ~Source() = default; + + virtual void push(::arm_compute::ITensor &tensor) const = 0; +}; + +// +// VectorSource +// +class VectorSource final : public Source +{ +public: + VectorSource(const int32_t vlen, const uint8_t *base, const size_t size) + : _vlen{vlen}, _base{base} + { + assert(size >= _vlen * sizeof(float)); + } + +public: + void push(::arm_compute::ITensor &tensor) const override + { + auto base = reinterpret_cast(_base); + + for (int32_t n = 0; n < _vlen; ++n) + { + auto from = base + n; + auto into = reinterpret_cast(tensor.ptr_to_element(::arm_compute::Coordinates{n})); + + *into = *from; + } + } + +private: + const int32_t _vlen; + const uint8_t *const _base; +}; + +// +// FeatureSource +// +class FeatureSource final : public Source +{ +public: + FeatureSource(const nnfw::util::feature::Shape &shape, const uint8_t *base, const size_t size) + : _shape{shape}, _base{base}, _size{size} + { + // DO NOTHING + } + +public: + void push(::arm_compute::ITensor &tensor) const override + { + // TODO: This is just workaround codes, It needs to refactor. + if (typeid(tensor) == typeid(::internal::cpu::Tensor)) + { + const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; + ::internal::nnapi::feature::View into{_shape, tensor.buffer(), _size}; + + ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(ch, row, col); + into.at(ch, row, col) = value; + }; + } + else if (typeid(tensor) == typeid(::arm_compute::CLTensor)) + { + const ::internal::nnapi::feature::Reader from{_shape, _base, _size}; + ::internal::arm_compute::feature::View into{&tensor}; + + ::nnfw::util::feature::iterate(_shape) << [&](uint32_t ch, uint32_t row, uint32_t col) { + const auto value = from.at(ch, row, col); + into.at(ch, row, col) = value; + }; + } + } + +private: + const nnfw::util::feature::Shape _shape; + const uint8_t *const _base; + const size_t _size; +}; + +#endif // __INTERNAL_SOURCE_H__ -- 2.7.4