Div.cpp 8.11 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
/* 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: Xu Chen (email: hello_master1954@163.com) 2018-08-01
 */

#include "../../XTensor.h"
#include "../../XName.h"
24
#include "../../XUtility.h"
25 26
#include "Div.h"
#include "Div.cuh"
xuchen committed
27
#include "DivDim.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

namespace nts { // namespace nts(NiuTrans.Tensor)

/*
element-wise division of two tensors

c(i) = a(i)/b(i) + \alpha * c(i)
where i is the index of the item

>> a - tensor a
>> b - tensor b
>> c - result tensor
>> alpha - the coefficient
>> leadingDim - the dimension along which we perform broadcasting
*/
void _Div(const XTensor * a, const XTensor * b, XTensor * c, DTYPE alpha, int leadingDim)
{
    CheckNTErrors((a->unitNum <= c->unitNum && b->unitNum <= c->unitNum),
                  "Unmatched tensors in multiplication!");
    CheckNTErrors((a->order == b->order && a->order == c->order), 
                  "Unmatched tensors!");

50 51 52 53
    CheckDev(a->devID, b->devID);

    int leadingDimRDI = a->order - leadingDim - 1;

54 55 56 57 58 59 60 61 62 63 64 65 66 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 138 139 140 141 142 143 144
#ifdef USE_CUDA
    if (a->devID >= 0 || b->devID >= 0 || c->devID >= 0) {
        _CudaDiv(a, b, c, alpha, leadingDim);
        return;
    }
#endif

    int stride = 1;
    int blockSizeA = 1;
    int blockSizeB = 1;
    int blockSizeC = 1;
    int blockNum = 1;
    int dimensionSizeA = a->dimSizeRDI[leadingDimRDI];
    int dimensionSizeB = b->dimSizeRDI[leadingDimRDI];
    int dimensionSizeC = c->dimSizeRDI[leadingDimRDI];

    for (int i = 0; i < a->order; i++) {
        if (i != leadingDimRDI) {
            CheckNTErrors((a->dimSizeRDI[i] == b->dimSizeRDI[i] && a->dimSizeRDI[i] == c->dimSizeRDI[i]),
                          "Unmatched tensors!");
        }
        if (i < leadingDimRDI)
            stride *= a->dimSizeRDI[i];
    }

    blockSizeA = stride * dimensionSizeA;
    blockSizeB = stride * dimensionSizeB;
    blockSizeC = stride * dimensionSizeC;
    blockNum = a->unitNum / blockSizeA;

    if (!a->isSparse && !b->isSparse) {
        if (a->dataType == DEFAULT_DTYPE && b->dataType == DEFAULT_DTYPE) {
            if (a->unitNum == c->unitNum && b->unitNum == c->unitNum) {
                int size = a->unitNum;
                DTYPE * ap = (DTYPE*)a->data;
                DTYPE * bp = (DTYPE*)b->data;
                DTYPE * cp = (DTYPE*)c->data;
                if (alpha == 0) {
                    for (int i = 0; i < size; i++)
                        cp[i] = ap[i] / bp[i];
                }
                else {
                    for (int i = 0; i < size; i++)
                        cp[i] = ap[i] / bp[i] + alpha * cp[i];
                }
            }
            else {
                for (int k = 0; k < blockNum; k++) {

                    for (int ci = 0, ai = 0, bi = 0; ci < dimensionSizeC; ci++, ai++, bi++) {
                        if (ai >= dimensionSizeA)
                            ai = 0;
                        if (bi >= dimensionSizeB)
                            bi = 0;
                        DTYPE * ap = (DTYPE*)a->data + k * blockSizeA + ai * stride;
                        DTYPE * bp = (DTYPE*)b->data + k * blockSizeB + bi * stride;
                        DTYPE * cp = (DTYPE*)c->data + k * blockSizeC + ci * stride;
                        for (int j = 0; j < stride; j++)
                            cp[j] = ap[j] / bp[j] + cp[j] * alpha;
                    }
                }
            }
        }
        else {
            // TODO!!
            ShowNTErrors("TODO!");
        }
    }
    else {
        // TODO!!
        ShowNTErrors("TODO!");
    }
}

/*
element-wise division of two tensors (do it on site)
keep the result in the input tensor a and return nothing

a(i) = a(i)*b(i) + \alpha * a(i)
where i is the index of the item

>> a - tensor a (where keep the result)
>> b - tensor b
>> alpha - the coefficient
>> leadingDim - the dimension along which we perform broadcasting
*/
void _DivMe(XTensor * a, const XTensor * b, DTYPE alpha, int leadingDim)
{
    _Div(a, b, a, alpha, leadingDim);
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/*
element-wise division of two tensors (do it on site)
keep the result in the input tensor a and return nothing

a(i) = a(i)*b(i) + \alpha * a(i)
where i is the index of the item

>> a - tensor a (where keep the result)
>> b - tensor b
>> alpha - the coefficient
>> leadingDim - the dimension along which we perform broadcasting
*/
void DivMe(XTensor& a, const XTensor& b, DTYPE alpha, int leadingDim)
{
    _Div(&a, &b, &a, alpha, leadingDim);
}

xuchen committed
162 163 164 165 166 167 168 169 170
/* 
return a dimension if the division is performed as DivDim (in more details in DivDim.h)
>> a - a tensor
>> b - another tensor for division
*/
int GetDivDimIndex(const XTensor &a, const XTensor &b)
{
    if(a.order < b.order)
        return -1;
xiaotong committed
171 172
    if(XTensor::IsSameShaped(&a, &b))
        return -1;
xuchen committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    int hitCount = 0;
    int hitDim = -1;
    for(int i = 0; i < b.order; i++){
        if(b.dimSize[b.order - 1 - i] == 1)
            continue;
        else if(b.dimSize[b.order - 1 - i] == a.dimSize[a.order - 1 - i]){
            hitCount++;
            hitDim = a.order - b.order + i;
        }
    }

    if(hitCount == 1)
        return hitDim;
    else
        return -1;
}

191
/*
xiaotong committed
192
element-wise division of two tensors (return an XTensor structure)
193 194 195 196 197 198 199
make a new tensor c to keep the result and return it

c(i) = a(i)*b(i)
where i is the index of the item

>> a - tensor a
>> b - tensor b
xuchen committed
200
>> alpha - the coefficient
201 202 203
>> leadingDim - the dimension along which we perform broadcasting
<< return - the product of the tensors
*/
xuchen committed
204
XTensor Div(const XTensor &a, const XTensor &b, DTYPE alpha, int leadingDim)
205 206
{
    XTensor c(&a);
xiaotong committed
207
    c.SetTMPFlag();
xuchen committed
208 209 210 211 212 213 214 215

    int n = GetDivDimIndex(a, b);

    if(n == -1){
        CheckNTErrors(a.dimSize[leadingDim] == b.dimSize[leadingDim], "TODO!");

        /* call _Div function */
        _Div(&a, &b, &c, alpha, leadingDim);
216
    
xuchen committed
217 218 219 220 221 222 223 224 225 226 227 228
        /* tensor connections */
        XLink::MakeLink(&a, &b, &c, MATH_DIV);
        XLink::AddParamToHead(&c, alpha);
        XLink::AddParamToHeadInt(&c, leadingDim);
    }
    else if(n >= 0 && n < a.order){
        /* call _DivDim function */
        _DivDim(&a, &b, &c, n, alpha);

        /* tensor connections */
        XLink::MakeLink(&a, &b, &c, MATH_DIVDIM);
        XLink::AddParamToHeadInt(&c, n);
229
        XLink::AddParamToHead(&c, alpha);
xuchen committed
230 231 232 233 234
    }
    else{
        ShowNTErrors("Something is wrong!");
    }

235 236 237
    return c;
}

238 239 240 241 242 243 244 245 246 247 248 249
/*
element-wise division of two tensors

c(i) = a(i)/b(i) + \alpha * c(i)
where i is the index of the item

>> a - tensor a
>> b - tensor b
>> c - result tensor
>> alpha - the coefficient
>> leadingDim - the dimension along which we perform broadcasting
*/
250
void Div(const XTensor &a, const XTensor &b, XTensor &c, DTYPE alpha, int leadingDim)
251 252 253 254 255 256 257 258 259 260 261 262 263
{
    if (!c.isInit || !XTensor::IsSameShaped(&a, &c)) {
        InitTensor(&c, &a);
    }

    int n = GetDivDimIndex(a, b);

    if (n == -1) {
        CheckNTErrors(a.dimSize[leadingDim] == b.dimSize[leadingDim], "TODO!");

        /* call _Div function */
        _Div(&a, &b, &c, 0, leadingDim);

264
        if (c.enableGrad) {
265 266 267 268 269 270 271 272 273 274
            /* tensor connections */
            XLink::MakeLink(&a, &b, &c, MATH_DIV);
            XLink::AddParamToHead(&c, alpha);
            XLink::AddParamToHeadInt(&c, leadingDim);
        }
    }
    else if (n >= 0 && n < a.order) {
        /* call _DivDim function */
        _DivDim(&a, &b, &c, n, alpha);

275
        if (c.enableGrad) {
276 277 278 279 280 281 282 283 284 285 286 287
            /* tensor connections */
            XLink::MakeLink(&a, &b, &c, MATH_DIVDIM);
            XLink::AddParamToHeadInt(&c, n);
            XLink::AddParamToHead(&c, alpha);
        }
    }
    else {
        ShowNTErrors("Something is wrong!");
    }

}

288
} // namespace nts(NiuTrans.Tensor)