elemwise_binary_trait_def.inl 2.4 KB
Newer Older
1 2 3 4
/**
 * \file src/opr/test/basic_arith/elemwise_binary_trait_def.inl
 * 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 9 10 11 12 13 14 15 16 17
 *
 * 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.
 */

#ifndef DEF_TRAIT
#error "DEF_TRAIT must be defined"
#endif

/* ======================= binary ======================= */
#define _CUR_ARITY 2
M
Megvii Engine Team 已提交
18
#define _EXPAND_PARAMS     \
19 20 21
    ctype x = inp[0][idx]; \
    ctype y = inp[1][idx]

M
Megvii Engine Team 已提交
22
#define _ALLOW_BOOL  true
M
Megvii Engine Team 已提交
23
#define _ALLOW_FLOAT false
M
Megvii Engine Team 已提交
24 25
#define _ALLOW_INT   false
DEF_TRAIT(AND, x&& y)
M
Megvii Engine Team 已提交
26 27 28 29
DEF_TRAIT(OR, x || y)
DEF_TRAIT(XOR, x ^ y)
#undef _ALLOW_INT
#undef _ALLOW_FLOAT
30

M
Megvii Engine Team 已提交
31
#define _ALLOW_INT   true
32 33 34 35 36
#define _ALLOW_FLOAT true
DEF_TRAIT(EQ, x == y)
DEF_TRAIT(LEQ, x <= y)
DEF_TRAIT(LT, x < y)

M
Megvii Engine Team 已提交
37 38
#undef _ALLOW_BOOL

M
Megvii Engine Team 已提交
39
#define _ALLOW_BOOL  false
40
#define _ALLOW_FLOAT true
M
Megvii Engine Team 已提交
41
#define _ALLOW_INT   true
42 43 44 45 46 47
DEF_TRAIT(ABS_GRAD, x > 0 ? y : -y)
DEF_TRAIT(ADD, x + y)
DEF_TRAIT(FLOOR_DIV, floor(x / y))
DEF_TRAIT(MAX, std::max(x, y))
DEF_TRAIT(MIN, std::min(x, y))
DEF_TRAIT(MOD, do_mod(x, y))
M
Megvii Engine Team 已提交
48 49
DEF_TRAIT(MUL, x* y)
DEF_TRAIT(SIGMOID_GRAD, x*(1 - x) * y)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
DEF_TRAIT(SUB, x - y)
DEF_TRAIT(SWITCH_GT0, x > 0 ? y : 0)
DEF_TRAIT(TANH_GRAD, (1 - x * x) * y)

DEF_TRAIT(FUSE_ADD_RELU, std::max<ctype>(x + y, 0))
#undef _ALLOW_INT

#define _ALLOW_INT false
DEF_TRAIT(POW, std::pow(x, y))
DEF_TRAIT(TRUE_DIV, x / y)
DEF_TRAIT(LOG_SUM_EXP, do_log_sum_exp(x, y))
DEF_TRAIT(FUSE_ADD_SIGMOID, 1 / (1 + std::exp(-(x + y))))
DEF_TRAIT(FUSE_ADD_TANH, std::tanh(x + y))
DEF_TRAIT(FUSE_ADD_H_SWISH, do_fuse_add_h_swish(x, y))
DEF_TRAIT(FAST_TANH_GRAD, do_fast_tanh_grad(x, y))
DEF_TRAIT(ATAN2, std::atan2(x, y))
DEF_TRAIT(H_SWISH_GRAD, do_h_swish_grad(x, y))
M
Megvii Engine Team 已提交
67 68 69
DEF_TRAIT(
        SILU_GRAD, y*(1 + std::exp(-x) + x * std::exp(-x)) / (1 + std::exp(-x)) /
                           (1 + std::exp(-x)))
70 71
DEF_TRAIT(GELU_GRAD, do_gelu_grad(x, y))

72 73 74 75
#undef _ALLOW_INT
#undef _ALLOW_FLOAT

#define _ALLOW_FLOAT false
M
Megvii Engine Team 已提交
76
#define _ALLOW_INT   true
77 78 79 80 81
DEF_TRAIT(SHL, do_shl(x, y))
DEF_TRAIT(SHR, do_shr(x, y))
DEF_TRAIT(RMULH, do_round_mulh_saturate(x, y))
#undef _ALLOW_INT
#undef _ALLOW_FLOAT
M
Megvii Engine Team 已提交
82
#undef _ALLOW_BOOL
83 84 85 86 87

#undef _CUR_ARITY
#undef _EXPAND_PARAMS

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