Parameters reading from file (#818)
authorEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Thu, 2 Aug 2018 07:42:14 +0000 (10:42 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Thu, 2 Aug 2018 07:42:14 +0000 (16:42 +0900)
Add parameters mapping and unmapping of parameters file

Signed-off-by: Efimov Alexander <a.efimov@samsung.com>
contrib/nnc/libs/backend/soft/include/cpp_operations.def
contrib/nnc/libs/backend/soft/include/param_constants.def [new file with mode: 0644]
contrib/nnc/libs/backend/soft/src/generator.cpp

index 34ca99a..02f4cd9 100644 (file)
@@ -1,16 +1,54 @@
 #include <string>
 #include <cstdint>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <cstring>
 
 using namespace std;
 
-void readParameters(char *&data, size_t &len, const string &path, uint32_t version, uint32_t hash)
+void readParameters(char *&data, size_t &len, const string &path,
+                    uint32_t expectedVersion, uint32_t expectedHash)
 {
-  // TODO map file to memory, check version and hash equality
+  static_assert(sizeof(expectedVersion) == params::VERSION_LEN, "version length mismatch");
+  static_assert(sizeof(expectedHash) == params::HASH_LEN, "hash length mismatch");
+  int fd;
+  struct stat st;
+  fd = open(path.c_str(), O_RDONLY);
+  assert(fd != -1);
+
+  // gather file info
+  int statRes = fstat(fd, &st);
+  (void)statRes;
+  assert(statRes != -1);
+  len = st.st_size;
+  assert(len >= params::HEADER_LEN);
+
+  // check magic correctness
+  char magic[params::MAGIC_LEN + 1] = {};
+  read(fd, magic, params::MAGIC_LEN);
+  assert(strncmp(magic, params::MAGIC, params::MAGIC_LEN) == 0);
+
+  // checkversion correctness
+  decltype(expectedVersion) version;
+  read(fd, &version, sizeof(version));
+  assert(version == expectedVersion);
+
+  // check hash correctness
+  decltype(expectedHash) hash;
+  read(fd, &hash, sizeof(hash));
+  assert(hash == expectedHash);
+
+  data = static_cast<char *>(mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0));
 }
 
 void releaseParameters(char *data, size_t len)
 {
-  // TODO unmap file from memory
+  int res = munmap(data, len);
+  (void)res;
+  assert(res == 0);
 }
 
 template <class ...Args>
diff --git a/contrib/nnc/libs/backend/soft/include/param_constants.def b/contrib/nnc/libs/backend/soft/include/param_constants.def
new file mode 100644 (file)
index 0000000..52661e3
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef _NNC_SOFT_BACKEND_PARAM_CONSTANTS_H_
+#define _NNC_SOFT_BACKEND_PARAM_CONSTANTS_H_
+
+namespace params
+{
+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
+}
+
+#endif // _NNC_SOFT_BACKEND_PARAM_CONSTANTS_H_
index 5bbc2b3..9ce0780 100644 (file)
 #include <fcntl.h>
 #include <map>
 
+#include "param_constants.def"
+
 using namespace std;
 using namespace nncc::contrib;
 using namespace nncc::contrib::core::IR::model;
 
 #include "cpp_header_types.h"
 #include "cpp_operations.h"
+#include "param_constants.h"
 
 #include "cpp_common_funcs.h"
 #include "cpp_add_bias.h"
@@ -41,16 +44,6 @@ 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
 {
 
@@ -129,7 +122,7 @@ void BaseCodeGenerator::checkCorrectness()
 
 void BaseCodeGenerator::materializeModelParams(ostream &out, const ModelAnalyzer &ma)
 {
-  using namespace parameters_format;
+  using namespace params;
 
   // First form a dump header
   char header[HEADER_LEN];
@@ -146,8 +139,8 @@ void BaseCodeGenerator::materializeModelParams(ostream &out, const ModelAnalyzer
   {
     throw PluginException("Failed to write model parameters header");
   }
-  auto &params = ma.getPackedParameters();
-  out.write(params.data(), params.size());
+  auto &paramsVec = ma.getPackedParameters();
+  out.write(paramsVec.data(), paramsVec.size());
   if (out.fail())
   {
     throw PluginException("Failed to write model Parameters");
@@ -247,7 +240,7 @@ void CPPCodeGenerator::materializeHeader(ostream &out, const ModelAnalyzer &ma)
 {
   string className = ma.getModelName() + "Model";
 
-  out << cpp_header_types;
+  out.write(cpp_header_types, sizeof(cpp_header_types));
   out << "class " << className << "\n"
          "{\n"
          "public:\n"
@@ -355,7 +348,7 @@ void CPPCodeGenerator::materializeInferenceSequence(ostream &out, const ModelAna
     // gather output arguments
     gatherOperationArguments(ma, op._outputs, args);
     // parameters offset
-    args.push_back("_parameters + " + to_string(parameters_format::HEADER_LEN + op._paramStartOffset));
+    args.push_back("_parameters + " + to_string(params::HEADER_LEN + op._paramStartOffset));
     // gather input arguments
     gatherOperationArguments(ma, op._inputs, args);
     // put arguments into stream
@@ -384,7 +377,9 @@ void CPPCodeGenerator::materializeCode(ostream &out, const ModelAnalyzer &ma)
   out.write(cpp_relu, sizeof(cpp_relu));
   out.write(cpp_softmax, sizeof(cpp_softmax));
 
-  out << cpp_operations;
+  out.write(param_constants, sizeof(param_constants));
+
+  out.write(cpp_operations, sizeof(cpp_operations));
 
   // gen NN constructor
   out << className << "::" << className << "(const string &parametersPath)\n"