inference.h 16.1 KB
Newer Older
1 2 3 4
/**
 * \file src/gopt/include/megbrain/gopt/inference.h
 * 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 14
 *
 * 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.
 */

#pragma once

#include "megbrain/gopt/framework.h"
15
#include "megbrain/graph/cg.h"
16
#include "megbrain/opr/dnn/convolution.h"
17
#include "megbrain/opr/search_policy/algo_chooser_helper.h"
18

19 20 21 22
#if MGB_CUDA
#include <cuda.h>
#endif

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
namespace mgb {
namespace gopt {

    /*!
     * \brief redistribute SharedDeviceTensor oprs
     *
     * Redistribute parameters. For example, ``conv(x, w) * k`` may be replaced
     * by ``conv(x, w*k)``.
     *
     * Usually this pass is used before ParamFusePass.
     */
    class ParamRedistributePass final: public Pass {
        class Impl;

        public:
            const char* name() const override;

            void apply(OptState &opt) const override;
    };


    /*!
     * \brief fuse SharedDeviceTensor oprs
     *
     * This would treat all SharedDeviceTensor operators as constant, and
     * replace oprs that only depend on them by the evaluated value at compile
     * time.
     *
     * Usually this pass is used after ParamRedistributePass.
     */
    class ParamFusePass final: public Pass {
        class ConstVarPropogateWithSizeCheck;
        class VarNamer;

        size_t m_param_grow_limit = std::numeric_limits<size_t>::max();

        public:
            /*!
             * \brief set the limit for max param size growth due to merging
             *
             * Param size may grow if param fusing causes low-rank result (i.e.
             * by broadcasting). Size growth is defined to be the difference
             * between new param size and max size of source oprs that it
             * depends on.
             *
             * This limit is given in bytes
             */
            ParamFusePass& param_grow_limit(size_t val) {
                m_param_grow_limit = val;
                return *this;
            }

            const char* name() const override;

            void apply(OptState &opt) const override;
    };

    /*!
     * \brief replace the dtype of opr from float32 to float16.
     */
    class ConvertF32ToF16Pass : public Pass {
        ThinHashMap<Typeinfo*, thin_function<OperatorNodeBase*(
                                       OperatorNodeBase*, const VarNodeArray&)>>
                m_opr_replace_func;
        VarReplaceCheckFlag m_var_replace_check_flag =
            VarReplaceCheckFlag::CHECK_ALL;

    public:
        const char* name() const override;

        ConvertF32ToF16Pass& set_var_replace_check_flag(
                VarReplaceCheckFlag flag) {
            m_var_replace_check_flag = flag;
            return *this;
        }

        void apply(OptState& opt) const override;

        static std::unique_ptr<ConvertF32ToF16Pass> make(bool use_f32_comp);
    };

    /*!
     * \brief convert tensor format to speed up inference on certain devices
     */
    class ConvertFormatPass : public Pass {
        ThinHashMap<Typeinfo*, thin_function<OperatorNodeBase*(
                                       OperatorNodeBase*, const VarNodeArray&)>>
                m_opr_replace_func;
        VarReplaceCheckFlag m_var_replace_check_flag =
                VarReplaceCheckFlag::CHECK_ALL;

    public:
        const char* name() const override {
            return mgb_cstr_log("convert_format_nhwcd4");
        }

        ConvertFormatPass& set_var_replace_check_flag(
                VarReplaceCheckFlag flag) {
            m_var_replace_check_flag = flag;
            return *this;
        }

        void apply(OptState& opt) const override;

        static std::unique_ptr<ConvertFormatPass> make_nhwcd4_converter();
    };

    /*!
     * \brief convert batch norm to elemwise
     * For inference phase, cudnnbn = scale * (x - mean) / variance + bias
     * In order to make the latter ParamDistributePass + ParamFusePass
     * to do const folding better
     */
    class ConvertBatchNormToElemwisePass : public Pass {
    public:
        const char* name() const override;
        void apply(OptState& opt) const override;
    };

    /*!
     * \brief fuse convolution, bias add, relu oprs to a ConvBiasForward opr
     */
    class FuseConvBiasNonlinPass : public Pass {
    public:
        const char* name() const override;
        void apply(OptState& opt) const override;
    };

    /*!
     * \brief fuse ConvBias, z oprs to a ConvBiasForward opr
     */
    class FuseConvBiasZPass : public Pass {
    public:
        const char* name() const override;
        void apply(OptState& opt) const override;
    };

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    /*!
     * \brief fuse preprocess, like pad channel, quint8 to qint8
     */
    class FuseNCHW4Int8Preprocess : public Pass {
    public:
        const char* name() const override;
        void apply(OptState& opt) const override;
        static std::unique_ptr<FuseNCHW4Int8Preprocess> make();
        using DepType = cg::OperatorNodeProp::DepType;
        using ReaderType =
                ThinHashMap<OperatorNodeBase*,
                            SmallVector<std::pair<OperatorNodeBase*, DepType>>>;

    private:
        ThinHashMap<Typeinfo*, thin_function<OperatorNodeBase*(
                                       OperatorNodeBase*, const VarNodeArray&,
                                       SubGraph::Rewriter&, ReaderType&)>>
                m_opr_replace_func;
    };

180 181 182 183 184 185 186 187 188 189
    /*!
     * \brief fuse warp perspective and dimshuffle, quint8/uint8 to qint8/float
     */
    class FuseWarpPerspectiveDimshufflePass : public Pass {
        public:
            const char* name() const override;
            void apply(OptState& opt) const override;
    };


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 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 260 261 262 263 264 265 266 267
    /*!
     * \brief fuse deconv and typecvt to a deconv opr
     */
    class FuseDeconvCvtPass : public Pass {
    public:
        const char* name() const override;
        void apply(OptState& opt) const override;
    };

    /*!
     * \brief merge all the SharedDeviceTensor oprs into one
     *      MultipleDeviceTensorHolder
     */
    class ParamMergePass final : public Pass {
    public:
        const char* name() const override;
        void apply(OptState& opt_state) const override;
    };

    /*!
     * \brief tensor format converter to accelerate inference speed on Nvidia
     * platform
     */
    class TensorReformatPass : public Pass {
        //! replace rule for endpoint var of computing graph
        virtual VarNode* on_graph_endpoint_var(VarNode* new_var,
                                               VarNode* orig_var) const = 0;
        //! insert relayout placeholder
        //! (nchw4->nchw32/nchw32->nchw4/nchw4->chwn4/chwn4->nchw4)
        void insert_pass(OptState& opt) const;
        //! translate relayout placeholder to actual implementation
        void translate_pass(OptState& opt) const;

    protected:
        ThinHashMap<Typeinfo*, thin_function<OperatorNodeBase*(
                                       OperatorNodeBase*, const VarNodeArray&)>>
                m_opr_replace_func;
        VarReplaceCheckFlag m_var_replace_check_flag =
                VarReplaceCheckFlag::CHECK_ALL;
        class RelayoutPlaceholder;

    public:
        TensorReformatPass& set_var_replace_check_flag(VarReplaceCheckFlag flag) {
            m_var_replace_check_flag = flag;
            return *this;
        }
        void apply(OptState& opt) const override;
    };

    /*!
     * \brief enable using tensorcore on Turing architecture
     */
    class EnableTensorCorePass final : public TensorReformatPass {
        VarNode* on_graph_endpoint_var(VarNode* new_var,
                                   VarNode* orig_var) const override;
    public:
        const char* name() const override {
            return mgb_cstr_log("enable_tensorcore");
        }
        //! make enable tensorcore opt pass
        static std::unique_ptr<EnableTensorCorePass> make_tensorcore_converter();
    };

    /*!
     * \brief enable using chwn4 tensor format on Nvidia Platform with compute
     * capability 6.1 or later
     */
    class EnableCHWN4Pass final : public TensorReformatPass {
        ThinHashSet<VarNode*> m_varshape_changed;
        VarNode* on_graph_endpoint_var(VarNode* new_var,
                                   VarNode* orig_var) const override;
    public:
        const char* name() const override { return mgb_cstr_log("enable_chwn4"); }

        //! make nchw4 -> chwn4 converter opt pass
        static std::unique_ptr<EnableCHWN4Pass> make_chwn4_converter();
    };

268 269 270 271 272 273 274
    /*!
     * \brief convert tensor format to nchw4 to speed up inference on CUDA
     */
    class EnableNCHW4Pass final : public TensorReformatPass {
        VarNode* on_graph_endpoint_var(VarNode* new_var,
                                       VarNode* orig_var) const override;
    public:
275 276 277 278
        const char* name() const override {
            return mgb_cstr_log("tensor_format_nchw4");
        }

279 280 281 282
        //! make nchw -> nchw4 converter opt pass
        static std::unique_ptr<EnableNCHW4Pass> make_nchw4_converter();
    };

283 284 285 286
    /*!
     * \brief convert tensor format to nchwxx to speed up inference on certain
     * devices
     */
287
    class EnableNchwxxPass : public TensorReformatPass {
288
        std::string m_name = "tensor_format_nchwxx";
289
        size_t m_pack_c_size;
290 291
        VarNode* on_graph_endpoint_var(VarNode* new_var,
                                       VarNode* orig_var) const override;
292 293 294
    public:
        EnableNchwxxPass(size_t pack_c_size) : m_pack_c_size(pack_c_size) {}

295 296
        //! the flag for conv to transform to nchwxx
        enum class TransType {
297 298
            TRANS_PURE_NCHWXX,    //!< weight and src all trans to nchwxx
            TRANS_HYBIRD_NCHWXX,  //!< input is nchw, output is nchwxx
299 300 301 302 303 304
            TRANS_NONE,           //!< no need trans
        };
        const char* name() const override {
            return mgb_cstr_log(m_name.c_str());
        }
        void set_name(std::string in_name) { m_name = in_name; }
305 306 307

        void fill_opr_convert_fun(size_t pack_c_size);

308 309 310 311 312 313
        //! make nchw -> nchwxx converter opt pass, pack_c_size is the x, like
        //! 4,8,16
        static std::unique_ptr<EnableNchwxxPass> make_nchwxx_converter(
                size_t pack_c_size);
    };

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    /*!
     * \brief convert tensor format from nchw44 to nchw44_dot to speed up
     * inference on armv8.2
     */
    class EnableNchw44DotPass final : public EnableNchwxxPass {
        std::string m_name = "tensor_format_nchw44_dot";
        VarNode* on_graph_endpoint_var(VarNode* new_var,
                                       VarNode* orig_var) const override;

    public:
        EnableNchw44DotPass() : EnableNchwxxPass(4) {}
        //! make nchw44 -> nchw44_dot converter opt pass
        static std::unique_ptr<EnableNchw44DotPass> make_nchw44_dot_converter();
    };

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
    struct OptimizeForInferenceOptions : cg::GraphCommonOptimizeOptions {
        uint64_t serialize() {
            uint64_t ret = 0;
            ret |= (uint64_t)layout_transform << 32;
            if (f16_io_f32_comp) ret |= 1u;
            if (f16_io_comp) ret |= 1u << 1;
            if (fuse_conv_bias_nonlinearity) ret |= 1u << 2;
            if (fuse_conv_bias_with_z) ret |= 1u << 3;
            if (weight_preprocess) ret |= 1u << 4;
            if (fuse_preprocess) ret |= 1u << 5;
            return ret;
        }

        static OptimizeForInferenceOptions deserialize(uint64_t buf) {
            OptimizeForInferenceOptions ret;
            ret.f16_io_f32_comp = buf & 1u;
            ret.f16_io_comp = buf & 1u << 1;
            ret.fuse_conv_bias_nonlinearity = buf & 1u << 2;
            ret.fuse_conv_bias_with_z = buf & 1u << 3;
            ret.weight_preprocess = buf & 1u << 4;
            ret.fuse_preprocess = buf & 1u << 5;
            ret.layout_transform = (LayoutTransform)(buf >> 32);
            return ret;
        }
    };
354 355 356 357 358 359 360 361 362 363 364

    /*!
     * \brief optimize a computing graph for inference
     *
     * This function applies a set of predefined optimizer passes to optimize
     * for inference. It assumes all params are constant.
     */
    SymbolVarArray optimize_for_inference(
            const SymbolVarArray& dest_vars,
            const OptimizeForInferenceOptions& opt = {});

365 366 367 368 369 370 371 372 373
    /*!
     * \brief modify execution strategy for oprs with multiple
     *      algorithms
     *
     * This would modify the operators inplace. It can be used for implement
     * the fast-run mode.
     */
    void modify_opr_algo_strategy_inplace(
            const VarNodeArrayView& dest_vars,
374
            opr::mixin::AlgoChooserHelper::ExecutionPolicy::Strategy strategy);
375

376 377 378 379 380 381 382 383 384 385 386 387 388
    /*!
     * \brief enable PROFILE execution strategy for oprs with multiple
     *      algorithms
     *
     * This would modify the operators inplace. It is usually used to implement
     * the fast-run mode.
     *
     * You may want to implement TimedFuncInvoker::ForkExecImpl and/or
     * PersistentCache for better performance in an SDK.
     */
    void enable_opr_algo_profiling_inplace(const VarNodeArrayView& dest_vars);

    /*!
389
     * \brief enable opr try profiling cache first, if failed, fallback to
390 391 392 393 394 395 396 397
     * heuristic
     *
     * This would modify the operators inplace. It is usually used to enable
     * fast-run's cache when fast-run mode is disabled.
     *
     * You may want to implement TimedFuncInvoker::ForkExecImpl and/or
     * PersistentCache for better performance in an SDK.
     */
398 399
    void enable_opr_use_profiling_cache_inplace(
            const VarNodeArrayView& dest_vars);
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

    /*!
     * \brief set workspace_limit for execution strategy for oprs with multiple
     *      algorithms
     *
     * This would modify the operators inplace. It is usually used to implement
     * the fast-run mode.
     *
     * \warning It will influence the default algo choosed, and maybe slower but
     * save memory.
     */
    void set_opr_algo_workspace_limit_inplace(
            const VarNodeArrayView& dest_vars, size_t workspace_limit);

    /*!
     * \brief transform consecutive tensor shuffle operations into
     * one shuffle operator or a Nop
     *
     * Transform shuffle/typecvt operator chains to one shuffle operator and
     * multiple typecvt operators. For example, a operator chain like
     * reformat(nchw -> nchw4), asQuantizedS8, reformat(nchw4 -> nchw),
     * asFloat32, would be changed to asQuantizedS8, asFloat32. Since the
     * reciprocal reformat operations have been removed from the operator chain,
     * the computation can be speed up with fewer memory operations. This pass
     * is usually used after EnableTensorCorePass, TensorRTReplacePass.
     */
    class ShuffleShuffleRemovePass final : public Pass {
        class Impl;

        public:
            const char* name() const override;
            void apply(OptState& opt) const override;
    };

434
#if CUDA_VERSION >= 10020
435 436 437 438 439
    class FoldingConvBiasDimshufflePass final : public Pass {
        public:
            const char* name() const override;
            void apply(OptState& opt) const override;
    };
440
#endif
441

442 443 444 445 446 447 448 449 450 451
    /*!
     * \brief padding channel to enable fast int8/int4 support
     * assume input network is built in NCHW tensor format
     */
    class PaddingChannelPass final : public Pass {
        public:
            const char* name() const override;
            void apply(OptState& opt) const override;
    };

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    /*!
     * \brief convert tensor format to nchw64 to enable tensorcore int4 on CUDA
     * we assume that the input network is in NCHW layout
     */
    class EnableNCHW64Pass final : public TensorReformatPass {
    public:
        using Format = opr::ConvBias::Param::Format;
        const char* name() const override {
            return mgb_cstr_log("tensor_format_nchw64");
        }

        //! make nchw -> nchw64 converter opt pass
        static std::unique_ptr<EnableNCHW64Pass> make_nchw64_converter();

    private:
        ThinHashMap<OperatorNodeBase*, Format> m_opr_format_map;

        VarNode* on_graph_endpoint_var(VarNode* new_var,
                                       VarNode* orig_var) const override;
    };

473 474 475 476
}  // namespace gopt
} // namespace mgb

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