extern_opr.cpp 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * \file imperative/src/impl/ops/extern_opr.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
#include "megbrain/imperative/ops/autogen.h"
#include "../op_trait.h"

#include "megbrain/serialization/extern_c_opr_io.h"

namespace mgb::imperative {

namespace { namespace externopr {

TensorShapeArray get_shapes(const std::vector<std::vector<size_t>>& shapes) {
    TensorShapeArray ret;
    for (auto&& i:shapes) {
        SmallVector<size_t> shape(i.begin(), i.end());
        TensorShape shp(shape);
        ret.push_back(shp);
    }
    return ret;
}

auto apply_on_var_node(
        const OpDef& def,
        const VarNodeArray& inputs) {

    auto&& op = static_cast<const ExternOpr&>(def);
    SymbolVarArray symbol_var_inputs(inputs.begin(), inputs.end());

    SmallVector<DType> output_dtypes(op.output_dtypes.begin(), op.output_dtypes.end());
    auto&& output_shapes = get_shapes(op.output_shapes);

    cg::OperatorNodeBase* opr = opr::ExternCOprRunner::make_placeholder(
            symbol_var_inputs, output_shapes, op.name.c_str(), op.data.c_str(),
            op.data_len, {}, output_dtypes);
    return opr;
}

OP_TRAIT_REG(ExternOpr, ExternOpr, opr::ExternCOprRunner)
    .apply_on_var_node(apply_on_var_node)
    .fallback();
}} // externopr

}  // namespace mgb::imperative