specializations.cpp 22.7 KB
Newer Older
1
/**
2
 * \file imperative/src/impl/ops/specialzations.cpp
3 4
 * 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 13 14 15
 */

// FIXME: split this file into separate files for each specialized op

#include "megbrain/imperative/ops/autogen.h"
M
Megvii Engine Team 已提交
16 17
#include "megbrain/opr/basic_arith.h"
#include "megbrain/opr/blas.h"
18
#include "megbrain/opr/dnn/adaptive_pooling.h"
M
Megvii Engine Team 已提交
19 20
#include "megbrain/opr/dnn/convolution.h"
#include "megbrain/opr/dnn/correlation.h"
21
#include "megbrain/opr/dnn/fake_quant.h"
M
Megvii Engine Team 已提交
22
#include "megbrain/opr/dnn/images2neibs.h"
23
#include "megbrain/opr/dnn/local.h"
M
Megvii Engine Team 已提交
24 25
#include "megbrain/opr/dnn/lsq.h"
#include "megbrain/opr/dnn/pooling.h"
26 27
#include "megbrain/opr/dnn/roi_align.h"
#include "megbrain/opr/dnn/roi_pooling.h"
M
Megvii Engine Team 已提交
28
#include "megbrain/opr/dnn/tqt.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include "megbrain/opr/imgproc.h"
#include "megbrain/opr/indexing.h"
#include "megbrain/opr/io.h"
#include "megbrain/opr/misc.h"
#include "megbrain/opr/nn_int.h"
#include "megbrain/opr/rand.h"
#include "megbrain/opr/tensor_gen.h"
#include "megbrain/opr/tensor_manip.h"
#include "megbrain/opr/utility.h"

#include "../op_trait.h"

namespace mgb::imperative {

M
Megvii Engine Team 已提交
43 44
namespace {
namespace dimshuffle {
45 46 47
std::shared_ptr<OpDef> make_from_op_node(cg::OperatorNodeBase* node_) {
    auto* node = &node_->cast_final_safe<opr::Dimshuffle>();
    std::vector<int> pattern(node->param().pattern_len);
M
Megvii Engine Team 已提交
48
    for (size_t i = 0; i < node->param().pattern_len; ++i) {
49 50 51 52 53
        pattern[i] = node->param().pattern[i];
    }
    return Dimshuffle::make(pattern);
}

M
Megvii Engine Team 已提交
54
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
55
    auto&& ds = static_cast<const Dimshuffle&>(def);
56 57
    OperatorNodeConfig config{ds.make_name()};
    return opr::Dimshuffle::make(inputs[0], ds.pattern, 0UL, config);
58 59 60
}

OP_TRAIT_REG(Dimshuffle, Dimshuffle, opr::Dimshuffle)
M
Megvii Engine Team 已提交
61 62 63 64 65
        .make_from_op_node(make_from_op_node)
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace dimshuffle
}  // namespace
66

M
Megvii Engine Team 已提交
67 68 69
namespace {
namespace add_axis {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
70 71 72 73 74 75
    auto&& add_axis = static_cast<const AddAxis&>(def);
    using Desc = opr::AxisAddRemove::AxisDesc;
    std::vector<Desc> param;
    for (auto&& i : add_axis.axis) {
        param.push_back(Desc::make_add(i));
    }
76 77
    OperatorNodeConfig config{add_axis.make_name()};
    return opr::AxisAddRemove::make(inputs[0], param, config);
78 79
}

M
Megvii Engine Team 已提交
80 81 82
OP_TRAIT_REG(AddAxis, AddAxis).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace add_axis
}  // namespace
83

M
Megvii Engine Team 已提交
84 85 86
namespace {
namespace remove_axis {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
87 88 89 90 91 92
    auto&& remove_axis = static_cast<const RemoveAxis&>(def);
    using Desc = opr::AxisAddRemove::AxisDesc;
    std::vector<Desc> param;
    for (auto&& i : remove_axis.axis) {
        param.push_back(Desc::make_remove(i));
    }
93 94
    OperatorNodeConfig config{remove_axis.make_name()};
    return opr::AxisAddRemove::make(inputs[0], param, config);
95 96 97
}

OP_TRAIT_REG(RemoveAxis, RemoveAxis)
M
Megvii Engine Team 已提交
98 99 100 101
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace remove_axis
}  // namespace
102

M
Megvii Engine Team 已提交
103 104 105
namespace {
namespace top_k {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
106
    auto&& topk = static_cast<const TopK&>(def);
107 108
    OperatorNodeConfig config{topk.make_name()};
    return opr::TopK::make(inputs[0], inputs[1], topk.param(), config)[0]
M
Megvii Engine Team 已提交
109 110
            .node()
            ->owner_opr();
111 112
}

M
Megvii Engine Team 已提交
113 114 115
OP_TRAIT_REG(TopK, TopK).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace top_k
}  // namespace
116

M
Megvii Engine Team 已提交
117 118 119
namespace {
namespace reduce {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
120
    auto&& reduce = static_cast<const Reduce&>(def);
121
    OperatorNodeConfig config{reduce.make_name()};
122
    if (inputs.size() > 1) {
123
        return opr::Reduce::make(inputs[0], reduce.param(), inputs[1], config);
124
    } else {
M
Megvii Engine Team 已提交
125 126
        return opr::Reduce::make(inputs[0], reduce.param(),
                                 (cg::VarNode*)nullptr, config);
127 128 129
    }
}

130 131 132 133 134 135
std::shared_ptr<OpDef> make_from_op_node(cg::OperatorNodeBase* node_) {
    auto* node = &node_->cast_final_safe<opr::Reduce>();
    return Reduce::make(node->param());
}

OP_TRAIT_REG(Reduce, Reduce, opr::Reduce)
M
Megvii Engine Team 已提交
136 137 138 139 140
        .make_from_op_node(make_from_op_node)
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace reduce
}  // namespace
141

M
Megvii Engine Team 已提交
142 143 144
namespace {
namespace adaptive_pooling {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
145
    auto&& pool = static_cast<const AdaptivePooling&>(def);
146
    OperatorNodeConfig config{pool.make_name()};
M
Megvii Engine Team 已提交
147 148
    return opr::AdaptivePooling::make(inputs[0], inputs[1], pool.param(),
                                      config);
149 150 151
}

OP_TRAIT_REG(AdaptivePooling, AdaptivePooling)
M
Megvii Engine Team 已提交
152 153 154 155
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace adaptive_pooling
}  // namespace
156

M
Megvii Engine Team 已提交
157 158 159
namespace {
namespace conv_bias {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
160 161
    auto&& conv = static_cast<const ConvBias&>(def);
    cg::OperatorNodeConfig config{conv.dtype};
162
    config.name(conv.make_name());
163
    if (inputs.size() == 2) {
M
Megvii Engine Team 已提交
164 165
        return opr::ConvBias::make(inputs[0], inputs[1], conv.param(),
                                   conv.policy(), config);
166
    } else if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
167 168
        return opr::ConvBias::make(inputs[0], inputs[1], inputs[2],
                                   conv.param(), conv.policy(), config);
169
    } else if (inputs.size() == 4) {
M
Megvii Engine Team 已提交
170 171
        return opr::ConvBias::make(inputs[0], inputs[1], inputs[2], inputs[3],
                                   conv.param(), conv.policy(), config);
172 173 174 175 176
    }
    mgb_assert(0);
}

OP_TRAIT_REG(ConvBias, ConvBias)
M
Megvii Engine Team 已提交
177 178 179 180
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace conv_bias
}  // namespace
181

M
Megvii Engine Team 已提交
182 183 184
namespace {
namespace batch_conv_bias {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
185 186
    auto&& conv = static_cast<const BatchConvBias&>(def);
    cg::OperatorNodeConfig config{conv.dtype};
187
    config.name(conv.make_name());
188
    if (inputs.size() == 2) {
M
Megvii Engine Team 已提交
189 190
        return opr::BatchConvBias::make(inputs[0], inputs[1], conv.param(),
                                        conv.policy(), config);
191
    } else if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
192 193
        return opr::BatchConvBias::make(inputs[0], inputs[1], inputs[2],
                                        conv.param(), conv.policy(), config);
194
    } else if (inputs.size() == 4) {
M
Megvii Engine Team 已提交
195 196 197
        return opr::BatchConvBias::make(inputs[0], inputs[1], inputs[2],
                                        inputs[3], conv.param(), conv.policy(),
                                        config);
198 199 200 201 202
    }
    mgb_assert(0);
}

OP_TRAIT_REG(BatchConvBias, BatchConvBias)
M
Megvii Engine Team 已提交
203 204 205 206
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace batch_conv_bias
}  // namespace
207

M
Megvii Engine Team 已提交
208 209 210
namespace {
namespace pooling {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
211
    auto&& pool = static_cast<const Pooling&>(def);
212 213
    OperatorNodeConfig config{pool.make_name()};
    return opr::Pooling::make(inputs[0], pool.param(), config);
214
}
M
Megvii Engine Team 已提交
215 216 217
OP_TRAIT_REG(Pooling, Pooling).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace pooling
}  // namespace
218

M
Megvii Engine Team 已提交
219 220 221
namespace {
namespace matrix_mul {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
222 223
    auto&& matmul = static_cast<const MatrixMul&>(def);
    mgb_assert(inputs.size() == 2);
224
    OperatorNodeConfig config{matmul.make_name()};
225
    return opr::MatrixMul::make(inputs[0], inputs[1], matmul.param(),
226
                                matmul.policy(), config);
227 228
}
OP_TRAIT_REG(MatrixMul, MatrixMul)
M
Megvii Engine Team 已提交
229 230 231 232
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace matrix_mul
}  // namespace
233

M
Megvii Engine Team 已提交
234 235 236
namespace {
namespace batched_matrix_mul {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
237 238
    auto&& matmul = static_cast<const BatchedMatrixMul&>(def);
    mgb_assert(inputs.size() == 2);
239
    OperatorNodeConfig config{matmul.make_name()};
240
    return opr::BatchedMatrixMul::make(inputs[0], inputs[1], matmul.param(),
241
                                       matmul.policy(), config);
242 243
}
OP_TRAIT_REG(BatchedMatrixMul, BatchedMatrixMul)
M
Megvii Engine Team 已提交
244 245 246 247
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace batched_matrix_mul
}  // namespace
248

M
Megvii Engine Team 已提交
249 250 251
namespace {
namespace dot {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
252
    auto&& op = def.cast_final_safe<Dot>();
253
    mgb_assert(inputs.size() == 2);
254 255
    OperatorNodeConfig config{op.make_name()};
    return opr::Dot::make(inputs[0], inputs[1], config);
256
}
M
Megvii Engine Team 已提交
257 258 259
OP_TRAIT_REG(Dot, Dot).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace dot
}  // namespace
260

M
Megvii Engine Team 已提交
261 262 263
namespace {
namespace argsort {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
264
    auto&& argsort = static_cast<const Argsort&>(def);
265 266
    OperatorNodeConfig config{argsort.make_name()};
    return opr::Argsort::make(inputs[0], argsort.param(), config);
267
}
M
Megvii Engine Team 已提交
268 269 270
OP_TRAIT_REG(Argsort, Argsort).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace argsort
}  // namespace
271

M
Megvii Engine Team 已提交
272 273 274
namespace {
namespace argmax {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
275
    auto&& argmax = static_cast<const Argmax&>(def);
276 277
    OperatorNodeConfig config{argmax.make_name()};
    return opr::Argmax::make(inputs[0], argmax.param(), config);
278
}
M
Megvii Engine Team 已提交
279 280 281
OP_TRAIT_REG(Argmax, Argmax).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace argmax
}  // namespace
282

M
Megvii Engine Team 已提交
283 284 285
namespace {
namespace argmin {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
286
    auto&& argmin = static_cast<const Argmin&>(def);
287 288
    OperatorNodeConfig config{argmin.make_name()};
    return opr::Argmin::make(inputs[0], argmin.param(), config);
289
}
M
Megvii Engine Team 已提交
290 291 292
OP_TRAIT_REG(Argmin, Argmin).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace argmin
}  // namespace
293

M
Megvii Engine Team 已提交
294 295 296
namespace {
namespace warp_perspective {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
297
    auto&& warp = static_cast<const WarpPerspective&>(def);
298
    OperatorNodeConfig config{warp.make_name()};
299
    if (inputs.size() == 3) {
M
Megvii Engine Team 已提交
300 301
        return opr::WarpPerspective::make(inputs[0], inputs[1], inputs[2],
                                          warp.param(), config);
302 303
    } else {
        mgb_assert(inputs.size() == 4);
M
Megvii Engine Team 已提交
304 305
        return opr::WarpPerspective::make(inputs[0], inputs[1], inputs[2],
                                          inputs[3], warp.param(), config);
306 307 308
    }
}
OP_TRAIT_REG(WarpPerspective, WarpPerspective)
M
Megvii Engine Team 已提交
309 310 311 312
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace warp_perspective
}  // namespace
313

M
Megvii Engine Team 已提交
314 315 316
namespace {
namespace group_local {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
317 318
    auto&& local = static_cast<const GroupLocal&>(def);
    mgb_assert(inputs.size() == 2);
319 320
    OperatorNodeConfig config{local.make_name()};
    return opr::GroupLocal::make(inputs[0], inputs[1], local.param(), config);
321 322
}
OP_TRAIT_REG(GroupLocal, GroupLocal)
M
Megvii Engine Team 已提交
323 324 325 326
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace group_local
}  // namespace
327

M
Megvii Engine Team 已提交
328 329 330
namespace {
namespace indexing_one_hot {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
331 332
    auto&& op = static_cast<const IndexingOneHot&>(def);
    mgb_assert(inputs.size() == 2);
333 334
    OperatorNodeConfig config{op.make_name()};
    return opr::IndexingOneHot::make(inputs[0], inputs[1], op.param(), config);
335 336
}
OP_TRAIT_REG(IndexingOneHot, IndexingOneHot)
M
Megvii Engine Team 已提交
337 338 339 340
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace indexing_one_hot
}  // namespace
341

M
Megvii Engine Team 已提交
342 343 344
namespace {
namespace indexing_set_one_hot {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
345 346
    auto&& op = static_cast<const IndexingSetOneHot&>(def);
    mgb_assert(inputs.size() == 3);
347
    OperatorNodeConfig config{op.make_name()};
M
Megvii Engine Team 已提交
348 349
    return opr::IndexingSetOneHot::make(inputs[0], inputs[1], inputs[2],
                                        op.param(), config);
350 351
}
OP_TRAIT_REG(IndexingSetOneHot, IndexingSetOneHot)
M
Megvii Engine Team 已提交
352 353 354 355
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace indexing_set_one_hot
}  // namespace
356

M
Megvii Engine Team 已提交
357 358 359
namespace {
namespace typecvt {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
360 361
    auto&& op = static_cast<const TypeCvt&>(def);
    mgb_assert(inputs.size() == 1);
362 363
    OperatorNodeConfig config{op.make_name()};
    return opr::TypeCvt::make(inputs[0], op.dtype, config);
364
}
M
Megvii Engine Team 已提交
365 366 367
OP_TRAIT_REG(TypeCvt, TypeCvt).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace typecvt
}  // namespace
368

M
Megvii Engine Team 已提交
369 370 371
namespace {
namespace concat {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
372 373
    auto&& op = static_cast<const Concat&>(def);
    cg::OperatorNodeConfig config{op.comp_node};
374
    config.name(op.make_name());
375 376
    return opr::Concat::make(inputs, op.axis, config);
}
M
Megvii Engine Team 已提交
377 378 379
OP_TRAIT_REG(Concat, Concat).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace concat
}  // namespace
380

M
Megvii Engine Team 已提交
381 382 383
namespace {
namespace copy {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
384 385 386
    auto&& op = static_cast<const Copy&>(def);
    mgb_assert(inputs.size() == 1);
    cg::OperatorNodeConfig config{op.comp_node};
387
    config.name(op.make_name());
388 389
    return opr::Copy::make(inputs[0], config);
}
M
Megvii Engine Team 已提交
390 391 392
OP_TRAIT_REG(Copy, Copy).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace copy
}  // namespace
393

394 395
namespace { namespace assert_equal {
auto apply_on_var_node(
396 397 398 399 400 401 402 403
        const OpDef& def,
        const VarNodeArray& inputs) {
    auto&& op = def.cast_final<AssertEqual>();
    if (inputs.size() == 2) {
        return opr::AssertEqual::make(inputs[0], inputs[1], op.param());
    } else {
        // workaround for MiniGraph, which only allow one opr in the graph
        mgb_assert(inputs.size() == 3);
M
Megvii Engine Team 已提交
404 405
        return opr::AssertEqual::make(inputs[0], inputs[1], inputs[2],
                                      op.param(), {});
406
    }
407
}
408

409
OP_TRAIT_REG(AssertEqual, AssertEqual)
M
Megvii Engine Team 已提交
410 411 412 413
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace assert_equal
}  // namespace
414

M
Megvii Engine Team 已提交
415 416 417
namespace {
namespace roi_align {
VarNodeArray apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
418 419
    auto&& op = static_cast<const ROIAlign&>(def);
    mgb_assert(inputs.size() == 2);
420
    OperatorNodeConfig config{op.make_name()};
M
Megvii Engine Team 已提交
421 422 423
    auto* opr = opr::ROIAlign::make(inputs[0], inputs[1], op.param(), config)
                        .node()
                        ->owner_opr();
424
    return {opr->output(0), opr->output(1)};
425 426
}
OP_TRAIT_REG(ROIAlign, ROIAlign)
M
Megvii Engine Team 已提交
427 428 429 430
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace roi_align
}  // namespace
431

M
Megvii Engine Team 已提交
432 433 434
namespace {
namespace correlation {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
435 436 437
    auto&& op = static_cast<const Correlation&>(def);
    mgb_assert(inputs.size() == 2);
    OperatorNodeConfig config{op.make_name()};
M
Megvii Engine Team 已提交
438
    return opr::Correlation::make(inputs[0], inputs[1], op.param(), config);
439 440
}
OP_TRAIT_REG(Correlation, Correlation)
M
Megvii Engine Team 已提交
441 442 443 444
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace correlation
}  // namespace
445

446
#if MGB_CUDA
M
Megvii Engine Team 已提交
447 448 449
namespace {
namespace nvof {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
450 451
    auto&& op = static_cast<const NvOf&>(def);
    mgb_assert(inputs.size() == 1);
452 453
    OperatorNodeConfig config{op.make_name()};
    return opr::NvOf::make(inputs[0], op.param(), config);
454
}
M
Megvii Engine Team 已提交
455 456 457
OP_TRAIT_REG(NvOf, NvOf).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace nvof
}  // namespace
458 459
#endif

M
Megvii Engine Team 已提交
460 461 462
namespace {
namespace linspace {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
463 464 465
    auto&& op = static_cast<const Linspace&>(def);
    mgb_assert(inputs.size() == 3);
    cg::OperatorNodeConfig config{op.comp_node};
466
    config.name(op.make_name());
M
Megvii Engine Team 已提交
467 468
    return opr::Linspace::make(inputs[0], inputs[1], inputs[2], op.param(),
                               config);
469 470
}
OP_TRAIT_REG(Linspace, Linspace)
M
Megvii Engine Team 已提交
471 472 473 474
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace linspace
}  // namespace
475

M
Megvii Engine Team 已提交
476 477 478
namespace {
namespace eye {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
479 480 481
    auto&& op = static_cast<const Eye&>(def);
    mgb_assert(inputs.size() == 1);
    cg::OperatorNodeConfig config{op.comp_node};
482
    config.name(op.make_name());
483 484 485
    opr::Eye::Param param{op.k, op.dtype.enumv()};
    return opr::Eye::make(inputs[0], param, config);
}
M
Megvii Engine Team 已提交
486 487 488
OP_TRAIT_REG(Eye, Eye).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace eye
}  // namespace
489

M
Megvii Engine Team 已提交
490 491 492
namespace {
namespace roi_pooling {
VarNodeArray apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
493 494
    auto&& op = static_cast<const ROIPooling&>(def);
    mgb_assert(inputs.size() == 3);
495
    OperatorNodeConfig config{op.make_name()};
M
Megvii Engine Team 已提交
496 497 498 499
    auto* opr = opr::ROIPooling::make(inputs[0], inputs[1], inputs[2],
                                      op.param(), config)
                        .node()
                        ->owner_opr();
500
    return {opr->output(0), opr->output(1)};
501 502
}
OP_TRAIT_REG(ROIPooling, ROIPooling)
M
Megvii Engine Team 已提交
503 504 505 506
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace roi_pooling
}  // namespace
507

M
Megvii Engine Team 已提交
508 509 510
namespace {
namespace remap {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
511 512
    auto&& op = static_cast<const Remap&>(def);
    mgb_assert(inputs.size() == 2);
513 514
    OperatorNodeConfig config{op.make_name()};
    return opr::Remap::make(inputs[0], inputs[1], op.param(), config);
515
}
M
Megvii Engine Team 已提交
516 517 518
OP_TRAIT_REG(Remap, Remap).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace remap
}  // namespace
519 520 521

namespace {
auto get_index(
M
Megvii Engine Team 已提交
522 523
        const VarNodeArray& inputs, size_t vidx,
        const std::vector<std::tuple<int8_t, bool, bool, bool, bool>>& mask) {
524 525
    size_t length = mask.size();
    opr::Subtensor::IndexDesc ret(length);
M
Megvii Engine Team 已提交
526
    for (size_t i = 0; i < length; ++i) {
527 528 529 530 531 532
        auto&& [axis, begin, end, step, idx] = mask[i];
        ret[i].axis = axis;
        if (idx) {
            ret[i].idx = inputs[vidx++];
        } else {
            mgb_assert(begin || end || step);
M
Megvii Engine Team 已提交
533 534 535 536 537 538
            if (begin)
                ret[i].begin = inputs[vidx++];
            if (end)
                ret[i].end = inputs[vidx++];
            if (step)
                ret[i].step = inputs[vidx++];
539 540 541 542 543 544 545 546
        }
    }
    mgb_assert(vidx == inputs.size());
    return ret;
}
#define IN1 inputs[0]
#define IN2 inputs[0], inputs[1]

M
Megvii Engine Team 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559
#define FANCY_INDEXING_IMPL(NAME, NR_INPUT)                                    \
    namespace NAME##_impl {                                                    \
        auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) { \
            auto&& op = static_cast<const NAME&>(def);                         \
            OperatorNodeConfig config{op.make_name()};                         \
            return opr::NAME::make(IN##NR_INPUT,                               \
                                   get_index(inputs, NR_INPUT, op.items),      \
                                   config);                                    \
        }                                                                      \
        OP_TRAIT_REG(NAME, NAME)                                               \
                .apply_on_var_node(apply_on_var_node)                          \
                .fallback();                                                   \
    }
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

FANCY_INDEXING_IMPL(Subtensor, 1)
FANCY_INDEXING_IMPL(SetSubtensor, 2)
FANCY_INDEXING_IMPL(IncrSubtensor, 2)
FANCY_INDEXING_IMPL(IndexingMultiAxisVec, 1)
FANCY_INDEXING_IMPL(IndexingSetMultiAxisVec, 2)
FANCY_INDEXING_IMPL(IndexingIncrMultiAxisVec, 2)
FANCY_INDEXING_IMPL(MeshIndexing, 1)
FANCY_INDEXING_IMPL(IncrMeshIndexing, 2)
FANCY_INDEXING_IMPL(SetMeshIndexing, 2)
FANCY_INDEXING_IMPL(BatchedMeshIndexing, 1)
FANCY_INDEXING_IMPL(BatchedIncrMeshIndexing, 2)
FANCY_INDEXING_IMPL(BatchedSetMeshIndexing, 2)

#undef FANCY_INDEXING_IMPL
#undef IN1
#undef IN2
M
Megvii Engine Team 已提交
577
}  // anonymous namespace
578

M
Megvii Engine Team 已提交
579 580 581
namespace {
namespace fake_quant {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
582 583
    auto&& op = static_cast<const FakeQuant&>(def);
    mgb_assert(inputs.size() == 3);
584
    OperatorNodeConfig config{op.make_name()};
M
Megvii Engine Team 已提交
585 586
    return opr::FakeQuant::make(inputs[0], inputs[1], inputs[2], op.param(),
                                config);
587 588
}
OP_TRAIT_REG(FakeQuant, FakeQuant)
M
Megvii Engine Team 已提交
589 590 591 592
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace fake_quant
}  // namespace
593

M
Megvii Engine Team 已提交
594 595 596
namespace {
namespace tqt {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
M
Megvii Engine Team 已提交
597 598
    auto&& op = static_cast<const TQT&>(def);
    mgb_assert(inputs.size() == 2);
599 600
    OperatorNodeConfig config{op.make_name()};
    return opr::TQT::make(inputs[0], inputs[1], op.param(), config);
M
Megvii Engine Team 已提交
601
}
M
Megvii Engine Team 已提交
602 603 604
OP_TRAIT_REG(TQT, TQT).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace tqt
}  // namespace
605

M
Megvii Engine Team 已提交
606 607 608
namespace {
namespace elemwise_multi_type {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
609 610
    auto&& op = static_cast<const ElemwiseMultiType&>(def);
    OperatorNodeConfig config{op.dtype};
611
    config.name(op.make_name());
612 613 614
    return opr::ElemwiseMultiType::make(inputs, op.param(), config);
}
OP_TRAIT_REG(ElemwiseMultiType, ElemwiseMultiType)
M
Megvii Engine Team 已提交
615 616 617 618
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace elemwise_multi_type
}  // namespace
619

M
Megvii Engine Team 已提交
620 621 622
namespace {
namespace svd {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
623 624
    auto&& op = static_cast<const SVD&>(def);
    mgb_assert(inputs.size() == 1);
625 626
    OperatorNodeConfig config{op.make_name()};
    return opr::SVD::make(inputs[0], op.param(), config)[0]
M
Megvii Engine Team 已提交
627 628 629
            .node()
            ->owner_opr()
            ->usable_output();
630
}
M
Megvii Engine Team 已提交
631 632 633
OP_TRAIT_REG(SVD, SVD).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace svd
}  // namespace
634

M
Megvii Engine Team 已提交
635 636 637
namespace {
namespace images2neibs {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
638 639 640 641 642
    auto&& op = static_cast<const Images2Neibs&>(def);
    OperatorNodeConfig config{op.make_name()};
    return opr::Images2Neibs::make(inputs[0], op.param(), config);
}
OP_TRAIT_REG(Images2Neibs, Images2Neibs)
M
Megvii Engine Team 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        .apply_on_var_node(apply_on_var_node)
        .fallback();
}  // namespace images2neibs
}  // namespace

namespace {
namespace lsq {
auto apply_on_var_node(const OpDef& def, const VarNodeArray& inputs) {
    auto&& op = static_cast<const LSQ&>(def);
    mgb_assert(inputs.size() == 4);
    OperatorNodeConfig config{op.make_name()};
    return opr::LSQ::make(inputs[0], inputs[1], inputs[2], inputs[3],
                          op.param(), config);
}
OP_TRAIT_REG(LSQ, LSQ).apply_on_var_node(apply_on_var_node).fallback();
}  // namespace lsq
}  // namespace
660

M
Megvii Engine Team 已提交
661
}  // namespace mgb::imperative