custom_opnode.sereg.h 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * \file src/opr/impl/custom_opnode.sereg.h
 * 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/opr/custom_opnode.h"
#include "megbrain/serialization/sereg.h"

namespace mgb {
namespace serialization {
M
Megvii Engine Team 已提交
17

18
void custom_dumper(OprDumpContext& ctx, const cg::OperatorNodeBase& opr) {
M
Megvii Engine Team 已提交
19
    auto&& custom_op = opr.cast_final_safe<opr::CustomOpNode>();
20 21 22

    std::string op_type = custom_op.op_type();
    ctx.dump_buf_with_len(op_type.c_str(), op_type.size());
M
Megvii Engine Team 已提交
23

24 25 26 27
    uint32_t tag = custom_op.param_tag();
    ctx.dump_buf_with_len(&tag, sizeof(tag));

    std::string bytes = custom_op.param().to_bytes();
M
Megvii Engine Team 已提交
28
    ctx.dump_buf_with_len(bytes.c_str(), bytes.size());
29 30
}

M
Megvii Engine Team 已提交
31 32 33
mgb::cg::OperatorNodeBase* custom_loader(
        OprLoadContext& ctx, const cg::VarNodeArray& inputs,
        const OperatorNodeConfig& config) {
34
    std::string op_type = ctx.load_buf_with_len();
M
Megvii Engine Team 已提交
35
    auto* op_manager = custom::CustomOpManager::inst();
36 37 38 39 40
    auto op = op_manager->find(op_type);

    std::string tag_str = ctx.load_buf_with_len();
    uint32_t tag = *reinterpret_cast<const uint32_t*>(tag_str.c_str());
    mgb_assert(
M
Megvii Engine Team 已提交
41 42 43
            tag == op->param_info().tag(),
            "Wrong Param TAG of Op %s, should be %u, but load %u\n", op_type.c_str(),
            op->param_info().tag(), tag);
44 45 46 47 48 49 50

    custom::Param param(op->param_info());
    std::string bytes = ctx.load_buf_with_len();
    param.from_bytes(bytes);
    return opr::CustomOpNode::make(op, inputs, param, config)[0]->owner_opr();
}

M
Megvii Engine Team 已提交
51 52
}  // namespace serialization
}  // namespace mgb
53

M
Megvii Engine Team 已提交
54 55 56 57 58 59 60 61 62 63
#define CUSTOM_OP_SEREG_REG(cls)                              \
    namespace {                                               \
    struct _OprReg##cls {                                     \
        static void entry() {                                 \
            MGB_SEREG_OPR_INTL_CALL_ADD(                      \
                    cls, ::mgb::serialization::custom_dumper, \
                    ::mgb::serialization::custom_loader);     \
        }                                                     \
    };                                                        \
    }                                                         \
64 65 66 67 68
    MGB_SEREG_OPR_INTL_CALL_ENTRY(cls, _OprReg##cls)

using namespace mgb;
using CustomOpNode = opr::CustomOpNode;
CUSTOM_OP_SEREG_REG(CustomOpNode);