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