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

Method init_dispatch_table

src/gopt/impl/basic_arith/trans.cpp:352–408  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

350}
351
352void FinalArithTransformPass::Impl::init_dispatch_table() {
353 /*
354 * Note: each rule takes var on original graph as input
355 */
356 auto add_dispatcher = [&](Mode mode) -> DispatchEntry& {
357 auto&& vec = m_dispatch_table[mode];
358 vec.emplace_back();
359 return vec.back();
360 };
361
362 auto add_dispatcher_with_name =
363 [&](Mode mode, const char* name) -> DispatchEntry::first_type& {
364 auto&& ret = add_dispatcher(mode);
365 ret.second = name;
366 return ret.first;
367 };
368
369#define REG(_mode, _name) \
370 add_dispatcher_with_name(Mode::_mode, mgb_cstr_log(_name)) = [this]( \
371 const VarNodeArray& inp) -> SymbolVar
372
373#define REG_THIS(_mode, _fn) \
374 add_dispatcher(Mode::_mode) = { \
375 std::bind(&Impl::_fn, this, std::placeholders::_1), mgb_cstr_log(#_fn)}
376
377 REG_THIS(ADD, merge_negate<MergeNegateAddTrait>);
378 REG_THIS(MUL, merge_negate<MergeNegateMulTrait>);
379
380 REG(POW, "powc and exp merge") {
381 auto exp_maybe = SymbolVar{inp[1]}.as_immutable_scalar_require_shape();
382 if (!exp_maybe.valid()) {
383 return {};
384 }
385 float exp = exp_maybe->get_cast<float>();
386 VarNode* base = m_rewriter.get_var(inp[0]);
387 Elemwise* base_pow;
388 if ((base_pow = as_elem_opr(base, Mode::POW)) && can_replace_var(base)) {
389 // powc(pow(x, a), b) => pow(x, a * b); a is not const scalar
390 VarNode* exp_new;
391 VarNode* exp_old = base_pow->input(1);
392 if (almost_equal(exp, -1.f)) {
393 // handle reciprocal
394 exp_new = get_neg_repl<Mode::ADD>(exp_old, true);
395 if (!exp_new) {
396 exp_new = opr::negate(exp_old).node();
397 }
398 } else {
399 exp_new = (SymbolVar{exp_old} * exp).node();
400 }
401 return opr::pow(base_pow->input(0), exp_new);
402 }
403 return opr::PowC::make(base, exp);
404 };
405
406#undef REG
407#undef REG_THIS
408}
409

Callers

nothing calls this directly

Calls 12

REGFunction · 0.85
as_elem_oprFunction · 0.85
almost_equalFunction · 0.85
emplace_backMethod · 0.80
backMethod · 0.80
powFunction · 0.50
makeFunction · 0.50
validMethod · 0.45
get_varMethod · 0.45
inputMethod · 0.45
nodeMethod · 0.45

Tested by

no test coverage detected