From e44bcdae8b801ba03dbdf49759194558ca0f1266 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EA=B9=80=EC=9A=A9=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: Mon, 24 Jun 2019 14:03:02 +0900 Subject: [PATCH] Add initConstant to Utils.h (#5467) This util `initConstant` will be used in backend::TensorBuilders Signed-off-by: Yongseop Kim --- runtimes/neurun/core/include/util/Utils.h | 85 +++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/runtimes/neurun/core/include/util/Utils.h b/runtimes/neurun/core/include/util/Utils.h index bdb478b..2fb6893 100644 --- a/runtimes/neurun/core/include/util/Utils.h +++ b/runtimes/neurun/core/include/util/Utils.h @@ -25,7 +25,9 @@ #include "model/InternalType.h" #include "model/Layout.h" +#include "model/Operand.h" #include "util/Coordinates.h" +#include "backend/operand/IObject.h" #define UNUSED_RELEASE(a) (void)(a) @@ -44,6 +46,89 @@ const char *to_string(const model::PaddingType &type); Coordinates convertCoordinates(const Coordinates &from_coordinates, model::Layout from_layout, model::Layout to_layout); +template +void initConstant(neurun::backend::operand::IObject &obj, const neurun::model::Operand &model_obj) +{ + const auto shape = model_obj.shape(); + auto base = reinterpret_cast(model_obj.data().base()); + + obj.access([&](::neurun::backend::operand::ITensor &tensor) { + switch (shape.rank()) + { + case 1: + { + auto vec_size = shape.asVector(); + for (int32_t n = 0; n < vec_size; ++n) + { + const T *from = reinterpret_cast(base) + n; + const auto value = *from; + + T *into = reinterpret_cast(tensor.buffer()) + n; + + *into = value; + } + break; + } + case 2: + { + const int32_t copy_len = shape.dim(1); + + for (auto i = 0; i < shape.dim(0); ++i) + { + util::Coordinates coords{i, 0}; + memcpy(tensor.buffer() + tensor.calcOffset(coords), base + 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); + + for (auto i = 0; i < shape.dim(0); ++i) + { + for (auto j = 0; j < shape.dim(1); ++j) + { + util::Coordinates coords{i, j, 0}; + memcpy(tensor.buffer() + tensor.calcOffset(coords), + base + i * width * copy_len + j * copy_len, copy_len * 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) + { + for (auto j = 0; j < shape.dim(1); ++j) + { + for (auto k = 0; k < shape.dim(2); ++k) + { + for (auto l = 0; l < shape.dim(3); ++l) + { + // TODO Change from_layout arg to front-end layout. + const auto coords = + util::convertCoordinates({i, j, k, l}, model::Layout::NHWC, tensor.layout()); + T *into = reinterpret_cast(tensor.buffer() + tensor.calcOffset(coords)); + T value = *(base + i * height * width * copy_len + j * width * copy_len + + k * copy_len + l); + *into = value; + } + } + } + } + break; + } + default: + throw std::runtime_error{"Not yet supported"}; + } + }); +} + } // namespace util } // namespace neurun -- 2.7.4