param_val.cpp 17.8 KB
Newer Older
1 2
#include "megbrain/common.h"

3 4
#if MGB_CUSTOM_OP

5
#include "megbrain/comp_node.h"
6
#include "megbrain/custom/adaptor.h"
7
#include "megbrain/custom/param_val.h"
8
#include "megbrain/custom/tensor.h"
9

10 11 12 13 14 15 16 17 18 19
#pragma GCC diagnostic ignored "-Wsign-compare"

using namespace mgb;

namespace custom {

/**
 * Macro Callback for Case
 */

M
Megvii Engine Team 已提交
20 21 22 23 24 25 26
#define CUSTOM_CASE_TO_ALLOC_ACCORD_TO_RHS(dyn_type, static_type)        \
    case (ParamDynType::dyn_type): {                                     \
        std::unique_ptr<void, void_deleter> new_ptr(                     \
                new static_type(TypedRef(static_type, rhs.m_ptr.get())), \
                impl_deleter<static_type>);                              \
        m_ptr.swap(new_ptr);                                             \
        break;                                                           \
27 28
    }

M
Megvii Engine Team 已提交
29 30 31 32
#define CUSTOM_CASE_TO_ASSIGN_ACCORD_TO_RHS(dyn_type, static_type)                   \
    case (ParamDynType::dyn_type): {                                                 \
        TypedRef(static_type, m_ptr.get()) = TypedRef(static_type, rhs.m_ptr.get()); \
        break;                                                                       \
33 34
    }

M
Megvii Engine Team 已提交
35 36 37 38
#define CUSTOM_ASSERT_OPERAND_VALID(operand, opr)                                \
    mgb_assert(                                                                  \
            operand.m_ptr != nullptr && operand.m_type != ParamDynType::Invalid, \
            "invalid %s of operator %s of ParamVal", #operand, #opr)
39

M
Megvii Engine Team 已提交
40 41 42
#define CUSTOM_INVALID_EXPR_EXCP(lhs, rhs, op)                       \
    mgb_assert(                                                      \
            lhs.m_type == rhs.m_type, "`%s` %s `%s` is not allowed", \
43
            ptype2name(lhs.m_type).c_str(), #op, ptype2name(rhs.m_type).c_str())
44

M
Megvii Engine Team 已提交
45 46 47 48
#define CUSTOM_CASE_TO_GET_BINARY_OP_RHS_AND_CAL(dyn_type, static_type, op) \
    case (ParamDynType::dyn_type): {                                        \
        const auto& rval = TypedRef(static_type, rhs.m_ptr.get());          \
        return lval op rval;                                                \
49 50
    }

M
Megvii Engine Team 已提交
51 52 53 54 55 56 57 58 59 60
#define CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_BASIC(dyn_type, static_type, op) \
    case (ParamDynType::dyn_type): {                                      \
        const auto& lval = TypedRef(static_type, lhs.m_ptr.get());        \
        switch (rhs.m_type) {                                             \
            CUSTOM_FOR_EACH_BASIC_PARAMTYPE_COPY(                         \
                    CUSTOM_CASE_TO_GET_BINARY_OP_RHS_AND_CAL, op)         \
            default:                                                      \
                CUSTOM_INVALID_EXPR_EXCP(lhs, rhs, op);                   \
        }                                                                 \
        break;                                                            \
61 62
    }

M
Megvii Engine Team 已提交
63 64 65 66 67 68
#define CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_NONBASIC(dyn_type, static_type, op) \
    case (ParamDynType::dyn_type): {                                         \
        CUSTOM_INVALID_EXPR_EXCP(lhs, rhs, op);                              \
        const auto& lval = TypedRef(static_type, lhs.m_ptr.get());           \
        const auto& rval = TypedRef(static_type, rhs.m_ptr.get());           \
        return lval op rval;                                                 \
69 70
    }

M
Megvii Engine Team 已提交
71 72 73 74 75 76 77 78 79 80 81 82
#define CUSTOM_DEFINE_BINARY_OP_FOR_BASIC(op, ret_type)              \
    ret_type operator op(const ParamVal& lhs, const ParamVal& rhs) { \
        CUSTOM_ASSERT_OPERAND_VALID(lhs, op);                        \
        CUSTOM_ASSERT_OPERAND_VALID(rhs, op);                        \
                                                                     \
        switch (lhs.m_type) {                                        \
            CUSTOM_FOR_EACH_BASIC_PARAMTYPE(                         \
                    CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_BASIC, op)      \
            default:                                                 \
                CUSTOM_INVALID_EXPR_EXCP(lhs, rhs, op);              \
        }                                                            \
        return {};                                                   \
83 84
    }

M
Megvii Engine Team 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
#define CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING(op, ret_type)                     \
    ret_type operator op(const ParamVal& lhs, const ParamVal& rhs) {                   \
        CUSTOM_ASSERT_OPERAND_VALID(lhs, op);                                          \
        CUSTOM_ASSERT_OPERAND_VALID(rhs, op);                                          \
                                                                                       \
        switch (lhs.m_type) {                                                          \
            CUSTOM_FOR_EACH_BASIC_PARAMTYPE(                                           \
                    CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_BASIC, op)                        \
            CUSTOM_FOR_STRING_PARAMTYPE(CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_NONBASIC, op) \
            default:                                                                   \
                CUSTOM_INVALID_EXPR_EXCP(lhs, rhs, op);                                \
        }                                                                              \
        return {};                                                                     \
98 99
    }

M
Megvii Engine Team 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#define CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(op, ret_type)            \
    ret_type operator op(const ParamVal& lhs, const ParamVal& rhs) {                   \
        CUSTOM_ASSERT_OPERAND_VALID(lhs, op);                                          \
        CUSTOM_ASSERT_OPERAND_VALID(rhs, op);                                          \
                                                                                       \
        switch (lhs.m_type) {                                                          \
            CUSTOM_FOR_EACH_BASIC_PARAMTYPE(                                           \
                    CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_BASIC, op)                        \
            CUSTOM_FOR_STRING_PARAMTYPE(CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_NONBASIC, op) \
            CUSTOM_FOR_EACH_LIST_PARAMTYPE(                                            \
                    CUSTOM_CASE_TO_CAL_BINARY_OP_FOR_NONBASIC, op)                     \
            default:                                                                   \
                CUSTOM_INVALID_EXPR_EXCP(lhs, rhs, op);                                \
        }                                                                              \
        return {};                                                                     \
115 116
    }

M
Megvii Engine Team 已提交
117 118 119 120 121
#define CUSTOM_CASE_TO_PRINT_NONLIST(dyn_type, static_type) \
    case (ParamDynType::dyn_type): {                        \
        auto rval = TypedRef(static_type, m_ptr.get());     \
        ss << rval;                                         \
        break;                                              \
122 123
    }

M
Megvii Engine Team 已提交
124 125 126 127 128
#define CUSTOM_CASE_TO_PRINT_LIST(dyn_type, static_type) \
    case (ParamDynType::dyn_type): {                     \
        auto rval = TypedRef(static_type, m_ptr.get());  \
        ss << vec2str(rval);                             \
        break;                                           \
129 130
    }

M
Megvii Engine Team 已提交
131 132 133 134
#define CUSTOM_CASE_TO_RET_SIZE(dyn_type, static_type)    \
    case (ParamDynType::dyn_type): {                      \
        return TypedRef(static_type, m_ptr.get()).size(); \
        break;                                            \
135 136 137 138 139 140 141 142 143 144
    }

#define CUSTOM_CASE_TO_DUMP_BASIC(dyn_type, static_type)                            \
    case (ParamDynType::dyn_type): {                                                \
        res.resize(sizeof(ParamDynType) + sizeof(static_type));                     \
        memcpy(&res[0], &(value.m_type), sizeof(ParamDynType));                     \
        memcpy(&res[sizeof(ParamDynType)], value.m_ptr.get(), sizeof(static_type)); \
        break;                                                                      \
    }

M
Megvii Engine Team 已提交
145 146 147 148 149 150 151 152 153 154
#define CUSTOM_CASE_TO_DUMP_LIST(dyn_type, static_type)                               \
    case (ParamDynType::dyn_type): {                                                  \
        auto& ref = TypedRef(static_type, value.m_ptr.get());                         \
        size_t len = ref.size();                                                      \
        size_t elem_size = len != 0 ? sizeof(ref[0]) : 0;                             \
        res.resize(sizeof(ParamDynType) + sizeof(len) + len * elem_size);             \
        memcpy(&res[0], &(value.m_type), sizeof(ParamDynType));                       \
        memcpy(&res[sizeof(ParamDynType)], &len, sizeof(len));                        \
        memcpy(&res[sizeof(ParamDynType) + sizeof(len)], ref.data(), len* elem_size); \
        break;                                                                        \
155 156
    }

M
Megvii Engine Team 已提交
157 158 159 160 161 162 163
#define CUSTOM_CASE_TO_LOAD_BASIC(dyn_type, static_type) \
    case (ParamDynType::dyn_type): {                     \
        static_type val;                                 \
        memcpy(&val, &bytes[offset], sizeof(val));       \
        offset += sizeof(val);                           \
        return val;                                      \
        break;                                           \
164 165
    }

M
Megvii Engine Team 已提交
166 167 168 169 170 171 172 173 174 175 176 177
#define CUSTOM_CASE_TO_LOAD_LIST(dyn_type, static_type)    \
    case (ParamDynType::dyn_type): {                       \
        size_t len = 0;                                    \
        memcpy(&len, &bytes[offset], sizeof(len));         \
        offset += sizeof(len);                             \
        static_type vals;                                  \
        vals.resize(len);                                  \
        size_t elem_size = len != 0 ? sizeof(vals[0]) : 0; \
        memcpy(&vals[0], &bytes[offset], len* elem_size);  \
        offset += len * elem_size;                         \
        return vals;                                       \
        break;                                             \
178 179
    }

180 181 182 183 184 185 186 187 188 189 190 191
std::string ptype2name(ParamDynType ptype) {
#define CUSTOM_REG_DYN_PARAMTYPE_NAME(dyn_type, static_type) \
    {ParamDynType::dyn_type, #dyn_type},

    static std::unordered_map<
            ParamDynType, std::string, EnumHash<ParamDynType>, EnumCmp<ParamDynType>>
            type2name = {CUSTOM_FOR_EACH_VALID_PARAMTYPE(CUSTOM_REG_DYN_PARAMTYPE_NAME){
                    ParamDynType::Invalid, "Invalid"}};
#undef CUSTOM_REG_DYN_PARAMTYPE_NAME
    return type2name[ptype];
}

M
Megvii Engine Team 已提交
192
ParamVal::ParamVal() : m_ptr(nullptr, [](void*) -> void {}) {
193 194 195
    m_type = ParamDynType::Invalid;
}

M
Megvii Engine Team 已提交
196
ParamVal::ParamVal(const char* str) : ParamVal(std::string(str)) {}
197

M
Megvii Engine Team 已提交
198 199
ParamVal::ParamVal(const std::initializer_list<const char*>& strs)
        : ParamVal(std::vector<const char*>(strs)) {}
200

M
Megvii Engine Team 已提交
201 202 203
ParamVal::ParamVal(const std::vector<const char*>& strs)
        : m_ptr(new std::vector<std::string>(),
                impl_deleter<std::vector<std::string>>) {
204
    m_type = ParamDynType::StringList;
M
Megvii Engine Team 已提交
205
    for (const auto& str : strs) {
206 207 208 209
        TypedRef(std::vector<std::string>, m_ptr.get()).emplace_back(str);
    }
}

M
Megvii Engine Team 已提交
210
ParamVal::ParamVal(const ParamVal& rhs) : m_ptr(nullptr, [](void*) -> void {}) {
211
    mgb_assert(
M
Megvii Engine Team 已提交
212 213
            rhs.m_type != ParamDynType::Invalid && rhs.m_ptr != nullptr,
            "invalid rhs of copy constructor of ParamVal");
214
    m_type = rhs.m_type;
M
Megvii Engine Team 已提交
215
    switch (m_type) {
216 217 218 219 220 221 222
        CUSTOM_FOR_EACH_VALID_PARAMTYPE(CUSTOM_CASE_TO_ALLOC_ACCORD_TO_RHS)
        default: {
            mgb_assert(false, "invalid rhs of copy constructor of ParamVal");
        }
    }
}

M
Megvii Engine Team 已提交
223
ParamVal& ParamVal::operator=(const char* str) {
224 225 226 227
    this->operator=(std::string(str));
    return *this;
}

M
Megvii Engine Team 已提交
228
ParamVal& ParamVal::operator=(const std::initializer_list<const char*>& strs) {
229 230 231 232
    this->operator=(std::vector<const char*>(strs));
    return *this;
}

M
Megvii Engine Team 已提交
233
ParamVal& ParamVal::operator=(const std::vector<const char*>& strs) {
234
    std::vector<std::string> tmp_strs;
M
Megvii Engine Team 已提交
235
    for (const auto& str : strs) {
236 237 238 239 240 241
        tmp_strs.emplace_back(str);
    }
    this->operator=(tmp_strs);
    return *this;
}

M
Megvii Engine Team 已提交
242
ParamVal& ParamVal::operator=(const ParamVal& rhs) {
243 244 245
    if (&rhs == this)
        return *this;
    mgb_assert(
M
Megvii Engine Team 已提交
246 247 248
            rhs.m_type != ParamDynType::Invalid && rhs.m_ptr != nullptr,
            "invalid rhs of assignment operator of ParamVal");

249
    if (rhs.m_type == m_type) {
M
Megvii Engine Team 已提交
250
        switch (m_type) {
251 252 253 254
            CUSTOM_FOR_EACH_VALID_PARAMTYPE(CUSTOM_CASE_TO_ASSIGN_ACCORD_TO_RHS);
            default:
                mgb_assert(false, "invalid rhs of assignment operator of ParamVal");
        }
M
Megvii Engine Team 已提交
255
    } else {
256
        m_type = rhs.m_type;
M
Megvii Engine Team 已提交
257
        switch (m_type) {
258 259 260 261 262 263 264 265
            CUSTOM_FOR_EACH_VALID_PARAMTYPE(CUSTOM_CASE_TO_ALLOC_ACCORD_TO_RHS);
            default:
                mgb_assert(false, "invalid rhs of assignment operator of ParamVal");
        }
    }
    return *this;
}

M
Megvii Engine Team 已提交
266
const void* ParamVal::raw_ptr(void) const {
267 268 269
    return m_ptr.get();
}

M
Megvii Engine Team 已提交
270
void* ParamVal::raw_ptr(void) {
271 272 273 274 275 276 277 278 279
    return m_ptr.get();
}

ParamDynType ParamVal::type(void) const {
    return m_type;
}

std::string ParamVal::str() const {
    std::stringstream ss;
280
    ss << "type: " << ptype2name(m_type) << "\n"
M
Megvii Engine Team 已提交
281
       << "value: ";
282 283 284 285
    switch (m_type) {
        CUSTOM_FOR_EACH_BASIC_PARAMTYPE(CUSTOM_CASE_TO_PRINT_NONLIST)
        CUSTOM_FOR_STRING_PARAMTYPE(CUSTOM_CASE_TO_PRINT_NONLIST)
        CUSTOM_FOR_EACH_LIST_PARAMTYPE(CUSTOM_CASE_TO_PRINT_LIST)
286 287 288 289 290
        case ParamDynType::Device: {
            auto&& rval = TypedRef(Device, m_ptr.get());
            ss << to_builtin_device(rval).to_string();
            break;
        }
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        default:
            mgb_assert(false, "invalid data of assignment operator of ParamVal");
    }
    return ss.str();
}

size_t ParamVal::size(void) const {
    switch (m_type) {
        CUSTOM_FOR_STRING_PARAMTYPE(CUSTOM_CASE_TO_RET_SIZE)
        CUSTOM_FOR_EACH_LIST_PARAMTYPE(CUSTOM_CASE_TO_RET_SIZE)
        default:
            mgb_assert(false, "there is no size() for basic data types");
    }
}

M
Megvii Engine Team 已提交
306
std::string ParamVal::to_bytes(const ParamVal& value) {
307 308 309
    std::string res;
    // because the specialization of std::vector<bool>
    if (value.type() == ParamDynType::BoolList) {
M
Megvii Engine Team 已提交
310
        std::vector<bool>& ref = TypedRef(std::vector<bool>, value.m_ptr.get());
311 312
        size_t len = ref.size();
        size_t elem_size = sizeof(bool);
M
Megvii Engine Team 已提交
313
        res.resize(sizeof(ParamDynType) + sizeof(len) + len * elem_size);
314 315
        memcpy(&res[0], &(value.m_type), sizeof(ParamDynType));
        memcpy(&res[sizeof(ParamDynType)], &len, sizeof(len));
M
Megvii Engine Team 已提交
316 317
        size_t startpos = sizeof(ParamDynType) + sizeof(len);
        for (size_t idx = 0; idx < len; idx++) {
318
            bool b = ref[idx];
M
Megvii Engine Team 已提交
319
            memcpy(&res[startpos + idx * sizeof(b)], &b, sizeof(b));
320 321
        }
        return res;
M
Megvii Engine Team 已提交
322 323 324
    } else if (value.type() == ParamDynType::StringList) {
        std::vector<std::string>& ref =
                TypedRef(std::vector<std::string>, value.m_ptr.get());
325 326 327 328
        size_t len = ref.size();
        res.resize(sizeof(ParamDynType) + sizeof(len));
        memcpy(&res[0], &(value.m_type), sizeof(ParamDynType));
        memcpy(&res[sizeof(ParamDynType)], &len, sizeof(len));
M
Megvii Engine Team 已提交
329
        for (size_t idx = 0; idx < ref.size(); ++idx) {
330 331 332 333 334 335 336 337
            size_t str_len = ref[idx].size();
            std::string bytes(sizeof(str_len) + str_len, ' ');
            memcpy(&bytes[0], &str_len, sizeof(str_len));
            memcpy(&bytes[sizeof(str_len)], ref[idx].data(), str_len);
            res += bytes;
        }
        return res;
    }
M
Megvii Engine Team 已提交
338
    switch (value.type()) {
339 340 341 342 343 344 345 346 347
        CUSTOM_FOR_EACH_BASIC_PARAMTYPE(CUSTOM_CASE_TO_DUMP_BASIC)
        CUSTOM_FOR_STRING_PARAMTYPE(CUSTOM_CASE_TO_DUMP_LIST)
        CUSTOM_FOR_EACH_BASIC_LIST_PARAMTYPE(CUSTOM_CASE_TO_DUMP_LIST)
        default:
            mgb_assert(false, "invalid param type");
    }
    return res;
}

M
Megvii Engine Team 已提交
348
ParamVal ParamVal::from_bytes(const std::string& bytes, size_t& offset) {
349 350 351 352 353 354 355 356
    ParamDynType data_type = ParamDynType::Invalid;
    memcpy(&data_type, &bytes[offset], sizeof(ParamDynType));
    offset += sizeof(ParamDynType);
    if (data_type == ParamDynType::BoolList) {
        std::vector<bool> ret;
        size_t len = 0;
        memcpy(&len, &bytes[offset], sizeof(len));
        offset += sizeof(len);
M
Megvii Engine Team 已提交
357
        for (size_t idx = 0; idx < len; ++idx) {
358 359 360 361 362 363
            bool b = true;
            memcpy(&b, &bytes[offset], sizeof(bool));
            offset += sizeof(bool);
            ret.push_back(b);
        }
        return ret;
M
Megvii Engine Team 已提交
364
    } else if (data_type == ParamDynType::StringList) {
365 366 367 368
        std::vector<std::string> ret;
        size_t len = 0;
        memcpy(&len, &bytes[offset], sizeof(len));
        offset += sizeof(len);
M
Megvii Engine Team 已提交
369
        for (size_t idx = 0; idx < len; ++idx) {
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
            size_t str_len = 0;
            memcpy(&str_len, &bytes[offset], sizeof(str_len));
            offset += sizeof(str_len);
            std::string str(str_len, ' ');
            memcpy(&str[0], &bytes[offset], str_len);
            offset += str_len;
            ret.push_back(str);
        }
        return ret;
    }

    switch (data_type) {
        CUSTOM_FOR_EACH_BASIC_PARAMTYPE(CUSTOM_CASE_TO_LOAD_BASIC)
        CUSTOM_FOR_STRING_PARAMTYPE(CUSTOM_CASE_TO_LOAD_LIST)
        CUSTOM_FOR_EACH_BASIC_LIST_PARAMTYPE(CUSTOM_CASE_TO_LOAD_LIST);
        default:
            mgb_assert(false, "invalid param type");
    }
    return {};
}

CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING(+, ParamVal)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC(-, ParamVal)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC(*, ParamVal)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC(/, ParamVal)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(==, bool)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(!=, bool)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(>=, bool)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(<=, bool)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(>, bool)
CUSTOM_DEFINE_BINARY_OP_FOR_BASIC_AND_STRING_AND_LIST(<, bool)

M
Megvii Engine Team 已提交
402
}  // namespace custom
403 404

#endif