dnn.sereg.h 47.7 KB
Newer Older
1
#include "megbrain/opr/basic_arith.h"
M
Megvii Engine Team 已提交
2
#include "megbrain/opr/dnn/adaptive_pooling.h"
3 4
#include "megbrain/opr/dnn/batch_norm.h"
#include "megbrain/opr/dnn/convolution.h"
5
#include "megbrain/opr/dnn/correlation.h"
M
Megvii Engine Team 已提交
6
#include "megbrain/opr/dnn/fake_quant.h"
7
#include "megbrain/opr/dnn/group_norm.h"
8
#include "megbrain/opr/dnn/images2neibs.h"
9
#include "megbrain/opr/dnn/instance_norm.h"
10
#include "megbrain/opr/dnn/layer_norm.h"
11 12
#include "megbrain/opr/dnn/local.h"
#include "megbrain/opr/dnn/lrn.h"
M
Megvii Engine Team 已提交
13 14
#include "megbrain/opr/dnn/lsq.h"
#include "megbrain/opr/dnn/pooling.h"
15
#include "megbrain/opr/dnn/rnn.h"
M
Megvii Engine Team 已提交
16 17
#include "megbrain/opr/dnn/roi_align.h"
#include "megbrain/opr/dnn/roi_pooling.h"
18
#include "megbrain/opr/dnn/sliding_window_transpose.h"
19
#include "megbrain/opr/dnn/softmax.h"
M
Megvii Engine Team 已提交
20
#include "megbrain/opr/dnn/tqt.h"
21 22 23
#include "megbrain/opr/io.h"
#include "megbrain/opr/tensor_manip.h"
#include "megbrain/serialization/oss_opr_load_dump.h"
24
#include "megbrain/serialization/sereg.h"
25 26
#include "megdnn/opr_param_defs.h"
#include "megdnn/oprs/nn.h"
27 28 29 30

namespace mgb {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

350 351 352 353 354 355 356 357 358 359 360 361 362
template <>
struct OprMaker<opr::SoftmaxBackward, 2> {
    using Param = opr::SoftmaxBackward::Param;
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
        MGB_MARK_USED_VAR(graph);
        return opr::SoftmaxBackward::make(i[0], i[1], param, config)
                .node()
                ->owner_opr();
    }
};

363 364
template <>
struct OprLoadDumpImpl<opr::AdaptivePoolingBackward, 0>
365
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
366 367 368
                  opr::AdaptivePoolingBackward,
                  MakeAdaptivePoolingBackwardCaller3<megdnn::AdaptivePoolingBackward>,
                  megdnn::param::AdaptivePooling> {};
369 370 371

template <>
struct OprLoadDumpImpl<opr::AdaptivePooling, 0>
372
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
373
                  opr::AdaptivePooling, MakeROIAlignCaller1<megdnn::AdaptivePooling>,
374 375 376 377
                  megdnn::param::AdaptivePooling> {};

template <>
struct OprLoadDumpImpl<opr::ROIAlign, 0>
378
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
379 380
                  opr::ROIAlign, MakeROIAlignCaller1<megdnn::ROIAlign>,
                  megdnn::param::ROIAlign> {};
381 382 383

template <>
struct OprLoadDumpImpl<opr::ROIAlignBackward, 0>
384
        : public GeneralOprLoadDumpImpl<
M
Megvii Engine Team 已提交
385
                  opr::ROIAlignBackward, MakeROIAlignCaller4<megdnn::ROIAlignBackward>,
386 387 388 389
                  megdnn::param::ROIAlign> {};

template <>
struct OprLoadDumpImpl<opr::Pooling, 0>
M
Megvii Engine Team 已提交
390 391 392
        : public PoolingLoadDumpImpl<
                  opr::Pooling, MakePoolingCaller1<megdnn::Pooling>,
                  megdnn::param::Pooling> {};
393 394 395 396 397 398 399 400 401 402

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 已提交
403 404 405
        : public ConvLoadDumpImpl<
                  opr::Convolution, MakeConvCaller2<megdnn::Convolution>,
                  megdnn::Convolution> {};
406 407
template <>
struct OprLoadDumpImpl<opr::ConvolutionBackwardData, 0>
M
Megvii Engine Team 已提交
408 409 410
        : public ConvLoadDumpImpl<
                  opr::ConvolutionBackwardData, MakeConvCaller2<megdnn::Convolution>,
                  megdnn::Convolution, MakeConvCaller3<megdnn::Convolution>> {};
411 412
template <>
struct OprLoadDumpImpl<opr::ConvolutionBackwardFilter, 0>
M
Megvii Engine Team 已提交
413 414 415
        : public ConvLoadDumpImpl<
                  opr::ConvolutionBackwardFilter, MakeConvCaller3<megdnn::Convolution>,
                  megdnn::Convolution> {};
416 417 418

template <>
struct OprLoadDumpImpl<opr::Convolution3D, 0>
M
Megvii Engine Team 已提交
419 420 421 422 423
        : public ConvLoadDumpImpl<
                  opr::Convolution3D, MakeConvCaller2<megdnn::Convolution3D>,
                  megdnn::Convolution3D, MakeConvCallerEmpty<megdnn::Convolution3D>,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  megdnn::param::Convolution3D> {};
424 425
template <>
struct OprLoadDumpImpl<opr::Convolution3DBackwardData, 0>
M
Megvii Engine Team 已提交
426 427 428 429 430 431
        : public ConvLoadDumpImpl<
                  opr::Convolution3DBackwardData,
                  MakeConvCaller2<megdnn::Convolution3D>, megdnn::Convolution3D,
                  MakeConvCaller3<megdnn::Convolution3D>,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  megdnn::param::Convolution3D> {};
432 433
template <>
struct OprLoadDumpImpl<opr::Convolution3DBackwardFilter, 0>
M
Megvii Engine Team 已提交
434 435 436 437 438 439
        : public ConvLoadDumpImpl<
                  opr::Convolution3DBackwardFilter,
                  MakeConvCaller3<megdnn::Convolution3D>, megdnn::Convolution3D,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  MakeConvCallerEmpty<megdnn::Convolution3D>,
                  megdnn::param::Convolution3D> {};
440

441 442
template <>
struct OprLoadDumpImpl<opr::ConvBiasForward, 0>
M
Megvii Engine Team 已提交
443 444 445 446
        : public ConvLoadDumpImpl<
                  opr::ConvBiasForward, MakeConvCaller2<megdnn::ConvBiasForward>,
                  megdnn::ConvBiasForward, MakeConvCaller3<megdnn::ConvBiasForward>,
                  MakeConvCaller4<megdnn::ConvBiasForward>, megdnn::param::ConvBias> {};
447 448
template <>
struct OprLoadDumpImpl<opr::BatchConvBiasForward, 0>
M
Megvii Engine Team 已提交
449 450 451 452 453 454 455
        : public ConvLoadDumpImpl<
                  opr::BatchConvBiasForward,
                  MakeConvCaller2<megdnn::BatchConvBiasForward>,
                  megdnn::BatchConvBiasForward,
                  MakeConvCaller3<megdnn::BatchConvBiasForward>,
                  MakeConvCaller4<megdnn::BatchConvBiasForward>,
                  megdnn::param::BatchConvBias> {};
456 457 458 459

template <>
struct OprMaker<opr::BatchNorm, 0> {
    using Param = opr::BatchNorm::Param;
M
Megvii Engine Team 已提交
460 461 462
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
463 464 465 466 467 468 469
        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 已提交
470
            return opr::BatchNorm::make(i[0], i[1], i[2], i[3], i[4], param, config)[0]
471 472
                    .node()
                    ->owner_opr();
473
        }
474 475 476
    }
};

477
// OprMaker in MGB_SEREG_OPR only support unique output opr
478
template <>
479
struct OprMaker<opr::BatchNormBackward, 6> {
480
    using Param = opr::BatchNormBackward::Param;
M
Megvii Engine Team 已提交
481 482 483
    static cg::OperatorNodeBase* make(
            const Param& param, const cg::VarNodeArray& i, ComputingGraph& graph,
            const OperatorNodeConfig& config) {
484
        MGB_MARK_USED_VAR(graph);
M
Megvii Engine Team 已提交
485 486
        return opr::BatchNormBackward::make(
                       i[0], i[1], i[2], i[3], i[4], i[5], param, config)[0]
487 488 489 490 491
                .node()
                ->owner_opr();
    }
};

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

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 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 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 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
template <>
struct OprMaker<opr::GroupNorm, 0> {
    using Param = opr::GroupNorm::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::GroupNorm::make(i[0], i[1], i[2], param, config)[0]
                    .node()
                    ->owner_opr();
        } else {
            mgb_assert(i.size() == 1);
            return opr::GroupNorm::make(i[0], param, config)[0].node()->owner_opr();
        }
    }
};

template <>
struct OprLoadDumpImplV2<opr::GroupNorm, 0> {
    using Opr = opr::GroupNorm;
    using Param = opr::GroupNorm::Param;
    using ElemwiseParam = opr::Elemwise::Param;
    using ReduceParam = opr::Reduce::Param;
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr) {
        ctx.write_param<Param>(opr.cast_final_safe<Opr>().param());
    }

    static cg::OperatorNodeBase* replace_opr(
            cg::OperatorNodeBase* opr, const VarNodeArray& inputs) {
        auto graph = inputs[0]->owner_graph();
        auto comp_node = inputs[0]->comp_node();
        // std::unique_ptr<StaticInferManager> m_static_infer_manager;
        auto opr_param = opr->cast_final_safe<opr::GroupNorm>().param();
        float eps = opr_param.eps;
        auto half = DTypeScalar(static_cast<megdnn::dt_float32>(0.5));
        auto param_eps = DTypeScalar(static_cast<megdnn::dt_float32>(eps));
        auto half_node = opr::ImmutableTensor::make(*graph, half, {comp_node});
        auto eps_node = opr::ImmutableTensor::make(*graph, param_eps, {comp_node});

        auto origin_shape = opr::GetVarShape::make(inputs[0]).node();

        TensorShape input_shape =
                inputs[0]->owner_graph()->static_infer_manager().infer_shape(inputs[0]);
        size_t N = input_shape[0];
        size_t inner_size = input_shape[1] * input_shape[2] * input_shape[3];
        int group = opr_param.group;
        int size = inner_size / group;
        HostTensorND hv = HostTensorND(inputs[0]->comp_node(), {3}, dtype::Int32());
        auto* ptr = hv.ptr<dt_int32>();
        ptr[0] = N;
        ptr[1] = group;
        ptr[2] = size;
        auto target_shape = opr::ImmutableTensor::make(*graph, hv, {comp_node});
        auto inp = opr::Reshape::make(inputs[0], target_shape);

        auto mean = opr::Reduce::make(inp, {ReduceParam::Mode::MEAN, 2});
        auto elemwise1 = opr::Elemwise::make({inp, inp}, {ElemwiseParam::Mode::MUL});
        auto temp_var = opr::Reduce::make(elemwise1, {ReduceParam::Mode::MEAN, 2});
        auto elemwise2 = opr::Elemwise::make({mean, mean}, {ElemwiseParam::Mode::MUL});
        auto var =
                opr::Elemwise::make({temp_var, elemwise2}, {ElemwiseParam::Mode::SUB});
        auto add_var = opr::Elemwise::make({var, eps_node}, {ElemwiseParam::Mode::ADD});
        auto sqrt =
                opr::Elemwise::make({add_var, half_node}, {ElemwiseParam::Mode::POW});
        auto div = opr::Elemwise::make({inp, mean}, {ElemwiseParam::Mode::SUB});
        auto temp_inp =
                opr::Elemwise::make({div, sqrt}, {ElemwiseParam::Mode::TRUE_DIV});
        auto res = opr::Reshape::make(temp_inp, origin_shape);

        if (inputs.size() == 3) {
            auto mul_temp =
                    opr::Elemwise::make({res, inputs[1]}, {ElemwiseParam::Mode::MUL});
            auto res = opr::Elemwise::make(
                    {mul_temp, inputs[2]}, {ElemwiseParam::Mode::ADD});
            return res.node()->owner_opr();
        } else {
            return res.node()->owner_opr();
        }
    }

    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
        // auto& fbs_ctx = CAST_TO_FBS_V2_CTX(ctx);
        return OprMaker<opr::GroupNorm, 0>::make(
                ctx.read_param<Param>(), inputs, ctx.graph(), config);
    }
};

// OprMaker in MGB_SEREG_OPR only support unique output opr
template <>
struct OprMaker<opr::GroupNormBackward, 0> {
    using Param = opr::GroupNormBackward::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::GroupNormBackward::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::GroupNormBackward::make(
                           i[0], i[1], i[2], i[3], param, config)[0]
                    .node()
                    ->owner_opr();
        }
    }
};

template <>
struct OprLoadDumpImplV2<opr::GroupNormBackward, 0> {
    using Opr = opr::GroupNormBackward;
    using Param = opr::GroupNormBackward::Param;
    using ElemwiseParam = opr::Elemwise::Param;
    using ReduceParam = opr::Reduce::Param;
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr) {
        ctx.write_param<Param>(opr.cast_final_safe<Opr>().param());
    }

    static cg::OperatorNodeBase* replace_opr(
            cg::OperatorNodeBase* opr, const VarNodeArray& inputs) {
        auto rstd = inputs[4];
        auto graph = inputs[1]->owner_graph();
        auto comp_node = inputs[1]->comp_node();
        auto opr_param = opr->cast_final_safe<opr::GroupNormBackward>().param();
        float eps = opr_param.eps;
        auto half = DTypeScalar(static_cast<megdnn::dt_float32>(0.5));
        auto param_eps = DTypeScalar(static_cast<megdnn::dt_float32>(eps));
        auto half_node = opr::ImmutableTensor::make(*graph, half, {comp_node});
        auto eps_node = opr::ImmutableTensor::make(*graph, param_eps, {comp_node});
        auto const_node =
                opr::ImmutableTensor::make(*graph, DTypeScalar(1), {comp_node});

        TensorShape input_shape =
                inputs[1]->owner_graph()->static_infer_manager().infer_shape(inputs[0]);
        auto origin_shape = opr::GetVarShape::make(inputs[1]).node();
        size_t N = input_shape[0];
        size_t C = input_shape[1];
        size_t inner_size = input_shape[1] * input_shape[2] * input_shape[3];
        int group = opr_param.group;
        int size = inner_size / group;
        HostTensorND hv = HostTensorND(inputs[1]->comp_node(), {3}, dtype::Int32());
        auto* ptr = hv.ptr<dt_int32>();
        ptr[0] = N;
        ptr[1] = group;
        ptr[2] = size;
        auto target_shape = opr::ImmutableTensor::make(*graph, hv, {comp_node});
        auto inp = opr::Reshape::make(inputs[1], target_shape);

        auto temp_rstd =
                opr::Elemwise::make({rstd, eps_node}, {ElemwiseParam::Mode::ADD});
        auto sqrt =
                opr::Elemwise::make({temp_rstd, half_node}, {ElemwiseParam::Mode::POW});
        auto slice_std = opr::Elemwise::make(
                {const_node, sqrt}, {ElemwiseParam::Mode::TRUE_DIV});
        auto sub_mean =
                opr::Elemwise::make({inp, inputs[3]}, {ElemwiseParam::Mode::SUB});
        auto x_hat =
                opr::Elemwise::make({sub_mean, slice_std}, {ElemwiseParam::Mode::MUL});
        x_hat = opr::Reshape::make(x_hat, origin_shape);
        auto size_node =
                opr::ImmutableTensor::make(*graph, DTypeScalar(size), {comp_node});
        auto temp1 = opr::Elemwise::make(
                {slice_std, size_node}, {ElemwiseParam::Mode::TRUE_DIV});

        auto dx_hat =
                opr::Elemwise::make({inputs[0], inputs[2]}, {ElemwiseParam::Mode::MUL});
        HostTensorND tshape = HostTensorND(inputs[1]->comp_node(), {5}, dtype::Int32());
        auto* ptr2 = tshape.ptr<dt_int32>();
        ptr2[0] = N;
        ptr2[1] = group;
        ptr2[2] = C / group;
        ptr2[3] = input_shape[2];
        ptr2[4] = input_shape[3];
        target_shape = opr::ImmutableTensor::make(*graph, tshape, {comp_node});
        x_hat = opr::Reshape::make(x_hat, target_shape);
        dx_hat = opr::Reshape::make(dx_hat, target_shape);
        auto temp2 =
                opr::Elemwise::make({size_node, dx_hat}, {ElemwiseParam::Mode::MUL});
        ptr2[2] = 1;
        ptr2[3] = 1;
        ptr2[4] = 1;
        target_shape = opr::ImmutableTensor::make(*graph, tshape, {comp_node});
        auto temp3 = opr::Reduce::make(dx_hat, {ReduceParam::Mode::SUM}, target_shape);
        auto sum_dx_hat =
                opr::Reduce::make(temp2, {ReduceParam::Mode::SUM}, target_shape);
        auto temp4 =
                opr::Elemwise::make({x_hat, sum_dx_hat}, {ElemwiseParam::Mode::MUL});
        auto temp5 = opr::Elemwise::make({temp2, temp3}, {ElemwiseParam::Mode::SUB});
        auto temp6 = opr::Elemwise::make({temp5, temp4}, {ElemwiseParam::Mode::SUB});
        auto dx_temp = opr::Elemwise::make({temp1, temp6}, {ElemwiseParam::Mode::MUL});
        auto dx = opr::Reshape::make(dx_temp, origin_shape);
        return dx.node()->owner_opr();
    }

    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
        return OprMaker<opr::GroupNormBackward, 0>::make(
                ctx.read_param<Param>(), inputs, ctx.graph(), config);
    }
};

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 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 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 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 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
template <>
struct OprMaker<opr::InstanceNorm, 0> {
    using Param = opr::GroupNorm::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::InstanceNorm::make(i[0], i[1], i[2], param, config)[0]
                    .node()
                    ->owner_opr();
        } else {
            mgb_assert(i.size() == 1);
            return opr::InstanceNorm::make(i[0], param, config)[0].node()->owner_opr();
        }
    }
};

template <>
struct OprLoadDumpImplV2<opr::InstanceNorm, 0> {
    using Opr = opr::InstanceNorm;
    using Param = opr::GroupNorm::Param;
    using ElemwiseParam = opr::Elemwise::Param;
    using ReduceParam = opr::Reduce::Param;
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr) {
        ctx.write_param<Param>(opr.cast_final_safe<Opr>().param());
    }

    static cg::OperatorNodeBase* replace_opr(
            cg::OperatorNodeBase* opr, const VarNodeArray& inputs) {
        auto graph = inputs[0]->owner_graph();
        auto comp_node = inputs[0]->comp_node();
        // std::unique_ptr<StaticInferManager> m_static_infer_manager;
        auto opr_param = opr->cast_final_safe<opr::InstanceNorm>().param();
        float eps = opr_param.eps;
        auto half = DTypeScalar(static_cast<megdnn::dt_float32>(0.5));
        auto param_eps = DTypeScalar(static_cast<megdnn::dt_float32>(eps));
        auto half_node = opr::ImmutableTensor::make(*graph, half, {comp_node});
        auto eps_node = opr::ImmutableTensor::make(*graph, param_eps, {comp_node});

        auto origin_shape = opr::GetVarShape::make(inputs[0]).node();

        TensorShape input_shape =
                inputs[0]->owner_graph()->static_infer_manager().infer_shape(inputs[0]);
        size_t N = input_shape[0];
        size_t C = input_shape[1];
        size_t inner_size = input_shape[1] * input_shape[2] * input_shape[3];
        int size = inner_size / C;
        HostTensorND hv = HostTensorND(inputs[0]->comp_node(), {3}, dtype::Int32());
        auto* ptr = hv.ptr<dt_int32>();
        ptr[0] = N;
        ptr[1] = C;
        ptr[2] = size;
        auto target_shape = opr::ImmutableTensor::make(*graph, hv, {comp_node});
        auto inp = opr::Reshape::make(inputs[0], target_shape);

        auto mean = opr::Reduce::make(inp, {ReduceParam::Mode::MEAN, 2});
        auto elemwise1 = opr::Elemwise::make({inp, inp}, {ElemwiseParam::Mode::MUL});
        auto temp_var = opr::Reduce::make(elemwise1, {ReduceParam::Mode::MEAN, 2});
        auto elemwise2 = opr::Elemwise::make({mean, mean}, {ElemwiseParam::Mode::MUL});
        auto var =
                opr::Elemwise::make({temp_var, elemwise2}, {ElemwiseParam::Mode::SUB});
        auto add_var = opr::Elemwise::make({var, eps_node}, {ElemwiseParam::Mode::ADD});
        auto sqrt =
                opr::Elemwise::make({add_var, half_node}, {ElemwiseParam::Mode::POW});
        auto div = opr::Elemwise::make({inp, mean}, {ElemwiseParam::Mode::SUB});
        auto temp_inp =
                opr::Elemwise::make({div, sqrt}, {ElemwiseParam::Mode::TRUE_DIV});
        auto res = opr::Reshape::make(temp_inp, origin_shape);

        if (inputs.size() == 3) {
            auto mul_temp =
                    opr::Elemwise::make({res, inputs[1]}, {ElemwiseParam::Mode::MUL});
            auto res = opr::Elemwise::make(
                    {mul_temp, inputs[2]}, {ElemwiseParam::Mode::ADD});
            return res.node()->owner_opr();
        } else {
            return res.node()->owner_opr();
        }
    }

    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
        // auto& fbs_ctx = CAST_TO_FBS_V2_CTX(ctx);

        // return OprMaker<opr::AdaptivePooling,0>::make(ctx.read_param<Param>(),
        // inputs, ctx.graph(), config);
        return OprMaker<opr::InstanceNorm, 0>::make(
                ctx.read_param<Param>(), inputs, ctx.graph(), config);
    }
};

// OprMaker in MGB_SEREG_OPR only support unique output opr
template <>
struct OprMaker<opr::InstanceNormBackward, 0> {
    using Param = opr::GroupNormBackward::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::InstanceNormBackward::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::InstanceNormBackward::make(
                           i[0], i[1], i[2], i[3], param, config)[0]
                    .node()
                    ->owner_opr();
        }
    }
};

template <>
struct OprLoadDumpImplV2<opr::InstanceNormBackward, 0> {
    using Opr = opr::InstanceNormBackward;
    using Param = opr::GroupNormBackward::Param;
    using ElemwiseParam = opr::Elemwise::Param;
    using ReduceParam = opr::Reduce::Param;
    static void dump(OprDumpContext& ctx, const cg::OperatorNodeBase& opr) {
        ctx.write_param<Param>(opr.cast_final_safe<Opr>().param());
    }

    static cg::OperatorNodeBase* replace_opr(
            cg::OperatorNodeBase* opr, const VarNodeArray& inputs) {
        auto rstd = inputs[4];
        auto graph = inputs[1]->owner_graph();
        auto comp_node = inputs[1]->comp_node();
        auto opr_param = opr->cast_final_safe<opr::InstanceNormBackward>().param();
        float eps = opr_param.eps;
        auto half = DTypeScalar(static_cast<megdnn::dt_float32>(0.5));
        auto param_eps = DTypeScalar(static_cast<megdnn::dt_float32>(eps));
        auto half_node = opr::ImmutableTensor::make(*graph, half, {comp_node});
        auto eps_node = opr::ImmutableTensor::make(*graph, param_eps, {comp_node});
        auto const_node =
                opr::ImmutableTensor::make(*graph, DTypeScalar(1), {comp_node});

        TensorShape input_shape =
                inputs[1]->owner_graph()->static_infer_manager().infer_shape(inputs[0]);
        auto origin_shape = opr::GetVarShape::make(inputs[1]).node();
        size_t N = input_shape[0];
        size_t C = input_shape[1];
        size_t inner_size = input_shape[1] * input_shape[2] * input_shape[3];
        int size = inner_size / C;
        HostTensorND hv = HostTensorND(inputs[1]->comp_node(), {3}, dtype::Int32());
        auto* ptr = hv.ptr<dt_int32>();
        ptr[0] = N;
        ptr[1] = C;
        ptr[2] = size;
        auto target_shape = opr::ImmutableTensor::make(*graph, hv, {comp_node});
        auto inp = opr::Reshape::make(inputs[1], target_shape);

        auto temp_rstd =
                opr::Elemwise::make({rstd, eps_node}, {ElemwiseParam::Mode::ADD});
        auto sqrt =
                opr::Elemwise::make({temp_rstd, half_node}, {ElemwiseParam::Mode::POW});
        auto slice_std = opr::Elemwise::make(
                {const_node, sqrt}, {ElemwiseParam::Mode::TRUE_DIV});
        auto sub_mean =
                opr::Elemwise::make({inp, inputs[3]}, {ElemwiseParam::Mode::SUB});
        auto x_hat =
                opr::Elemwise::make({sub_mean, slice_std}, {ElemwiseParam::Mode::MUL});
        x_hat = opr::Reshape::make(x_hat, origin_shape);
        auto size_node =
                opr::ImmutableTensor::make(*graph, DTypeScalar(size), {comp_node});
        auto temp1 = opr::Elemwise::make(
                {slice_std, size_node}, {ElemwiseParam::Mode::TRUE_DIV});

        auto dx_hat =
                opr::Elemwise::make({inputs[0], inputs[2]}, {ElemwiseParam::Mode::MUL});
        HostTensorND tshape = HostTensorND(inputs[1]->comp_node(), {5}, dtype::Int32());
        auto* ptr2 = tshape.ptr<dt_int32>();
        ptr2[0] = N;
        ptr2[1] = C;
        ptr2[2] = 1;
        ptr2[3] = input_shape[2];
        ptr2[4] = input_shape[3];
        target_shape = opr::ImmutableTensor::make(*graph, tshape, {comp_node});
        x_hat = opr::Reshape::make(x_hat, target_shape);
        dx_hat = opr::Reshape::make(dx_hat, target_shape);
        auto temp2 =
                opr::Elemwise::make({size_node, dx_hat}, {ElemwiseParam::Mode::MUL});
        ptr2[2] = 1;
        ptr2[3] = 1;
        ptr2[4] = 1;
        target_shape = opr::ImmutableTensor::make(*graph, tshape, {comp_node});
        auto temp3 = opr::Reduce::make(dx_hat, {ReduceParam::Mode::SUM}, target_shape);
        auto sum_dx_hat =
                opr::Reduce::make(temp2, {ReduceParam::Mode::SUM}, target_shape);
        auto temp4 =
                opr::Elemwise::make({x_hat, sum_dx_hat}, {ElemwiseParam::Mode::MUL});
        auto temp5 = opr::Elemwise::make({temp2, temp3}, {ElemwiseParam::Mode::SUB});
        auto temp6 = opr::Elemwise::make({temp5, temp4}, {ElemwiseParam::Mode::SUB});
        auto dx_temp = opr::Elemwise::make({temp1, temp6}, {ElemwiseParam::Mode::MUL});
        auto dx = opr::Reshape::make(dx_temp, origin_shape);
        return dx.node()->owner_opr();
    }

    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
        return OprMaker<opr::InstanceNormBackward, 0>::make(
                ctx.read_param<Param>(), inputs, ctx.graph(), config);
    }
};

949 950 951
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCaller2 {
    template <typename Opr>
M
Megvii Engine Team 已提交
952 953 954 955
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
956
        if (inputs.size() == 2) {
M
Megvii Engine Team 已提交
957
            return Opr::make(inputs[0], inputs[1], param, execution_policy, config)
958
                    .node();
959
        }
960 961 962 963 964 965
        return nullptr;
    }
};
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCaller3 {
    template <typename Opr>
M
Megvii Engine Team 已提交
966 967 968 969
    static VarNode* make(
            const cg::VarNodeArray& inputs, const typename MegDNNConv::Param& param,
            const megdnn::param::ExecutionPolicy& execution_policy,
            const OperatorNodeConfig& config) {
970
        if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
971 972 973
            return Opr::make(
                           inputs[0], inputs[1], inputs[2], param, execution_policy,
                           config)
974
                    .node();
975
        }
976 977 978 979 980 981
        return nullptr;
    }
};
template <class MegDNNConv = megdnn::LocalShare>
struct MakeLocalShareCallerEmpty {
    template <typename Opr>
M
Megvii Engine Team 已提交
982 983 984
    static VarNode* make(
            const cg::VarNodeArray&, const typename MegDNNConv::Param&,
            const megdnn::param::ExecutionPolicy&, const OperatorNodeConfig&) {
985 986 987 988
        return nullptr;
    }
};

M
Megvii Engine Team 已提交
989 990 991 992 993
template <
        class Opr, class Maker0, class MegDNNConv,
        class Maker1 = MakeLocalShareCallerEmpty<MegDNNConv>,
        class Maker2 = MakeLocalShareCallerEmpty<MegDNNConv>,
        typename LocalShareParam = megdnn::param::LocalShare>
994 995 996 997 998 999 1000
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 已提交
1001 1002 1003 1004 1005 1006
    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);
1007
        if (!ret) {
M
Megvii Engine Team 已提交
1008
            ret = Maker1::template make<Opr>(inputs, param, execution_policy, config);
1009
        }
1010
        if (!ret) {
M
Megvii Engine Team 已提交
1011
            ret = Maker2::template make<Opr>(inputs, param, execution_policy, config);
1012
        }
1013 1014 1015 1016
        mgb_assert(ret);
        return ret;
    }

M
Megvii Engine Team 已提交
1017 1018 1019
    static cg::OperatorNodeBase* load(
            OprLoadContext& ctx, const cg::VarNodeArray& inputs,
            const OperatorNodeConfig& config) {
1020
        auto param = ctx.read_param<LocalShareParam>();
M
Megvii Engine Team 已提交
1021
        auto execution_policy = ctx.read_param<megdnn::param::ExecutionPolicy>();
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        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 已提交
1035
                  MakeLocalShareCaller3<megdnn::LocalShare>, megdnn::LocalShare> {};
1036 1037 1038 1039
template <>
struct OprLoadDumpImpl<opr::LocalShareBackwardFilter, 0>
        : public LocalShareLoadDumpImpl<
                  opr::LocalShareBackwardFilter,
M
Megvii Engine Team 已提交
1040
                  MakeLocalShareCaller3<megdnn::LocalShare>, megdnn::LocalShare> {};
1041 1042 1043 1044
template <>
struct OprLoadDumpImpl<opr::DeformableConvForward, 0>
        : public ConvLoadDumpImpl<
                  opr::DeformableConvForward,
M
Megvii Engine Team 已提交
1045 1046
                  MakeConvCaller4<megdnn::DeformableConvForward>, megdnn::Convolution> {
};
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
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> {};
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

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

1072
}  // namespace serialization
1073 1074

namespace opr {
1075 1076 1077
using ConvolutionV2 = Convolution;
using ConvolutionBackwardDataV2 = ConvolutionBackwardData;
using ConvolutionBackwardFilterV2 = ConvolutionBackwardFilter;
1078 1079 1080 1081
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);
1082 1083 1084 1085

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

1086 1087 1088
MGB_SEREG_OPR(SlidingWindowTranspose, 1);
MGB_SEREG_OPR(SlidingWindowTransposeBackward, 2);

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
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);
1102 1103 1104 1105 1106

MGB_SEREG_OPR(LRN, 1);
MGB_SEREG_OPR(LRNBackward, 3);
using PoolingV1 = Pooling;
using PoolingBackwardV1 = PoolingBackward;
1107 1108
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);
1109 1110 1111 1112 1113 1114 1115 1116
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);

1117 1118
using MaskConvolutionV2 = MaskConvolution;
MGB_SEREG_OPR(MaskConvolutionV2, 3);
1119 1120
MGB_SEREG_OPR(MaskPropagate, 1);

1121 1122 1123 1124
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);
1125 1126

using ConvBiasForwardV4 = ConvBiasForward;
1127
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(ConvBiasForwardV4, 0, opr_shallow_copy_conv);
1128

1129 1130 1131 1132
using BatchNormV1 = BatchNorm;
using BatchNormBackwardV1 = BatchNormBackward;
MGB_SEREG_OPR(BatchNormV1, 0);
MGB_SEREG_OPR(BatchNormBackwardV1, 6);
1133 1134 1135 1136

using LocalShareForwardV1 = LocalShareForward;
using LocalShareBackwardDataV1 = LocalShareBackwardData;
using LocalShareBackwardFilterV1 = LocalShareBackwardFilter;
1137 1138 1139 1140
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);
1141 1142 1143 1144 1145

using ROIAlignV1 = ROIAlign;
using ROIAlignBackwardV1 = ROIAlignBackward;
MGB_SEREG_OPR(ROIAlignV1, 2);
MGB_SEREG_OPR(ROIAlignBackwardV1, 4);
1146 1147 1148
using DeformableConvForwardV1 = DeformableConvForward;
using DeformableConvBackwardDataV1 = DeformableConvBackwardData;
using DeformableConvBackwardFilterV1 = DeformableConvBackwardFilter;
1149 1150 1151 1152 1153
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);
1154

1155 1156 1157 1158
MGB_SEREG_OPR(CorrelationForward, 2);
MGB_SEREG_OPR(CorrelationBackwardData1, 3);
MGB_SEREG_OPR(CorrelationBackwardData2, 3);

1159 1160 1161 1162
MGB_SEREG_OPR(DeformablePSROIPoolingForward, 3);
MGB_SEREG_OPR(DeformablePSROIPoolingBackward, 5);

using BatchConvBiasForwardV1 = BatchConvBiasForward;
1163
MGB_SEREG_OPR_AND_REG_SHALLOW_COPY(BatchConvBiasForwardV1, 0, opr_shallow_copy_conv);
1164 1165 1166 1167
MGB_SEREG_OPR(FakeQuant, 3);
MGB_SEREG_OPR(FakeQuantBackward, 4);
MGB_SEREG_OPR(TQT, 2);
MGB_SEREG_OPR(TQTBackward, 3);
M
Megvii Engine Team 已提交
1168 1169
MGB_SEREG_OPR(LSQ, 4);
MGB_SEREG_OPR(LSQBackward, 5);
1170 1171
MGB_SEREG_OPR(LayerNorm, 0);
MGB_SEREG_OPR(LayerNormBackward, 0);
1172 1173
MGB_SEREG_OPR(GroupNorm, 0);
MGB_SEREG_OPR(GroupNormBackward, 0);
1174 1175
MGB_SEREG_OPR(InstanceNorm, 0);
MGB_SEREG_OPR(InstanceNormBackward, 0);
1176 1177
MGB_SEREG_OPR(RNNCellForward, 6);
MGB_SEREG_OPR(LSTMCellForward, 7);
1178 1179 1180 1181
MGB_SEREG_OPR(RNNForward, 3);
MGB_SEREG_OPR(RNNBackward, 7);
MGB_SEREG_OPR(LSTMForward, 4);
MGB_SEREG_OPR(LSTMBackward, 9);
1182 1183
MGB_SEREG_OPR(Softmax, 1);
MGB_SEREG_OPR(SoftmaxBackward, 2);
1184 1185 1186 1187 1188 1189 1190 1191
MGB_SEREG_OPR_V2(
        GroupNorm, 0,
        (mgb::serialization::OprLoadDumpImplV2<opr::GroupNorm, 0>::replace_opr),
        VERSION_2, CURRENT_VERSION);
MGB_SEREG_OPR_V2(
        GroupNormBackward, 0,
        (mgb::serialization::OprLoadDumpImplV2<opr::GroupNormBackward, 0>::replace_opr),
        VERSION_2, CURRENT_VERSION);
1192 1193 1194 1195 1196 1197 1198 1199 1200
MGB_SEREG_OPR_V2(
        InstanceNorm, 0,
        (mgb::serialization::OprLoadDumpImplV2<opr::InstanceNorm, 0>::replace_opr),
        VERSION_2, CURRENT_VERSION);
MGB_SEREG_OPR_V2(
        InstanceNormBackward, 0,
        (mgb::serialization::OprLoadDumpImplV2<
                opr::InstanceNormBackward, 0>::replace_opr),
        VERSION_2, CURRENT_VERSION);
1201 1202 1203
}  // namespace opr

}  // namespace mgb
1204 1205

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