ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 53 of file MinMaxData.h.

Constructor & Destructor Documentation

◆ RawMinMaxDumper()

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

Definition at line 24 of file MinMaxData.cc.

24: _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 26 of file MinMaxData.cc.

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