Dump input minmax map.
28{
29
30 auto file = std::fopen(_filename.c_str(),
"rb+");
31 uint32_t runs = 1;
32
33
34
35
36 const uint32_t MAGIC_CODE = 0x4F4D4D44;
37 const uint32_t VERSION = 1;
38 if (!file)
39 {
40
41 file = std::fopen(_filename.c_str(),
"wb+");
42 if (!file)
43 throw std::runtime_error{"RawMinMaxDumper: Failed to open minmax file " + _filename};
44
45
46 std::fwrite(&MAGIC_CODE, sizeof(uint32_t), 1, file);
47 std::fwrite(&VERSION, sizeof(uint32_t), 1, file);
48 }
49 else
50 {
51
52 std::fseek(file, 0, SEEK_SET);
53 uint32_t read_magic_code = 0;
54 uint32_t read_version = 0;
55 bool rewrite = true;
56 if (std::fread(&read_magic_code, sizeof(uint32_t), 1, file) == 1 &&
57 read_magic_code == MAGIC_CODE &&
58 std::fread(&read_version, sizeof(uint32_t), 1, file) == 1 && read_version == VERSION)
59 rewrite = false;
60
61
62 if (rewrite)
63 {
64 std::fclose(file);
65 file = std::fopen(_filename.c_str(),
"wb+");
66 if (!file)
67 throw std::runtime_error{"RawMinMaxDumper: Failed to rewrite minmax file " + _filename};
68
69
70 std::fwrite(&MAGIC_CODE, sizeof(uint32_t), 1, file);
71 std::fwrite(&VERSION, sizeof(uint32_t), 1, file);
72 }
73 }
74
75
76 if (std::fread(&runs, sizeof(uint32_t), 1, file) == 1)
77 runs++;
78 else
79 runs = 1;
80
81
82
83
84 std::fseek(file, sizeof(MAGIC_CODE) + sizeof(VERSION), SEEK_SET);
85 std::fwrite(&runs, sizeof(uint32_t), 1, file);
86
87
88 std::fseek(file, 0, SEEK_END);
89
90 uint32_t input_count = input_minmax.size();
91 uint32_t op_count = op_minmax.size();
92
93
94 std::fwrite(&op_count, sizeof(uint32_t), 1, file);
95 std::fwrite(&input_count, sizeof(uint32_t), 1, file);
96
97
98 for (auto &&[index, minmax] : op_minmax)
99 {
100 const uint32_t model_idx = 0;
101 const uint32_t subg_idx =
index.first.value();
102 const uint32_t op_idx =
index.second.value();
103
104
105 std::fwrite(&model_idx, sizeof(uint32_t), 1, file);
106 std::fwrite(&subg_idx, sizeof(uint32_t), 1, file);
107 std::fwrite(&op_idx, sizeof(uint32_t), 1, file);
108
109
110 std::fwrite(minmax.data, sizeof(float), 2, file);
111 }
112
113
114 for (auto &&[index, minmax] : input_minmax)
115 {
116 const uint32_t model_idx = 0;
117 const uint32_t subg_idx =
index.first.value();
118 const uint32_t input_idx =
index.second.value();
119
120
121 std::fwrite(&model_idx, sizeof(uint32_t), 1, file);
122 std::fwrite(&subg_idx, sizeof(uint32_t), 1, file);
123 std::fwrite(&input_idx, sizeof(uint32_t), 1, file);
124
125
126 std::fwrite(minmax.data, sizeof(float), 2, file);
127 }
128
129 std::fclose(file);
130}
loco::GraphInputIndex index(const TFPlaceholder *node)