From: 윤현식/동작제어Lab(SR)/Principal Engineer/삼성전자 Date: Fri, 7 Dec 2018 01:48:21 +0000 (+0900) Subject: [enco/tfl/frontend] Function to copy tensor value into coco::Data (#2511) X-Git-Tag: nncc_backup~1161 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fef818629650b567269025c32c747a901590e5c6;p=platform%2Fcore%2Fml%2Fnnfw.git [enco/tfl/frontend] Function to copy tensor value into coco::Data (#2511) * [enco/tfl/frontend] Function to copy unfilled tensor value into coco::Data This commit adds a function to copy unfilled tensor value into coco::Data. Signed-off-by: Hyun Sik Yoon * pr fix: added if with continue, assert, int32 message --- diff --git a/contrib/enco/frontend/tflite/src/Frontend.cpp b/contrib/enco/frontend/tflite/src/Frontend.cpp index 5fe345d..c64f181 100644 --- a/contrib/enco/frontend/tflite/src/Frontend.cpp +++ b/contrib/enco/frontend/tflite/src/Frontend.cpp @@ -23,6 +23,8 @@ #include #include +#include + using namespace nncc::core::ADT; namespace tflimport @@ -76,6 +78,48 @@ void set_module_outputs(coco::Module *m, TensorContext &ctx, TensorBags &bags, } } +/** + * @brief Copy values of tfl tensors into coco::Data if the data was not copied + */ +void copy_tensors(GraphBuilderContext *ctx) +{ + auto d = ctx->d(); + + // for each bag, check if bag is not allocated but tflite tensor has values + for (auto &iter : ctx->bags()) + { + auto tfl_tensor_id = iter.first; + auto bag = iter.second; + + auto tfl_buffer = ctx->buffer().tensor_buffer(ctx->graph(), tfl_tensor_id); + + // TODO remove this line when support int32 is ready + if (ctx->tensor().type(tfl_tensor_id) == tflite::TensorType::TensorType_INT32) + { + std::cout << "*** INT32 COPYING IS NOT SUPPORTED ***" << std::endl; + continue; + } + + assert(ctx->tensor().type(tfl_tensor_id) == tflite::TensorType::TensorType_FLOAT32); + + auto span = d->f32()->weight(bag); // TODO support other type + + if (!(span.data() == nullptr && span.size() == 0)) // already allocated + continue; + + if (tfl_buffer.ptr == nullptr || tfl_buffer.len == 0) // no data to copy + continue; + + d->f32()->allocate(bag); + + auto ifm_span = d->f32()->weight(bag); + for (uint32_t idx = 0; idx < tfl_buffer.len; ++idx) + { + ifm_span[idx] = tfl_buffer.ptr[idx]; + } + } +} + } // namespace tflimport Frontend::Frontend(std::unique_ptr &&raw) : _raw{std::move(raw)} @@ -139,6 +183,9 @@ enco::Bundle Frontend::load(void) const std::string opcodename = opcode_context.opcode_name(op); throw std::runtime_error{"Not supported: " + opcodename}; } + + // copying unfilled tensor value + copy_tensors(&opbuilder_context); } // Create "Bundle" diff --git a/contrib/enco/frontend/tflite/src/TensorBags.h b/contrib/enco/frontend/tflite/src/TensorBags.h index 84bec0b..29558b8 100644 --- a/contrib/enco/frontend/tflite/src/TensorBags.h +++ b/contrib/enco/frontend/tflite/src/TensorBags.h @@ -51,6 +51,11 @@ public: coco::Bag *bag(int32_t tensor_id) { return _bag_ctx[tensor_id]; } +public: + std::map::iterator begin() { return _bag_ctx.begin(); } + + std::map::iterator end() { return _bag_ctx.end(); } + private: std::map _bag_ctx; };