fastrun_options.cpp 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 90 91 92 93 94 95 96 97 98 99 100 101 102 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 223 224 225 226 227 228 229 230 231
/**
 * \file lite/load_and_run/src/options/fastrun_options.cpp
 *
 * This file is part of MegEngine, a deep learning framework developed by
 * Megvii.
 *
 * \copyright Copyright (c) 2020-2021 Megvii Inc. All rights reserved.
 */

#include <gflags/gflags.h>

#if defined(_WIN32)
#include <io.h>
#define F_OK         0
#define access(a, b) _access(a, b)
#elif __linux__ || __unix__ || __APPLE__
#include <unistd.h>
#endif
#include "fastrun_options.h"
#include "megbrain/gopt/inference.h"
#include "megbrain/utils/infile_persistent_cache.h"
#include "misc.h"
#include "models/model_lite.h"
#include "models/model_mdl.h"

namespace lar {

template <>
void FastRunOption::config_model_internel<ModelLite>(
        RuntimeParam& runtime_param, std::shared_ptr<ModelLite> model) {
    if (runtime_param.stage == RunStage::BEFORE_MODEL_LOAD) {
        //! set the algo policy before model load
        using Strategy = ModelLite::Strategy;
        uint32_t strategy = 0;
#if MGB_ENABLE_FASTRUN
        if (enable_full_run) {
            LITE_WARN("enable full-run strategy for algo profile");
            strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_PROFILE) | strategy;
        } else if (enable_fast_run) {
            LITE_WARN("enable fast-run strategy for algo profile");
            strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_PROFILE) |
                       static_cast<uint32_t>(Strategy::LITE_ALGO_OPTIMIZED) | strategy;
        } else {
            strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_HEURISTIC) | strategy;
        }
#else
        strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_HEURISTIC) | strategy;
#endif
        if (batch_binary_equal || enable_reproducible) {
            LITE_WARN("enable reproducible strategy for algo profile");
            if (batch_binary_equal)
                strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_REPRODUCIBLE) |
                           strategy;
        }
        auto lite_strategy = static_cast<Strategy>(strategy);
        model->set_lite_strategy(lite_strategy);
    } else if (runtime_param.stage == RunStage::AFTER_MODEL_LOAD) {
        auto lite_network = model->get_lite_network();
        auto lite_strategy = model->get_lite_strategy();
        //! set algo policy for model
        lite::Runtime::set_network_algo_policy(
                lite_network, lite_strategy, share_batch_size, batch_binary_equal);
        if (!m_fast_run_cache.empty()) {
            if (!access(m_fast_run_cache.c_str(), F_OK)) {
                lite::set_persistent_cache(m_fast_run_cache);
            } else {
                lite::set_persistent_cache(m_fast_run_cache, true);
            }
            //! TODO:this is from mdl model settings but not matched settings in
            //! lite model
            // if (!enable_full_run && !enable_fast_run)
            //     mgb::gopt::enable_opr_use_profiling_cache_inplace(vars);
        }
    } else if (runtime_param.stage == RunStage::AFTER_MODEL_RUNNING) {
#if MGB_ENABLE_FASTRUN
        //! dump algo cache
        if (!m_fast_run_cache.empty()) {
            lite::dump_persistent_cache(m_fast_run_cache);
        }
#endif
    }
}

template <>
void FastRunOption::config_model_internel<ModelMdl>(
        RuntimeParam& runtime_param, std::shared_ptr<ModelMdl> model) {
    if (runtime_param.stage == RunStage::BEFORE_MODEL_LOAD) {
        //! set the algo policy before model load
        using Strategy = ModelMdl::Strategy;
        auto strategy = static_cast<Strategy>(0);
#if MGB_ENABLE_FASTRUN
        if (enable_full_run) {
            mgb_log_warn("enable full-run strategy for algo profile");
            strategy = Strategy::PROFILE | strategy;
        } else if (enable_fast_run) {
            mgb_log_warn("enable fast-run strategy for algo profile");
            strategy = Strategy::PROFILE | Strategy::OPTIMIZED | strategy;
        } else {
            strategy = Strategy::HEURISTIC | strategy;
        }
#else
        strategy = Strategy::HEURISTIC | strategy;
#endif
        if (batch_binary_equal || enable_reproducible) {
            mgb_log_warn("enable reproducible strategy for algo profile");
            strategy = Strategy::REPRODUCIBLE | strategy;
        }
        model->set_mdl_strategy(strategy);

        //! set binary_equal_between_batch and shared_batch_size
        if (batch_binary_equal) {
            mgb_log_warn("enable batch binary equal");
            model->get_mdl_config()
                    .comp_graph->options()
                    .fast_run_config.binary_equal_between_batch = true;
        }
        if (share_batch_size > 0) {
            mgb_log_warn("set shared shared batch");
            model->get_mdl_config()
                    .comp_graph->options()
                    .fast_run_config.shared_batch_size = share_batch_size;
        }
    } else if (runtime_param.stage == RunStage::AFTER_MODEL_LOAD) {
        auto vars = model->get_mdl_load_result().output_var_list;
        auto strategy = model->get_mdl_strategy();
        mgb::gopt::modify_opr_algo_strategy_inplace(vars, strategy);
        // set algo cache path
        if (!m_fast_run_cache.empty()) {
            if (!access(m_fast_run_cache.c_str(), F_OK)) {
                mgb::PersistentCache::set_impl(
                        std::make_shared<mgb::InFilePersistentCache>(
                                m_fast_run_cache.c_str()));
            } else {
                mgb::PersistentCache::set_impl(
                        std::make_shared<mgb::InFilePersistentCache>());
            }
#if MGB_ENABLE_FASTRUN
            if (!enable_full_run && !enable_fast_run)
#endif
                mgb::gopt::enable_opr_use_profiling_cache_inplace(vars);
        }
    } else if (runtime_param.stage == RunStage::AFTER_MODEL_RUNNING) {
#if MGB_ENABLE_FASTRUN
        //! dump algo cache
        if (!m_fast_run_cache.empty()) {
            static_cast<mgb::InFilePersistentCache&>(mgb::PersistentCache::inst())
                    .dump_cache(m_fast_run_cache.c_str());
        }
#endif
    }
}

}  // namespace lar

using namespace lar;

FastRunOption::FastRunOption() {
    m_option_name = "fastrun";
#if MGB_ENABLE_FASTRUN
    enable_fast_run = FLAGS_fast_run;
    enable_full_run = FLAGS_full_run;
#endif
    batch_binary_equal = FLAGS_binary_equal_between_batch;
    enable_reproducible = FLAGS_reproducible;
    m_fast_run_cache = FLAGS_fast_run_algo_policy;
    share_batch_size = FLAGS_fast_run_shared_batch_size;
#if MGB_ENABLE_FASTRUN
    //! while fastrun cache file path is not empty and can't be accessed
    if (!m_fast_run_cache.empty() && access(m_fast_run_cache.c_str(), F_OK)) {
        mgb_assert(
                enable_full_run || enable_fast_run,
                "--fast-run or --full-run should be enabled");
    }
    if (share_batch_size) {
        mgb_assert(
                enable_full_run || enable_fast_run || !m_fast_run_cache.empty(),
                "--fast-run-shared-batch-size should be used with "
                "--fast-run|--full-run|--fast-run-algo-policy");
    }
#endif
}

bool FastRunOption::is_valid() {
    bool ret = false;
#if MGB_ENABLE_FASTRUN
    ret = ret || FLAGS_fast_run;
    ret = ret || FLAGS_full_run;
#endif
    ret = ret || FLAGS_binary_equal_between_batch;
    ret = ret || FLAGS_fast_run_shared_batch_size > 0;
    ret = ret || FLAGS_reproducible;
    ret = ret || FLAGS_fast_run_algo_policy.size() > 0;

    return ret;
}

std::shared_ptr<OptionBase> FastRunOption::create_option() {
    static std::shared_ptr<FastRunOption> option(new FastRunOption);
    if (FastRunOption::is_valid()) {
        return std::static_pointer_cast<OptionBase>(option);
    } else {
        return nullptr;
    }
}

void FastRunOption::config_model(
        RuntimeParam& runtime_param, std::shared_ptr<ModelBase> model) {
    CONFIG_MODEL_FUN;
}

#if MGB_ENABLE_FASTRUN
DEFINE_bool(fast_run, false, "whether to use fast-run in model run");
DEFINE_bool(full_run, false, "whether to use full-run in model run");
#endif

DEFINE_bool(
        binary_equal_between_batch, false,
        "Each batch of output is promised binary equal if each batch of "
        "input is binary equal\n Note that if this option is turned on, "
        "`--reproducible` will also be turned on.");
DEFINE_bool(
        reproducible, false,
        "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.");
DEFINE_uint32(fast_run_shared_batch_size, 0, "Set the batch size used during fastrun");
DEFINE_string(fast_run_algo_policy, "", "fast-run cache path.");

REGIST_OPTION_CREATOR(fastrun, lar::FastRunOption::create_option);