#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>
#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"
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
{
void BaseCodeGenerator::materializeModelParams(ostream &out, const ModelAnalyzer &ma)
{
- using namespace parameters_format;
+ using namespace params;
// First form a dump header
char header[HEADER_LEN];
{
throw PluginException("Failed to write model parameters header");
}
- auto ¶ms = ma.getPackedParameters();
- out.write(params.data(), params.size());
+ auto ¶msVec = ma.getPackedParameters();
+ out.write(paramsVec.data(), paramsVec.size());
if (out.fail())
{
throw PluginException("Failed to write model Parameters");
{
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"
// 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
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 ¶metersPath)\n"