ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::exec::RawMinMaxDumper Class Reference

#include <MinMaxData.h>

Public Member Functions

 RawMinMaxDumper (const std::string &filename)
 
void dump (const exec::IOMinMaxMap &in_minmax, const exec::OpMinMaxMap &op_minmax) const
 Dump input minmax map.
 

Detailed Description

Definition at line 55 of file MinMaxData.h.

Constructor & Destructor Documentation

◆ RawMinMaxDumper()

onert::exec::RawMinMaxDumper::RawMinMaxDumper ( const std::string &  filename)

Definition at line 26 of file MinMaxData.cc.

26: _filename(filename) {}

Member Function Documentation

◆ dump()

void onert::exec::RawMinMaxDumper::dump ( const exec::IOMinMaxMap in_minmax,
const exec::OpMinMaxMap op_minmax 
) const

Dump input minmax map.

Parameters
[in]in_minmaxinput minmax map
[in]op_minmaxop minmax map

Definition at line 28 of file MinMaxData.cc.

30{
31 // Find file is already exist for modifying
32 auto file = std::fopen(_filename.c_str(), "rb+");
33 uint32_t runs = 1;
34
35 // Magic code and version
36 // Match with runtime/onert/odc/MinMaxReader.cc
37 // TODO Use util to share code and version
38 const uint32_t MAGIC_CODE = 0x4F4D4D44;
39 const uint32_t VERSION = 1;
40 if (!file)
41 {
42 // If file is not exist, create new file
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 // Write magic code and version
48 std::fwrite(&MAGIC_CODE, sizeof(uint32_t), 1, file);
49 std::fwrite(&VERSION, sizeof(uint32_t), 1, file);
50 }
51 else
52 {
53 // Check magic code and version
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 // Destroy and create if file is not valid
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 // Write magic code and version
72 std::fwrite(&MAGIC_CODE, sizeof(uint32_t), 1, file);
73 std::fwrite(&VERSION, sizeof(uint32_t), 1, file);
74 }
75 }
76
77 // Read run count
78 if (std::fread(&runs, sizeof(uint32_t), 1, file) == 1)
79 runs++;
80 else
81 runs = 1;
82
83 // TODO Verify file size
84
85 // Overwrite run count
86 std::fseek(file, sizeof(MAGIC_CODE) + sizeof(VERSION), SEEK_SET);
87 std::fwrite(&runs, sizeof(uint32_t), 1, file);
88
89 // Go to end of file to append new data
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 // Write op_count and input_count
96 std::fwrite(&op_count, sizeof(uint32_t), 1, file);
97 std::fwrite(&input_count, sizeof(uint32_t), 1, file);
98
99 // For each op
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 // Write model/subg/op index
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 // Write min/max
112 std::fwrite(minmax.data, sizeof(float), 2, file);
113 }
114
115 // For each input
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 // Write model/subg/input index
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 // Write min/max
128 std::fwrite(minmax.data, sizeof(float), 2, file);
129 }
130
131 std::fclose(file);
132}
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References onert::util::MinMaxMap< N, Hash >::size().


The documentation for this class was generated from the following files: