Binary.cpp 10.7 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
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2017, Natural Language Processing Lab, Northestern University.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* $Created by: JIANG Yufan (email: jiangyufan2018@outlook.com) 2019-04-05
*/

#include <math.h>
#include "../../XName.h"
#include "Binary.h"
#include "Binary.cuh"

namespace nts {

29
template<class T1, class T2>
30
T1 BinaryDescale(T1 x, T2 num)
31
{
32
    return (T1)(x / num);
33 34
}

35
template<class T1, class T2>
36
T1 BinaryPower(T1 x, T2 num)
37
{
38 39 40
    if (num == 0)
        return (T1)1.0;
    else if (num == 0.5)
41
        return (T1)sqrt(x);
42 43 44 45
    else if (num == 2)
        return x * x;
    else {
        if (x == 0 && num < 0)
46
            return (T1)1e20F;
47 48 49
        else
            return (T1)pow(x, num);
    }
50 51
}

52
template<class T1, class T2>
53
T1 BinaryScale(T1 x, T2 num)
54
{
55
    return (T1)(x * num);
56 57
}

58
template<class T1, class T2>
59
T1 BinaryShift(T1 x, T2 num)
60
{
61
    return (T1)(x + num);
62 63
}

64
int BinaryMod(int x, int num)
65
{
66
    return x % num;
67 68
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
/* define three marco separately, specify the respective function names */
#define _SIMPLE_BINARY_FUNCTION(_funcName, _cudaFuncName, origFunc)                  \
template<class T>                                                                    \
void _funcName(const XTensor * a, XTensor * b, T num)                                \
{                                                                                    \
    /* run it on GPUs */                                                             \
    if (a->devID >= 0) {                                                             \
        if (useCUDA) {                                                               \
            _cudaFuncName(a, b, num);                                                \
            return;                                                                  \
        }                                                                            \
        else                                                                         \
            ShowNTErrors("No GPU devices support!")                                  \
    }                                                                                \
    CheckNTErrors((XTensor::IsSameShaped(a, b)),                                     \
                  "Input tensors should have the same data type!");                  \
    if (a->dataType == X_INT) {                                                      \
        int * d = (int*)a->data;                                                     \
        int * db = (int*)b->data;                                                    \
        for (int i = 0; i < a->unitNum; i++)                                         \
            db[i] = (int)origFunc((int)d[i], (T)num);                                \
    }                                                                                \
    else if (a->dataType == X_FLOAT) {                                               \
        float * d = (float*)a->data;                                                 \
        float * db = (float*)b->data;                                                \
        for (int i = 0; i < a->unitNum; i++)                                         \
            db[i] = (float)origFunc((float)d[i], (T)num);                            \
    }                                                                                \
    else if (a->dataType == X_DOUBLE) {                                              \
        double * d = (double*)a->data;                                               \
        double * db = (double*)b->data;                                              \
        for (int i = 0; i < a->unitNum; i++)                                         \
            db[i] = (double)origFunc((double)d[i], (T)num);                          \
    }                                                                                \
    else                                                                             \
        ShowNTErrors("TO DO!");                                                      \
}                                                                                    \
template void _funcName<int>(const XTensor*, XTensor*, int);                         \
template void _funcName<float>(const XTensor*, XTensor*, float);                     \
template void _funcName<double>(const XTensor*, XTensor*, double);

#define _SIMPLE_BINARY_FUNCTION_ME(_funcNameMe, _funcName)                           \
template<class T>                                                                    \
void _funcNameMe(XTensor * a, T num)                                                 \
{                                                                                    \
    _funcName(a, a, num);                                                            \
}                                                                                    \
template void _funcNameMe<int>(XTensor*, int);                                       \
template void _funcNameMe<float>(XTensor*, float);                                   \
template void _funcNameMe<double>(XTensor*, double);                                                                                    
                                                                                     
#define SIMPLE_BINARY_FUNCTION_ME(funcNameMe, _funcName)                             \
template<class T>                                                                    \
void funcNameMe(XTensor &a, T num)                                                   \
{                                                                                    \
    _funcName(&a, &a, num);                                                          \
}                                                                                    \
template void funcNameMe<int>(XTensor&, int);                                        \
template void funcNameMe<float>(XTensor&, float);                                    \
template void funcNameMe<double>(XTensor&, double);                                                                                    
                                                                                     
#define SIMPLE_BINARY_FUNCTION(funcName, _funcName, operationId)                     \
template<class T>                                                                    \
XTensor funcName(const XTensor &a, T num)                                            \
{                                                                                    \
    XTensor b(&a);                                                                   \
    b.SetTMPFlag();                                                                  \
    _funcName(&a, &b, num);                                                          \
    XLink::MakeLink(&a, NULL, &b, operationId);                                      \
138
    XLink::AddParamToHead(&b, num);                                                  \
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    return b;                                                                        \
}                                                                                    \
template XTensor funcName<int>(const XTensor&, int);                                 \
template XTensor funcName<float>(const XTensor&, float);                             \
template XTensor funcName<double>(const XTensor&, double);                                                                                    
                                                                                     
#define SIMPLE_BINARY_FUNCTION_VOID(funcName, _funcName, operationId)                \
template<class T>                                                                    \
void funcName(const XTensor &a, XTensor &b, T num)                                   \
{                                                                                    \
    if (!b.isInit || !XTensor::IsSameShaped(&a, &b)) {                               \
        InitTensor(&b, &a);                                                          \
    }                                                                                \
    _funcName(&a, &b, num);                                                          \
    if (b.enableGrad) {                                                              \
        XLink::MakeLink(&a, NULL, &b, operationId);                                  \
155
        XLink::AddParamToHead(&b, num);                                              \
156 157 158 159 160 161
    }                                                                                \
}                                                                                    \
template void funcName<int>(const XTensor&, XTensor&, int);                          \
template void funcName<float>(const XTensor&, XTensor&, float);                      \
template void funcName<double>(const XTensor&, XTensor&, double);                                                                           

162
_SIMPLE_BINARY_FUNCTION(_Descale, _CudaDescale, BinaryDescale)
163 164 165 166
_SIMPLE_BINARY_FUNCTION_ME(_DescaleMe, _Descale)
SIMPLE_BINARY_FUNCTION_ME(DescaleMe, _Descale)
SIMPLE_BINARY_FUNCTION(Descale, _Descale, MATH_DESCALE)
SIMPLE_BINARY_FUNCTION_VOID(Descale, _Descale, MATH_DESCALE)
167

168
_SIMPLE_BINARY_FUNCTION(_Mod, _CudaMod, BinaryMod)
169 170 171 172
_SIMPLE_BINARY_FUNCTION_ME(_ModMe, _Mod)
SIMPLE_BINARY_FUNCTION_ME(ModMe, _Mod)
SIMPLE_BINARY_FUNCTION(Mod, _Mod, MATH_MOD)
SIMPLE_BINARY_FUNCTION_VOID(Mod, _Mod, MATH_MOD)
173

174
_SIMPLE_BINARY_FUNCTION(_Power, _CudaPower, BinaryPower)
175 176 177 178
_SIMPLE_BINARY_FUNCTION_ME(_PowerMe, _Power)
SIMPLE_BINARY_FUNCTION_ME(PowerMe, _Power)
SIMPLE_BINARY_FUNCTION(Power, _Power, MATH_POWER)
SIMPLE_BINARY_FUNCTION_VOID(Power, _Power, MATH_POWER)
179

180
_SIMPLE_BINARY_FUNCTION(_Scale, _CudaScale, BinaryScale)
181 182
_SIMPLE_BINARY_FUNCTION_ME(_ScaleMe, _Scale)
SIMPLE_BINARY_FUNCTION_ME(ScaleMe, _Scale)
183 184 185
SIMPLE_BINARY_FUNCTION(Scale, _Scale, MATH_SCALE)
SIMPLE_BINARY_FUNCTION_VOID(Scale, _Scale, MATH_SCALE)

186
_SIMPLE_BINARY_FUNCTION(_Shift, _CudaShift, BinaryShift)
187 188
_SIMPLE_BINARY_FUNCTION_ME(_ShiftMe, _Shift)
SIMPLE_BINARY_FUNCTION_ME(ShiftMe, _Shift)
189 190 191 192
SIMPLE_BINARY_FUNCTION(Shift, _Shift, MATH_SHIFT)
SIMPLE_BINARY_FUNCTION_VOID(Shift, _Shift, MATH_SHIFT)

} // namespace nts(NiuTrans.Tensor)