MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / flush_record_oprs

Method flush_record_oprs

src/core/impl/graph/eager_eval.cpp:380–440  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

378}
379
380void EagerEvalManager::flush_record_oprs(const VarNodeArray& dest_vars) {
381 if (!enabled()) {
382 mgb_assert(m_record_oprs.empty());
383 return;
384 }
385 m_record_mode = false;
386 using NodeProp = OperatorNodeBase::NodeProp;
387 ThinHashSet<OperatorNodeBase*> need_exec_oprs;
388 ThinHashSet<OperatorNodeBase*> dest_oprs;
389 std::function<void(OperatorNodeBase*)> visit = [&](OperatorNodeBase* opr) {
390 if (!m_record_oprs.count(opr) || need_exec_oprs.count(opr))
391 return;
392 need_exec_oprs.insert(opr);
393 for (auto inp : opr->input()) {
394 visit(inp->owner_opr());
395 }
396 };
397 for (auto var : dest_vars) {
398 dest_oprs.insert(var->owner_opr());
399 visit(var->owner_opr());
400 }
401 for (auto opr : need_exec_oprs) {
402 auto&& node_prop = opr->node_prop();
403 for (auto&& pair : node_prop.dep_map()) {
404 if (NodeProp::is_device_value_dep(pair.second) &&
405 need_exec_oprs.count(pair.first->owner_opr())) {
406 if (!dest_oprs.count(pair.first->owner_opr()))
407 m_var2nr_readers[pair.first] += opr->output().size();
408 }
409 }
410 }
411 SmallVector<std::pair<OperatorNodeBase*, size_t>> stack;
412 ThinHashSet<OperatorNodeBase*> instack;
413 auto push_stack = [&](OperatorNodeBase* opr) {
414 if (need_exec_oprs.erase(opr)) {
415 stack.push_back({opr, 0});
416 instack.insert(opr);
417 } else {
418 mgb_assert(instack.count(opr) || m_opr2version.count(opr));
419 }
420 };
421 for (auto&& var : dest_vars) {
422 push_stack(var->owner_opr());
423 }
424 while (!stack.empty()) {
425 auto&& frame = stack.back();
426 auto opr = frame.first;
427 if (frame.second < opr->input().size()) {
428 auto var = opr->input()[frame.second++];
429 push_stack(var->owner_opr());
430 } else {
431 MGB_TRY { do_on_opr_insert(opr); }
432 MGB_CATCH(MegBrainError & exc, {
433 if (!exc.extra_info()) {
434 OperatorNodeExcExtraInfo::record(opr, exc);
435 }
436 throw;
437 });

Callers 1

gradMethod · 0.80

Calls 14

backMethod · 0.80
extra_infoMethod · 0.80
MGB_CATCHFunction · 0.70
enabledFunction · 0.50
emptyMethod · 0.45
countMethod · 0.45
insertMethod · 0.45
inputMethod · 0.45
owner_oprMethod · 0.45
sizeMethod · 0.45
outputMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected