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 80 of file SNPEEventWriter.cc.

81{
82 struct Stat
83 {
84 uint64_t sum = 0;
85 uint64_t count = 0;
86 uint64_t max = 0;
87 uint64_t min = std::numeric_limits<uint64_t>::max();
88
89 void accumulate(uint64_t val)
90 {
91 sum += val;
92 count++;
93 max = std::max(max, val);
94 min = std::min(min, val);
95 }
96 };
97
98 Json::Value root;
99 root["version"] = SNPE_JSON_SCHEMA_VERSION;
100
101 auto &exec_data = root["Execution_Data"] = Json::Value{Json::objectValue};
102
103 // Memory
104 {
105 std::unordered_map<std::string, Stat> mem_stats;
106 for (const auto &recorder : recorders)
107 {
108 for (const auto &evt : recorder->counter_events())
109 {
110 auto &mem_stat = mem_stats[evt.name];
111 uint64_t val = std::stoull(evt.values.at("value"));
112 mem_stat.accumulate(val);
113 }
114 }
115
116 auto &mem = exec_data["memory"] = Json::Value{Json::objectValue};
117 for (const auto &[key, val] : mem_stats)
118 {
119 mem[key]["Avg_Size"] = val.sum / val.count;
120 mem[key]["Max_Size"] = val.max;
121 mem[key]["Min_Size"] = val.min;
122 mem[key]["Runtime"] = "NA";
123 }
124 }
125
126 // Operation Execution Time
127 {
128 // NOTE This assumes _duration_events is sorted by "ts" ascending
129
130 // 2D keys : stats[tid][name]
131 std::unordered_map<std::string, std::unordered_map<std::string, Stat>> stats;
132 std::unordered_map<std::string, std::unordered_map<std::string, uint64_t>> begin_timestamps;
133 for (const auto &recorder : recorders)
134 {
135 for (const auto &evt : recorder->duration_events())
136 {
137 std::string evt_name = getLabel(*evt);
138 std::string evt_tid = getBackend(*evt);
139
140 auto &stat = stats[evt_tid][evt_name];
141 auto &begin_ts = begin_timestamps[evt_tid][evt_name];
142 uint64_t timestamp = std::stoull(evt->ts);
143 if (evt->ph == "B")
144 {
145 if (begin_ts != 0)
146 throw std::runtime_error{"Invalid Data"};
147 begin_ts = timestamp;
148 }
149 else if (evt->ph == "E")
150 {
151 if (begin_ts == 0 || timestamp < begin_ts)
152 throw std::runtime_error{"Invalid Data"};
153 stat.accumulate(timestamp - begin_ts);
154 begin_ts = 0;
155 }
156 else
157 throw std::runtime_error{"Invalid Data - invalid value for \"ph\" : \"" + evt->ph + "\""};
158 }
159 }
160
161 for (const auto &kv : begin_timestamps)
162 for (const auto &kv2 : kv.second)
163 if (kv2.second != 0)
164 throw std::runtime_error{"Invalid Data - B and E pair does not match."};
165
166 for (const auto &[tid, stat_map] : stats)
167 {
168 auto &json_tid = exec_data[tid] = Json::Value{Json::objectValue};
169 for (const auto &[name, val] : stat_map)
170 {
171 json_tid[name]["Avg_Time"] = val.sum / val.count;
172 json_tid[name]["Max_Time"] = val.max;
173 json_tid[name]["Min_Time"] = val.min;
174 json_tid[name]["Runtime"] = tid;
175 }
176 }
177 }
178
179 _os << root;
180}
#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

References EventFormatWriter::_os, and SNPE_JSON_SCHEMA_VERSION.


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