dnn.sereg.h 27.0 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
#include "megbrain/opr/dnn/images2neibs.h"
19
#include "megbrain/opr/dnn/layer_norm.h"
20 21
#include "megbrain/opr/dnn/local.h"
#include "megbrain/opr/dnn/lrn.h"
M
Megvii Engine Team 已提交
22 23
#include "megbrain/opr/dnn/lsq.h"
#include "megbrain/opr/dnn/pooling.h"
24
#include "megbrain/opr/dnn/rnn.h"
M
Megvii Engine Team 已提交
25 26
#include "megbrain/opr/dnn/roi_align.h"
#include "megbrain/opr/dnn/roi_pooling.h"
27
#include "megbrain/opr/dnn/sliding_window_transpose.h"
M
Megvii Engine Team 已提交
28
#include "megbrain/opr/dnn/tqt.h"
29
#include "megbrain/serialization/sereg.h"
30 31
#include "megdnn/opr_param_defs.h"
#include "megdnn/oprs/nn.h"
32 33 34 35

namespace mgb {

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

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

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

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

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

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

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

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

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

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

M
Megvii Engine Team 已提交
186 187 188 189 190
template <
        class Opr, class Maker0, class MegDNNConv,
        class Maker1 = MakeConvCallerEmpty<MegDNNConv>,
        class Maker2 = MakeConvCallerEmpty<MegDNNConv>,
        typename ConvParam = megdnn::param::Convolution>
191 192 193 194
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 已提交
195 196
        ctx.write_param<megdnn::param::ExecutionPolicy>(
                opr.execution_policy_transient());
197 198
    }

M
Megvii Engine Team 已提交
199 200 201 202 203 204
    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);
205
        if (!ret) {
M
Megvii Engine Team 已提交
206
            ret = Maker1::template make<Opr>(inputs, param, execution_policy, config);
207
        }
208
        if (!ret) {
M
Megvii Engine Team 已提交
209
            ret = Maker2::template make<Opr>(inputs, param, execution_policy, config);
M
Megvii Engine Team 已提交
210
        }
211 212 213 214
        mgb_assert(ret);
        return ret;
    }

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

M
Megvii Engine Team 已提交
224
template <class Opr, class Maker0, typename PoolingParam = megdnn::param::Pooling>
225 226 227 228 229 230
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 已提交
231 232
    static VarNode* make(
            const cg::VarNodeArray& inputs, const PoolingParam& param,
233
            const megdnn::param::ExecutionPolicy& execution_policy,
M
Megvii Engine Team 已提交
234
            const OperatorNodeConfig& config) {
235 236
        VarNode* ret =
                Maker0::template make<Opr>(inputs, param, execution_policy, config);
237 238 239 240
        mgb_assert(ret);
        return ret;
    }

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

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

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();
    }
};

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

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

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

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

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

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

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

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

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

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
template <>
struct OprMaker<opr::LayerNorm, 0> {
    using Param = opr::LayerNorm::Param;
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
        MGB_MARK_USED_VAR(graph);
        if (i.size() == 3) {
            return opr::LayerNorm::make(i[0], i[1], i[2], param, config)[0]
                    .node()
                    ->owner_opr();
        } else {
            mgb_assert(i.size() == 1);
            return opr::LayerNorm::make(i[0], param, config)[0].node()->owner_opr();
        }
    }
};

// OprMaker in MGB_SEREG_OPR only support unique output opr
template <>
struct OprMaker<opr::LayerNormBackward, 0> {
    using Param = opr::LayerNormBackward::Param;
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
        MGB_MARK_USED_VAR(graph);
        if (i.size() == 5) {
            return opr::LayerNormBackward::make(
                           i[0], i[1], i[2], i[3], i[4], param, config)[0]
                    .node()
                    ->owner_opr();
        } else {
            mgb_assert(i.size() == 4);
            return opr::LayerNormBackward::make(
                           i[0], i[1], i[2], i[3], param, config)[0]
                    .node()
                    ->owner_opr();
        }
    }
};

496 497 498
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCaller2 {
    template <typename Opr>
M
Megvii Engine Team 已提交
499 500 501 502
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
503
        if (inputs.size() == 2) {
M
Megvii Engine Team 已提交
504
            return Opr::make(inputs[0], inputs[1], param, execution_policy, config)
505
                    .node();
506
        }
507 508 509 510 511 512
        return nullptr;
    }
};
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCaller3 {
    template <typename Opr>
M
Megvii Engine Team 已提交
513 514 515 516
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
517
        if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
518 519 520
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], param, execution_policy,
                           config)
521
                    .node();
522
        }
523 524 525 526 527 528
        return nullptr;
    }
};
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCallerEmpty {
    template <typename Opr>
M
Megvii Engine Team 已提交
529 530 531
    static VarNode* make(
            const cg::VarNodeArray&, const typename MegDNNConv::Param&,
            const megdnn::param::ExecutionPolicy&, const OperatorNodeConfig&) {
532 533 534 535
        return nullptr;
    }
};

M
Megvii Engine Team 已提交
536 537 538 539 540
template <
        class Opr, class Maker0, class MegDNNConv,
        class Maker1 = MakeLocalShareCallerEmpty<MegDNNConv>,
        class Maker2 = MakeLocalShareCallerEmpty<MegDNNConv>,
        typename LocalShareParam = megdnn::param::LocalShare>
541 542 543 544 545 546 547
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 已提交
548 549 550 551 552 553
    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);
554
        if (!ret) {
M
Megvii Engine Team 已提交
555
            ret = Maker1::template make<Opr>(inputs, param, execution_policy, config);
556
        }
557
        if (!ret) {
M
Megvii Engine Team 已提交
558
            ret = Maker2::template make<Opr>(inputs, param, execution_policy, config);
559
        }
560 561 562 563
        mgb_assert(ret);
        return ret;
    }

M
Megvii Engine Team 已提交
564 565 566
    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
567
        auto param = ctx.read_param<LocalShareParam>();
M
Megvii Engine Team 已提交
568
        auto execution_policy = ctx.read_param<megdnn::param::ExecutionPolicy>();
569 570 571 572 573 574 575 576 577 578 579 580 581
        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 已提交
582
                  MakeLocalShareCaller3<megdnn::LocalShare>, megdnn::LocalShare> {};
583 584 585 586
template <>
struct OprLoadDumpImpl<opr::LocalShareBackwardFilter, 0>
        : public LocalShareLoadDumpImpl<
                  opr::LocalShareBackwardFilter,
M
Megvii Engine Team 已提交
587
                  MakeLocalShareCaller3<megdnn::LocalShare>, megdnn::LocalShare> {};
588 589 590 591
template <>
struct OprLoadDumpImpl<opr::DeformableConvForward, 0>
        : public ConvLoadDumpImpl<
                  opr::DeformableConvForward,
M
Megvii Engine Team 已提交
592 593
                  MakeConvCaller4<megdnn::DeformableConvForward>, megdnn::Convolution> {
};
594 595 596 597 598 599 600 601 602 603 604 605
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> {};
606 607 608 609 610 611 612 613 614 615 616 617 618

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();
}

619
}  // namespace serialization
620 621

namespace opr {
622 623 624
using ConvolutionV2 = Convolution;
using ConvolutionBackwardDataV2 = ConvolutionBackwardData;
using ConvolutionBackwardFilterV2 = ConvolutionBackwardFilter;
625 626 627 628
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);
629 630 631 632

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

633 634 635
MGB_SEREG_OPR(SlidingWindowTranspose, 1);
MGB_SEREG_OPR(SlidingWindowTransposeBackward, 2);

636 637 638 639 640 641 642 643 644 645 646 647 648
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);
649 650 651 652 653

MGB_SEREG_OPR(LRN, 1);
MGB_SEREG_OPR(LRNBackward, 3);
using PoolingV1 = Pooling;
using PoolingBackwardV1 = PoolingBackward;
654 655
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);
656 657 658 659 660 661 662 663
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);

664 665
using MaskConvolutionV2 = MaskConvolution;
MGB_SEREG_OPR(MaskConvolutionV2, 3);
666 667
MGB_SEREG_OPR(MaskPropagate, 1);

668 669 670 671
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);
672 673

using ConvBiasForwardV4 = ConvBiasForward;
674
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(ConvBiasForwardV4, 0, opr_shallow_copy_conv);
675

676 677 678 679
using BatchNormV1 = BatchNorm;
using BatchNormBackwardV1 = BatchNormBackward;
MGB_SEREG_OPR(BatchNormV1, 0);
MGB_SEREG_OPR(BatchNormBackwardV1, 6);
680 681 682 683

using LocalShareForwardV1 = LocalShareForward;
using LocalShareBackwardDataV1 = LocalShareBackwardData;
using LocalShareBackwardFilterV1 = LocalShareBackwardFilter;
684 685 686 687
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);
688 689 690 691 692

using ROIAlignV1 = ROIAlign;
using ROIAlignBackwardV1 = ROIAlignBackward;
MGB_SEREG_OPR(ROIAlignV1, 2);
MGB_SEREG_OPR(ROIAlignBackwardV1, 4);
693 694 695
using DeformableConvForwardV1 = DeformableConvForward;
using DeformableConvBackwardDataV1 = DeformableConvBackwardData;
using DeformableConvBackwardFilterV1 = DeformableConvBackwardFilter;
696 697 698 699 700
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);
701

702 703 704 705
MGB_SEREG_OPR(CorrelationForward, 2);
MGB_SEREG_OPR(CorrelationBackwardData1, 3);
MGB_SEREG_OPR(CorrelationBackwardData2, 3);

706 707 708 709
MGB_SEREG_OPR(DeformablePSROIPoolingForward, 3);
MGB_SEREG_OPR(DeformablePSROIPoolingBackward, 5);

using BatchConvBiasForwardV1 = BatchConvBiasForward;
710
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(BatchConvBiasForwardV1, 0, opr_shallow_copy_conv);
711 712 713 714
MGB_SEREG_OPR(FakeQuant, 3);
MGB_SEREG_OPR(FakeQuantBackward, 4);
MGB_SEREG_OPR(TQT, 2);
MGB_SEREG_OPR(TQTBackward, 3);
M
Megvii Engine Team 已提交
715 716
MGB_SEREG_OPR(LSQ, 4);
MGB_SEREG_OPR(LSQBackward, 5);
717 718
MGB_SEREG_OPR(LayerNorm, 0);
MGB_SEREG_OPR(LayerNormBackward, 0);
719 720 721 722
MGB_SEREG_OPR(RNNForward, 3);
MGB_SEREG_OPR(RNNBackward, 7);
MGB_SEREG_OPR(LSTMForward, 4);
MGB_SEREG_OPR(LSTMBackward, 9);
723 724 725
}  // namespace opr

}  // namespace mgb
726 727

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