ONE - On-device Neural Engine
Loading...
Searching...
No Matches
SNPEWriter Class Reference

#include <EventWriter.h>

Collaboration diagram for SNPEWriter:

Public Member Functions

 SNPEWriter (const std::string &filepath)
 
 ~SNPEWriter ()
 
void flush (const std::vector< std::unique_ptr< EventRecorder > > &) override
 
- Public Member Functions inherited from EventFormatWriter
 EventFormatWriter (const std::string &filepath)
 
virtual ~EventFormatWriter ()
 

Additional Inherited Members

- Protected Attributes inherited from EventFormatWriter
std::ofstream _os
 

Detailed Description

Definition at line 42 of file EventWriter.h.

Constructor & Destructor Documentation

◆ SNPEWriter()

SNPEWriter::SNPEWriter ( const std::string &  filepath)
inline

Definition at line 45 of file EventWriter.h.

45 : EventFormatWriter(filepath)
46 { /* empty */
47 }

◆ ~SNPEWriter()

SNPEWriter::~SNPEWriter ( )
inline

Definition at line 48 of file EventWriter.h.

48{}

Member Function Documentation

◆ flush()

void SNPEWriter::flush ( const std::vector< std::unique_ptr< EventRecorder > > &  recorders)
overridevirtual

Implements EventFormatWriter.

Definition at line 85 of file SNPEEventWriter.cc.

86{
87 struct Stat
88 {
89 uint64_t sum = 0;
90 uint64_t count = 0;
91 uint64_t max = 0;
92 uint64_t min = std::numeric_limits<uint64_t>::max();
93
94 void accumulate(uint64_t val)
95 {
96 sum += val;
97 count++;
98 max = std::max(max, val);
99 min = std::min(min, val);
100 }
101 };
102
103 Json::Value root;
104 root["version"] = SNPE_JSON_SCHEMA_VERSION;
105
106 auto &exec_data = root["Execution_Data"] = Json::Value{Json::objectValue};
107
108 // Memory
109 {
110 std::unordered_map<std::string, Stat> mem_stats;
111 for (const auto &recorder : recorders)
112 {
113 for (const auto &evt : recorder->counter_events())
114 {
115 auto &mem_stat = mem_stats[evt.name];
116 uint64_t val = std::stoull(evt.values.at("value"));
117 mem_stat.accumulate(val);
118 }
119 }
120
121 auto &mem = exec_data["memory"] = Json::Value{Json::objectValue};
122 for (const auto &[key, val] : mem_stats)
123 {
124 mem[key]["Avg_Size"] = val.sum / val.count;
125 mem[key]["Max_Size"] = val.max;
126 mem[key]["Min_Size"] = val.min;
127 mem[key]["Runtime"] = "NA";
128 }
129 }
130
131 // Operation Execution Time
132 {
133 // NOTE This assumes _duration_events is sorted by "ts" ascending
134
135 // TODO: Apply Heterogeneous lookup for unordered containers (transparent hashing) since C++20
136 // to use `std::string_view` with lookup functions in unordered containers
137 // 2D keys : stats[tid][name]
138 std::unordered_map<std::string, std::unordered_map<std::string, Stat>> stats;
139 std::unordered_map<std::string, std::unordered_map<std::string, uint64_t>> begin_timestamps;
140 for (const auto &recorder : recorders)
141 {
142 for (const auto &evt : recorder->duration_events())
143 {
144 std::string evt_name = getLabel(*evt);
145 std::string evt_tid = getBackend(*evt);
146
147 auto &stat = stats[evt_tid][evt_name];
148 auto &begin_ts = begin_timestamps[evt_tid][evt_name];
149 uint64_t timestamp = std::stoull(evt->ts);
150 if (evt->ph == "B")
151 {
152 if (begin_ts != 0)
153 throw std::runtime_error{"Invalid Data"};
154 begin_ts = timestamp;
155 }
156 else if (evt->ph == "E")
157 {
158 if (begin_ts == 0 || timestamp < begin_ts)
159 throw std::runtime_error{"Invalid Data"};
160 stat.accumulate(timestamp - begin_ts);
161 begin_ts = 0;
162 }
163 else
164 throw std::runtime_error{"Invalid Data - invalid value for \"ph\" : \"" + evt->ph + "\""};
165 }
166 }
167
168 for (const auto &kv : begin_timestamps)
169 for (const auto &kv2 : kv.second)
170 if (kv2.second != 0)
171 throw std::runtime_error{"Invalid Data - B and E pair does not match."};
172
173 for (const auto &[tid, stat_map] : stats)
174 {
175 auto &json_tid = exec_data[tid] = Json::Value{Json::objectValue};
176 for (const auto &[name, val] : stat_map)
177 {
178 json_tid[name]["Avg_Time"] = val.sum / val.count;
179 json_tid[name]["Max_Time"] = val.max;
180 json_tid[name]["Min_Time"] = val.min;
181 json_tid[name]["Runtime"] = tid;
182 }
183 }
184 }
185
186 _os << root;
187}
#define SNPE_JSON_SCHEMA_VERSION
Version of SNPE format In version 1.
std::ofstream _os
Definition EventWriter.h:39
Op * root(Op *)
Return the root Op from a given Op node.
Definition Op.cpp:144
name
Definition setup.py:158

References EventFormatWriter::_os, and SNPE_JSON_SCHEMA_VERSION.


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