From ed70fb407c2db4ffa8b6de0cf78489a998a9a6b4 Mon Sep 17 00:00:00 2001 From: "Efimov Alexander/AI Tools Lab/./Samsung Electronics" Date: Sun, 15 Jul 2018 09:29:48 +0300 Subject: [PATCH] Dump compressed parameters (#601) Implementation of compressed parameters dump Signed-off-by: Efimov Alexander --- .../nnc/libs/backend/soft/include/model_analyzer.h | 13 +++++++-- contrib/nnc/libs/backend/soft/src/generator.cpp | 34 +++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/contrib/nnc/libs/backend/soft/include/model_analyzer.h b/contrib/nnc/libs/backend/soft/include/model_analyzer.h index 1bcc8de..bd556ce 100644 --- a/contrib/nnc/libs/backend/soft/include/model_analyzer.h +++ b/contrib/nnc/libs/backend/soft/include/model_analyzer.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace nncc { @@ -78,15 +79,23 @@ public: return _packedParameters; } - uint64_t getFormatVersion() const + uint32_t getFormatVersion() const { return _formatVersion; } + // generate hash from analyzed Model IR + uint32_t getModelHash() const + { + assert(!_inferenceSequence.empty() && "Empty model! Did you apply visitor?"); + return _modelHash; + } + private: void addOpDescr(ADT::INode *node, const std::string &name); - const uint64_t _formatVersion = 1; + const uint32_t _formatVersion = 1; + uint32_t _modelHash = 0; std::vector _packedParameters; std::vector _inferenceSequence; size_t _localTensorsN = 0; diff --git a/contrib/nnc/libs/backend/soft/src/generator.cpp b/contrib/nnc/libs/backend/soft/src/generator.cpp index 04b6151..9d9df76 100644 --- a/contrib/nnc/libs/backend/soft/src/generator.cpp +++ b/contrib/nnc/libs/backend/soft/src/generator.cpp @@ -25,6 +25,16 @@ namespace backend namespace soft { +namespace parameters_format +{ + const int MAGIC_LEN = 4; + const int VERSION_LEN = 4; + const int HASH_LEN = 4; + const int HEADER_LEN = MAGIC_LEN + VERSION_LEN + HASH_LEN; + + const char MAGIC[MAGIC_LEN + 1] = "NNMP"; // Neural Network Model Parameters +} + namespace { @@ -103,7 +113,29 @@ void BaseCodeGenerator::checkCorrectness() void BaseCodeGenerator::materializeModelParams(ostream &out, const ModelAnalyzer &ma) { - // TODO dump compressed model data + using namespace parameters_format; + + // First form a dump header + char header[HEADER_LEN]; + uint32_t version = ma.getFormatVersion(); + uint32_t hash = ma.getModelHash(); + static_assert(VERSION_LEN == sizeof(version), "version length mismatch"); + static_assert(HASH_LEN == sizeof(hash), "hash length mismatch"); + memcpy(header, MAGIC, MAGIC_LEN); + memcpy(header + MAGIC_LEN, &version, VERSION_LEN); + memcpy(header + MAGIC_LEN + VERSION_LEN, &hash, HASH_LEN); + + out.write(header, HEADER_LEN); + if (out.fail()) + { + throw PluginException("Failed to write model parameters header"); + } + auto ¶ms = ma.getPackedParameters(); + out.write(params.data(), params.size()); + if (out.fail()) + { + throw PluginException("Failed to write model Parameters"); + } } void BaseCodeGenerator::generate(Graph *g) -- 2.7.4