network_impl.cpp 52.3 KB
Newer Older
1 2 3 4 5 6
#include "lite_build_config.h"

#if LITE_BUILD_WITH_MGE
#include "common.h"
#include "lite/network.h"
#include "memory_allocator.h"
M
Megvii Engine Team 已提交
7
#include "network_impl.h"
8
#include "parse_info/parse_info_base.h"
M
Megvii Engine Team 已提交
9
#include "parse_model/model_parser.h"
10 11 12 13 14 15

#include "megbrain/common.h"
#include "megbrain/comp_node.h"
#include "megbrain/comp_node_env.h"
#include "megbrain/graph.h"
#include "megbrain/graph/cg.h"
16
#include "megbrain/opr/imgproc.h"
17
#include "megbrain/opr/io.h"
18
#include "megbrain/opr/tensor_manip.h"
19 20 21 22 23 24
#include "megbrain/tensor.h"

#if MGB_OPENCL
#include "megcore_opencl.h"
#endif

25 26 27 28
#if defined(MGB_ENABLE_CPUINFO_CHECK) && MGB_ENABLE_CPUINFO
#include "cpuinfo.h"
#endif

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <fstream>
#include <memory>
#include <set>

using namespace lite;
using namespace mgb;

LITE_DYN_TYPE_OBJ_FINAL_IMPL(NetworkImplDft);

void NetworkImplDft::set_config(const Config& config) {
    *m_user_config = config;
    m_compnode_locator = to_compnode_locator(m_user_config->device_type);
    m_compnode_locator.device = config.device_id;
}

void NetworkImplDft::shared_weight_with(const NetworkImplBase* src_network) {
    application_config();
    const auto& src_impl = src_network->cast_final_safe<NetworkImplDft>();
M
Megvii Engine Team 已提交
47
    LITE_ASSERT(src_impl.m_loader, "Clone network must after the network is loaded.");
48 49
    m_load_result = src_impl.m_loader->load(m_load_config, true);

50
    configure_after_loaded();
51 52 53 54 55
}

void NetworkImplDft::application_config() {
    auto device_type = m_user_config->device_type;
    m_compnode_locator.type = to_compnode_locator(device_type).type;
56 57 58 59
    //! when the device id is not configured, configure it
    if (m_compnode_locator.device == -1) {
        m_compnode_locator.device = m_user_config->device_id;
    }
60 61
    if (m_nr_threads > 1 && device_type == LiteDeviceType::LITE_CPU) {
        m_compnode_locator.type = mgb::CompNode::DeviceType::MULTITHREAD;
62 63 64
        if (m_compnode_locator.device == -1) {
            m_compnode_locator.device = m_user_config->device_id;
        }
65 66 67 68 69 70 71 72 73 74 75 76 77
    }
    //! model options
#define ConfigOption(mge_name, lite_name) \
    options.mge_name = m_user_config->options.lite_name;

    auto&& options = m_load_config.comp_graph->options();
    ConfigOption(graph_opt.weight_preprocess, weight_preprocess);
    ConfigOption(graph_opt.fuse_preprocess, fuse_preprocess);
    ConfigOption(fake_next_exec, fake_next_exec);
    ConfigOption(var_sanity_check_first_run, var_sanity_check_first_run);
    m_load_config.const_var_shape = m_user_config->options.const_shape;
    ConfigOption(force_dynamic_alloc, force_dynamic_alloc);
    ConfigOption(force_output_dynamic_alloc, force_output_dynamic_alloc);
78 79 80
    ConfigOption(
            force_output_use_user_specified_memory,
            force_output_use_user_specified_memory);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    ConfigOption(no_profiling_on_shape_change, no_profiling_on_shape_change);
    ConfigOption(graph_opt.jit, jit_level);
    ConfigOption(comp_node_seq_record_level, comp_node_seq_record_level);
    ConfigOption(graph_opt_level, graph_opt_level);
    ConfigOption(async_exec_level, async_exec_level);

#undef ConfigOption
#define ConfigOptionLayoutTransform(name) \
    if (m_user_config->options.name) {    \
        options.graph_opt.name();         \
    }
    ConfigOptionLayoutTransform(enable_nchw44);
    ConfigOptionLayoutTransform(enable_nchw44_dot);
    ConfigOptionLayoutTransform(enable_nchw88);
    ConfigOptionLayoutTransform(enable_nhwcd4);
    ConfigOptionLayoutTransform(enable_nchw4);
    ConfigOptionLayoutTransform(enable_nchw32);
    ConfigOptionLayoutTransform(enable_nchw64);
#undef ConfigOptionLayoutTransform
100 101 102
    if (m_user_config->options.enable_f16_io_comp) {
        options.graph_opt.enable_f16_io_comp();
    }
103 104 105 106
    if (m_user_config->has_compression) {
        m_load_config.tensor_value_loader = decompressed_tensor_value_loader;
    }

107 108
    //! if device is LITE_NONE, the compnode information is stored in model or
    //! xpu in MegEngine
109
    if (device_type != LiteDeviceType::LITE_DEVICE_DEFAULT) {
110 111 112 113 114
        m_load_config.comp_node_mapper = [this](mgb::CompNode::Locator& loc) {
            if (loc.type == mgb::CompNode::DeviceType::UNSPEC) {
                loc.type = m_compnode_locator.type;
            }
            loc.device = m_compnode_locator.device;
115 116
            //! the user configured stream
            auto stream = m_compnode_locator.stream;
117
            //! if user set the thread number and the compnode is multithread
118 119 120 121 122 123 124 125 126 127 128 129 130
            if (loc.type == mgb::CompNode::DeviceType::MULTITHREAD) {
                if (m_nr_threads != 1) {
                    loc.nr_threads = m_nr_threads;
                }
                //! user set the stream to separate the different multithread
                if (stream != 0) {
                    auto device = m_compnode_locator.device;
                    //! the device is also set by user, so combine them to one
                    //! int
                    if (device == -1) {
                        loc.device = stream;
                    }
                }
131 132 133 134
            } else {
                loc.stream = m_compnode_locator.stream;
            }
        };
135 136 137
    }
}

M
Megvii Engine Team 已提交
138
void NetworkImplDft::set_memory_allocator(std::shared_ptr<Allocator> user_allocator) {
139 140 141 142 143 144
    auto allocator = std::make_shared<UserStaticMemAlloc>(user_allocator);
    LITE_ASSERT(m_load_config.comp_graph);
    m_load_config.comp_graph->set_device_memory_allocator(allocator);
}

//! share the runtime memory with other network, the weights is not shared
M
Megvii Engine Team 已提交
145
void NetworkImplDft::share_runtime_memory_with(Network::NetworkImplBase* network_impl) {
146 147
    LITE_ASSERT(network_impl);
    LITE_ASSERT(m_load_config.comp_graph);
M
Megvii Engine Team 已提交
148 149
    m_load_config.comp_graph->share_device_memory_with(*(
            network_impl->cast_final_safe<NetworkImplDft>().m_load_config.comp_graph));
150 151 152
}

void NetworkImplDft::set_cpu_inplace_mode() {
M
Megvii Engine Team 已提交
153 154 155
    LITE_ASSERT(
            m_user_config->device_type == LiteDeviceType::LITE_CPU,
            "cpu inplace mode is only avaliable in CPU.");
156 157 158
    m_is_cpu_inplace_mode = true;
    if (m_compnode_locator.type == mgb::CompNode::DeviceType::CPU) {
        m_compnode_locator.device = mgb::CompNode::Locator::DEVICE_CPU_DEFAULT;
159
        m_user_config->device_id = mgb::CompNode::Locator::DEVICE_CPU_DEFAULT;
160 161 162 163
    } else {
        LITE_ASSERT(
                m_compnode_locator.type == CompNode::DeviceType::MULTITHREAD,
                "cpu inplace mode is only avaliable in CPU.");
M
Megvii Engine Team 已提交
164
        m_compnode_locator.device = mgb::CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
165
        m_user_config->device_id = mgb::CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
166 167 168 169
    }
}

void NetworkImplDft::set_cpu_threads_number(size_t nr_threads) {
M
Megvii Engine Team 已提交
170 171 172
    LITE_ASSERT(
            m_user_config->device_type == LiteDeviceType::LITE_CPU,
            "multi threads mode is only avaliable in CPU.");
173 174 175
    if (nr_threads > 1) {
        m_nr_threads = nr_threads;
        m_compnode_locator.type = mgb::CompNode::DeviceType::MULTITHREAD;
176 177 178 179 180 181
        if (m_is_cpu_inplace_mode) {
            m_compnode_locator.device =
                    mgb::CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
            m_user_config->device_id =
                    mgb::CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
        }
182 183 184 185 186 187
        m_compnode_locator.nr_threads = nr_threads;
    }
}

void NetworkImplDft::set_runtime_thread_affinity(
        const ThreadAffinityCallback& thread_affinity_callback) {
M
Megvii Engine Team 已提交
188 189 190
    LITE_ASSERT(
            m_user_config->device_type == LiteDeviceType::LITE_CPU,
            "multi threads mode is only avaliable in CPU.");
191 192 193 194 195 196 197 198
    mgb::CompNode::Locator loc;
    m_load_config.comp_node_mapper(loc);
    auto cn = mgb::CompNode::load(loc);
    if (m_nr_threads > 1) {
        mgb::CompNodeEnv::from_comp_node(cn).cpu_env().set_affinity(
                thread_affinity_callback);
    } else {
        mgb::CompNodeEnv::from_comp_node(cn).cpu_env().dispatch(
M
Megvii Engine Team 已提交
199
                [thread_affinity_callback](void) { thread_affinity_callback(0); });
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }
}

void NetworkImplDft::set_device_id(int device_id) {
    m_compnode_locator.device = device_id;
    m_user_config->device_id = device_id;
}

void NetworkImplDft::set_stream_id(int stream_id) {
    m_compnode_locator.stream = stream_id;
}

void NetworkImplDft::use_tensorrt() {
    auto&& options = m_load_config.comp_graph->options();
    options.graph_opt.tensorrt = true;
}

//! set the callback in async model
void NetworkImplDft::set_async_callback(const AsyncCallback& callback) {
M
Megvii Engine Team 已提交
219 220 221 222 223
    LITE_ASSERT(!m_is_cpu_inplace_mode, "cpu inplace mode not support async mode");
    LITE_ASSERT(
            m_user_config->device_type == LiteDeviceType::LITE_CPU ||
                    m_user_config->device_type == LiteDeviceType::LITE_CUDA,
            "Now only cpu and cuda>10.0 support async mode");
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    m_async = true;
    m_async_callback = std::move(callback);
}

void NetworkImplDft::make_output_spec() {
    m_output_spec.clear();
    for (auto&& out : m_network_io->outputs) {
        if (m_load_result.output_var_map.count(out.name)) {
            auto&& load_out = m_load_result.output_var_map[out.name];
            auto cb = [&out, this](const mgb::DeviceTensorND& dv) mutable {
                mgb::CompNode comp_node = dv.comp_node();
                if (out.io_type == LiteIOType::LITE_IO_SHAPE) {
                    auto mgb_layout = dv.layout();
                    out.lite_tensor->set_layout(to_lite_layout(mgb_layout));
                } else {
                    TensorHelper::implement(out.lite_tensor)
                            ->cast_final_safe<TensorImplDft>()
                            .copy_from_mge_tensor(dv);
                    out.lite_tensor->update_from_implement();
                }
                if (m_async) {
                    out.have_sync = true;
                    bool need_exec_cb = true;
                    for (auto&& j : m_network_io->outputs) {
                        if (!j.have_sync) {
                            need_exec_cb = false;
                        }
                    }
                    if (need_exec_cb) {
                        for (auto&& j : m_network_io->outputs) {
                            j.have_sync = false;
                        }
                        comp_node.add_callback([this]() { finish(); });
                    }
                }
            };
260 261 262 263 264 265 266
            //! if write to user-specified memory, the CallbackCaller must be nullptr.
            if (m_user_config->options.force_output_use_user_specified_memory ||
                m_user_config->options.force_output_dynamic_alloc) {
                m_output_spec.emplace_back(load_out, nullptr);
            } else {
                m_output_spec.emplace_back(load_out, std::move(cb));
            }
267
        } else {
M
Megvii Engine Team 已提交
268
            LITE_THROW(ssprintf("no output named : %s in the mode", out.name.c_str()));
269 270 271 272
        }
    }
}

273 274 275 276 277 278 279 280
void NetworkImplDft::replace_src_discrete_input_opr_pass() {
    mgb::ThinHashMap<mgb::SymbolVar, mgb::SymbolVar> out_var_map;

    auto dest_with_extra_deps =
            get_dest_vars_with_extra_deps(m_load_result.output_var_list);
    gopt::SubGraph graph{dest_with_extra_deps};
    auto rewriter = graph.make_rewriter();

281 282 283 284 285 286 287 288 289 290 291
    auto on_opr = [&](cg::OperatorNodeBase* opr) {
        bool replace_output = false;
        for (auto inp : opr->input()) {
            if ((inp->owner_opr()->same_type<mgb::opr::Host2DeviceCopy>() ||
                 inp->owner_opr()->same_type<mgb::opr::VolatileSharedDeviceTensor>()) &&
                inp->name() == m_user_config->discrete_input_name) {
                bool is_h2d = true;
                if (inp->owner_opr()->same_type<mgb::opr::Host2DeviceCopy>()) {
                    is_h2d = true;
                } else {
                    is_h2d = false;
292
                }
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

                SymbolVarArray srcs;
                if (is_h2d) {
                    auto h2d = inp->owner_opr();
                    for (auto&& i :
                         get_discrete_tensors(m_user_config->discrete_input_name)) {
                        auto val = TensorHelper::implement(i)
                                           ->cast_final_safe<TensorImplDft>()
                                           .m_host_tensor;
                        LITE_ASSERT(val);
                        srcs.push_back(mgb::opr::Host2DeviceCopy::make(
                                *m_load_result.graph, val, h2d->config()));
                    }
                } else {
                    auto volatiled = inp->owner_opr();
                    for (auto&& i :
                         get_discrete_tensors(m_user_config->discrete_input_name)) {
                        auto val = TensorHelper::implement(i)
                                           ->cast_final_safe<TensorImplDft>()
                                           .m_dev_tensor;
                        LITE_ASSERT(val);
                        srcs.push_back(mgb::opr::VolatileSharedDeviceTensor::make(
                                *m_load_result.graph, val, volatiled->config()));
                    }
317 318
                }

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                if (opr->same_type<mgb::opr::WarpPerspective>()) {
                    auto& warp = opr->cast_final<mgb::opr::WarpPerspective>();
                    SymbolVar new_out;
                    if (opr->input().size() == 3) {
                        new_out = mgb::opr::WarpPerspective::make(
                                srcs, warp.input(1), warp.input(2), warp.param(),
                                warp.config());
                    } else {
                        LITE_ASSERT(opr->input().size() == 4);
                        new_out = mgb::opr::WarpPerspective::make(
                                srcs, warp.input(1), warp.input(2), warp.input(3),
                                warp.param(), warp.config());
                    }
                    rewriter.replace_var(
                            warp.output(0), new_out.node(),
                            "replace WarpPerspective to WarpPerspective multi src "
                            "version.");
                    replace_output = true;
                } else {
                    auto concat = mgb::opr::Concat::make(srcs, 0);
                    rewriter.replace_var(inp, concat.node(), "add a concat opr.");
                }
341
            }
342 343
        }
        if (!replace_output) {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            rewriter.auto_replace_outputs(opr);
        }
    };
    graph.iter(on_opr);
    rewriter.apply_inplace();
    auto new_ovar = graph.endpoint_vars();
    new_ovar.resize(m_load_result.output_var_list.size());

    for (size_t i = 0; i < new_ovar.size(); ++i) {
        out_var_map[m_load_result.output_var_list[i]] = new_ovar[i];
    }
    for (auto&& i : m_load_result.output_var_map) {
        i.second = out_var_map.at(i.second);
    }
    for (auto&& i : m_load_result.output_var_map_id) {
        i.second = out_var_map.at(i.second);
    }
    for (size_t i = 0; i < m_load_result.output_var_list.size(); i++) {
        new_ovar[i].rename(m_load_result.output_var_list[i].node()->name());
    }
    m_load_result.output_var_list = std::move(new_ovar);
}

367 368 369 370 371 372 373 374 375 376 377
void NetworkImplDft::replace_dev_input_pass() {
    mgb::CompNode::Locator locator;
    m_load_config.comp_node_mapper(locator);
    //! CPU is not need use device input
    if (locator.type == mgb::CompNode::DeviceType::CPU) {
        return;
    }
    //! repalce the H2D with VolatileSharedDeviceTensor, and keep the dev tensor
    //! in m_network_io.input, user can directly change the dev tensor
    //! storage through m_network_io.input.lite_tensor->reset() befor forward
    using DeviceTensorMap =
M
Megvii Engine Team 已提交
378
            std::unordered_map<std::string, std::shared_ptr<mgb::DeviceTensorND>>;
379 380 381 382 383 384 385 386
    DeviceTensorMap name2dev_tensor;

    mgb::ThinHashMap<mgb::HostTensorND*, mgb::SymbolVar> host_val2var;

    //! construct host_val2var that maps from host tensor to corresponding var
    auto on_opr = [&](mgb::cg::OperatorNodeBase* opr) {
        if (opr->same_type<mgb::opr::Host2DeviceCopy>()) {
            mgb::HostTensorND* tensor =
M
Megvii Engine Team 已提交
387
                    opr->cast_final<mgb::opr::Host2DeviceCopy>().host_data().get();
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
            host_val2var[tensor] = opr->output(0);
        }
    };
    mgb::cg::DepOprIter dep_iter{on_opr};
    for (auto i : m_load_result.output_var_list) {
        dep_iter.add(i.node()->owner_opr());
    }

    mgb::ThinHashMap<mgb::SymbolVar, mgb::SymbolVar> inp_var_map, out_var_map;

    mgb::SmallVector<std::string> to_clear;
    for (auto&& config_in : m_network_io->inputs) {
        if (!config_in.is_host) {
            auto host_val = m_load_result.tensor_map[config_in.name];
            auto dev_val = TensorHelper::implement(config_in.lite_tensor)
                                   ->cast_final_safe<TensorImplDft>()
                                   .m_dev_tensor;
            auto dev_var = mgb::opr::VolatileSharedDeviceTensor::make(
                    *m_load_result.graph, dev_val, {config_in.name});
            inp_var_map[host_val2var.at(host_val.get())] = dev_var;
            name2dev_tensor[config_in.name] = dev_val;
        }
410 411 412 413
        //! reset lite_tensor in discrete mode
        if (config_in.name == m_user_config->discrete_input_name) {
            config_in.lite_tensor.reset();
        }
414
    }
M
Megvii Engine Team 已提交
415
    auto new_ovar = mgb::cg::replace_vars(m_load_result.output_var_list, inp_var_map);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    for (size_t i = 0; i < new_ovar.size(); ++i) {
        out_var_map[m_load_result.output_var_list[i]] = new_ovar[i];
    }
    for (auto&& i : m_load_result.output_var_map) {
        i.second = out_var_map.at(i.second);
    }
    for (auto&& i : m_load_result.output_var_map_id) {
        i.second = out_var_map.at(i.second);
    }
    for (size_t i = 0; i < m_load_result.output_var_list.size(); i++) {
        new_ovar[i].rename(m_load_result.output_var_list[i].node()->name());
    }
    m_load_result.output_var_list = std::move(new_ovar);
}

void NetworkImplDft::cross_compnode_model_detect() {
    mgb::ThinHashSet<LiteDeviceType> nr_used_device_type;
    auto on_opr = [&](mgb::cg::OperatorNodeBase* opr) {
        for (auto j : opr->output()) {
            if (j->comp_node() != mgb::CompNode::default_cpu()) {
                nr_used_device_type.insert(
                        get_device_from_locator(j->comp_node().locator()));
            }
        }
    };
    mgb::cg::DepOprIter dep_iter{on_opr};
    for (auto i : m_load_result.output_var_list) {
        dep_iter.add(i.node()->owner_opr());
    }
M
Megvii Engine Team 已提交
445
    m_nr_device_type = nr_used_device_type.size();
446 447
}

448
void NetworkImplDft::layout_transform_optimization() {
449
    if (m_set_layout_transform) {
450 451
        mgb::ThinHashMap<mgb::SymbolVar, mgb::SymbolVar> out_var_map;
        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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    } else if (m_user_config->auto_optimize_inference) {
        //! set model weight preprocess
        m_load_config.comp_graph->options().graph_opt.weight_preprocess = true;
        LITE_LOG(
                "weight_preprocess is enabled, this maybe use more memory when "
                "infernece.");
        //! get the current format and data type of the model
        bool is_model_nchw = true;
        //! is any convolution is int8
        bool is_model_int8 = false;
        //! is all convolution is float32
        bool is_model_float32 = true;
        float conv_cnt = 0;
        float dimshuffle_cnt = 0;

        auto detect_int8_model = [&](const VarNode* input) {
            if (input->dtype().enumv() == megdnn::DTypeEnum::QuantizedS8 ||
                input->dtype().enumv() == megdnn::DTypeEnum::Quantized8Asymm) {
                is_model_int8 = true;
                is_model_float32 = false;
            } else if (input->dtype().enumv() == megdnn::DTypeEnum::Float32) {
                is_model_float32 = (is_model_float32 && true);
            } else {
                is_model_float32 = false;
            }
        };

        cg::DepOprIter dep([&](cg::OperatorNodeBase* opr) {
            if (auto conv = opr->try_cast_final<opr::ConvolutionForward>()) {
                if (conv->param().format != megdnn::param::ConvBias::Format::NCHW) {
                    is_model_nchw = false;
                }
                conv_cnt++;
                detect_int8_model(conv->input(0));
            } else if (auto conv_bias = opr->try_cast_final<opr::ConvBias>()) {
                if (conv_bias->param().format !=
                    megdnn::param::ConvBias::Format::NCHW) {
                    is_model_nchw = false;
                }
                conv_cnt++;
                detect_int8_model(conv->input(0));
            } else if (auto dimshuffle = opr->try_cast_final<opr::Dimshuffle>()) {
                LITE_MARK_USED_VAR(dimshuffle);
                dimshuffle_cnt++;
            }
        });
        for (auto&& i : m_load_result.output_var_list)
            dep.add(i);

        float radio_dimshuffle_conv = 0;
        if (conv_cnt > 0) {
            radio_dimshuffle_conv = dimshuffle_cnt / conv_cnt;
        }
        //! format optimize can only applied on nchw model,
        //! shufflenet like model will hurt the performance when using nchw88 or nchw44
        //! format, here just heuristically decide the gate radio of
        //! dimshuffle and convolution
        if (!is_model_nchw || radio_dimshuffle_conv > 0.15f) {
            return;
        }

        //! determine the layout by the device information
        //! TODO: shufflenet like model use nchw88 or nchw44 will hurt the
        //! performance
        if (m_user_config->device_type == LITE_CPU) {
#if defined(MGB_ENABLE_CPUINFO_CHECK) && MGB_ENABLE_CPUINFO
            cpuinfo_initialize();
            //! if all convolution and matmul data type is float32
            if (is_model_float32) {
                //! if device is x86
                //! if x86 support avx, use format nchw88
                if (cpuinfo_has_x86_avx()) {
                    m_load_config.comp_graph->options().graph_opt.enable_nchw88();
                    LITE_LOG("Configure model inference with nchw88 format.");
                } else if (cpuinfo_has_x86_sse2() && !cpuinfo_has_x86_sse3()) {
                    //! if x86 only support sse2, use format nchw44
                    m_load_config.comp_graph->options().graph_opt.enable_nchw44();
                    LITE_LOG("Configure model inference with nchw44 format.");
                } else if (cpuinfo_has_arm_neon()) {
                    //! if device is arm, use format nchw44
                    m_load_config.comp_graph->options().graph_opt.enable_nchw44();
                    LITE_LOG("Configure model inference with nchw44 format.");
                }
            } else if (is_model_int8) {
                //! if date type of convolution  is int8
                //! if device is arm and support dot, use nchw44-dot format
                if (cpuinfo_has_arm_neon() && cpuinfo_has_arm_neon_dot()) {
                    m_load_config.comp_graph->options().graph_opt.enable_nchw44_dot();
                    LITE_LOG("Configure model inference with nchw44-dot format.");
                } else if (cpuinfo_has_arm_neon()) {
                    //! if device is arm and do not support dot, use nchw44 format
                    m_load_config.comp_graph->options().graph_opt.enable_nchw44();
                    LITE_LOG("Configure model inference with nchw44 format.");
                }
            }
#endif
        }
551 552 553
    }
}

554 555 556 557
void NetworkImplDft::load_model(
        std::shared_ptr<void> model_mem, size_t size,
        std::unordered_map<std::string, LiteAny> separate_config_map) {
    if (!m_loader) {
M
Megvii Engine Team 已提交
558 559
        m_input_file =
                mgb::serialization::InputFile::make_mem_proxy(model_mem, size, false);
560
        m_format = mgb::serialization::GraphLoader::identify_graph_dump_format(
M
Megvii Engine Team 已提交
561
                *m_input_file);
562
        if (!m_format.valid()) {
563 564 565
            LITE_THROW("invalid model format");
        }
        m_loader = mgb::serialization::GraphLoader::make(
566
                std::move(m_input_file), m_format.val());
567 568 569 570 571 572 573
    }

    //! applay the user configration to mge model
    application_config();

    //! config some flag get from json config file
    if (separate_config_map.find("device_id") != separate_config_map.end()) {
574
        set_device_id(separate_config_map["device_id"].safe_cast<int>());
575
    }
M
Megvii Engine Team 已提交
576
    if (separate_config_map.find("number_threads") != separate_config_map.end() &&
577
        separate_config_map["number_threads"].safe_cast<uint32_t>() > 1) {
578
        set_cpu_threads_number(
579
                separate_config_map["number_threads"].safe_cast<uint32_t>());
580
    }
M
Megvii Engine Team 已提交
581
    if (separate_config_map.find("enable_inplace_model") != separate_config_map.end() &&
582
        separate_config_map["enable_inplace_model"].safe_cast<bool>()) {
583 584 585
        set_cpu_inplace_mode();
    }
    if (separate_config_map.find("use_tensorrt") != separate_config_map.end() &&
586
        separate_config_map["use_tensorrt"].safe_cast<bool>()) {
587 588 589
        use_tensorrt();
    }

590
    m_load_result = m_loader->load(m_load_config, true);
591 592
    configure_after_loaded();
}
593

594
void NetworkImplDft::configure_after_loaded() {
595 596
    modify_exection_policy();

597
    layout_transform_optimization();
598

599
    //! find how many compnode the model has, this should call before update_io
600 601 602
    cross_compnode_model_detect();

    //! update the IO of the network
M
Megvii Engine Team 已提交
603 604 605 606 607 608
    update_input();
    replace_dev_input_pass();
    if (!m_user_config->discrete_input_name.empty()) {
        replace_src_discrete_input_opr_pass();
    }
    update_output();
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

    //! replace the IO when there is device input or output
    compile_graph();
}

void NetworkImplDft::compile_graph() {
    make_output_spec();
    m_execute_func = m_load_result.graph_compile(m_output_spec);
}

void NetworkImplDft::start() const {
    if (m_start_callback) {
        std::unordered_map<std::string, std::pair<IO, std::shared_ptr<Tensor>>>
                input_io_map;
        for (auto&& io_inner : m_network_io->inputs) {
            input_io_map[io_inner.name] = {
                    IO{io_inner.name, io_inner.is_host, io_inner.io_type,
                       io_inner.config_layout},
                    io_inner.lite_tensor};
        }
        m_start_callback(input_io_map);
    }
}

void NetworkImplDft::forward() {
    start();
635 636 637 638
    if (m_load_config.comp_graph &&
        m_user_config->options.comp_node_seq_record_level == 2) {
        m_load_config.comp_graph.reset();
    }
639 640 641 642 643 644 645 646 647 648 649 650 651
    LITE_ASSERT(m_execute_func, "forward must be called after network loaded.");
    m_execute_func->execute();
}

void NetworkImplDft::wait() {
    if (!m_async) {
        m_execute_func->wait();
    }
    finish();
}

void NetworkImplDft::finish() const {
    if (m_async) {
M
Megvii Engine Team 已提交
652
        LITE_ASSERT(m_async_callback, "The callback func must set when async mode.");
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
        m_async_callback();
    }
    if (m_finish_callback) {
        std::unordered_map<std::string, std::pair<IO, std::shared_ptr<Tensor>>>
                output_io_map;
        for (auto&& io_inner : m_network_io->outputs) {
            output_io_map[io_inner.name] = {
                    IO{io_inner.name, io_inner.is_host, io_inner.io_type,
                       io_inner.config_layout},
                    io_inner.lite_tensor};
        }
        m_finish_callback(output_io_map);
    }
    output_plugin_result();
}

void NetworkImplDft::set_io(const NetworkIO& network_io) {
    for (auto&& in : network_io.inputs) {
        m_network_io->inputs.emplace_back(in);
    }
    for (auto&& out : network_io.outputs) {
        m_network_io->outputs.emplace_back(out);
    }
}

678
void NetworkImplDft::try_infer_tensor_layout(std::shared_ptr<Tensor> tensor, Var var) {
679 680 681 682
    using InferType = mgb::cg::static_infer::InferType;
    auto&& static_infer_mgr = m_load_config.comp_graph->static_infer_manager();
    if (static_infer_mgr.get_infer_type(var.node()).shape &
        (InferType::CONST | InferType::RT_STATIC)) {
683 684 685 686 687 688
        auto shape = static_infer_mgr.infer_shape_fallible(var.node());
        if (!shape) {
            LITE_WARN(
                    "Lite infer output shape failed, maybe the model is "
                    "dynamic "
                    "shape.\n");
689 690 691 692
            LITE_ASSERT(
                    !m_user_config->options.force_output_use_user_specified_memory,
                    "force_output_use_user_specified_memory can't be used when output "
                    "shape can't be derived.");
693 694
            return;
        }
695
        Layout layout = to_lite_layout(TensorLayout{*shape, var.dtype()});
696 697 698 699
        tensor->set_layout(layout);
    }
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
void NetworkImplDft::update_io() {
    update_input();
    update_output();
}

void NetworkImplDft::update_input() {
    auto device_type = m_user_config->device_type;
    auto device_id = m_compnode_locator.device;
    auto stream_id = m_compnode_locator.stream;
    //! if cpu all input and output are host
    if (device_type == LiteDeviceType::LITE_CPU) {
        for (auto&& in : m_network_io->inputs) {
            in.is_host = true;
        }
    }
    //! if cross compnode model, modify the device input if it is not valid
    if (m_nr_device_type > 1) {
        for (auto&& in_tensor_iter : m_load_result.tensor_map) {
            for (auto&& config_in : m_network_io->inputs) {
                //! if tensor is set to device input
M
Megvii Engine Team 已提交
720
                if (in_tensor_iter.first == config_in.name && !config_in.is_host) {
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
                    //! if the origin compnode of the tensor is not the device,
                    //! set the input to host
                    if (get_device_from_locator(
                                in_tensor_iter.second->comp_node().locator()) ==
                        LiteDeviceType::LITE_CPU) {
                        config_in.is_host = true;
                        LITE_WARN(
                                "The input tensor %s of the cross device model "
                                "should not from device.",
                                config_in.name.c_str());
                    }
                }
            }
        }
    }
    for (auto&& in_tensor_iter : m_load_result.tensor_map) {
        bool found = false;
        for (auto&& config_in : m_network_io->inputs) {
            if (in_tensor_iter.first == config_in.name) {
                found = true;
                if (config_in.is_host) {
                    config_in.lite_tensor = std::make_shared<Tensor>(
                            device_id, stream_id, device_type, true);
                    TensorHelper::implement(config_in.lite_tensor)
                            ->cast_final_safe<TensorImplDft>()
                            .m_host_tensor = in_tensor_iter.second;
                    config_in.lite_tensor->update_from_implement();
                } else {
M
Megvii Engine Team 已提交
749 750
                    config_in.lite_tensor =
                            std::make_shared<Tensor>(device_id, stream_id, device_type);
751 752 753
                    config_in.lite_tensor->set_layout(
                            to_lite_layout(in_tensor_iter.second->layout()));
                }
754 755 756 757
                TensorHelper::implement(config_in.lite_tensor)
                        ->cast_final_safe<TensorImplDft>()
                        .m_record_reset =
                        m_user_config->options.comp_node_seq_record_level > 0;
758
                if (config_in.config_layout.ndim &&
M
Megvii Engine Team 已提交
759
                    !(config_in.config_layout == config_in.lite_tensor->get_layout())) {
760 761 762 763 764 765 766
                    config_in.lite_tensor->set_layout(config_in.config_layout);
                }
            }
        }
        if (!found) {
            IOInner io_in;
            io_in.name = in_tensor_iter.first;
M
Megvii Engine Team 已提交
767 768
            io_in.lite_tensor =
                    std::make_shared<Tensor>(device_id, stream_id, device_type, true);
769 770 771
            TensorHelper::implement(io_in.lite_tensor)
                    ->cast_final_safe<TensorImplDft>()
                    .m_host_tensor = in_tensor_iter.second;
772 773 774 775
            TensorHelper::implement(io_in.lite_tensor)
                    ->cast_final_safe<TensorImplDft>()
                    .m_record_reset =
                    m_user_config->options.comp_node_seq_record_level > 0;
776 777 778 779
            io_in.lite_tensor->update_from_implement();
            m_network_io->inputs.push_back(io_in);
        }
    }
780 781 782 783 784

    if (!m_user_config->discrete_input_name.empty()) {
        update_input_lite_tensors();
    }

785
    //! delete the IO that is not the network
M
Megvii Engine Team 已提交
786
    for (auto it = m_network_io->inputs.begin(); it != m_network_io->inputs.end();) {
787
        if (it->lite_tensor == nullptr) {
M
Megvii Engine Team 已提交
788
            LITE_LOG("%s is not the network input, ignore it.", it->name.c_str());
789 790 791 792 793 794 795
            it = m_network_io->inputs.erase(it);
        } else {
            it++;
        }
    }
}

796
//! initialization lite_tensors when input is composed of discrete multiple tensors
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
void NetworkImplDft::update_input_lite_tensors() {
    auto device_type = m_user_config->device_type;
    auto device_id = m_compnode_locator.device;
    auto stream_id = m_compnode_locator.stream;

    for (auto&& in_tensor_iter : m_load_result.tensor_map) {
        if (in_tensor_iter.first != m_user_config->discrete_input_name) {
            continue;
        }
        for (auto&& config_in : m_network_io->inputs) {
            if (in_tensor_iter.first == config_in.name) {
                size_t bs = in_tensor_iter.second->shape(0);
                auto shape = in_tensor_iter.second->shape();
                if (config_in.config_layout.ndim) {
                    bs = config_in.config_layout.shapes[0];
812 813 814
                    for (size_t i = 0; i < config_in.config_layout.ndim; ++i) {
                        shape.shape[i] = config_in.config_layout.shapes[i];
                    }
815
                }
816
                shape.shape[0] = 1;
817
                for (size_t i = 0; i < bs; ++i) {
818 819 820 821
                    HostTensorND tensor(
                            in_tensor_iter.second->comp_node(), shape,
                            in_tensor_iter.second->dtype(),
                            in_tensor_iter.second->format());
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
                    if (config_in.is_host) {
                        config_in.lite_tensors.push_back(std::make_shared<Tensor>(
                                device_id, stream_id, device_type, true));
                        TensorHelper::implement(config_in.lite_tensors[i])
                                ->cast_final_safe<TensorImplDft>()
                                .m_host_tensor = std::make_shared<HostTensorND>(tensor);
                        config_in.lite_tensors[i]->update_from_implement();
                    } else {
                        config_in.lite_tensors.push_back(std::make_shared<Tensor>(
                                device_id, stream_id, device_type));
                        config_in.lite_tensors[i]->set_layout(
                                to_lite_layout(tensor.layout()));
                    }
                    TensorHelper::implement(config_in.lite_tensors[i])
                            ->cast_final_safe<TensorImplDft>()
                            .m_record_reset =
                            m_user_config->options.comp_node_seq_record_level > 0;
                }
            }
        }
    }
}

845 846 847 848 849 850 851 852 853 854 855 856
void NetworkImplDft::update_output() {
    auto device_type = m_user_config->device_type;
    auto device_id = m_compnode_locator.device;
    auto stream_id = m_compnode_locator.stream;
    if (device_type == LiteDeviceType::LITE_CPU) {
        for (auto&& out : m_network_io->outputs) {
            out.is_host = true;
        }
    }
    //! delete the output that is not the network
    for (auto out_it = m_network_io->outputs.begin();
         out_it != m_network_io->outputs.end();) {
M
Megvii Engine Team 已提交
857 858
        if (std::find_if(
                    m_load_result.output_var_list.begin(),
859
                    m_load_result.output_var_list.end(), [out_it](const SymbolVar var) {
M
Megvii Engine Team 已提交
860 861 862
                        return var.node()->name() == out_it->name;
                    }) == m_load_result.output_var_list.end()) {
            LITE_LOG("%s is not the network output, ignore it.", out_it->name.c_str());
863 864 865 866 867 868 869
            out_it = m_network_io->outputs.erase(out_it);
        } else {
            out_it++;
        }
    }
    //! user config the output tensor, so only compute the config output
    if (m_compute_configured_output_only) {
M
Megvii Engine Team 已提交
870 871 872
        LITE_ASSERT(
                m_network_io->outputs.size() > 0,
                "compute configured output only with no configure output.");
873 874 875 876 877 878 879
        for (auto out_it = m_network_io->outputs.begin();
             out_it != m_network_io->outputs.end(); out_it++) {
            //! use pinned memory to copy form device
            if (out_it->is_host) {
                out_it->lite_tensor = std::make_shared<Tensor>(
                        device_id, stream_id, device_type, true);
            } else {
M
Megvii Engine Team 已提交
880 881
                out_it->lite_tensor =
                        std::make_shared<Tensor>(device_id, stream_id, device_type);
882
            }
883
            SymbolVar var;
884 885 886 887 888 889 890
            for (auto&& out_var : m_load_result.output_var_list) {
                if (out_var.node()->name() == out_it->name) {
                    var = out_var;
                    break;
                }
            }
            try_infer_tensor_layout(out_it->lite_tensor, var);
891
            output_tensor_copy_optimize(var, out_it->lite_tensor);
892 893 894 895
            TensorHelper::implement(out_it->lite_tensor)
                    ->cast_final_safe<TensorImplDft>()
                    .m_record_reset =
                    m_user_config->options.comp_node_seq_record_level > 0;
896 897 898 899
        }
        //! user not set, use default output
    } else {
        for (auto&& out : m_load_result.output_var_list) {
900
            std::shared_ptr<Tensor> lite_tensor = nullptr;
M
Megvii Engine Team 已提交
901
            auto device = get_device_from_locator(out.node()->comp_node().locator());
M
Megvii Engine Team 已提交
902 903 904
            auto it = std::find_if(
                    m_network_io->outputs.begin(), m_network_io->outputs.end(),
                    [&out](const IOInner io) { return io.name == out.node()->name(); });
905 906 907
            if (it != m_network_io->outputs.end()) {
                if (it->is_host) {
                    it->lite_tensor = std::make_shared<Tensor>(
M
Megvii Engine Team 已提交
908
                            device_id, stream_id, device, true);
909
                } else {
M
Megvii Engine Team 已提交
910
                    it->lite_tensor =
M
Megvii Engine Team 已提交
911
                            std::make_shared<Tensor>(device_id, stream_id, device);
912
                }
913
                try_infer_tensor_layout(it->lite_tensor, out);
914
                lite_tensor = it->lite_tensor;
915 916 917
            } else {
                IOInner output;
                output.name = out.node()->name();
M
Megvii Engine Team 已提交
918 919
                output.lite_tensor =
                        std::make_shared<Tensor>(device_id, stream_id, device, true);
920
                m_network_io->outputs.push_back({output});
921
                try_infer_tensor_layout(output.lite_tensor, out);
922
                lite_tensor = output.lite_tensor;
923
            }
924
            output_tensor_copy_optimize(out, lite_tensor);
925 926 927 928
            TensorHelper::implement(lite_tensor)
                    ->cast_final_safe<TensorImplDft>()
                    .m_record_reset =
                    m_user_config->options.comp_node_seq_record_level > 0;
929 930 931 932
        }
    }
}

933 934
void NetworkImplDft::output_tensor_copy_optimize(
        Var var, std::shared_ptr<Tensor> tensor) {
935 936 937 938 939 940 941
    size_t index;
    for (index = 0; index < m_load_result.output_var_list.size(); ++index) {
        if (m_load_result.output_var_list[index].node() == var.node()) {
            break;
        }
    }
    LITE_ASSERT(index != m_load_result.output_var_list.size());
942 943 944 945 946 947
    LITE_ASSERT(
            !(m_user_config->options.force_output_use_user_specified_memory &&
              m_user_config->options.force_output_dynamic_alloc),
            "Can't set force_output_use_user_specified_memory and "
            "force_output_dynamic_alloc at the same time.");
    if (m_user_config->options.force_output_use_user_specified_memory) {
948
        bool in_record = m_user_config->options.comp_node_seq_record_level > 0;
949 950
        TensorHelper::implement(tensor)
                ->cast_final_safe<TensorImplDft>()
951 952 953 954 955 956 957 958 959 960 961 962 963 964
                .set_reset_callback(
                        [this, index, in_record](TensorImplDft* dft_tensor) {
                            auto var = this->m_load_result.output_var_list[index];
                            dft_tensor->device_share_host_memory();
                            auto dv = dft_tensor->dev_tensor().get();
                            dv->comp_node(var.node()->comp_node(), true);
                            var.node()->init_mem_plan(dv);
                            if (in_record) {
                                auto&& device_tensor = var.node()->mutable_dev_tensor();
                                device_tensor.only_reset_raw_storage(dv->storage());
                            } else {
                                var.node()->reset_dev_tensor_from_tensor(*dv);
                            }
                        });
965 966 967 968
    }
    if (m_user_config->options.force_output_dynamic_alloc) {
        TensorHelper::implement(tensor)
                ->cast_final_safe<TensorImplDft>()
969 970
                .set_get_memory_callback([this, index](TensorImplDft* dft_tensor) {
                    auto var = this->m_load_result.output_var_list[index];
971 972 973 974 975 976 977 978 979 980 981 982
                    if (dft_tensor->is_host()) {
                        auto host_tensor = dft_tensor->m_host_tensor;
                        *host_tensor =
                                HostTensorND::make_proxy(var.node()->dev_tensor());
                    } else {
                        auto dev_tensor = dft_tensor->m_dev_tensor;
                        *dev_tensor = var.node()->dev_tensor();
                    }
                });
    }
}

M
Megvii Engine Team 已提交
983 984 985
std::shared_ptr<Tensor> NetworkImplDft::get_io_tensor(
        std::string io_name, LiteTensorPhase phase) {
    if (phase == LiteTensorPhase::LITE_INPUT || phase == LiteTensorPhase::LITE_IO) {
986 987
        for (auto&& config_in : m_network_io->inputs) {
            if (io_name == config_in.name) {
988 989 990 991 992 993 994 995 996
                if (config_in.lite_tensor) {
                    return config_in.lite_tensor;
                } else {
                    LITE_THROW(mgb::ssprintf(
                            "%s input tensor is in discrete mode, you can use "
                            "get_discrete_tensors to get this input.",
                            io_name.c_str()));
                    return nullptr;
                }
997 998 999
            }
        }
    }
M
Megvii Engine Team 已提交
1000
    if (phase == LiteTensorPhase::LITE_OUTPUT || phase == LiteTensorPhase::LITE_IO) {
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
        for (auto&& config_out : m_network_io->outputs) {
            if (io_name == config_out.name) {
                config_out.lite_tensor->update_from_implement();
                return config_out.lite_tensor;
            }
        }
    }
    LITE_THROW(mgb::ssprintf(
            "tensor name must be %s input tensor name or the registered "
            "output tensor name if NetworkIO is set, if NetworkIO is not set, "
            "the output tensor is all the network output tensor, or the output "
            "tensor is only the registered tensor.",
            io_name.c_str()));
    return nullptr;
}

1017
std::vector<std::shared_ptr<Tensor>> NetworkImplDft::get_discrete_tensors(
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
        std::string io_name, LiteTensorPhase phase) {
    if (phase == LiteTensorPhase::LITE_INPUT) {
        for (auto&& config_in : m_network_io->inputs) {
            if (io_name == config_in.name &&
                config_in.name == m_user_config->discrete_input_name) {
                return config_in.lite_tensors;
            }
        }
    }
    LITE_THROW(mgb::ssprintf(
            "tensor name must be %s input tensor name.", io_name.c_str()));
    return {};
}

1032 1033 1034 1035
std::shared_ptr<Tensor> NetworkImplDft::get_input_tensor(size_t index) {
    return get_io_tensor(get_input_name(index));
}

1036
std::vector<std::shared_ptr<Tensor>> NetworkImplDft::get_input_tensors(size_t index) {
1037
    return get_discrete_tensors(get_input_name(index));
1038 1039
}

1040 1041 1042 1043 1044
std::shared_ptr<Tensor> NetworkImplDft::get_output_tensor(size_t index) {
    return get_io_tensor(get_output_name(index));
}

//! set opr algorithm selection strategy in the network
M
Megvii Engine Team 已提交
1045 1046 1047
void NetworkImplDft::set_network_algo_policy(
        LiteAlgoSelectStrategy strategy, uint32_t shared_batch_size,
        bool binary_equal_between_batch) {
1048 1049
    using S = megdnn::param::ExecutionPolicy::Strategy;
    auto dst_strategy = static_cast<S>(0);
M
Megvii Engine Team 已提交
1050
    if (static_cast<uint32_t>(strategy) & LiteAlgoSelectStrategy::LITE_ALGO_HEURISTIC) {
1051 1052
        dst_strategy = dst_strategy | S::HEURISTIC;
    }
M
Megvii Engine Team 已提交
1053
    if (static_cast<uint32_t>(strategy) & LiteAlgoSelectStrategy::LITE_ALGO_PROFILE) {
1054 1055 1056 1057 1058 1059
        dst_strategy = dst_strategy | S::PROFILE;
    }
    if (static_cast<uint32_t>(strategy) &
        LiteAlgoSelectStrategy::LITE_ALGO_REPRODUCIBLE) {
        dst_strategy = dst_strategy | S::REPRODUCIBLE;
    }
M
Megvii Engine Team 已提交
1060
    if (static_cast<uint32_t>(strategy) & LiteAlgoSelectStrategy::LITE_ALGO_OPTIMIZED) {
1061 1062
        dst_strategy = dst_strategy | S::OPTIMIZED;
    }
1063 1064
    if (static_cast<uint32_t>(dst_strategy) != 0)
        m_execution_policy = dst_strategy;
1065

M
Megvii Engine Team 已提交
1066
    auto&& fast_run_config = m_load_config.comp_graph->options().fast_run_config;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    fast_run_config.binary_equal_between_batch = binary_equal_between_batch;
    fast_run_config.shared_batch_size = shared_batch_size;

    if (m_execute_func) {
        LITE_WARN(
                "set_network_algo_policy maybe cause error after loaded "
                "network!!!!");
        modify_exection_policy();
    }
}

void NetworkImplDft::modify_exection_policy() {
1079 1080
    auto& vars = m_load_result.output_var_list;
    if (static_cast<uint32_t>(m_execution_policy) != 0) {
1081
        mgb::gopt::modify_opr_algo_strategy_inplace(vars, m_execution_policy);
1082
    }
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
}

//! set opr algorithm selection strategy in the network
void NetworkImplDft::set_network_algo_workspace_limit(size_t workspace_limit) {
    mgb::SymbolVarArray vars;
    for (auto i : m_output_spec) {
        vars.push_back(i.first);
    }
    mgb::gopt::set_opr_algo_workspace_limit_inplace(vars, workspace_limit);
}

//! get the input tensor name in the order of graph
std::vector<const char*> NetworkImplDft::get_all_output_name() const {
    std::vector<const char*> output_names;
    for (auto& output : m_network_io->outputs) {
        output_names.push_back(output.name.c_str());
    }
    return output_names;
}

//! get the input tensor name in the order of graph
std::vector<const char*> NetworkImplDft::get_all_input_name() const {
    std::vector<const char*> input_names;
    for (auto& input : m_load_result.tensor_map) {
        input_names.push_back(input.first.c_str());
    }
    return input_names;
}

//! get the output tensor name in the order of graph
const char* NetworkImplDft::get_output_name(size_t index) const {
    LITE_ASSERT(
            index < m_load_result.output_var_list.size(),
            "The output tensor index is large than the total outputs number.");
    return m_load_result.output_var_list[index].node()->name().c_str();
}

//! get the input tensor name in the order of graph
const char* NetworkImplDft::get_input_name(size_t index) const {
    LITE_ASSERT(
            index < m_load_result.tensor_map.size(),
            "The input tensor index is large than the total inputs number.");
    size_t i = 0;
    for (auto& input : m_load_result.tensor_map) {
        if (i == index) {
            return input.first.c_str();
        }
        i++;
    }
    LITE_THROW(ssprintf("no input tensor of index %zu.", index));
}

//! Plugin part
void NetworkImplDft::enable_profile_performance(std::string profile_json_file) {
#if MGB_ENABLE_JSON
M
Megvii Engine Team 已提交
1138
    m_profiler = std::make_unique<mgb::GraphProfiler>(m_load_config.comp_graph.get());
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    m_profiler_output_file = profile_json_file;
#else
    LITE_MARK_USED_VAR(profile_json_file);
    LITE_THROW("JSON is disable at compile time.");
#endif
}

void NetworkImplDft::enable_io_txt_dump(std::string io_txt_out_file) {
    auto iodump = std::make_unique<mgb::TextOprIODump>(
            m_load_config.comp_graph.get(), io_txt_out_file.c_str());
    iodump->print_addr(false);
    m_iodump = std::move(iodump);
}

void NetworkImplDft::enable_io_bin_dump(std::string io_bin_out_dir) {
    m_iodump = std::make_unique<mgb::BinaryOprIODump>(
            m_load_config.comp_graph.get(), io_bin_out_dir.c_str());
}

void inline NetworkImplDft::output_plugin_result() const {
#if MGB_ENABLE_JSON
    if (m_profiler && m_execute_func) {
        m_profiler->to_json_full(m_execute_func.get())
                ->writeto_fpath(m_profiler_output_file);
    }
#endif
}
1166 1167 1168 1169 1170 1171 1172

void NetworkImplDft::get_static_memory_alloc_info(const std::string& log_dir) const {
#ifndef __IN_TEE_ENV__
#if MGB_ENABLE_JSON
    m_execute_func->get_static_memory_alloc_info(log_dir);
    return;
#endif
1173
#endif
1174 1175
    LITE_MARK_USED_VAR(log_dir);
}
1176

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
void NetworkImplDft::enable_global_layout_transform() {
    m_layout_transform_target = mgb::gopt::GraphTuningOptions::Target::UNSPEC;

    switch (m_user_config->device_type) {
        case LiteDeviceType::LITE_CPU:
            m_layout_transform_target = mgb::gopt::GraphTuningOptions::Target::CPU;
            break;
        case LiteDeviceType::LITE_CUDA:
            m_layout_transform_target = mgb::gopt::GraphTuningOptions::Target::CUDA;
            break;
        default:
            m_layout_transform_target = mgb::gopt::GraphTuningOptions::Target::UNSPEC;
            LITE_WARN(
                    "lite compnode type: enum value: %d. is unspecial for layout "
                    "transform",
                    (int)(m_user_config->device_type));
    }
    m_set_layout_transform = true;
}

void NetworkImplDft::dump_layout_transform_model(std::string optimized_model_path) {
    if (m_set_layout_transform) {
        auto out_file = mgb::serialization::OutputFile::make_fs(
                optimized_model_path.c_str(), 'w');
        using DumpConfig = mgb::serialization::GraphDumper::DumpConfig;
        DumpConfig config{1, false, false};
        auto dumper = mgb::serialization::GraphDumper::make(
                std::move(out_file), m_format.val());
        dumper->dump(m_load_result.output_var_list, config);
    } else {
        LITE_THROW(
                ssprintf("dump layout transform model should call "
                         "enable_global_layout_transform before"));
    }
}
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281

NetworkIO lite::get_model_io_info_dft(
        const std::string& model_path, const Config& config) {
    FILE* fin = fopen(model_path.c_str(), "rb");
    LITE_ASSERT(fin, "failed to open %s: %s", model_path.c_str(), strerror(errno));
    fseek(fin, 0, SEEK_END);
    size_t size = ftell(fin);
    fseek(fin, 0, SEEK_SET);
    void* ptr = malloc(size);
    std::shared_ptr<void> buf{ptr, ::free};
    auto nr = fread(buf.get(), 1, size, fin);
    LITE_ASSERT(nr == size);
    fclose(fin);
    return get_model_io_info_dft(ptr, size, config);
}

NetworkIO lite::get_model_io_info_dft(
        const void* model_mem, size_t size, const Config& config) {
    std::shared_ptr<void> model{const_cast<void*>(model_mem), [](void*) {}};
    auto input_file = mgb::serialization::InputFile::make_mem_proxy(model, size, false);
    auto format =
            mgb::serialization::GraphLoader::identify_graph_dump_format(*input_file);
    if (!format.valid()) {
        LITE_THROW("invalid model format");
    }
    auto loader =
            mgb::serialization::GraphLoader::make(std::move(input_file), format.val());

    mgb::serialization::GraphLoadConfig load_config;
    load_config.comp_graph = mgb::ComputingGraph::make();
    if (config.has_compression) {
        load_config.tensor_value_loader = decompressed_tensor_value_loader;
    }
    auto compnode_locator = to_compnode_locator(config.device_type);
    load_config.comp_node_mapper = [=](mgb::CompNode::Locator& loc) {
        if (loc.type == mgb::CompNode::DeviceType::UNSPEC) {
            loc.type = compnode_locator.type;
        }
        loc.device = compnode_locator.device;
    };
    auto load_result = loader->load(load_config, true);
    NetworkIO IOs;
    for (auto&& in_tensor_iter : load_result.tensor_map) {
        IO in_io;
        in_io.name = in_tensor_iter.first;
        in_io.config_layout = to_lite_layout(in_tensor_iter.second->layout());
        IOs.inputs.push_back(in_io);
    }
    auto infer_shape = [=](mgb::cg::SymbolVar var) -> const megdnn::TensorShape* {
        auto&& static_infer_mgr = load_config.comp_graph->static_infer_manager();
        using InferType = mgb::cg::static_infer::InferType;
        if (static_infer_mgr.get_infer_type(var.node()).shape &
            (InferType::CONST | InferType::RT_STATIC)) {
            return static_infer_mgr.infer_shape_fallible(var.node());
        } else {
            return nullptr;
        }
    };
    for (auto&& out : load_result.output_var_list) {
        IO out_io;
        out_io.name = out.node()->name();
        if (auto shape = infer_shape(out)) {
            out_io.config_layout = to_lite_layout(TensorLayout{*shape, out.dtype()});
        } else {
            out_io.config_layout = to_lite_layout(TensorLayout{{}, out.dtype()});
        }
        IOs.outputs.push_back(out_io);
    }
    return IOs;
}
1282
#endif
1283
// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}