| 8 | namespace mgb::imperative::interpreter::intl { |
| 9 | |
| 10 | ProfilerPlugin::ProfilerPlugin(cg::ComputingGraph* graph) : PluginBase(graph) { |
| 11 | using namespace cg; |
| 12 | using namespace cg::event; |
| 13 | using namespace profiler; |
| 14 | auto on_seq_start = [this](CompSeqExecBeforeStart const& event) { |
| 15 | // reset |
| 16 | mgb_assert(!event.graph->options().imperative_proxy_graph); |
| 17 | CompNode::foreach ([](CompNode device) { |
| 18 | MGB_RECORD_EVENT_IF( |
| 19 | (Profiler::get_option("profile_device", 0)), RecordDeviceEvent, |
| 20 | Timer::record_device(device)); |
| 21 | }); |
| 22 | if (m_opr_dict.empty() && m_var_dict.empty()) { |
| 23 | init_seq(event.exec); |
| 24 | } |
| 25 | Profiler::record<ScopeEvent>("Constants"); |
| 26 | for (auto&& [var, var_info] : m_var_dict) { |
| 27 | if (var_info->is_const) { |
| 28 | bool valid = var->dev_tensor_valid(); |
| 29 | auto layout = valid ? var->layout() : TensorLayout(); |
| 30 | var_info->id = Profiler::next_id(); |
| 31 | Profiler::record<TensorDeclareEvent>(var_info->id, var->name()); |
| 32 | Profiler::record<TensorProduceEvent>( |
| 33 | var_info->id, layout, var->comp_node(), |
| 34 | valid ? var->dev_tensor().raw_ptr() : nullptr); |
| 35 | } else { |
| 36 | var_info->rt_ref_cnt = var_info->ref_cnt; |
| 37 | } |
| 38 | } |
| 39 | Profiler::record<ScopeFinishEvent>("Constants"); |
| 40 | Profiler::record<ScopeEvent>("DispatchOprs"); |
| 41 | event.exec->iter_opr_seq([this](OperatorNodeBase* opr) -> bool { |
| 42 | auto& opr_info = get_opr_info(opr); |
| 43 | for (auto output : opr->output()) { |
| 44 | auto& var_id = get_var_info(output).id; |
| 45 | var_id = Profiler::next_id(); |
| 46 | Profiler::record<TensorDeclareEvent>(var_id, output->name()); |
| 47 | } |
| 48 | auto opr_name = opr->dyn_typeinfo()->name; |
| 49 | auto copy_params = [params = opr_info.params] { return *params; }; |
| 50 | SmallVector<uint64_t> inputs, outputs; |
| 51 | for (auto input : opr->input()) { |
| 52 | inputs.push_back(get_var_info(input).id); |
| 53 | } |
| 54 | for (auto output : opr->output()) { |
| 55 | outputs.push_back(get_var_info(output).id); |
| 56 | } |
| 57 | Profiler::record<OpDispatchEvent>( |
| 58 | opr_info.id = Profiler::next_id(), opr_name, copy_params, inputs, |
| 59 | outputs); |
| 60 | return true; |
| 61 | }); |
| 62 | Profiler::record<ScopeFinishEvent>("DispatchOprs"); |
| 63 | }; |
| 64 | auto on_opr_start = [this](OprExecStart const& event) { |
| 65 | OperatorNodeBase* opr = event.opr; |
| 66 | auto& opr_info = get_opr_info(opr); |
| 67 | auto comp_node = opr_info.comp_node; |
nothing calls this directly
no test coverage detected