dnn.sereg.h 25.4 KB
Newer Older
1 2 3 4
/**
 * \file src/opr/impl/dnn/dnn.sereg.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
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
M
Megvii Engine Team 已提交
9 10
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
11 12
 */

M
Megvii Engine Team 已提交
13
#include "megbrain/opr/dnn/adaptive_pooling.h"
14 15
#include "megbrain/opr/dnn/batch_norm.h"
#include "megbrain/opr/dnn/convolution.h"
16
#include "megbrain/opr/dnn/correlation.h"
M
Megvii Engine Team 已提交
17
#include "megbrain/opr/dnn/fake_quant.h"
18 19 20
#include "megbrain/opr/dnn/images2neibs.h"
#include "megbrain/opr/dnn/local.h"
#include "megbrain/opr/dnn/lrn.h"
M
Megvii Engine Team 已提交
21 22
#include "megbrain/opr/dnn/lsq.h"
#include "megbrain/opr/dnn/pooling.h"
23
#include "megbrain/opr/dnn/rnn.h"
M
Megvii Engine Team 已提交
24 25
#include "megbrain/opr/dnn/roi_align.h"
#include "megbrain/opr/dnn/roi_pooling.h"
26
#include "megbrain/opr/dnn/sliding_window_transpose.h"
M
Megvii Engine Team 已提交
27
#include "megbrain/opr/dnn/tqt.h"
28
#include "megbrain/serialization/sereg.h"
29 30
#include "megdnn/opr_param_defs.h"
#include "megdnn/oprs/nn.h"
31 32 33 34

namespace mgb {

namespace serialization {
35 36 37
template <class MegDNNPooling = megdnn::Pooling>
struct MakePoolingCaller1 {
    template <typename Opr>
M
Megvii Engine Team 已提交
38 39
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNPooling::Param& param,
40
            const megdnn::param::ExecutionPolicy& execution_policy,
M
Megvii Engine Team 已提交
41
            const OperatorNodeConfig& config) {
42
        if (inputs.size() == 1) {
43
            return Opr::make(inputs[0], param, execution_policy, config).node();
44
        }
45 46 47 48 49 50 51
        return nullptr;
    }
};

template <class MegDNNROIALIGN = megdnn::ROIAlign>
struct MakeROIAlignCaller1 {
    template <typename Opr>
M
Megvii Engine Team 已提交
52 53 54
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNROIALIGN::Param& param,
            const OperatorNodeConfig& config) {
55 56 57
        if (inputs.size() == 2) {
            return Opr::make(inputs[0], inputs[1], param, config).node();
        } else {
58 59
            return nullptr;
        }
60 61 62 63 64 65
    }
};

template <class MegDNNROIALIGN = megdnn::ROIAlignBackward>
struct MakeROIAlignCaller4 {
    template <typename Opr>
M
Megvii Engine Team 已提交
66 67 68
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNROIALIGN::Param& param,
            const OperatorNodeConfig& config) {
69
        if (inputs.size() == 4) {
M
Megvii Engine Team 已提交
70
            return Opr::make(inputs[0], inputs[1], inputs[2], inputs[3], param, config)
71 72
                    .node();
        } else {
73 74
            return nullptr;
        }
75 76 77 78 79 80
    }
};

template <class MegDNNPooling = megdnn::PoolingBackward>
struct MakePoolingBackwardCaller3 {
    template <typename Opr>
M
Megvii Engine Team 已提交
81 82
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNPooling::Param& param,
83
            const megdnn::param::ExecutionPolicy& execution_policy,
M
Megvii Engine Team 已提交
84
            const OperatorNodeConfig& config) {
85
        if (inputs.size() == 3) {
86 87 88 89
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], param, execution_policy,
                           config)
                    .node();
90
        }
91 92 93 94 95 96 97
        return nullptr;
    }
};

template <class MegDNNPooling = megdnn::AdaptivePoolingBackward>
struct MakeAdaptivePoolingBackwardCaller3 {
    template <typename Opr>
M
Megvii Engine Team 已提交
98 99 100
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNPooling::Param& param,
            const OperatorNodeConfig& config) {
101
        if (inputs.size() == 4) {
M
Megvii Engine Team 已提交
102
            return Opr::make(inputs[0], inputs[1], inputs[2], inputs[3], param, config)
103
                    .node();
104
        }
105 106 107 108 109 110 111
        return nullptr;
    }
};

template <class MegDNNConv = megdnn::Convolution>
struct MakeConvCaller2 {
    template <typename Opr>
M
Megvii Engine Team 已提交
112 113 114 115
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
116
        if (inputs.size() == 2) {
M
Megvii Engine Team 已提交
117
            return Opr::make(inputs[0], inputs[1], param, execution_policy, config)
118
                    .node();
119
        }
120 121 122 123 124 125 126
        return nullptr;
    }
};

template <class MegDNNConv = megdnn::Convolution>
struct MakeConvCaller3 {
    template <typename Opr>
M
Megvii Engine Team 已提交
127 128 129 130
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
131
        if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
132 133 134
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], param, execution_policy,
                           config)
135
                    .node();
136
        }
137 138 139 140 141 142 143
        return nullptr;
    }
};

template <class MegDNNConv = megdnn::Convolution>
struct MakeConvCaller4 {
    template <typename Opr>
M
Megvii Engine Team 已提交
144 145 146 147
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
148
        if (inputs.size() == 4) {
M
Megvii Engine Team 已提交
149 150 151
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], inputs[3], param,
                           execution_policy, config)
152
                    .node();
153
        }
154 155 156 157 158 159 160
        return nullptr;
    }
};

template <class MegDNNConv = megdnn::Convolution>
struct MakeConvCaller5 {
    template <typename Opr>
M
Megvii Engine Team 已提交
161 162 163 164
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
165
        if (inputs.size() == 5) {
M
Megvii Engine Team 已提交
166 167 168
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], inputs[3], inputs[4], param,
                           execution_policy, config)
169
                    .node();
170
        }
171 172 173 174 175 176 177
        return nullptr;
    }
};

template <class MegDNNConv = megdnn::Convolution>
struct MakeConvCallerEmpty {
    template <typename Opr>
M
Megvii Engine Team 已提交
178 179 180
    static VarNode* make(
            const cg::VarNodeArray&, const typename MegDNNConv::Param&,
            const megdnn::param::ExecutionPolicy&, const OperatorNodeConfig&) {
181 182 183 184
        return nullptr;
    }
};

M
Megvii Engine Team 已提交
185 186 187 188 189
template <
        class Opr, class Maker0, class MegDNNConv,
        class Maker1 = MakeConvCallerEmpty<MegDNNConv>,
        class Maker2 = MakeConvCallerEmpty<MegDNNConv>,
        typename ConvParam = megdnn::param::Convolution>
190 191 192 193
struct ConvLoadDumpImpl {
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr_) {
        auto&& opr = opr_.cast_final_safe<Opr>();
        ctx.write_param<ConvParam>(opr.param());
M
Megvii Engine Team 已提交
194 195
        ctx.write_param<megdnn::param::ExecutionPolicy>(
                opr.execution_policy_transient());
196 197
    }

M
Megvii Engine Team 已提交
198 199 200 201 202 203
    static VarNode* make(
            const cg::VarNodeArray& inputs, const ConvParam& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
        VarNode* ret =
                Maker0::template make<Opr>(inputs, param, execution_policy, config);
204
        if (!ret) {
M
Megvii Engine Team 已提交
205
            ret = Maker1::template make<Opr>(inputs, param, execution_policy, config);
206
        }
207
        if (!ret) {
M
Megvii Engine Team 已提交
208
            ret = Maker2::template make<Opr>(inputs, param, execution_policy, config);
M
Megvii Engine Team 已提交
209
        }
210 211 212 213
        mgb_assert(ret);
        return ret;
    }

M
Megvii Engine Team 已提交
214 215 216
    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
217
        auto param = ctx.read_param<ConvParam>();
M
Megvii Engine Team 已提交
218
        auto execution_policy = ctx.read_param<megdnn::param::ExecutionPolicy>();
219 220 221 222
        return make(inputs, param, execution_policy, config)->owner_opr();
    }
};

M
Megvii Engine Team 已提交
223
template <class Opr, class Maker0, typename PoolingParam = megdnn::param::Pooling>
224 225 226 227 228 229
struct PoolingLoadDumpImpl {
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr_) {
        auto&& opr = opr_.cast_final_safe<Opr>();
        ctx.write_param<PoolingParam>(opr.param());
    }

M
Megvii Engine Team 已提交
230 231
    static VarNode* make(
            const cg::VarNodeArray& inputs, const PoolingParam& param,
232
            const megdnn::param::ExecutionPolicy& execution_policy,
M
Megvii Engine Team 已提交
233
            const OperatorNodeConfig& config) {
234 235
        VarNode* ret =
                Maker0::template make<Opr>(inputs, param, execution_policy, config);
236 237 238 239
        mgb_assert(ret);
        return ret;
    }

M
Megvii Engine Team 已提交
240 241 242
    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
243
        auto param = ctx.read_param<PoolingParam>();
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        return make(inputs, param, {}, config)->owner_opr();
    }
};

template <class Opr, class Maker0, typename GeneralOprParam = megdnn::param::ROIAlign>
struct GeneralOprLoadDumpImpl {
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr_) {
        auto&& opr = opr_.cast_final_safe<Opr>();
        ctx.write_param<GeneralOprParam>(opr.param());
    }

    static VarNode* make(
            const cg::VarNodeArray& inputs, const GeneralOprParam& param,
            const OperatorNodeConfig& config) {
        VarNode* ret = Maker0::template make<Opr>(inputs, param, config);
        mgb_assert(ret);
        return ret;
    }

    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
        auto param = ctx.read_param<GeneralOprParam>();
267 268 269 270 271 272 273
        return make(inputs, param, config)->owner_opr();
    }
};

template <>
struct OprMaker<opr::TQTBackward, 3> {
    using Param = opr::TQTBackward::Param;
M
Megvii Engine Team 已提交
274 275 276
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
277 278 279 280 281 282 283
        MGB_MARK_USED_VAR(graph);
        return opr::TQTBackward::make(i[0], i[1], i[2], param, config)[0]
                .node()
                ->owner_opr();
    }
};

M
Megvii Engine Team 已提交
284 285 286
template <>
struct OprMaker<opr::LSQBackward, 5> {
    using Param = opr::LSQBackward::Param;
M
Megvii Engine Team 已提交
287 288 289
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
M
Megvii Engine Team 已提交
290
        MGB_MARK_USED_VAR(graph);
M
Megvii Engine Team 已提交
291
        return opr::LSQBackward::make(i[0], i[1], i[2], i[3], i[4], param, config)[0]
M
Megvii Engine Team 已提交
292 293 294 295
                .node()
                ->owner_opr();
    }
};
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

template <>
struct OprMaker<opr::RNNBackward, 7> {
    using Param = opr::RNNBackward::Param;
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
        MGB_MARK_USED_VAR(graph);
        return opr::RNNBackward::make(
                       i[0], i[1], i[2], i[3], i[4], i[5], i[6], param, config)[0]
                .node()
                ->owner_opr();
    }
};

template <>
struct OprMaker<opr::LSTMBackward, 9> {
    using Param = opr::LSTMBackward::Param;
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
        MGB_MARK_USED_VAR(graph);
        return opr::LSTMBackward::make(
                       i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8], param,
                       config)[0]
                .node()
                ->owner_opr();
    }
};

326 327
template <>
struct OprLoadDumpImpl<opr::AdaptivePoolingBackward, 0>
328
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
329 330 331
                  opr::AdaptivePoolingBackward,
                  MakeAdaptivePoolingBackwardCaller3<megdnn::AdaptivePoolingBackward>,
                  megdnn::param::AdaptivePooling> {};
332 333 334

template <>
struct OprLoadDumpImpl<opr::AdaptivePooling, 0>
335
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
336
                  opr::AdaptivePooling, MakeROIAlignCaller1<megdnn::AdaptivePooling>,
337 338 339 340
                  megdnn::param::AdaptivePooling> {};

template <>
struct OprLoadDumpImpl<opr::ROIAlign, 0>
341
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
342 343
                  opr::ROIAlign, MakeROIAlignCaller1<megdnn::ROIAlign>,
                  megdnn::param::ROIAlign> {};
344 345 346

template <>
struct OprLoadDumpImpl<opr::ROIAlignBackward, 0>
347
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
348
                  opr::ROIAlignBackward, MakeROIAlignCaller4<megdnn::ROIAlignBackward>,
349 350 351 352
                  megdnn::param::ROIAlign> {};

template <>
struct OprLoadDumpImpl<opr::Pooling, 0>
M
Megvii Engine Team 已提交
353 354 355
        : public PoolingLoadDumpImpl<
                  opr::Pooling, MakePoolingCaller1<megdnn::Pooling>,
                  megdnn::param::Pooling> {};
356 357 358 359 360 361 362 363 364 365

template <>
struct OprLoadDumpImpl<opr::PoolingBackward, 0>
        : public PoolingLoadDumpImpl<
                  opr::PoolingBackward,
                  MakePoolingBackwardCaller3<megdnn::PoolingBackward>,
                  megdnn::param::Pooling> {};

template <>
struct OprLoadDumpImpl<opr::Convolution, 0>
M
Megvii Engine Team 已提交
366 367 368
        : public ConvLoadDumpImpl<
                  opr::Convolution, MakeConvCaller2<megdnn::Convolution>,
                  megdnn::Convolution> {};
369 370
template <>
struct OprLoadDumpImpl<opr::ConvolutionBackwardData, 0>
M
Megvii Engine Team 已提交
371 372 373
        : public ConvLoadDumpImpl<
                  opr::ConvolutionBackwardData, MakeConvCaller2<megdnn::Convolution>,
                  megdnn::Convolution, MakeConvCaller3<megdnn::Convolution>> {};
374 375
template <>
struct OprLoadDumpImpl<opr::ConvolutionBackwardFilter, 0>
M
Megvii Engine Team 已提交
376 377 378
        : public ConvLoadDumpImpl<
                  opr::ConvolutionBackwardFilter, MakeConvCaller3<megdnn::Convolution>,
                  megdnn::Convolution> {};
379 380 381

template <>
struct OprLoadDumpImpl<opr::Convolution3D, 0>
M
Megvii Engine Team 已提交
382 383 384 385 386
        : public ConvLoadDumpImpl<
                  opr::Convolution3D, MakeConvCaller2<megdnn::Convolution3D>,
                  megdnn::Convolution3D, MakeConvCallerEmpty<megdnn::Convolution3D>,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  megdnn::param::Convolution3D> {};
387 388
template <>
struct OprLoadDumpImpl<opr::Convolution3DBackwardData, 0>
M
Megvii Engine Team 已提交
389 390 391 392 393 394
        : public ConvLoadDumpImpl<
                  opr::Convolution3DBackwardData,
                  MakeConvCaller2<megdnn::Convolution3D>, megdnn::Convolution3D,
                  MakeConvCaller3<megdnn::Convolution3D>,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  megdnn::param::Convolution3D> {};
395 396
template <>
struct OprLoadDumpImpl<opr::Convolution3DBackwardFilter, 0>
M
Megvii Engine Team 已提交
397 398 399 400 401 402
        : public ConvLoadDumpImpl<
                  opr::Convolution3DBackwardFilter,
                  MakeConvCaller3<megdnn::Convolution3D>, megdnn::Convolution3D,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  megdnn::param::Convolution3D> {};
403 404
template <>
struct OprLoadDumpImpl<opr::ConvBiasForward, 0>
M
Megvii Engine Team 已提交
405 406 407 408
        : public ConvLoadDumpImpl<
                  opr::ConvBiasForward, MakeConvCaller2<megdnn::ConvBiasForward>,
                  megdnn::ConvBiasForward, MakeConvCaller3<megdnn::ConvBiasForward>,
                  MakeConvCaller4<megdnn::ConvBiasForward>, megdnn::param::ConvBias> {};
409 410
template <>
struct OprLoadDumpImpl<opr::BatchConvBiasForward, 0>
M
Megvii Engine Team 已提交
411 412 413 414 415 416 417
        : public ConvLoadDumpImpl<
                  opr::BatchConvBiasForward,
                  MakeConvCaller2<megdnn::BatchConvBiasForward>,
                  megdnn::BatchConvBiasForward,
                  MakeConvCaller3<megdnn::BatchConvBiasForward>,
                  MakeConvCaller4<megdnn::BatchConvBiasForward>,
                  megdnn::param::BatchConvBias> {};
418 419 420 421

template <>
struct OprMaker<opr::BatchNorm, 0> {
    using Param = opr::BatchNorm::Param;
M
Megvii Engine Team 已提交
422 423 424
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
425 426 427 428 429 430 431
        MGB_MARK_USED_VAR(graph);
        if (i.size() == 3) {
            return opr::BatchNorm::make(i[0], i[1], i[2], param, config)[0]
                    .node()
                    ->owner_opr();
        } else {
            mgb_assert(i.size() == 5);
M
Megvii Engine Team 已提交
432
            return opr::BatchNorm::make(i[0], i[1], i[2], i[3], i[4], param, config)[0]
433 434
                    .node()
                    ->owner_opr();
435
        }
436 437 438
    }
};

439
// OprMaker in MGB_SEREG_OPR only support unique output opr
440
template <>
441
struct OprMaker<opr::BatchNormBackward, 6> {
442
    using Param = opr::BatchNormBackward::Param;
M
Megvii Engine Team 已提交
443 444 445
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
446
        MGB_MARK_USED_VAR(graph);
M
Megvii Engine Team 已提交
447 448
        return opr::BatchNormBackward::make(
                       i[0], i[1], i[2], i[3], i[4], i[5], param, config)[0]
449 450 451 452 453 454 455 456
                .node()
                ->owner_opr();
    }
};

template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCaller2 {
    template <typename Opr>
M
Megvii Engine Team 已提交
457 458 459 460
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
461
        if (inputs.size() == 2) {
M
Megvii Engine Team 已提交
462
            return Opr::make(inputs[0], inputs[1], param, execution_policy, config)
463
                    .node();
464
        }
465 466 467 468 469 470
        return nullptr;
    }
};
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCaller3 {
    template <typename Opr>
M
Megvii Engine Team 已提交
471 472 473 474
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
475
        if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
476 477 478
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], param, execution_policy,
                           config)
479
                    .node();
480
        }
481 482 483 484 485 486
        return nullptr;
    }
};
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCallerEmpty {
    template <typename Opr>
M
Megvii Engine Team 已提交
487 488 489
    static VarNode* make(
            const cg::VarNodeArray&, const typename MegDNNConv::Param&,
            const megdnn::param::ExecutionPolicy&, const OperatorNodeConfig&) {
490 491 492 493
        return nullptr;
    }
};

M
Megvii Engine Team 已提交
494 495 496 497 498
template <
        class Opr, class Maker0, class MegDNNConv,
        class Maker1 = MakeLocalShareCallerEmpty<MegDNNConv>,
        class Maker2 = MakeLocalShareCallerEmpty<MegDNNConv>,
        typename LocalShareParam = megdnn::param::LocalShare>
499 500 501 502 503 504 505
struct LocalShareLoadDumpImpl {
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr_) {
        auto&& opr = opr_.cast_final_safe<Opr>();
        ctx.write_param<LocalShareParam>(opr.param());
        ctx.write_param<megdnn::param::ExecutionPolicy>(opr.execution_policy());
    }

M
Megvii Engine Team 已提交
506 507 508 509 510 511
    static VarNode* make(
            const cg::VarNodeArray& inputs, const LocalShareParam& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
        VarNode* ret =
                Maker0::template make<Opr>(inputs, param, execution_policy, config);
512
        if (!ret) {
M
Megvii Engine Team 已提交
513
            ret = Maker1::template make<Opr>(inputs, param, execution_policy, config);
514
        }
515
        if (!ret) {
M
Megvii Engine Team 已提交
516
            ret = Maker2::template make<Opr>(inputs, param, execution_policy, config);
517
        }
518 519 520 521
        mgb_assert(ret);
        return ret;
    }

M
Megvii Engine Team 已提交
522 523 524
    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
525
        auto param = ctx.read_param<LocalShareParam>();
M
Megvii Engine Team 已提交
526
        auto execution_policy = ctx.read_param<megdnn::param::ExecutionPolicy>();
527 528 529 530 531 532 533 534 535 536 537 538 539
        return make(inputs, param, execution_policy, config)->owner_opr();
    }
};

template <>
struct OprLoadDumpImpl<opr::LocalShare, 0>
        : public LocalShareLoadDumpImpl<
                  opr::LocalShare, MakeLocalShareCaller2<megdnn::LocalShare>,
                  megdnn::LocalShare> {};
template <>
struct OprLoadDumpImpl<opr::LocalShareBackwardData, 0>
        : public LocalShareLoadDumpImpl<
                  opr::LocalShareBackwardData,
M
Megvii Engine Team 已提交
540
                  MakeLocalShareCaller3<megdnn::LocalShare>, megdnn::LocalShare> {};
541 542 543 544
template <>
struct OprLoadDumpImpl<opr::LocalShareBackwardFilter, 0>
        : public LocalShareLoadDumpImpl<
                  opr::LocalShareBackwardFilter,
M
Megvii Engine Team 已提交
545
                  MakeLocalShareCaller3<megdnn::LocalShare>, megdnn::LocalShare> {};
546 547 548 549
template <>
struct OprLoadDumpImpl<opr::DeformableConvForward, 0>
        : public ConvLoadDumpImpl<
                  opr::DeformableConvForward,
M
Megvii Engine Team 已提交
550 551
                  MakeConvCaller4<megdnn::DeformableConvForward>, megdnn::Convolution> {
};
552 553 554 555 556 557 558 559 560 561 562 563
template <>
struct OprLoadDumpImpl<opr::DeformableConvBackwardData, 0>
        : public ConvLoadDumpImpl<
                  opr::DeformableConvBackwardData,
                  MakeConvCaller5<megdnn::DeformableConvBackwardData>,
                  megdnn::Convolution> {};
template <>
struct OprLoadDumpImpl<opr::DeformableConvBackwardFilter, 0>
        : public ConvLoadDumpImpl<
                  opr::DeformableConvBackwardFilter,
                  MakeConvCaller5<megdnn::DeformableConvBackwardFilter>,
                  megdnn::Convolution> {};
564 565 566 567 568 569 570 571 572 573 574 575 576

template <typename Opr>
cg::OperatorNodeBase* opr_shallow_copy_conv(
        const serialization::OprShallowCopyContext& ctx,
        const cg::OperatorNodeBase& opr_, const VarNodeArray& inputs,
        const OperatorNodeConfig& config) {
    MGB_MARK_USED_VAR(ctx);
    auto&& opr = opr_.cast_final_safe<Opr>();
    return OprLoadDumpImpl<Opr, 0>::make(
                   inputs, opr.param(), opr.execution_policy_transient(), config)
            ->owner_opr();
}

577
}  // namespace serialization
578 579

namespace opr {
580 581 582
using ConvolutionV2 = Convolution;
using ConvolutionBackwardDataV2 = ConvolutionBackwardData;
using ConvolutionBackwardFilterV2 = ConvolutionBackwardFilter;
583 584 585 586
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(ConvolutionV2, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(ConvolutionBackwardDataV2, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(
        ConvolutionBackwardFilterV2, 0, opr_shallow_copy_conv);
587 588 589 590

MGB_SEREG_OPR(Images2Neibs, 1);
MGB_SEREG_OPR(Images2NeibsBackward, 2);

591 592 593
MGB_SEREG_OPR(SlidingWindowTranspose, 1);
MGB_SEREG_OPR(SlidingWindowTransposeBackward, 2);

594 595 596 597 598 599 600 601 602 603 604 605 606
using LocalV2 = Local;
using LocalBackwardDataV2 = LocalBackwardData;
using LocalBackwardFilterV2 = LocalBackwardFilter;
MGB_SEREG_OPR(LocalV2, 2);
MGB_SEREG_OPR(LocalBackwardDataV2, 3);
MGB_SEREG_OPR(LocalBackwardFilterV2, 3);

using GroupLocalV2 = GroupLocal;
using GroupLocalBackwardDataV2 = GroupLocalBackwardData;
using GroupLocalBackwardFilterV2 = GroupLocalBackwardFilter;
MGB_SEREG_OPR(GroupLocalV2, 2);
MGB_SEREG_OPR(GroupLocalBackwardDataV2, 3);
MGB_SEREG_OPR(GroupLocalBackwardFilterV2, 3);
607 608 609 610 611

MGB_SEREG_OPR(LRN, 1);
MGB_SEREG_OPR(LRNBackward, 3);
using PoolingV1 = Pooling;
using PoolingBackwardV1 = PoolingBackward;
612 613
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(PoolingV1, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(PoolingBackwardV1, 0, opr_shallow_copy_conv);
614 615 616 617 618 619 620 621
using AdaptivePoolingV1 = AdaptivePooling;
using AdaptivePoolingBackwardV1 = AdaptivePoolingBackward;
MGB_SEREG_OPR(AdaptivePoolingV1, 2);
MGB_SEREG_OPR(AdaptivePoolingBackwardV1, 4);

MGB_SEREG_OPR(ROIPooling, 3);
MGB_SEREG_OPR(ROIPoolingBackward, 4);

622 623
using MaskConvolutionV2 = MaskConvolution;
MGB_SEREG_OPR(MaskConvolutionV2, 3);
624 625
MGB_SEREG_OPR(MaskPropagate, 1);

626 627 628 629
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(Convolution3D, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(Convolution3DBackwardData, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(
        Convolution3DBackwardFilter, 0, opr_shallow_copy_conv);
630 631

using ConvBiasForwardV4 = ConvBiasForward;
632
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(ConvBiasForwardV4, 0, opr_shallow_copy_conv);
633

634 635 636 637
using BatchNormV1 = BatchNorm;
using BatchNormBackwardV1 = BatchNormBackward;
MGB_SEREG_OPR(BatchNormV1, 0);
MGB_SEREG_OPR(BatchNormBackwardV1, 6);
638 639 640 641

using LocalShareForwardV1 = LocalShareForward;
using LocalShareBackwardDataV1 = LocalShareBackwardData;
using LocalShareBackwardFilterV1 = LocalShareBackwardFilter;
642 643 644 645
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(LocalShareForwardV1, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(LocalShareBackwardDataV1, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(
        LocalShareBackwardFilterV1, 0, opr_shallow_copy_conv);
646 647 648 649 650

using ROIAlignV1 = ROIAlign;
using ROIAlignBackwardV1 = ROIAlignBackward;
MGB_SEREG_OPR(ROIAlignV1, 2);
MGB_SEREG_OPR(ROIAlignBackwardV1, 4);
651 652 653
using DeformableConvForwardV1 = DeformableConvForward;
using DeformableConvBackwardDataV1 = DeformableConvBackwardData;
using DeformableConvBackwardFilterV1 = DeformableConvBackwardFilter;
654 655 656 657 658
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(DeformableConvForwardV1, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(
        DeformableConvBackwardDataV1, 0, opr_shallow_copy_conv);
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(
        DeformableConvBackwardFilterV1, 0, opr_shallow_copy_conv);
659

660 661 662 663
MGB_SEREG_OPR(CorrelationForward, 2);
MGB_SEREG_OPR(CorrelationBackwardData1, 3);
MGB_SEREG_OPR(CorrelationBackwardData2, 3);

664 665 666 667
MGB_SEREG_OPR(DeformablePSROIPoolingForward, 3);
MGB_SEREG_OPR(DeformablePSROIPoolingBackward, 5);

using BatchConvBiasForwardV1 = BatchConvBiasForward;
668
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(BatchConvBiasForwardV1, 0, opr_shallow_copy_conv);
669 670 671 672
MGB_SEREG_OPR(FakeQuant, 3);
MGB_SEREG_OPR(FakeQuantBackward, 4);
MGB_SEREG_OPR(TQT, 2);
MGB_SEREG_OPR(TQTBackward, 3);
M
Megvii Engine Team 已提交
673 674
MGB_SEREG_OPR(LSQ, 4);
MGB_SEREG_OPR(LSQBackward, 5);
675 676 677 678
MGB_SEREG_OPR(RNNForward, 3);
MGB_SEREG_OPR(RNNBackward, 7);
MGB_SEREG_OPR(LSTMForward, 4);
MGB_SEREG_OPR(LSTMBackward, 9);
679 680 681
}  // namespace opr

}  // namespace mgb
682 683

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