2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "exec/ExecutionObservers.h"
21 #include "util/logging.h"
22 #include "model/operation/Permute.h"
23 #include "exec/IExecutor.h"
24 #include "misc/polymorphic_downcast.h"
25 #include "model/Subgraph.h"
33 void ProfileObserver::handleBegin(neurun::exec::IExecutor *, const neurun::model::Subgraph *,
34 const neurun::backend::Backend *backend)
36 _timer = backend->config()->timer();
37 if (_timer == nullptr)
38 throw std::runtime_error("To profile backend timer() method must be implemented");
39 _timer->handleBegin();
42 void ProfileObserver::handleEnd(IExecutor *exec, const model::Subgraph *subgraph,
43 const backend::Backend *backend)
46 const auto timer_res = _timer->getTime();
48 // NOTE This assumes there is just one operation in a subgraph
49 auto node = subgraph->operations().at(0).node;
50 auto node_name = node->name();
51 VERBOSE(ProfileInfo) << "Time for " << node_name << " : " << timer_res << std::endl;
54 bool is_quantized = exec->graph().operands().at(node->getInputs().at(0)).typeInfo().type() ==
55 model::DataType::QUANT8_ASYMM;
58 for (const auto &input : node->getInputs())
60 size += exec->graph().operands().at(input).info().total_size();
62 for (const auto &output : node->getOutputs())
64 size += exec->graph().operands().at(output).info().total_size();
66 if (node_name == "Permute")
68 auto *permute_node = nnfw::misc::polymorphic_downcast<const model::operation::Permute *>(node);
69 assert(permute_node != nullptr);
70 _et->updatePermuteTime(permute_node->param().input_backend_ctx->backend,
71 permute_node->param().output_backend_ctx->backend, is_quantized, size,
76 _et->updateOperationExecTime(backend, node_name, is_quantized, size, timer_res);
80 ChromeTracingObserver::ChromeTracingObserver(const std::string &filepath)
81 : _ofs{filepath, std::ofstream::out}, _recorder{}, _collector{&_recorder}
85 ChromeTracingObserver::~ChromeTracingObserver() { _recorder.writeToFile(_ofs); }
87 void ChromeTracingObserver::handleBegin(IExecutor *)
89 _collector.onEvent(EventCollector::Event{EventCollector::Edge::BEGIN, "runtime", "Graph"});
92 void ChromeTracingObserver::handleBegin(IExecutor *, const model::Subgraph *subgraph,
93 const backend::Backend *backend)
95 std::string backend_id = backend->config()->id();
97 EventCollector::Event{EventCollector::Edge::BEGIN, backend_id, subgraphTag(subgraph)});
100 void ChromeTracingObserver::handleEnd(IExecutor *, const model::Subgraph *subgraph,
101 const backend::Backend *backend)
103 std::string backend_id = backend->config()->id();
105 EventCollector::Event{EventCollector::Edge::END, backend_id, subgraphTag(subgraph)});
108 void ChromeTracingObserver::handleEnd(IExecutor *)
110 _collector.onEvent(EventCollector::Event{EventCollector::Edge::END, "runtime", "Graph"});
113 std::string ChromeTracingObserver::subgraphTag(const model::Subgraph *subgraph)
115 if (subgraph->size() == 0)
116 return "Empty Subgraph";
118 auto first_op = subgraph->operations().at(0);
119 std::string tag = "$" + std::to_string(first_op.index.value());
120 tag += " " + first_op.node->name();
121 if (subgraph->size() > 1)
123 tag += " (+" + std::to_string(subgraph->size() - 1) + ")";
130 } // namespace neurun