Bugfix: Add NN parameters in soft backend artefact (#706)
authorEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Wed, 25 Jul 2018 13:45:17 +0000 (16:45 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Wed, 25 Jul 2018 13:45:17 +0000 (22:45 +0900)
Fix absence of NN parameters in artefact

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

index 9eea247..b095df5 100644 (file)
@@ -32,12 +32,12 @@ 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 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
+const char MAGIC[MAGIC_LEN + 1] = "NNMP"; // Neural Network Model Parameters
 }
 
 namespace
@@ -240,7 +240,8 @@ void CPPCodeGenerator::materializeHeader(ostream &out, const ModelAnalyzer &ma)
   out << "class " << className << "\n"
          "{\n"
          "public:\n"
-         "  " << className << "(const std::string &parametersPath);\n";
+         "  " << className << "(const std::string &parametersPath);\n"
+         "  ~" << className << "();\n";
   // generate input setters
   for (const size_t inId: ma.getInputs())
   {
@@ -269,6 +270,9 @@ void CPPCodeGenerator::materializeHeader(ostream &out, const ModelAnalyzer &ma)
     const string &tName = _formattedTensors[outId];
     out << "  std::shared_ptr<Tensor> " << tName << ";\n";
   }
+  // pointer to NN parameters
+  out << "  char * _parameters;\n";
+  out << "  size_t _paramSize;\n";
   out << "};\n";
 }
 
@@ -340,7 +344,7 @@ void CPPCodeGenerator::materializeInferenceSequence(ostream &out, const ModelAna
     // gather output arguments
     gatherOperationArguments(ma, op._outputs, args);
     // parameters offset
-    args.push_back(to_string(op._paramStartOffset));
+    args.push_back("_parameters + " + to_string(parameters_format::HEADER_LEN + op._paramStartOffset));
     // gather input arguments
     gatherOperationArguments(ma, op._inputs, args);
     // put arguments into stream
@@ -353,15 +357,22 @@ void CPPCodeGenerator::materializeInferenceSequence(ostream &out, const ModelAna
 void CPPCodeGenerator::materializeCode(ostream &out, const ModelAnalyzer &ma)
 {
   string className = ma.getModelName() + "Model";
+
+  out << "#include \"" << _headerFile << "\"\n";
+
   out << cpp_operations;
 
   // gen NN constructor
   out << className << "::" << className << "(const string &parametersPath)\n"
          "{\n"
-         "  readParameters(_parameters, parametersPath, " <<
-                           ma.getFormatVersion() << ", " << ma.getModelHash() << ");\n"
+         "  readParameters(_parameters, _paramSize, parametersPath, "
+      << ma.getFormatVersion() << ", " << ma.getModelHash() << ");\n"
+         "}\n\n";
+  // gen NN destructor
+  out << className << "::~" << className << "()\n"
+         "{\n"
+         "  releaseParameters(_parameters, _paramSize);\n"
          "}\n\n";
-
   // gen input setters
   for (size_t inId: ma.getInputs())
   {