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

Method layout_transform_optimization

lite/src/mge/network_impl.cpp:448–552  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

446}
447
448void NetworkImplDft::layout_transform_optimization() {
449 if (m_set_layout_transform) {
450 mgb::ThinHashMap<mgb::SymbolVar, mgb::SymbolVar> out_var_map;
451 auto output_var_array = mgb::gopt::layout_transform(
452 m_load_result.output_var_list, m_layout_transform_target);
453 m_load_result.update_output_var_list(output_var_array);
454 } else if (m_user_config->auto_optimize_inference) {
455 //! set model weight preprocess
456 m_load_config.comp_graph->options().graph_opt.weight_preprocess = true;
457 LITE_LOG(
458 "weight_preprocess is enabled, this maybe use more memory when "
459 "infernece.");
460 //! get the current format and data type of the model
461 bool is_model_nchw = true;
462 //! is any convolution is int8
463 bool is_model_int8 = false;
464 //! is all convolution is float32
465 bool is_model_float32 = true;
466 float conv_cnt = 0;
467 float dimshuffle_cnt = 0;
468
469 auto detect_int8_model = [&](const VarNode* input) {
470 if (input->dtype().enumv() == megdnn::DTypeEnum::QuantizedS8 ||
471 input->dtype().enumv() == megdnn::DTypeEnum::Quantized8Asymm) {
472 is_model_int8 = true;
473 is_model_float32 = false;
474 } else if (input->dtype().enumv() == megdnn::DTypeEnum::Float32) {
475 is_model_float32 = (is_model_float32 && true);
476 } else {
477 is_model_float32 = false;
478 }
479 };
480
481 cg::DepOprIter dep([&](cg::OperatorNodeBase* opr) {
482 if (auto conv = opr->try_cast_final<opr::ConvolutionForward>()) {
483 if (conv->param().format != megdnn::param::ConvBias::Format::NCHW) {
484 is_model_nchw = false;
485 }
486 conv_cnt++;
487 detect_int8_model(conv->input(0));
488 } else if (auto conv_bias = opr->try_cast_final<opr::ConvBias>()) {
489 if (conv_bias->param().format !=
490 megdnn::param::ConvBias::Format::NCHW) {
491 is_model_nchw = false;
492 }
493 conv_cnt++;
494 detect_int8_model(conv->input(0));
495 } else if (auto dimshuffle = opr->try_cast_final<opr::Dimshuffle>()) {
496 LITE_MARK_USED_VAR(dimshuffle);
497 dimshuffle_cnt++;
498 }
499 });
500 for (auto&& i : m_load_result.output_var_list)
501 dep.add(i);
502
503 float radio_dimshuffle_conv = 0;
504 if (conv_cnt > 0) {
505 radio_dimshuffle_conv = dimshuffle_cnt / conv_cnt;

Callers

nothing calls this directly

Calls 6

enumvMethod · 0.45
dtypeMethod · 0.45
paramMethod · 0.45
inputMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected