From 754b3e5c1c6b9c1ae86c25eed91dfab8484333e8 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: Fri, 14 Jun 2019 09:53:14 +0900 Subject: [PATCH] Apply Coordinates for sinking and sourcing (#5398) This commit applies Coordinates for sinking and sourcing. Signed-off-by: jiseob.jang --- runtimes/neurun/core/src/exec/Sink.h | 96 ++++++++-------------------- runtimes/neurun/core/src/exec/Source.h | 111 ++++++++++----------------------- 2 files changed, 61 insertions(+), 146 deletions(-) diff --git a/runtimes/neurun/core/src/exec/Sink.h b/runtimes/neurun/core/src/exec/Sink.h index 9951bfe..a105260 100644 --- a/runtimes/neurun/core/src/exec/Sink.h +++ b/runtimes/neurun/core/src/exec/Sink.h @@ -23,6 +23,7 @@ #include "graph/operand/Layout.h" #include "util/feature/nhwc/View.h" #include "util/feature/nchw/View.h" +#include "util/Utils.h" #include namespace neurun @@ -69,35 +70,35 @@ public: } case 2: { - auto matrix_shape = _shape.asMatrix(); + const int32_t copy_len = _shape.dim(1); - for (auto h = 0; h < matrix_shape.H; ++h) + for (auto i = 0; i < _shape.dim(0); ++i) { - neurun::util::feature::Coordinate4D coord{0, h, 0, 0}; - memcpy(_output_buffer + h * matrix_shape.W, input_buffer + tensor.calcOffset(coord), - matrix_shape.W * sizeof(T)); + neurun::util::Coordinates coords{i, 0}; + memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coords), + copy_len * sizeof(T)); } break; } case 3: { - const int32_t depth = _shape.dim(0); - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); + const int32_t dim1 = _shape.dim(1); + const int32_t dim2 = _shape.dim(2); - for (auto c = 0; c < depth; ++c) + for (auto i = 0; i < _shape.dim(0); ++i) { - for (auto h = 0; h < height; ++h) + for (auto j = 0; j < _shape.dim(1); ++j) { - neurun::util::feature::Coordinate4D coord{0, h, 0, c}; - memcpy(_output_buffer + c * height * width + h * width, - input_buffer + tensor.calcOffset(coord), width * sizeof(T)); + neurun::util::Coordinates coords{i, j, 0}; + memcpy(_output_buffer + i * dim1 * dim2 + j * dim2, + input_buffer + tensor.calcOffset(coords), dim2 * sizeof(T)); } } break; } case 4: { + // TODO Support from nhwc to nchw auto feature = _shape.asFeature(); const util::feature::nchw::View from{&tensor}; @@ -158,59 +159,33 @@ public: for (auto i = 0; i < _shape.dim(0); ++i) { - neurun::util::feature::Coordinate4D coord; - if (tensor.layout() == neurun::graph::operand::Layout::NHWC) - { - coord.w(i); - } - else if (tensor.layout() == neurun::graph::operand::Layout::NCHW) - { - coord.h(i); - } - else - { - throw std::runtime_error("Wrong Layout"); - } - memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coord), + neurun::util::Coordinates coords{i, 0}; + memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coords), copy_len * sizeof(T)); } break; } case 3: { - const int32_t width = _shape.dim(1); - const int32_t copy_len = _shape.dim(2); + const int32_t dim1 = _shape.dim(1); + const int32_t dim2 = _shape.dim(2); for (auto i = 0; i < _shape.dim(0); ++i) { for (auto j = 0; j < _shape.dim(1); ++j) { - neurun::util::feature::Coordinate4D coord; - if (tensor.layout() == neurun::graph::operand::Layout::NHWC) - { - coord.h(i); - coord.w(j); - } - else if (tensor.layout() == neurun::graph::operand::Layout::NCHW) - { - coord.c(i); - coord.h(j); - } - else - { - throw std::runtime_error("Wrong Layout"); - } - memcpy(_output_buffer + i * width * copy_len + j * copy_len, - input_buffer + tensor.calcOffset(coord), copy_len * sizeof(T)); + neurun::util::Coordinates coords{i, j, 0}; + memcpy(_output_buffer + i * dim1 * dim2 + j * dim2, + input_buffer + tensor.calcOffset(coords), dim2 * sizeof(T)); } } break; } case 4: { - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); - const int32_t copy_len = _shape.dim(3); + const int32_t dim1 = _shape.dim(1); + const int32_t dim2 = _shape.dim(2); + const int32_t dim3 = _shape.dim(3); for (auto i = 0; i < _shape.dim(0); ++i) { @@ -218,26 +193,9 @@ public: { for (auto k = 0; k < _shape.dim(2); ++k) { - neurun::util::feature::Coordinate4D coord; - if (tensor.layout() == neurun::graph::operand::Layout::NHWC) - { - coord.n(i); - coord.h(j); - coord.w(k); - } - else if (tensor.layout() == neurun::graph::operand::Layout::NCHW) - { - coord.n(i); - coord.c(j); - coord.h(k); - } - else - { - throw std::runtime_error("Wrong Layout"); - } - memcpy(_output_buffer + i * height * width * copy_len + j * width * copy_len + - k * copy_len, - input_buffer + tensor.calcOffset(coord), copy_len * sizeof(T)); + neurun::util::Coordinates coords{i, j, k, 0}; + memcpy(_output_buffer + i * dim1 * dim2 * dim3 + j * dim2 * dim3 + k * dim3, + input_buffer + tensor.calcOffset(coords), dim3 * sizeof(T)); } } } diff --git a/runtimes/neurun/core/src/exec/Source.h b/runtimes/neurun/core/src/exec/Source.h index ba27cf2..1af3e3a 100644 --- a/runtimes/neurun/core/src/exec/Source.h +++ b/runtimes/neurun/core/src/exec/Source.h @@ -23,7 +23,7 @@ #include "graph/operand/Layout.h" #include "util/feature/nchw/View.h" #include "util/feature/nhwc/Reader.h" -#include "util/feature/Coordinate4D.h" +#include "util/Utils.h" #include #include "model/Shape.h" @@ -71,29 +71,29 @@ public: } case 2: { - auto matrix_shape = _shape.asMatrix(); + const auto copy_len = _shape.dim(1); - for (auto h = 0; h < matrix_shape.H; ++h) + for (auto i = 0; i < _shape.dim(0); ++i) { - neurun::util::feature::Coordinate4D coord{0, h, 0, 0}; - memcpy(output_buffer + tensor.calcOffset(coord), _input_buffer + h * matrix_shape.W, - matrix_shape.W * sizeof(T)); + neurun::util::Coordinates coords{i, 0}; + memcpy(output_buffer + tensor.calcOffset(coords), _input_buffer + i * copy_len, + copy_len * sizeof(T)); } break; } case 3: { - const int32_t depth = _shape.dim(0); - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); + const int32_t dim0 = _shape.dim(0); + const int32_t dim1 = _shape.dim(1); + const int32_t dim2 = _shape.dim(2); - for (auto c = 0; c < depth; ++c) + for (auto i = 0; i < dim0; ++i) { - for (auto h = 0; h < height; ++h) + for (auto j = 0; j < dim1; ++j) { - neurun::util::feature::Coordinate4D coord{0, h, 0, c}; - memcpy(output_buffer + tensor.calcOffset(coord), - _input_buffer + c * height * width + h * width, width * sizeof(T)); + neurun::util::Coordinates coords{i, j, 0}; + memcpy(output_buffer + tensor.calcOffset(coords), + _input_buffer + i * dim1 * dim2 + j * dim2, dim2 * sizeof(T)); } } break; @@ -158,88 +158,45 @@ public: { const int32_t copy_len = _shape.dim(1); - for (auto i = 0; i < _shape.dim(0); ++i) // w + for (auto i = 0; i < _shape.dim(0); ++i) { - neurun::util::feature::Coordinate4D coord; - if (tensor.layout() == neurun::graph::operand::Layout::NHWC) - { - coord.w(i); - } - else if (tensor.layout() == neurun::graph::operand::Layout::NCHW) - { - coord.h(i); - } - else - { - throw std::runtime_error("Wrong Layout"); - } - memcpy(output_buffer + tensor.calcOffset(coord), _input_buffer + i * copy_len, + neurun::util::Coordinates coords{i, 0}; + memcpy(output_buffer + tensor.calcOffset(coords), _input_buffer + i * copy_len, copy_len * sizeof(T)); } break; } case 3: { - const int32_t width = _shape.dim(1); - const int32_t copy_len = _shape.dim(2); + const int32_t dim1 = _shape.dim(1); + const int32_t dim2 = _shape.dim(2); - for (auto i = 0; i < _shape.dim(0); ++i) // h + for (auto i = 0; i < _shape.dim(0); ++i) { - for (auto j = 0; j < _shape.dim(1); ++j) // w + for (auto j = 0; j < _shape.dim(1); ++j) { - neurun::util::feature::Coordinate4D coord; - if (tensor.layout() == neurun::graph::operand::Layout::NHWC) - { - coord.h(i); - coord.w(j); - } - else if (tensor.layout() == neurun::graph::operand::Layout::NCHW) - { - coord.c(i); - coord.h(j); - } - else - { - throw std::runtime_error("Wrong Layout"); - } - memcpy(output_buffer + tensor.calcOffset(coord), - _input_buffer + i * width * copy_len + j * copy_len, copy_len * sizeof(T)); + neurun::util::Coordinates coords{i, j, 0}; + memcpy(output_buffer + tensor.calcOffset(coords), + _input_buffer + i * dim1 * dim2 + j * dim2, dim2 * sizeof(T)); } } break; } case 4: { - const int32_t height = _shape.dim(1); - const int32_t width = _shape.dim(2); - const int32_t copy_len = _shape.dim(3); - for (auto i = 0; i < _shape.dim(0); ++i) // n + const int32_t dim1 = _shape.dim(1); + const int32_t dim2 = _shape.dim(2); + const int32_t dim3 = _shape.dim(3); + for (auto i = 0; i < _shape.dim(0); ++i) { - for (auto j = 0; j < _shape.dim(1); ++j) // h + for (auto j = 0; j < _shape.dim(1); ++j) { - for (auto k = 0; k < _shape.dim(2); ++k) // w + for (auto k = 0; k < _shape.dim(2); ++k) { - neurun::util::feature::Coordinate4D coord; - if (tensor.layout() == neurun::graph::operand::Layout::NHWC) - { - coord.n(i); - coord.h(j); - coord.w(k); - } - else if (tensor.layout() == neurun::graph::operand::Layout::NCHW) - { - coord.n(i); - coord.c(j); - coord.h(k); - } - else - { - throw std::runtime_error("Wrong Layout"); - } - memcpy(output_buffer + tensor.calcOffset(coord), - _input_buffer + i * height * width * copy_len + j * width * copy_len + - k * copy_len, - copy_len * sizeof(T)); + neurun::util::Coordinates coords{i, j, k, 0}; + memcpy(output_buffer + tensor.calcOffset(coords), + _input_buffer + i * dim1 * dim2 * dim3 + j * dim2 * dim3 + k * dim3, + dim3 * sizeof(T)); } } } -- 2.7.4