From 88d25d5ea27de34a321f3320ad7e6dd220395961 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9E=A5=EC=A7=80=EC=84=AD/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 31 Oct 2019 18:30:56 +0900 Subject: [PATCH] Make Readers and Views support both ITensor and buffer (#8644) This commit makes Readers and Views support both ITensor and buffer. Signed-off-by: jiseob.jang --- runtimes/neurun/core/include/util/feature/nchw/View.h | 13 +++++++++++++ .../neurun/core/include/util/feature/nhwc/Reader.h | 18 ++++++++++++++++++ runtimes/neurun/core/include/util/feature/nhwc/View.h | 19 ++++++++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/runtimes/neurun/core/include/util/feature/nchw/View.h b/runtimes/neurun/core/include/util/feature/nchw/View.h index a565ad9..e9c7408 100644 --- a/runtimes/neurun/core/include/util/feature/nchw/View.h +++ b/runtimes/neurun/core/include/util/feature/nchw/View.h @@ -38,6 +38,19 @@ namespace nchw template class View final : public nnfw::misc::feature::Reader { public: + // Construct for buffer of model inputs + View(const ::nnfw::misc::feature::Shape &shape, T *ptr, size_t len) + : _shape{shape}, _ptr{reinterpret_cast(ptr)} + { + UNUSED_RELEASE(len); // Workaround for unused variable in release mode + assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len); + + _strides.W = sizeof(T); + _strides.H = shape.W * sizeof(T); + _strides.C = shape.W * shape.H * sizeof(T); + _strides.N = shape.W * shape.H * shape.C * sizeof(T); + } + // Construct for backend tensor View(::neurun::backend::operand::ITensor *tensor) : _ptr{tensor->buffer() + tensor->calcOffset({0, 0, 0, 0})}, _len{tensor->total_size()} diff --git a/runtimes/neurun/core/include/util/feature/nhwc/Reader.h b/runtimes/neurun/core/include/util/feature/nhwc/Reader.h index e902a6a..9f0a037 100644 --- a/runtimes/neurun/core/include/util/feature/nhwc/Reader.h +++ b/runtimes/neurun/core/include/util/feature/nhwc/Reader.h @@ -51,6 +51,24 @@ public: _strides.N = shape.C * shape.W * shape.H * sizeof(T); } + // Construct for backend tensor + Reader(const backend::operand::ITensor *tensor) + : _ptr{tensor->buffer() + tensor->calcOffset({0, 0, 0, 0})}, _len{tensor->total_size()} + { + assert(tensor->layout() == model::Layout::NHWC); + + const auto start_offset = tensor->calcOffset({0, 0, 0, 0}); + _strides.C = tensor->dimension(3) == 1 ? 0 : tensor->calcOffset({0, 0, 0, 1}) - start_offset; + _strides.W = tensor->dimension(2) == 1 ? 0 : tensor->calcOffset({0, 0, 1, 0}) - start_offset; + _strides.H = tensor->dimension(1) == 1 ? 0 : tensor->calcOffset({0, 1, 0, 0}) - start_offset; + _strides.N = tensor->dimension(0) == 1 ? 0 : tensor->calcOffset({1, 0, 0, 0}) - start_offset; + + _shape.C = tensor->dimension(3); + _shape.W = tensor->dimension(2); + _shape.H = tensor->dimension(1); + _shape.N = tensor->dimension(0); + } + public: T at(uint32_t row, uint32_t col, uint32_t ch) const override { diff --git a/runtimes/neurun/core/include/util/feature/nhwc/View.h b/runtimes/neurun/core/include/util/feature/nhwc/View.h index 1f991d3..a8f32d2 100644 --- a/runtimes/neurun/core/include/util/feature/nhwc/View.h +++ b/runtimes/neurun/core/include/util/feature/nhwc/View.h @@ -35,7 +35,6 @@ namespace feature namespace nhwc { -// This class is for cpu buffer only, and do not support padding. template class View final : public nnfw::misc::feature::Reader { public: @@ -53,6 +52,24 @@ public: _strides.N = shape.C * shape.W * shape.H * sizeof(T); } + // Construct for backend tensor + View(backend::operand::ITensor *tensor) + : _ptr{tensor->buffer() + tensor->calcOffset({0, 0, 0, 0})}, _len{tensor->total_size()} + { + assert(tensor->layout() == model::Layout::NHWC); + + const auto start_offset = tensor->calcOffset({0, 0, 0, 0}); + _strides.C = tensor->dimension(3) == 1 ? 0 : tensor->calcOffset({0, 0, 0, 1}) - start_offset; + _strides.W = tensor->dimension(2) == 1 ? 0 : tensor->calcOffset({0, 0, 1, 0}) - start_offset; + _strides.H = tensor->dimension(1) == 1 ? 0 : tensor->calcOffset({0, 1, 0, 0}) - start_offset; + _strides.N = tensor->dimension(0) == 1 ? 0 : tensor->calcOffset({1, 0, 0, 0}) - start_offset; + + _shape.C = tensor->dimension(3); + _shape.W = tensor->dimension(2); + _shape.H = tensor->dimension(1); + _shape.N = tensor->dimension(0); + } + public: T at(uint32_t row, uint32_t col, uint32_t ch) const override { -- 2.7.4