mgblar.cpp 60.5 KB
Newer Older
1 2 3 4
/**
 * \file sdk/load-and-run/src/mgblar.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11 12 13
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "./mgblar.h"
#include "./infile_persistent_cache.h"
14 15
#include "./json_loader.h"
#include "./npy.h"
16
#include "./text_table.h"
17

18 19 20
#include "megbrain/comp_node_env.h"
#include "megbrain/gopt/inference.h"
#include "megbrain/graph/extern_copr_api.h"
21
#include "megbrain/opr/dnn/convolution.h"
22
#include "megbrain/opr/io.h"
23
#include "megbrain/opr/search_policy/algo_chooser_helper.h"
24 25 26
#include "megbrain/opr/utility.h"
#include "megbrain/plugin/cpu_dispatch_checker.h"
#include "megbrain/plugin/num_range_checker.h"
27 28 29
#include "megbrain/plugin/opr_io_dump.h"
#include "megbrain/plugin/profiler.h"
#include "megbrain/plugin/var_value_checker.h"
30 31 32
#include "megbrain/serialization/extern_c_opr.h"
#include "megbrain/serialization/serializer.h"
#include "megbrain/utils/debug.h"
33 34 35 36 37

#include "megbrain/system.h"
#include "megbrain/version.h"
#include "megdnn/version.h"

38
#include <cctype>
39 40
#include <cerrno>
#include <cstdio>
41 42 43
#include <cstdlib>
#include <cstring>
#include <memory>
44
#include <numeric>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include <sstream>

#if defined(_WIN32)
#include <io.h>
#define F_OK 0
#define access(a, b) _access(a, b)
#elif __linux__ || __unix__ || __APPLE__
#include <unistd.h>
#include <dlfcn.h>
#endif

#if MGB_ENABLE_TENSOR_RT
#include "megbrain/tensorrt/tensorrt_engine_cache.h"
#endif

using namespace mgb;

namespace {

const char* OPTIONS_DESC =
R"__usage__(
  --cpu|--cpu-default
)__usage__"
R"__usage__(
    Require to compute on CPU or OpenCL. By default CUDA is used if available,
    and CPU is used if CUDA is not available. Use --cpu-default to compute on
    CPU and dispatch all tasks in the caller thread.
  --multithread|--multithread-default <nr_thread>
    Use --multithread to compute on CPU with multi threads.
    Use --multithread-default to compute on CPU with multi threads and
    the caller thread is main thread of the multi thread pool, follow by
    thread number
  --multi-thread-core-ids
    The multi thread affinity core set, separated with ',', the number of digital
    will be the thread number. for example:--multi-thread-core-ids "0,1,2,3", the
    number thread if 4,the main thread binding the last core '3',
    for best performance, the main thread should binding to the fast core.
  --profile|--profile-host <output>
    Write profiling result to given file. The output file is in JSON format and
    can be processed by scripts in MegHair/utils/debug.
    Note:
        For some backends (like opencl), special options need to be enabled for
        profiling device time, which may cause additional overhead and make it
        hard to profile host time. Use --profile-host to focus on host time
        profiling.
90
  --input [ filepath | string]
91
    Set up inputs for megbrain model. for example: --data image.ppm --data
92 93 94
    param.json --data bbox:bbox.npy@batchid:b.npy --data rect:[0,0,227,227];
    batchid:0,1,2,3. --io-dump or --bin-io-dump
    should be enabled at the same time.
95
  --model-info
96
    Format and display model input/output  tensor info.
97 98 99 100
  --io-dump <output> | --bin-io-dump <output dir>
    Dump input/output values of all internal variables to output file or
    directory, in text or binary format. The binary file can be parsed by
    `megbrain.plugin.load_tensor_binary`.
101 102
  --io-dump-stdout | --io-dump-stderr
    Dump input/output values of all internal variables to stdout or stderr in text format
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  --bin-out-dump <output dir>
    Dump output tensor values in binary format to given directory.
  --iter <num>
    Number of iterations to run for each testcase.
  --warmup-iter <num>
    Number of warm-up iterations, which are not included in the time statistics.
  --range <value>
    Enable tensor value range check. Exception would be raised if the absolute
    value of any element of any variable does not fit in given range. This can
    be used to debug NaN values.
  --check-dispatch
    Enable CPU dispatch checker, which prints a warning message if on operator
    does not the dispatch function. This is used to find potential bugs in
    MegDNN.
  --check-var-value <switch_interval[:start_i dx]>
    Enable VarValueChecker plugin. Refer to its doc for more details.
  --no-sanity-check
    Disable var sanity check on the first run. Var sanity check is enabled on
    the first-time execution by default, and can be used to find some potential
    memory access errors in the operator implementation.
  --disable-mem-opt
    Disable memory optimizations. This is used to check whether memory
    optimization is the cause for unexpected behavior.
  --fake-first
    Enable fake exec for the first run. In fake exec mode, some initialization
    job would be done, but no actual computing is performed. This can be used in
    an SDK right after loading the model to reduce execution latency in the real
    fist-time computing. It requires input shapes to be correctly setup.
  --const-shape
    Set `GraphLoadConfig::const_var_shape` to true before loading the graph.
    This can be used to reduce memory usage since some static inference data
    structures can be omitted.
  --share-param-mem
    Share the memory used by model params with model storage. This can be used
    to reduce memory usage when computing on CPU.
  --record-comp-seq | --record-comp-seq2
    Record the computing sequence, in level 1 or 2. It reduces overhead of API
    calls of some asynchronous computing devices, especially for OpenCL. In
    level 2 the computing graph can be destructed to reduce memory usage. Read
    the doc of `ComputingGraph::Options::comp_node_seq_record_level` for more
    details.
144
)__usage__"
145 146
#ifndef __IN_TEE_ENV__
#if MGB_ENABLE_JSON
147
R"__usage__(
148
  --get-static-mem-info <log_dir_path>
149
    Record the static graph's static memory info.
150
)__usage__"
151
#endif
152
#endif
153 154
#if MGB_ENABLE_FASTRUN
R"__usage__(
155 156
 --full-run
    Enable full-run mode. Operators with multiple algorithms would be profiled
157 158 159
    on the real device with actual input shapes, all algorithms will be profiled
    include naive algorithms.
    See `mgb::gopt::enable_opr_algo_profiling_inplace` for more details.
160 161
 --fast-run
    Enable fast-run mode. Operators with multiple algorithms would be profiled
162 163
    on the real device with actual input shapes, this mode will only profile the
    well optimized algorithms to get the profile result fast.
164 165 166 167 168 169
    See `mgb::gopt::enable_opr_algo_profiling_inplace` for more details.
)__usage__"
#endif
R"__usage__(
  --fast-run-algo-policy <path>
    It will read the cache file before profile, and save new fastrun in cache file.
170 171 172 173 174
  --fast-run-shared-batch-size
    Set the batch size used during fastrun, Note that it may not be the same as the actual running batch size
  --binary-equal-between-batch
    Each batch of output is promised binary equal if each batch of input is binary equal.
    Note that if this option is turned on, `--reproducible` will also be turned on.
175 176 177 178
  --reproducible
    Enable choose algo which is reproducible. It mainly used for cudnn algos.
    See https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index.html#reproducibility
    for more details.
179 180 181 182
  --wait-gdb
    Print PID and wait for a line from stdin before starting execution. Useful
    for waiting for gdb attach.
  --c-opr-lib <path>
183
    Load external operator library. It must implement MGB_C_OPR_INIT_FUNC_STR as the
184
    entry point.
185 186 187
  --c-opr-lib-with-param
    Run c opr lib with param, use to benchmark speed and check result, need c opr loader implemente
    `copr_param_device_ptr_malloc, copr_param_device_ptr_free and copr_param_device_ptr_h2d symbols`.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  --thread <num>
    Number of threads to run concurrently. All threads perform the same work of
    loading and executing models. This is used for test thread safety, not for
    speed up on multiple cores.
  --disable-assert-throw
    Do not throw exception in case AssertEqual fails. Note that the exit code
    would also be zero if this option is enabled. This should only be used for
    debug.
  --copy-to-host
    Whether copy output from device to host.
    This is used for checking the performance in real scenarios including output copy.
  --workspace-limit <num>
    set workspace_limit for execution strategy for oprs with multiple algorithms.
    The default is SIZE_MAX(bytes).
  --verbose
    Increase verbosity for megbrain log.
)__usage__"
#if MGB_ENABLE_TENSOR_RT
R"__usage__(
  --tensorrt
    Execute supported operators with TensorRT. Can only be used on Nvidia GPUs,
    i.e. comp node is xpu or gpu.
  --tensorrt-cache <path>
    Set the TensorRT engine cache path for serialized prebuilt ICudaEngine
)__usage__"
#endif
R"__usage__(
  --enable-jit
    Execute supported operators with JIT(now only support NVRTC). Can only be used on Nvidia GPUs.
)__usage__"
R"__usage__(
  --enable-chwn4
    Execute operators with kernels implemented in MegDNN with CHWN4 tensor format. Can only be used
    on Nvidia GPUs, whose compute capability is above 6.1.
)__usage__"
223 224 225 226 227 228
R"__usage__(
  --enable-nchw44
    Execute operators with kernels implemented in MegDNN with NCHW44 tensor format. This can only
    be used on arm of armv7 and arm64, support data tyep of float32, qint8 and int8x8x16.
)__usage__"
R"__usage__(
229
  --enable-nchw88
230 231 232 233
    Execute operators with kernels implemented in MegDNN with NCHW88 tensor format. This can only
    be used on x86 with data type float.
)__usage__"
R"__usage__(
234
  --enable-nchw44-dot
235 236 237 238 239 240 241 242
    Execute operators with kernels implemented in MegDNN with NCHW44-DOT tensor format. This Can
    only be used on arm32 and arm64 with dot-product supported, and only support qint8 model
)__usage__"
R"__usage__(
  --weight-preprocess
    Execute operators with weight preprocess, which can optimize the operator execution time with
    algo of winograd, im2col ,etc., but it may consume more memory.
)__usage__"
243 244 245 246
R"__usage__(
  --enable-fuse-preprocess
    Fusion astype\pad_channel\dimshuffle and etc opr from h2d op
)__usage__"
247 248 249 250 251
R"__usage__(
  --enable-nchw64
    Execute operators with kernels implemented in MegDNN with NCHW64 tensor format. Can only be used
    on Nvidia GPUs, which natively support fast int4 tensorcore inference.
)__usage__"
252 253 254 255 256 257 258 259 260 261
R"__usage__(
  --layout-transform [cuda|x86|arm|opencl|unspec]
    Enable global layout transform optimization for computing graph. User should specify the device target for the optimization, and a series of passes will be applied on the computing graph. The passes will benchmark the elapsed time of operators on different tensor layouts, and select fastest implementation for the operators. The optimization process will take some time. The default target is unspec, which all the available for operators will be profiled. So the optimize time will be longer.
  --layout-transform-dump <dump_path>
    The computing graph after global layout transform will be dumped to the given file path.
  --layout-transform-verify
    After applying the layout transform optimization, the results of the computing graph before and after layout transform passes will be compared to verify the correctness of the passes.
)__usage__"
R"__usage__(
)__usage__"
262 263
;

264 265


266 267
struct DataParser {
    struct Brace {
268
        std::weak_ptr<Brace> parent;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        std::vector<std::shared_ptr<Brace>> chidren;
    };

    void feed(const std::string& path) {
        std::string blob_name = "data", blob_string = path;
        size_t sep = path.find(":");
        if (sep != std::string::npos) {
            blob_name = path.substr(0, sep);
            blob_string = path.substr(sep + 1);
        }

        auto endWith = [blob_string](std::string suffix) -> bool {
            return blob_string.rfind(suffix) ==
                   (blob_string.length() - suffix.length());
        };

        if (endWith(".ppm") || endWith(".pgm")) {
            parse_image(blob_name, blob_string);
        } else if (endWith(".json")) {
            parse_json(blob_string);
        } else if (endWith(".npy")) {
            parse_npy(blob_name, blob_string);
        } else {
            parse_string(blob_name, blob_string);
        }
    }

    std::map<std::string, HostTensorND> inputs;

private:
    void parse_json(const std::string& path) {
        JsonLoader json;
        std::shared_ptr<JsonLoader::Value> root = json.load(path.c_str());

        mgb_assert(root != nullptr, "parse json %s fail", path.c_str());

        // parse json to data map
        const std::string SHAPE = "shape", TYPE = "type", RAW = "raw";
        for (auto& item : root->objects()) {
            auto&& value = *item.second;
            auto&& shape = value[SHAPE];
            mgb_assert(shape->is_array());

            auto&& type = value[TYPE];
            mgb_assert(type->is_str());

            auto&& raw = value[RAW];
            mgb_assert(raw->is_array());

            megdnn::SmallVector<size_t> data_shape;
            for (auto&& shape_ptr : shape->array()) {
                data_shape.append(
                        {static_cast<size_t>(std::round(shape_ptr->number()))});
            }

            // get type
            const std::map<std::string, megdnn::DType> type_map = {
                    {"float32", dtype::Float32()}, {"float", dtype::Float32()},
                    {"int32", dtype::Int32()},     {"int", dtype::Int32()},
                    {"int8", dtype::Int8()},       {"uint8", dtype::Uint8()}};

            const std::string& type_str = type->str();
            mgb_assert(type_map.find(type_str) != type_map.end(),
                       "unknown json data type for --data");

            DType datatype = type_map.at(type_str);
            HostTensorND hv;
            hv.comp_node(mgb::CompNode::default_cpu(), true)
                    .dtype(datatype)
                    .resize(data_shape);
            dt_byte* raw_ptr = hv.raw_ptr();
            size_t elem_size = datatype.size();

            // get raw
            const size_t array_size = raw->len();
            for (size_t idx = 0; idx < array_size; ++idx) {
                double tmp = (*raw)[idx]->number();

                switch (datatype.enumv()) {
                    case megdnn::DTypeEnum::Int32: {
                        int32_t ival = std::round(tmp);
                        memcpy(raw_ptr + idx * elem_size, &ival, elem_size);
                    } break;
                    case megdnn::DTypeEnum::Uint8:
                    case megdnn::DTypeEnum::Int8: {
                        int8_t cval = std::round(tmp);
                        memcpy(raw_ptr + idx, &cval, sizeof(int8_t));
                    } break;
                    case megdnn::DTypeEnum::Float32: {
                        float fval = tmp;
                        memcpy(raw_ptr + idx * elem_size, &fval, elem_size);
                    } break;
                    default:
                        break;
                }
            }

            inputs.insert(std::make_pair(item.first, std::move(hv)));
        }
    }

    void parse_image(const std::string& name, const std::string& path) {
        // load ppm/pgm
        std::ifstream fin;
        fin.open(path, std::ifstream::binary | std::ifstream::in);
        mgb_assert(fin.is_open(), "open file %s failed for --input",
                   path.c_str());

        size_t w = 0, h = 0, channel = 0;
        char buf[128] = {0};

        fin.getline(buf, 128);
        if ('5' == buf[1]) {
            channel = 1;
        } else if ('6' == buf[1]) {
            channel = 3;
        } else {
            mgb_assert(0, "not a formal ppm/pgm");
        }

        while (fin.getline(buf, 128)) {
            // skip OCV comment, check
            // https://github.com/opencv/opencv/pull/17006
            if (buf[0] == '#') {
                continue;
            }
            break;
        }
        std::stringstream ss;
        ss << std::string(buf);
        ss >> w;
        ss >> h;

        mgb_assert(w > 0 and h > 0);

        HostTensorND hv;
        hv.comp_node(mgb::CompNode::default_cpu(), true)
                .dtype(dtype::Uint8())
                .resize({1, h, w, channel});

        fin.read((char*)(hv.raw_ptr()), hv.layout().total_nr_elems());
        fin.close();
        inputs.insert(std::make_pair(name, std::move(hv)));
    }

    void parse_npy(const std::string& name, const std::string& path) {
        std::string type_str;
        std::vector<npy::ndarray_len_t> stl_shape;
        std::vector<int8_t> raw;
        npy::LoadArrayFromNumpy(path, type_str, stl_shape, raw);

        megdnn::SmallVector<size_t> shape;
        for (auto val : stl_shape) {
            shape.append({static_cast<size_t>(val)});
        }

        const std::map<std::string, megdnn::DType> type_map = {
                {"f4", dtype::Float32()},
                {"i4", dtype::Int32()},
                {"i1", dtype::Int8()},
                {"u1", dtype::Uint8()}};

        megdnn::DType hv_type;
        for (auto& item : type_map) {
            if (type_str.find(item.first) != std::string::npos) {
                hv_type = item.second;
                break;
            }
        }

        HostTensorND hv;
        hv.comp_node(mgb::CompNode::default_cpu(), true)
                .dtype(hv_type)
                .resize(shape);
        dt_byte* raw_ptr = hv.raw_ptr();
        memcpy(raw_ptr, raw.data(), raw.size());

        inputs.insert(std::make_pair(name, std::move(hv)));
    }

    void parse_string(const std::string name, const std::string& str) {
        // data type
        megdnn::DType data_type = dtype::Int32();
        if (str.find(".") != std::string::npos or
            str.find(".") != std::string::npos) {
            data_type = dtype::Float32();
        }
        // shape
        size_t number_cnt = 0;

        std::shared_ptr<Brace> brace_root = std::make_shared<Brace>();
        std::shared_ptr<Brace> cur = brace_root;
        for (size_t i = 0; i < str.size(); ++i) {
            char c = str[i];
            if (c == '[') {
                std::shared_ptr<Brace> child = std::make_shared<Brace>();
                child->parent = cur;
                cur->chidren.emplace_back(child);
                cur = child;
            } else if (c == ']') {
469
                cur = cur->parent.lock();
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
            } else if (c == ',') {
                number_cnt++;
            }
            continue;
        }
        ++number_cnt;

        mgb_assert(cur == brace_root, "braces not closed for --input");
        megdnn::SmallVector<size_t> shape;
        cur = brace_root;
        while (not cur->chidren.empty()) {
            shape.append({cur->chidren.size()});
            number_cnt /= cur->chidren.size();
            cur = cur->chidren[0];
        }
        mgb_assert(number_cnt > 0);
        shape.append({number_cnt});

        // data
        std::string json_arr;
        for (size_t i = 0; i < str.size(); ++i) {
            char c = str[i];
            if (c != '[' and c != ']') {
                json_arr += c;
            }
        }
        json_arr = "[" + json_arr + "]";

        // reuse json parser to resolve raw data
        JsonLoader json;
        std::shared_ptr<JsonLoader::Value> json_root =
                json.load(json_arr.data(), json_arr.size());
        mgb_assert(json_root != nullptr, "parse json fail in parse_string");

        HostTensorND hv;
        hv.comp_node(mgb::CompNode::default_cpu(), true)
                .dtype(data_type)
                .resize(shape);
        dt_byte* raw_ptr = hv.raw_ptr();

        const size_t array_len = json_root->len();
        const size_t elem_size = data_type.size();
        for (size_t idx = 0; idx < array_len; ++idx) {
            double tmp = json_root->array()[idx]->number();
            switch (data_type.enumv()) {
                case megdnn::DTypeEnum::Int32: {
                    int32_t ival = std::round(tmp);
                    memcpy(raw_ptr + idx * elem_size, &ival, elem_size);
                } break;
                case megdnn::DTypeEnum::Float32: {
                    float fval = tmp;
                    memcpy(raw_ptr + idx * elem_size, &fval, elem_size);
                } break;
                default:
                    break;
            }
        }
        inputs.insert(std::make_pair(name, std::move(hv)));
    };
};

531 532 533 534
struct Args {
    int args_parse_ret = 0;

    std::string model_path;
535 536 537 538 539 540 541 542 543 544 545 546 547 548
    struct COprArgs {
        //! for run c opr
        bool is_run_c_opr = false;
        bool is_run_c_opr_with_param = false;
        typedef void (*COPR_PARAM_DEVICE_PTR_MEM_T)(ExternCOprParam* param);
        typedef void (*COPR_PARAM_DEVICE_PTR_H2D_T)(
                ExternCOprParam* param, void* host_ptr,
                size_t extern_device_tensor_id);
        COPR_PARAM_DEVICE_PTR_MEM_T copr_param_device_ptr_malloc = nullptr;
        COPR_PARAM_DEVICE_PTR_MEM_T copr_param_device_ptr_free = nullptr;
        COPR_PARAM_DEVICE_PTR_H2D_T copr_param_device_ptr_h2d = nullptr;
    };

    COprArgs c_opr_args;
549

550
    bool display_model_info = false;
551 552 553
    bool disable_assert_throw = false;
    bool share_param_mem = false;
#if MGB_ENABLE_FASTRUN
554 555
    bool use_full_run = false;
    bool use_fast_run = false;
556
#endif
557
    bool reproducible = false;
558
    std::string fast_run_cache_path;
559
#ifndef __IN_TEE_ENV__
560 561 562
#if MGB_ENABLE_JSON
    std::string static_mem_log_dir_path;
#endif
563
#endif
564 565 566 567 568 569
    bool copy_to_host = false;
    int nr_run = 10;
    int nr_warmup = 1;
    int nr_thread = 1;
    int multithread_number = 1;
    size_t workspace_limit = SIZE_MAX;
570
    std::vector<std::string> data_files;
571 572 573 574 575 576 577 578 579 580 581 582 583 584
    serialization::GraphLoader::LoadResult load_ret;
#if MGB_ENABLE_JSON
    std::unique_ptr<GraphProfiler> profiler;
#endif
    std::string profiler_output;
    std::string bin_out_dump;

    std::unique_ptr<OprIODumpBase> iodump;
    std::unique_ptr<NumRangeChecker> num_range_checker;
    std::unique_ptr<CPUDispatchChecker> cpu_dispatch_checker;
    std::unique_ptr<VarValueChecker> var_value_checker;
    serialization::GraphLoader::LoadConfig load_config;
    thin_function<void(size_t)> affinity_cb;

585 586 587 588 589
    bool layout_transform = false;
    gopt::GraphTuningOptions::Target layout_transform_target =
            gopt::GraphTuningOptions::Target::UNSPEC;
    std::string layout_transform_dump_path;

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 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 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    static Args from_argv(int argc, char **argv);
};

uint32_t read_nr_test(serialization::InputFile &fin) {
    char magic[8];
    fin.read(magic, sizeof(magic));
    if (strncmp(magic, "mgbtest0", 8)) {
        fin.rewind();
        return 0;
    }
    uint32_t ret;
    fin.read(&ret, sizeof(ret));
    return ret;
}

size_t get_file_size(FILE *fptr) {
    fseek(fptr, 0, SEEK_END);
    size_t size = ftell(fptr);
    fseek(fptr, 0, SEEK_SET);
    return size;
}

/**
 * \brief dump output tensor.
 *
 * graph would be destructed if comp_node_seq_record_level == 2; so we should
 * store graph info before graph_compile().
 */
class OutputDumper {
    struct DumpInfo {
        HostTensorND hv = {};
        std::string var_info;
        std::string owner_inputs_info;
        size_t id;
    };
    SmallVector<DumpInfo> m_infos;

    size_t m_run_id = 0;
    size_t m_bind_id = 0;
    const Args& m_env;

public:
    OutputDumper(const Args& env) : m_env{env} {
        for (auto&& i : m_env.load_ret.output_var_list) {
            auto&& var = i.node();
            DumpInfo info;
            info.var_info = cg::dump_var_info({var});
            info.owner_inputs_info =
                    cg::dump_var_info(var->owner_opr()->input());
            info.id = var->id();
            m_infos.push_back(info);
        }
    }

    ComputingGraph::Callback bind() {
        auto& info = m_infos.at(m_bind_id++);
        ComputingGraph::Callback cb = [&info](const DeviceTensorND& dv) {
            info.hv.copy_from(dv);
        };
        return cb;
    }

    void write_to_file() {
        if (!m_env.bin_out_dump.empty()) {
            for (auto&& info : m_infos) {
                auto value = debug::dump_tensor(
                        info.hv, ssprintf("var=%s owner_opr_inputs=%s",
                                          info.var_info.c_str(),
                                          info.owner_inputs_info.c_str()));
                debug::write_to_file(
                        ssprintf("%s/run%zu-var%zd", m_env.bin_out_dump.c_str(),
                                 m_run_id, info.id)
                                .c_str(),
                        value);
            }

        }
        m_run_id ++;
    }
};

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
void format_and_print(const std::string& tablename, const Args& env) {
    auto table = mgb::TextTable(tablename);
    table.padding(1);
    table.align(mgb::TextTable::Align::Mid)
        .add("type")
        .add("name")
        .add("shape")
        .eor();

    for (auto &&i: env.load_ret.tensor_map) {
        table.align(mgb::TextTable::Align::Mid)
            .add("INPUT")
            .add(i.first)
            .add(i.second->shape().to_string())
            .eor();
    }
    
    for (auto&& i : env.load_ret.output_var_list) {
        table.align(mgb::TextTable::Align::Mid)
            .add("OUTPUT")
            .add(i.node()->name())
            .add(i.shape().to_string())
            .eor();
    }

    std::stringstream ss;
    ss << table;
    printf("%s\n\n", ss.str().c_str());
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
void run_test_st(Args &env) {
    std::unique_ptr<serialization::InputFile> inp_file;

    if (env.share_param_mem) {
        FILE *fin = fopen(env.model_path.c_str(), "rb");
        mgb_assert(fin, "failed to open %s: %s", env.model_path.c_str(),
                strerror(errno));
        auto size = get_file_size(fin);
        void *ptr = malloc(size);
        std::shared_ptr<void> buf{ptr, free};
        auto nr = fread(buf.get(), 1, size, fin);
        mgb_assert(nr == size);
        fclose(fin);
        inp_file = serialization::InputFile::make_mem_proxy(buf, size);
    } else {
        inp_file = serialization::InputFile::make_fs(
                env.model_path.c_str());
    }
    auto nr_test = read_nr_test(*inp_file);

    auto format =
            serialization::GraphLoader::identify_graph_dump_format(*inp_file);
723 724 725
    mgb_assert(format.valid(),
               "invalid model: unknown model format, please make sure input "
               "file is generated by GraphDumper");
726 727 728 729 730 731 732 733 734 735
    auto loader =
            serialization::GraphLoader::make(std::move(inp_file), format.val());
    RealTimer timer;
    env.load_ret = loader->load(env.load_config, false);

    // graph is no longer needed; reset so memory can be reclaimed
    env.load_config.comp_graph.reset();

    printf("load model: %.3fms\n", timer.get_msecs_reset());

736 737 738
    auto& output_var_list = env.load_ret.output_var_list;
    mgb::gopt::set_opr_algo_workspace_limit_inplace(output_var_list,
                                                    env.workspace_limit);
739
    using S = opr::mixin::AlgoChooserHelper::ExecutionPolicy::Strategy;
740
    S strategy = static_cast<S>(0);
741 742 743
    if (env.reproducible) {
        strategy = S::REPRODUCIBLE;
    }
744
#if MGB_ENABLE_FASTRUN
745
    if (env.use_full_run) {
746
        strategy = S::PROFILE | strategy;
747
    } else if (env.use_fast_run) {
748 749 750
        strategy = S::PROFILE | S::OPTIMIZED | strategy;
    } else {
        strategy = S::HEURISTIC | strategy;
751 752
    }
#else
753
    strategy = S::HEURISTIC | strategy;
754
#endif
755
    mgb::gopt::modify_opr_algo_strategy_inplace(output_var_list, strategy);
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    if (!env.fast_run_cache_path.empty()) {
#if MGB_ENABLE_FASTRUN
        if (!access(env.fast_run_cache_path.c_str(), F_OK)) {
#else
        mgb_assert(access(env.fast_run_cache_path.c_str(), F_OK) == 0,
                   "fast-run cache file can't be accessed");
#endif
            FILE* fin = fopen(env.fast_run_cache_path.c_str(), "rb");
            auto flen = get_file_size(fin);
            std::unique_ptr<uint8_t[]> buf{new uint8_t[flen]};
            size_t ret = fread(buf.get(), flen, 1, fin);
            MGB_MARK_USED_VAR(ret);
            mgb_assert(ret == 1, "read 1 block (got %zu), and block size %zu.",
                       ret, flen);
            fclose(fin);
            PersistentCache::set_impl(
                    std::make_shared<InFilePersistentCache>(buf.get(), flen));
#if MGB_ENABLE_FASTRUN
        } else {
775 776
            mgb_assert(env.use_full_run || env.use_fast_run,
                       "fast-run or fast-run should be enabled");
777 778 779
            PersistentCache::set_impl(
                    std::make_shared<InFilePersistentCache>());
        }
780
        if (!env.use_full_run && !env.use_fast_run)
781
#endif
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
            mgb::gopt::enable_opr_use_profiling_cache_inplace(output_var_list);
    }

    // load testcase
    decltype(env.load_ret) testcase;
    if (nr_test) {
        loader = serialization::GraphLoader::make(loader->reset_file(),
                                                  loader->format());
        testcase = loader->load(env.load_config, false);
    }

    if (env.layout_transform) {
        env.load_ret.output_var_list = gopt::layout_transform(
                env.load_ret.output_var_list, env.layout_transform_target);
        if (!env.layout_transform_dump_path.empty()) {
            auto out_file = serialization::OutputFile::make_fs(
                    env.layout_transform_dump_path.c_str(), 'w');
            if (nr_test) {
                const char* magic = "mgbtest0";
                constexpr size_t len = sizeof(magic);
                out_file->write(magic, len);
                uint32_t nr_inp_tensors = testcase.output_var_list.size();
                out_file->write(&nr_inp_tensors, sizeof(nr_inp_tensors));
            }
            auto dumper = serialization::GraphDumper::make(std::move(out_file),
                                                           format.val());
            using DumpConfig = serialization::GraphDumper::DumpConfig;
            DumpConfig config{1, false, false};
            dumper->dump(env.load_ret.output_var_list, config);
            if (nr_test) {
                out_file = serialization::OutputFile::make_fs(
                        env.layout_transform_dump_path.c_str(), 'a');
                auto testdumper = serialization::GraphDumper::make(
                        std::move(out_file), format.val());
                testdumper->dump(testcase.output_var_list, config);
            }
        }
    }

    // compile function to compute all outputs
    ComputingGraph::OutputSpec out_spec;
    std::string output_names;
    
    if (env.display_model_info) {
        format_and_print("Original Model Info", env);
    }

    OutputDumper output_dumper(env);
    for (auto&& i : env.load_ret.output_var_list) {
        if (&i != env.load_ret.output_var_list.data()) {
            output_names += " ";
        }
        output_names.append(i.node()->name() + i.shape().to_string());
        ComputingGraph::Callback cb;
        if (!env.bin_out_dump.empty()) {
            cb = output_dumper.bind();
        } else if (env.copy_to_host) {
            HostTensorND val;
            cb = [val](const DeviceTensorND& dv) mutable {
                val.copy_from(dv);
            };
        }
        out_spec.emplace_back(i, std::move(cb));
    }

    if (env.disable_assert_throw) {
        auto on_opr = [](cg::OperatorNodeBase* opr) {
            if (opr->same_type<opr::AssertEqual>()) {
                opr->cast_final<opr::AssertEqual>().disable_throw_on_error();
            }
        };
        cg::DepOprIter iter{on_opr};
        for (auto&& i : out_spec) {
            iter.add(i.first.node()->owner_opr());
        }
    }

    SymbolVarArray vars;
    for (auto i : out_spec) {
        vars.push_back(i.first);
862 863 864
    }

    auto func = env.load_ret.graph_compile(out_spec);
865
#ifndef __IN_TEE_ENV__
866 867 868
#if MGB_ENABLE_JSON
    if (!env.static_mem_log_dir_path.empty()) {
        func->get_static_memory_alloc_info(env.static_mem_log_dir_path);
869
    }
870
#endif
871
#endif
872 873 874 875 876 877 878 879 880
    auto warmup = [&]() {
        printf("=== prepare: %.3fms; going to warmup\n",
               timer.get_msecs_reset());
        for (int run = 0; run < env.nr_warmup; ++run) {
            func->execute().wait();
            printf("warmup %d: %.3fms\n", run, timer.get_msecs_reset());
        }
    };

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    auto run_iters = [&](uint32_t case_idx) -> float {
        double time_sqrsum = 0, time_sum = 0,
               min_time = std::numeric_limits<double>::max(), max_time = 0;
        for (int run = 0; run < env.nr_run; ++run) {
            mgb_log_debug("load_and_run: before running iter %d", run);
            timer.reset();
            func->execute();
            mgb_log_debug("load_and_run: before waiting iter %d", run);
            auto exec_time = timer.get_msecs();
            func->wait();
            output_dumper.write_to_file();
            auto cur = timer.get_msecs();
            printf("iter %d/%d: %.3fms (exec=%.3f,device=%.3f)\n", run,
                   env.nr_run, cur, exec_time,
                   func->get_prev_exec_time() * 1e3);
            time_sum += cur;
            time_sqrsum += cur * cur;
            fflush(stdout);
            if (cur < min_time) {
                min_time = cur;
            }
            if (cur > max_time) {
                max_time = cur;
            }
        }
        printf("=== finished test #%u: time=%.3fms avg_time=%.3fms "
               "sd=%.3fms minmax=%.3f,%.3f\n\n",
               case_idx, time_sum, time_sum / env.nr_run,
               std::sqrt((time_sqrsum * env.nr_run - time_sum * time_sum) /
                         (env.nr_run * (env.nr_run - 1))),
               min_time, max_time);
        return time_sum;

    };

916 917 918 919 920 921 922 923 924 925 926 927 928
    if (nr_test) {
        // run testcase, generated by dump_with_testcase.py

        std::vector<std::pair<std::string, HostTensorND*>> inp_tensors;
        for (auto &&i: env.load_ret.tensor_map) {
            inp_tensors.emplace_back(i.first, i.second.get());
        }
        std::sort(inp_tensors.begin(), inp_tensors.end());

        printf("=== going to run %u testcases; output vars: %s\n", nr_test,
                output_names.c_str());
        double tot_time = 0;
        for (uint32_t i = 0; i < nr_test; ++ i) {
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
            std::shared_ptr<ExternCOprParam> c_opr_param;
            auto dtype_cpp2c = [](DType dtype) -> MGBDType {
                switch (dtype.enumv()) {
                    case DTypeEnum::Float32:
                        return MGB_DTYPE_FLOAT32;
                    case DTypeEnum::Int32:
                        return MGB_DTYPE_INT32;
                    case DTypeEnum::Int16:
                        return MGB_DTYPE_INT16;
                    case DTypeEnum::Uint8:
                        return MGB_DTYPE_UINT8;
#if !MEGDNN_DISABLE_FLOAT16
                    case DTypeEnum::Float16:
                        return MGB_DTYPE_FLOAT16;
#endif
                    default:
                        mgb_throw(InternalError,
                                  "unsupported dtype for extern C API: %s",
                                  dtype.name());
                }
            };

            auto tensor_shape_to_c = [](const TensorShape& shape,
                                        MGBTensorShape& mgb_shape) {
                mgb_assert(shape.ndim <= MGB_TENSOR_MAX_NDIM,
                           "shape ndim too large: %zu", shape.ndim);
                mgb_shape.ndim = shape.ndim;
                for (size_t i = 0; i < shape.ndim; ++i) {
                    mgb_shape.shape[i] = shape[i];
                }
            };

            if (env.c_opr_args.is_run_c_opr_with_param) {
                c_opr_param = std::make_shared<ExternCOprParam>();
                memset(c_opr_param.get(), 0, sizeof(ExternCOprParam));
                //! we just test input on npu case, do not test output on
                //! npu case, so we just init input shape and type
                c_opr_param->nr_input = inp_tensors.size();
                c_opr_param->input = (ExternDeviceTensor*)malloc(
                        sizeof(ExternDeviceTensor) * inp_tensors.size());
                memset(c_opr_param->input, 0,
                       sizeof(ExternDeviceTensor) * inp_tensors.size());
                //! init input ExternDeviceTensor shape and dtype
                for (size_t input_index = 0; input_index < inp_tensors.size();
                     input_index++) {
                    auto& mgb_tensor_layout =
                            c_opr_param->input[input_index].layout;
                    auto host_tensor_nd_p = inp_tensors[input_index].second;
                    mgb_tensor_layout.dtype =
                            dtype_cpp2c(host_tensor_nd_p->dtype());
                    tensor_shape_to_c(inp_tensors[input_index].second->shape(),
                                      mgb_tensor_layout.shape);
                }
                c_opr_param->nr_output = 0;
                //! now call copr_param_device_ptr_malloc to malloc
                //! device_ptr
                env.c_opr_args.copr_param_device_ptr_malloc(c_opr_param.get());
            }

988 989 990 991
            mgb_assert(testcase.output_var_list.size() == inp_tensors.size());
            for (size_t i = 0; i < inp_tensors.size(); ++ i) {
                auto &&opr = testcase.output_var_list[i].node()->owner_opr()->
                    cast_final_safe<opr::SharedDeviceTensor>();
992 993 994 995 996 997 998 999 1000 1001 1002 1003
                if (env.c_opr_args.is_run_c_opr_with_param) {
                    //! now call copr_param_device_ptr_h2d to fill data
                    env.c_opr_args.copr_param_device_ptr_h2d(
                            c_opr_param.get(), opr.dev_data()->raw_ptr(), i);
                } else {
                    inp_tensors[i].second->copy_from(
                            HostTensorND::make_proxy(*opr.dev_data()));
                }
            }
            //! now config c opr dynamic param
            if (env.c_opr_args.is_run_c_opr_with_param) {
                config_extern_c_opr_dynamic_param(func, c_opr_param);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
            }

            if (!i) {
                warmup();
            }

            timer.reset();
            printf("=== going to run test #%u for %d times\n", i, env.nr_run);
            if (!env.nr_run) {
                continue;
            }
1015
            tot_time += run_iters(i);
1016 1017 1018 1019 1020 1021

            //! now free c opr device_ptr
            if (env.c_opr_args.is_run_c_opr_with_param) {
                env.c_opr_args.copr_param_device_ptr_free(c_opr_param.get());
                free(c_opr_param->input);
            }
1022 1023 1024
        }

        printf("=== total time: %.3fms\n", tot_time);
1025
    } else if (not env.data_files.empty()) {
1026 1027
        mgb_assert(!env.c_opr_args.is_run_c_opr_with_param,
                   "run c opr with param only support dump_with_testcase!!");
1028 1029 1030 1031 1032 1033 1034
        auto& tensormap = env.load_ret.tensor_map;

        DataParser parser;
        for (auto path : env.data_files) {
            parser.feed(path);
        }
        auto inputs = parser.inputs;
1035 1036 1037
        if (inputs.size() > 1) {
            for (auto& i : inputs) {
                mgb_assert(tensormap.find(i.first) != tensormap.end());
1038

1039 1040 1041 1042 1043 1044
                auto& in = tensormap.find(i.first)->second;
                in->copy_from(i.second);
            }
        } else {
            auto& in = tensormap.begin()->second;
            in->copy_from(inputs.begin()->second);
1045 1046
        }

1047
        warmup();
1048
        timer.reset();
1049 1050
        printf("=== going to run input for %d times\n", env.nr_run);
        run_iters(0);
1051
    } else {
1052 1053
        mgb_assert(!env.c_opr_args.is_run_c_opr_with_param,
                   "run c opr with param only support dump_with_testcase!!");
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        // run speed test for a raw mgb graph
        mgb_assert(env.load_ret.tensor_map.empty(),
                "model should not require input values; input vars should be "
                "replaced by SharedDeviceTensor "
                "(i.e. megskull.opr.ParamProvider)");

        warmup();
        timer.reset();
        printf("=== going to run for %d times; output vars: %s\n",
                env.nr_run, output_names.c_str());
        for (int i = 0; i < env.nr_run; ++ i) {
            mgb_log_debug("load_and_run: before benchmark iter %d", i);
            auto start = timer.get_msecs();
            func->execute().wait();
            output_dumper.write_to_file();
            printf("=== finished run #%d: time=%.3fms\n", i,
                    timer.get_msecs() - start);
            fflush(stdout);
        }
        printf("avg time: %.3fms\n", timer.get_msecs() / env.nr_run);
    }

#if MGB_ENABLE_JSON
    if (env.profiler) {
        env.profiler->to_json_full(func.get())->writeto_fpath(
                env.profiler_output);
        mgb_log("profiling result written to %s", env.profiler_output.c_str());
    }
#endif
#if MGB_ENABLE_FASTRUN
    if (!env.fast_run_cache_path.empty()) {
        static_cast<InFilePersistentCache&>(PersistentCache::inst())
                .dump_cache(env.fast_run_cache_path.c_str());
    }
#endif
#if MGB_ENABLE_TENSOR_RT
    if (TensorRTEngineCache::enable_engine_cache()) {
        TensorRTEngineCache::inst().dump_cache();
    }
#endif
1094
    
1095
    if (env.display_model_info) {
1096 1097
        format_and_print("Runtime Model Info", env);
    }
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
}

}  // anonymous namespace

int mgb_load_and_run_main(int argc, char** argv) {
    {
        auto v0 = get_version();
        auto v1 = megdnn::get_version();
        printf("mgb load-and-run: using MegBrain "
               "%d.%d.%d(%d) and MegDNN %d.%d.%d\n",
               v0.major, v0.minor, v0.patch, v0.is_dev, v1.major, v1.minor,
               v1.patch);
    }
    auto env = Args::from_argv(argc, argv);

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    if (env.c_opr_args.is_run_c_opr_with_param)
        mgb_assert(env.c_opr_args.is_run_c_opr &&
                           env.c_opr_args.copr_param_device_ptr_malloc &&
                           env.c_opr_args.copr_param_device_ptr_free &&
                           env.c_opr_args.copr_param_device_ptr_h2d,
                   "--c-opr-lib-with-param need config with --c-opr-lib, also "
                   "extern c opr loader need implemente "
                   "copr_param_device_ptr_malloc, copr_param_device_ptr_free "
                   "and copr_param_device_ptr_h2d symbols");

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 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 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    if (env.args_parse_ret != 0) {
        return env.args_parse_ret;
    }

    if (env.nr_thread == 1) {
        run_test_st(env);
    } else {
#if MGB_HAVE_THREAD
        mgb_log_warn("use %d threads", env.nr_thread);
        std::vector<std::thread> threads;
        auto run = [argc, argv]() {
            auto env = Args::from_argv(argc, argv);
            run_test_st(env);
        };

        for (int i = 0; i < env.nr_thread; ++i) {
            threads.emplace_back(run);
        }

        for (auto&& i : threads) {
            i.join();
        }
#else
        mgb_log_error("%d threads requested, but load-and-run was compiled "
                      "without thread support.");
#endif
    }

    return 0;
}

Args Args::from_argv(int argc, char **argv) {
    Args ret;
    if (argc < 2) {
        printf("usage: %s <model file> [options...]\nWhere options are:%s",
               argv[0], OPTIONS_DESC);
        ret.args_parse_ret = -1;
        return ret;
    }
    set_log_level(LogLevel::WARN);
    ret.model_path = argv[1];
    ret.load_config.comp_graph = ComputingGraph::make();
    auto &&graph_opt = ret.load_config.comp_graph->options();
    graph_opt.graph_opt_level = 0;

    for (int i = 2; i < argc; ++ i) {
        if (!strcmp(argv[i], "--cpu")) {
            mgb_log_warn("use cpu mode");
            ret.load_config.comp_node_mapper = [](CompNode::Locator &loc) {
                loc.type = CompNode::DeviceType::CPU;
            };
            continue;
        }
        if (!strcmp(argv[i], "--cpu-default")) {
            mgb_log_warn("use cpu:default mode");
            ret.load_config.comp_node_mapper = [](CompNode::Locator &loc) {
                loc.type = CompNode::DeviceType::CPU;
                loc.device = CompNode::Locator::DEVICE_CPU_DEFAULT;
            };
            continue;
        }
        if (!strcmp(argv[i], "--multithread")) {
            mgb_log_warn("use multithread mode");
            ++ i;
            ret.multithread_number = std::stoi(argv[i]);
            ret.load_config.comp_node_mapper =
1189
                    [nr_threads =
1190 1191 1192
                             ret.multithread_number](CompNode::Locator& loc) {
                        loc.type = CompNode::DeviceType::MULTITHREAD;
                        loc.device = 0;
1193
                        loc.stream = nr_threads;
1194 1195 1196 1197 1198 1199 1200
                    };
            continue;
        }
        if (!strcmp(argv[i], "--multithread-default")) {
            mgb_log_warn("use multithread:default mode");
            ++i;
            ret.multithread_number = std::stoi(argv[i]);
1201 1202 1203
            ret.load_config.comp_node_mapper = [nr_threads =
                                                        ret.multithread_number](
                                                       CompNode::Locator& loc) {
1204 1205
                loc.type = CompNode::DeviceType::MULTITHREAD;
                loc.device = CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
1206
                loc.nr_threads = nr_threads;
1207 1208 1209 1210 1211 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
            };
            continue;
        }
        if (!strcmp(argv[i], "--multi-thread-core-ids")) {
            ++i;
            std::string core_id_string = argv[i];
            std::stringstream input_stringstream(core_id_string);
            std::string id;
            size_t nr_threads = 0;
            std::vector<int> core_ids;
            mgb_log_warn("multi thread core ids: %s", core_id_string.c_str());
            while(getline(input_stringstream, id, ',')) {
                nr_threads++;
                core_ids.push_back(atoi(id.c_str()));
            }
            mgb_assert(ret.multithread_number > 0 &&
                               ret.load_config.comp_node_mapper,
                       "the core id should set behind the --multithread param");
            mgb_assert(static_cast<size_t>(ret.multithread_number) ==
                               core_ids.size(),
                       "the core id should equal to the multi thread number");
            auto affinity_cb = [core_ids](int thread_id) {
                mgb::sys::set_cpu_affinity({core_ids[thread_id]});
            };
            CompNode::Locator loc;
            ret.load_config.comp_node_mapper(loc);
            mgb_assert(loc.type == CompNode::DeviceType::MULTITHREAD,
                       "core id only set on multithread compnode");
            auto cn = CompNode::load(loc);
            CompNodeEnv::from_comp_node(cn).cpu_env().set_affinity(affinity_cb);
            continue;
        }
#if MGB_ENABLE_TENSOR_RT
        if (!strcmp(argv[i], "--tensorrt")) {
            mgb_log_warn("use tensorrt mode");
            graph_opt.graph_opt.tensorrt = true;
            continue;
        }
        if (!strcmp(argv[i], "--tensorrt-cache")) {
            ++i;
            mgb_assert(i < argc, "value not given for --tensorrt-cache");
            char* tensorrt_cache_path = argv[i];
            mgb_log_warn("use tensorrt cache: %s", tensorrt_cache_path);
            TensorRTEngineCache::enable_engine_cache(true);
            TensorRTEngineCache::set_impl(
                    std::make_shared<TensorRTEngineCacheIO>(
                            tensorrt_cache_path));
            continue;
        }
#endif
1257 1258 1259 1260 1261 1262 1263 1264

#define cb(_layout)                                       \
    if (!strcmp(argv[i], "--enable-" #_layout)) {         \
        mgb_log_warn("enable " #_layout " optimization"); \
        graph_opt.graph_opt.enable_##_layout();           \
        continue;                                         \
    }

1265
        cb(nchw4);
1266 1267 1268 1269 1270
        cb(chwn4);
        cb(nchw44);
        cb(nchw88);
        cb(nchw32);
        cb(nhwcd4);
1271
        cb(nchw64);
1272
#undef cb
1273 1274 1275 1276 1277
        if (!strcmp(argv[i], "--enable-nchw44-dot")) {
            mgb_log_warn("enable-nchw44-dot optimization");
            graph_opt.graph_opt.enable_nchw44_dot();
            continue;
        }
1278 1279 1280 1281 1282
        if (!strcmp(argv[i], "--enable-fuse-preprocess")) {
            mgb_log_warn("enable-fuse-preprocess optimization");
            graph_opt.graph_opt.enable_fuse_preprocess();
            continue;
        }
1283 1284 1285 1286 1287 1288 1289 1290
        if (!strcmp(argv[i], "--enable-fuse-conv-bias-nonlinearity")) {
            mgb_log_warn("enable fuse-conv-bias-nonlinearity optimization");
            graph_opt.graph_opt.enable_fuse_conv_bias_nonlinearity();
            continue;
        }
        if (!strcmp(argv[i], "--enable-fuse-conv-bias-with-z")) {
            mgb_log_warn("enable fuse_conv_bias_with_z optimization");
            graph_opt.graph_opt.enable_fuse_conv_bias_with_z();
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
            continue;
        }
#if MGB_ENABLE_JSON
        if (!strcmp(argv[i], "--profile") ||
            !strcmp(argv[i], "--profile-host")) {
            if (!strcmp(argv[i], "--profile")) {
                mgb_log_warn("enable profiling");
            } else {
                mgb_log_warn("enable profiling for host");
            }
            ++i;
            mgb_assert(i < argc, "output file not given for --profile");
            ret.profiler = std::make_unique<GraphProfiler>(
                    ret.load_config.comp_graph.get());
            ret.profiler_output = argv[i];
            continue;
        }
#endif
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
        if (!strcmp(argv[i], "--input")) {
            ++i;
            mgb_assert(i < argc, "input file not given for --input");

            size_t start = 0;
            std::string cmd = argv[i];

            while (true) {
                auto end = cmd.find(";", start);
                if (end == std::string::npos) {
                    ret.data_files.emplace_back(cmd.substr(start));
                    break;
                }
1322
                std::string substr = cmd.substr(start, end - start);
1323 1324 1325 1326 1327
                ret.data_files.emplace_back(substr);
                start = end + 1;
            }
            continue;
        }
1328
        if (!strcmp(argv[i], "--model-info")) {
1329
            ++i;
1330
            ret.display_model_info = true;
1331 1332
            continue;
        }
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
        if (!strcmp(argv[i], "--io-dump")) {
            mgb_log_warn("enable opr io dump");
            ++ i;
            mgb_assert(i < argc, "output file not given for --io-dump");
            auto iodump = std::make_unique<TextOprIODump>(
                    ret.load_config.comp_graph.get(), argv[i]);
            iodump->print_addr(false);
            ret.iodump = std::move(iodump);
            continue;
        }
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
        if (!strcmp(argv[i], "--io-dump-stdout")) {
            mgb_log_warn("enable opr io dump to stdout");
            std::shared_ptr<FILE> sp(stdout, [](FILE*){});
            auto iodump = std::make_unique<TextOprIODump>(
                    ret.load_config.comp_graph.get(), sp);
            iodump->print_addr(false);
            ret.iodump = std::move(iodump);
            continue;
        }
        if (!strcmp(argv[i], "--io-dump-stderr")) {
            mgb_log_warn("enable opr io dump to stderr");
            std::shared_ptr<FILE> sp(stderr, [](FILE*){});
            auto iodump = std::make_unique<TextOprIODump>(
                    ret.load_config.comp_graph.get(), sp);
            iodump->print_addr(false);
            ret.iodump = std::move(iodump);
            continue;
        }
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
        if (!strcmp(argv[i], "--bin-io-dump")) {
            mgb_log_warn("enable opr binary io dump");
            ++ i;
            mgb_assert(i < argc,
                    "output directory not given for --bin-io-dump");
            ret.iodump = std::make_unique<BinaryOprIODump>(
                    ret.load_config.comp_graph.get(), argv[i]);
            continue;
        }
        if (!strcmp(argv[i], "--bin-out-dump")) {
1371
            ++ i;
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
            mgb_assert(i < argc,
                    "output directory not given for --bin-out-dump");
            ret.bin_out_dump = argv[i];
            continue;
        }
        if (!strcmp(argv[i], "--iter")) {
            ++ i;
            mgb_assert(i < argc, "value not given for --iter");
            ret.nr_run = std::stoi(argv[i]);
            mgb_assert(ret.nr_run >= 0);
            continue;
        }
        if (!strcmp(argv[i], "--warmup-iter")) {
            ++ i;
            mgb_assert(i < argc, "value not given for --warmup-iter");
            ret.nr_warmup = std::stoi(argv[i]);
            mgb_assert(ret.nr_warmup >= 0);
            continue;
        }
        if (!strcmp(argv[i], "--range")) {
            ++ i;
            mgb_assert(i < argc, "value not given for --range");
            auto range = std::atof(argv[i]);
            mgb_assert(range > 0);
            ret.num_range_checker = std::make_unique<NumRangeChecker>(
                    ret.load_config.comp_graph.get(), range);
            continue;
        }
        if (!strcmp(argv[i], "--check-dispatch")) {
            ret.cpu_dispatch_checker =
                std::make_unique<CPUDispatchChecker>(
                        ret.load_config.comp_graph.get());
            continue;
        }
        if (!strcmp(argv[i], "--disable-mem-opt")) {
            graph_opt.seq_opt.enable_mem_reuse_alloc = false;
            graph_opt.seq_opt.enable_mem_plan_opt = false;
            continue;
        }
        if (!strcmp(argv[i], "--copy-to-host")) {
            ret.copy_to_host = true;
            continue;
        }
        if (!strcmp(argv[i], "--verbose")) {
            graph_opt.log_level = 2;
            set_log_level(LogLevel::DEBUG);
            continue;
        }
        if (!strcmp(argv[i], "--check-var-value")) {
            ++ i;
            mgb_assert(i < argc, "value not given for --check-var-value");
            std::string arg(argv[i]);
            auto sep = arg.find(':');
            size_t switch_interval, start = 0;
            if (sep != std::string::npos) {
                switch_interval = std::stoul(arg.substr(0, sep));
                start = std::stoul(arg.substr(sep + 1));
            } else {
                switch_interval = std::stoul(arg);
            }
            ret.var_value_checker = std::make_unique<VarValueChecker>(
                    ret.load_config.comp_graph.get(), switch_interval, start);
            continue;
        }
        if (!strcmp(argv[i], "--no-sanity-check")) {
            graph_opt.var_sanity_check_first_run = false;
            continue;
        }
        if (!strcmp(argv[i], "--fake-first")) {
            graph_opt.fake_next_exec = true;
            continue;
        }
        if (!strcmp(argv[i], "--record-comp-seq")) {
            graph_opt.comp_node_seq_record_level = 1;
            continue;
        }
        if (!strcmp(argv[i], "--record-comp-seq2")) {
            graph_opt.comp_node_seq_record_level = 2;
            continue;
        }
1452
#ifndef __IN_TEE_ENV__
1453
#if MGB_ENABLE_JSON
1454 1455 1456
        if (!strcmp(argv[i], "--get-static-mem-info")) {
            ++i;
            mgb_assert(i < argc, "value not given for --get-static-mem-info");
1457
            ret.static_mem_log_dir_path = argv[i];
1458 1459
            continue;
        }
1460
#endif
1461
#endif
1462 1463
#if MGB_ENABLE_FASTRUN
        if (!strcmp(argv[i], "--fast-run")) {
1464
            ret.use_fast_run = true;
1465 1466
            continue;
        }
1467 1468
        if (!strcmp(argv[i], "--full-run")) {
            ret.use_full_run = true;
1469 1470 1471 1472 1473 1474 1475 1476
            continue;
        }
#endif
        if (!strcmp(argv[i], "--fast-run-algo-policy")) {
            ++i;
            ret.fast_run_cache_path = argv[i];
            continue;
        }
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
        if (!strcmp(argv[i], "--fast-run-shared-batch-size")) {
            ++i;
            mgb_assert(i < argc,
                       "value not given for --fast-run-shared-batch-size");
            int32_t batch_size = std::stoi(argv[i]);
            mgb_assert(batch_size >= 0);
            graph_opt.fast_run_config.shared_batch_size = batch_size;
            continue;
        }
        if (!strcmp(argv[i], "--binary-equal-between-batch")) {
            graph_opt.fast_run_config.binary_equal_between_batch = true;
            ret.reproducible = true;
            continue;
        }
1491 1492 1493 1494
        if (!strcmp(argv[i], "--reproducible")) {
            ret.reproducible = true;
            continue;
        }
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
        if (!strcmp(argv[i], "--const-shape")) {
            ret.load_config.const_var_shape = true;
            continue;
        }
        if (!strcmp(argv[i], "--share-param-mem")) {
            ret.share_param_mem = true;
            continue;
        }
        if (!strcmp(argv[i], "--disable-assert-throw")) {
            ret.disable_assert_throw = true;
            continue;
        }
        if (!strcmp(argv[i], "--workspace-limit")) {
            ++i;
            ret.workspace_limit = std::stoll(argv[i]);
            continue;
        }
#if __linux__ || __unix__
        if (!strcmp(argv[i], "--wait-gdb")) {
            printf("wait for gdb attach (pid=%d): ", getpid());
            getchar();
            continue;
        }
        if (!strcmp(argv[i], "--c-opr-lib")) {
            ++ i;
1520
            ret.c_opr_args.is_run_c_opr = true;
1521 1522 1523 1524
            mgb_assert(i < argc, "value not given for --c-opr-lib");
            auto handle = dlopen(argv[i], RTLD_LAZY);
            mgb_assert(handle, "failed to open c opr lib %s: %s",
                    argv[i], dlerror());
1525
            const char* entry = MGB_C_OPR_INIT_FUNC_STR;
1526 1527 1528 1529 1530 1531 1532
            auto func = dlsym(handle, entry);
            mgb_assert(func, "can not resolve %s: %s", entry, dlerror());
            typedef void (*entry_f_t)(void*);
            reinterpret_cast<entry_f_t>(func)(
                    reinterpret_cast<void*>(
                        &mgb_get_extern_c_opr_api_versioned));
            printf("loaded C opr library: %s\n", argv[i]);
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562

            entry = "copr_param_device_ptr_malloc";
            func = dlsym(handle, entry);
            if (func) {
                printf("get %s from: %s\n", entry, argv[i]);
                ret.c_opr_args.copr_param_device_ptr_malloc =
                        reinterpret_cast<COprArgs::COPR_PARAM_DEVICE_PTR_MEM_T>(
                                func);
            }
            entry = "copr_param_device_ptr_free";
            func = dlsym(handle, entry);
            if (func) {
                printf("get %s from: %s\n", entry, argv[i]);
                ret.c_opr_args.copr_param_device_ptr_free =
                        reinterpret_cast<COprArgs::COPR_PARAM_DEVICE_PTR_MEM_T>(
                                func);
            }
            entry = "copr_param_device_ptr_h2d";
            func = dlsym(handle, entry);
            if (func) {
                printf("get %s from: %s\n", entry, argv[i]);
                ret.c_opr_args.copr_param_device_ptr_h2d =
                        reinterpret_cast<COprArgs::COPR_PARAM_DEVICE_PTR_H2D_T>(
                                func);
            }

            continue;
        }
        if (!strcmp(argv[i], "--c-opr-lib-with-param")) {
            ret.c_opr_args.is_run_c_opr_with_param = true;
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
            continue;
        }
#endif
        if (!strcmp(argv[i], "--thread")) {
            ++ i;
            mgb_assert(i < argc, "value not given for --thread");
            ret.nr_thread = std::stoi(argv[i]);
            continue;
        }
        if (!strcmp(argv[i], "--enable-jit")) {
            graph_opt.graph_opt.jit = 1;
            continue;
        }
1576 1577 1578 1579 1580
        if (!strcmp(argv[i], "--weight-preprocess")) {
            mgb_log_warn("enable weight-preprocess optimization");
            graph_opt.graph_opt.enable_weight_preprocess();
            continue;
        }
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
        if (!strcmp(argv[i], "--layout-transform")) {
            ret.layout_transform = true;
            ++i;
            if (i >= argc) {
                --i;
                continue;
            }
           
            using Target = gopt::GraphTuningOptions::Target;
            if (!strcmp(argv[i], "cuda")) {
                ret.layout_transform_target = Target::CUDA;
            } else if (!strcmp(argv[i], "x86")) {
                ret.layout_transform_target = Target::X86;
            } else if (!strcmp(argv[i], "arm")) {
                ret.layout_transform_target = Target::ARM;
            } else if (!strcmp(argv[i], "opencl")) {
                ret.layout_transform_target = Target::OPENCL;
            } else if (!strncmp(argv[i], "--", 2)) {
                --i;
            } else {
                mgb_assert(false,
                           "unsupported target(got:%s) for global layout "
                           "transform",
                           argv[i]);
            }

            continue;
        }
1609

1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
        if (!strcmp(argv[i], "--layout-transform-dump")) {
            ++i;
            mgb_assert(i < argc,
                       "dump path not given for --layout-transform-dump");
            mgb_assert(strncmp(argv[i], "--", 2),
                       "dump path not given for --layout-transform-dump");
            ret.layout_transform_dump_path = argv[i];
            continue;
        }
       
1620 1621 1622 1623 1624
        fprintf(stderr, "invalid arg: %s\n", argv[i]);
        ret.args_parse_ret = -1;
        return ret;
    }

1625 1626 1627 1628 1629 1630 1631 1632
#if MGB_ENABLE_FASTRUN
    if (graph_opt.fast_run_config.shared_batch_size) {
        mgb_assert(ret.use_fast_run || ret.use_full_run ||
                           !ret.fast_run_cache_path.empty(),
                   "--fast-run-shared-batch-size should be used with "
                   "--fast-run/--full-run/--fast-run-algo-policy");
    }
#endif
1633 1634 1635 1636
    return ret;
}

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}