forked from embedded-dev-research/ITLabAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_weights.cpp
More file actions
120 lines (96 loc) · 2.79 KB
/
Copy pathreader_weights.cpp
File metadata and controls
120 lines (96 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "Weights_Reader/reader_weights.hpp"
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace it_lab_ai {
using json = nlohmann::json;
json read_json(const std::string& filename) {
#ifdef _WIN32
HANDLE file = CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
throw std::runtime_error("Cannot open file: " + filename);
}
DWORD size = GetFileSize(file, NULL);
if (size == 0) {
CloseHandle(file);
return json{};
}
HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
char* data = (char*)MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
json result = json::parse(data, data + size);
UnmapViewOfFile(data);
CloseHandle(mapping);
CloseHandle(file);
return result;
#else
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
throw std::runtime_error("Cannot open file: " + filename);
}
struct stat sb {};
fstat(fd, &sb);
if (sb.st_size == 0) {
close(fd);
return json{};
}
char* data = static_cast<char*>(
mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
json result = json::parse(data, data + sb.st_size);
munmap(data, sb.st_size);
close(fd);
return result;
#endif
}
void extract_values_from_json(const json& j, std::vector<float>& values) {
if (j.is_array()) {
for (const auto& item : j) {
extract_values_from_json(item, values);
}
} else if (j.is_number()) {
values.push_back(j.get<float>());
}
}
void parse_json_shape(const json& j, std::vector<size_t>& shape,
size_t dim = 0) {
if (!j.is_array()) {
if (dim == 0) shape.push_back(0);
return;
}
if (shape.size() <= dim) {
shape.push_back(j.size());
}
if (!j.empty()) {
parse_json_shape(j[0], shape, dim + 1);
}
}
Tensor create_tensor_from_json(const json& layer_data, Type type) {
if (type != Type::kFloat) {
throw std::invalid_argument("Only float type is supported");
}
std::vector<float> weights;
if (layer_data.contains("weights") && !layer_data["weights"].empty()) {
extract_values_from_json(layer_data["weights"], weights);
}
std::vector<float> bias;
if (layer_data.contains("bias") && !layer_data["bias"].empty()) {
extract_values_from_json(layer_data["bias"], bias);
}
std::vector<size_t> shape;
if (layer_data.contains("weights")) {
parse_json_shape(layer_data["weights"], shape);
}
return make_tensor<float>(weights, Shape(shape), bias);
}
} // namespace it_lab_ai