Dump compressed parameters (#601)
authorEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Sun, 15 Jul 2018 06:29:48 +0000 (09:29 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Sun, 15 Jul 2018 06:29:48 +0000 (15:29 +0900)
Implementation of compressed parameters dump

Signed-off-by: Efimov Alexander <a.efimov@samsung.com>
contrib/nnc/libs/backend/soft/include/model_analyzer.h
contrib/nnc/libs/backend/soft/src/generator.cpp

index 1bcc8de..bd556ce 100644 (file)
@@ -6,6 +6,7 @@
 #include <vector>
 #include <utility>
 #include <string>
+#include <cassert>
 
 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<char> _packedParameters;
   std::vector<OpDescr> _inferenceSequence;
   size_t _localTensorsN = 0;
index 04b6151..9d9df76 100644 (file)
@@ -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 &params = ma.getPackedParameters();
+  out.write(params.data(), params.size());
+  if (out.fail())
+  {
+    throw PluginException("Failed to write model Parameters");
+  }
 }
 
 void BaseCodeGenerator::generate(Graph *g)