|
| 1 | +// Copyright (c) 2021 Intel Corporation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef ENGINE_EXECUTOR_INCLUDE_PROFILING_TRACE_HPP_ |
| 16 | +#define ENGINE_EXECUTOR_INCLUDE_PROFILING_TRACE_HPP_ |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | +#include <fstream> |
| 20 | +#include <memory> |
| 21 | +#include <vector> |
| 22 | +#include <string> |
| 23 | +#include "operator.hpp" |
| 24 | +#include "dispatcher.hpp" |
| 25 | +#include "tensor.hpp" |
| 26 | + |
| 27 | +namespace executor { |
| 28 | +class ProfilingTracer { |
| 29 | + public: |
| 30 | + ProfilingTracer() : TotalTime(0), iterations_during() {} |
| 31 | + |
| 32 | + void BeginTrace(const std::string& filepath = "result.json") { |
| 33 | + OutputStream.open(filepath); |
| 34 | + TracerHeader(); |
| 35 | + } |
| 36 | + |
| 37 | + void EndTrace() { |
| 38 | + TracerFooter(); |
| 39 | + OutputStream.close(); |
| 40 | + } |
| 41 | + |
| 42 | + void WriteProfile(const vector<shared_ptr<Dispatcher>>& operators_, const vector<vector<Tensor*>>& input_tensors, |
| 43 | + const vector<vector<Tensor*>>& output_tensors) { |
| 44 | + IterationTotalTime(operators_); |
| 45 | + OutputStream << "{"; |
| 46 | + OutputStream << "\"cat\":\"inference\","; |
| 47 | + OutputStream << "\"dur\":" << TotalTime*1000<< ","; |
| 48 | + OutputStream << "\"name\":\"" << "model_inference" << "\","; |
| 49 | + OutputStream << "\"ph\":\"X\","; |
| 50 | + OutputStream << "\"pid\": 0,"; |
| 51 | + OutputStream << "\"tid\": \"" << "inference" << "\","; |
| 52 | + OutputStream << "\"ts\": " << 0; |
| 53 | + OutputStream << "}"; |
| 54 | + float iter_start = 0; |
| 55 | + for (int i = 0; i < operators_[1]->latency().size(); ++i) { |
| 56 | + OutputStream << ","; |
| 57 | + OutputStream << "{"; |
| 58 | + OutputStream << "\"cat\":\"" << "iteration" << "\","; |
| 59 | + OutputStream << "\"dur\":" << iterations_during[i]*1000 << ","; |
| 60 | + OutputStream << "\"name\":\"" << "Iteration" << i << "\","; |
| 61 | + OutputStream << "\"ph\":\"X\","; |
| 62 | + OutputStream << "\"pid\": 0,"; |
| 63 | + OutputStream << "\"tid\": \"" << "Iteration" << "\","; |
| 64 | + OutputStream << "\"ts\":" << iter_start*1000; |
| 65 | + OutputStream << "}"; |
| 66 | + float op_start = 0; |
| 67 | + for (int j = 1; j < operators_.size()-1; ++j) { |
| 68 | + const shared_ptr<Dispatcher>& op = operators_[j]; |
| 69 | + vector<Tensor*> its = input_tensors[j]; |
| 70 | + vector<Tensor*> ots = output_tensors[j]; |
| 71 | + OutputStream << ","; |
| 72 | + OutputStream << "{"; |
| 73 | + OutputStream << "\"cat\":\"" << op->type() << "\","; |
| 74 | + OutputStream << "\"dur\":" << op->latency()[i]*1000 + op->get_reshape_time()[i]*1000<< ","; |
| 75 | + OutputStream << "\"name\":\"" << op->name() << "\","; |
| 76 | + OutputStream << "\"ph\":\"X\","; |
| 77 | + OutputStream << "\"pid\": 0,"; |
| 78 | + OutputStream << "\"tid\": \"" << "Operator" << "\","; |
| 79 | + OutputStream << "\"ts\":" << (op_start + iter_start)*1000<< ","; |
| 80 | + OutputStream << "\"args\": {"; |
| 81 | + if (!op->post_op().empty()) { |
| 82 | + OutputStream << "\"post_op\" :\"" << op->post_op() << "\","; |
| 83 | + } |
| 84 | + OutputStream << "\"reshape_time\" :\"" << op->get_reshape_time()[i] << "ms" << "\","; |
| 85 | + OutputStream << "\"forward_time\" :\"" << op->latency()[i] << "ms" << "\","; |
| 86 | + OutputStream << "\"input_tensor_name\" :\"" << TensorsName(its) << "\","; |
| 87 | + OutputStream << "\"input_type\" :\"" << TensorsType(its) << "\","; |
| 88 | + OutputStream << "\"input_shape\" : "<< TensorsShape(op->get_it_shape(), i, its.size()) << ","; |
| 89 | + OutputStream << "\"output_tensor_name\" :\"" << TensorsName(ots) << "\","; |
| 90 | + OutputStream << "\"output_type\" :\"" << TensorsType(ots) << "\","; |
| 91 | + OutputStream << "\"output_shape\" : "<< TensorsShape(op->get_ot_shape(), i, 1); |
| 92 | + OutputStream << "}"; |
| 93 | + OutputStream << "}"; |
| 94 | + OutputStream.flush(); |
| 95 | + op_start += (op->latency()[i] + op->get_reshape_time()[i]); |
| 96 | + } |
| 97 | + iter_start += iterations_during[i]; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + void TracerHeader() { |
| 102 | + OutputStream << "{\"otherData\": {}, \"traceEvents\": ["; |
| 103 | + OutputStream.flush(); |
| 104 | + } |
| 105 | + |
| 106 | + void TracerFooter() { |
| 107 | + OutputStream << "]}"; |
| 108 | + OutputStream.flush(); |
| 109 | + } |
| 110 | +// get total time and per iteration's time |
| 111 | + void IterationTotalTime(const vector<shared_ptr<Dispatcher>>& operators_) { |
| 112 | + for (int i = 0; i < operators_[1]->latency().size(); ++i) { |
| 113 | + float PerIterTime = 0; |
| 114 | + for (int j = 1; j < operators_.size()-1; ++j) { |
| 115 | + PerIterTime += operators_[j]->get_reshape_time()[i]; |
| 116 | + PerIterTime += operators_[j]->latency()[i]; |
| 117 | + } |
| 118 | + iterations_during.emplace_back(PerIterTime); |
| 119 | + TotalTime += PerIterTime; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + std::string TensorsName(const vector<Tensor*>& Tensors) { |
| 124 | + std::string result = ""; |
| 125 | + for (int i = 0; i < Tensors.size(); ++i) { |
| 126 | + if (i == Tensors.size() -1) { |
| 127 | + result += Tensors[i]->name(); |
| 128 | + } else { |
| 129 | + result += Tensors[i]->name(); |
| 130 | + result += ","; |
| 131 | + } |
| 132 | + } |
| 133 | + return result; |
| 134 | + } |
| 135 | + |
| 136 | + std::string TensorsType(const vector<Tensor*>& Tensors) { |
| 137 | + std::string result = ""; |
| 138 | + for (int i = 0; i < Tensors.size(); ++i) { |
| 139 | + if (i == Tensors.size() -1) { |
| 140 | + result += Tensors[i]->dtype(); |
| 141 | + } else { |
| 142 | + result += Tensors[i]->dtype(); |
| 143 | + result += ","; |
| 144 | + } |
| 145 | + } |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + std::string TensorsShape(const vector<vector<int64_t>>& tensor_shape, |
| 150 | + int iteration_time, int tensor_size ) { |
| 151 | + std::string result = "\""; |
| 152 | + for (int i = iteration_time*tensor_size; i < (iteration_time + 1)*tensor_size; ++i) { |
| 153 | + if (i == (iteration_time + 1)*tensor_size -1) { |
| 154 | + for (int j = 0; j < tensor_shape[i].size(); ++j) { |
| 155 | + if (j == tensor_shape[i].size()-1) { |
| 156 | + result += std::to_string(tensor_shape[i][j]); |
| 157 | + } else { |
| 158 | + result += std::to_string(tensor_shape[i][j]); |
| 159 | + result += "*"; |
| 160 | + } |
| 161 | + } |
| 162 | + } else { |
| 163 | + for (int j = 0; j < tensor_shape[i].size(); ++j) { |
| 164 | + if (j == tensor_shape[i].size()-1) { |
| 165 | + result += std::to_string(tensor_shape[i][j]); |
| 166 | + result += ","; |
| 167 | + } else { |
| 168 | + result += std::to_string(tensor_shape[i][j]); |
| 169 | + result += "*"; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + result += "\""; |
| 175 | + return result; |
| 176 | + } |
| 177 | + |
| 178 | + protected: |
| 179 | + std::ofstream OutputStream; |
| 180 | + float TotalTime; |
| 181 | + vector<float> iterations_during; |
| 182 | +}; |
| 183 | + |
| 184 | +} // namespace executor |
| 185 | + |
| 186 | +#endif // ENGINE_EXECUTOR_INCLUDE_PEOFILING_TRACE_HPP_ |
0 commit comments