ReduceSum.cpp 11.2 KB
Newer Older
xiaotong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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.
 */

/*
19 20
 * $Created by: XIAO Tong (email: xiaotong@mail.neu.edu.cn) 2018-04-24
 */
xiaotong committed
21 22 23 24

#include <math.h>
#include "ReduceSum.h"
#include "ReduceSum.cuh"
25
#include "../../XName.h"
xiaotong committed
26 27 28 29

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

/* 
30 31 32
sum the items along a dimension of the tensor

For a 1-dimensional data array a,
xiaotong committed
33 34
sum = \sum_i (a_i - shift)^power if isExp == false
sum = \sum_i exp((a_i - shift)^power) if isExp == true
35

xiaotong committed
36 37 38 39 40 41 42
>> input - the input tensor
>> output - the output tensor
>> dim - the dimension where the reduction is performed on
>> shift - shift the input
>> ieExp - specify if the exp() is performed
>> power - we perform pow(item_i, power) on each item in the array
*/
43
void _ReduceSum(const XTensor * input, XTensor * output, int dim, const XTensor * shift, DTYPE power, bool isExp)
xiaotong committed
44 45
{
    CheckNTErrors((input->devID == output->devID || (input->devID < 0 && output->devID < 0)), 
46
                  "This code must be run on the same device!");
xiaotong committed
47 48 49 50
    CheckNTErrors((input && output), "Empty input or output tensors!");
    CheckNTErrors((input->order == output->order + 1), "Incorrect tensor sizes!");
    CheckNTErrors((input->order > dim && dim >=0), "Illegal dimension to reduce!");
    CheckNTErrors((input->dataType == output->dataType), "Unmatched data types!");
51
    CheckNTErrors((shift == NULL || XTensor::IsSameShaped(output, shift)), "Incorrect shift tensor size!");
xiaotong committed
52 53

	int dimRDI = input->order - dim - 1;
xiaotong committed
54 55
    CheckNTErrors(dimRDI >= 0, "Wrong dimension!");

xiaotong committed
56 57
    for(int i = 0; i < input->order; i++){
        if(i < dimRDI){
58
            CheckNTErrors((input->dimSizeRDI[i] == output->dimSizeRDI[i]), "Unmatched tensors!");
xiaotong committed
59 60
        }
        else if(i > dimRDI){
61
            CheckNTErrors((input->dimSizeRDI[i] == output->dimSizeRDI[i - 1]), "Unmatched tensors!");
xiaotong committed
62 63 64 65 66
        }
    }

    if(input->devID >= 0){
#ifdef USE_CUDA
67
        _CudaReduceSum(input, output, dim, shift, power, isExp);
xiaotong committed
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#endif
    }
    else{
        CheckNTErrors((input->dataType == DEFAULT_DTYPE), "TODO!");

        int stride = 1;
        int strideNum = input->dimSizeRDI[dimRDI];
        int blockSize = 1;
        int blockNum = 1;
        for (int i = 0; i < input->order; i++) {
            if (i < dimRDI)
                stride *= input->dimSizeRDI[i];
            else if (i > dimRDI)
                blockNum *= input->dimSizeRDI[i];
        }
        blockSize = stride * strideNum;

        for(int k = 0; k < blockNum; k++){
            DTYPE * ip = (DTYPE*)input->data + blockSize * k;
            DTYPE * op = (DTYPE*)output->data + stride * k;
            DTYPE * sp = shift != NULL ? (DTYPE*)shift->data + stride * k : NULL;
            for(int i = 0; i < stride; i++){
                DTYPE sum = 0;
                DTYPE bias = shift != NULL ? *(sp + i) : 0;
                DTYPE * ipe = ip + blockSize;
                if(isExp){
                    if(bias == 0){
                        if(power == (DTYPE)1.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride)
                                sum += (DTYPE)exp(*ipb);
                        }
                        else if(power == (DTYPE)2.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb);
                                sum += (DTYPE)exp(value * value);
                            }
                        }
                        else if(power == (DTYPE)0.5){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb);
                                sum += (DTYPE)exp(sqrt(value));
                            }
                        }
                        else{
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb);
                                sum += (DTYPE)exp(pow(value, power));
                            }
                        }
                    }
                    else{
                        if(power == (DTYPE)1.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride)
                                sum += (DTYPE)exp(*ipb - bias);
                        }
                        else if(power == (DTYPE)2.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb) - bias;
                                sum += (DTYPE)exp(value * value);
                            }
                        }
                        else if(power == (DTYPE)0.5){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb) - bias;
                                sum += (DTYPE)exp(sqrt(value));
                            }
                        }
                        else{
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb) - bias;
                                sum += (DTYPE)exp(pow(value, power));
                            }
                        }
                    }
                }
                else{
                    if(bias == 0){
                        if(power == (DTYPE)1.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride)
                                sum += *ipb;
                        }
                        else if(power == (DTYPE)2.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb);
                                sum += value * value;
                            }
                        }
                        else if(power == (DTYPE)0.5){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb);
                                sum += (DTYPE)sqrt(value);
                            }
                        }
                        else{
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb);
                                sum += (DTYPE)pow(value, power);
                            }
                        }
                    }
                    else{
                        if(power == (DTYPE)1.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride)
                                sum += *ipb;
                            sum -= strideNum * bias;
                        }
                        else if(power == (DTYPE)2.0){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb) - bias;
                                sum += value * value;
                            }
                        }
                        else if(power == (DTYPE)0.5){
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb) - bias;
                                sum += (DTYPE)sqrt(value);
                            }
                        }
                        else{
                            for(DTYPE * ipb = ip + i; ipb < ipe; ipb += stride){
                                DTYPE value = (*ipb) - bias;
                                sum += (DTYPE)pow(value, power);
                            }
                        }
                    }
                }
                *(op + i) = sum;
            }
        }
    }
}

200
/* 
xiaotong committed
201
sum the items along a dimension of the tensor (return an XTensor structure)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
make a new tensor to keep the result and return it

For a 1-dimensional data array a,
sum = \sum_i (a_i - shift)^power if isExp == false
sum = \sum_i exp((a_i - shift)^power) if isExp == true

>> input - the input tensor
>> dim - the dimension where the reduction is performed on
>> shift - shift the input
>> ieExp - specify if the exp() is performed
>> power - we perform pow(item_i, power) on each item in the array
<< return - the sum along a dimension of the tensor
*/
XTensor ReduceSum(const XTensor &input, int dim, const XTensor &shift, DTYPE power, bool isExp)
{
217
    CheckNTErrors(dim >= 0 && dim < input.order, "Illegal dimension to reduce!");
218 219 220
	
    int order = input.order - 1;
    int * dimSize = new int[order];
221
    for(int i = 0; i < order; i++){
222 223
        if(i < dim)
            dimSize[i] = input.dimSize[i];
224
        else if(i >= dim)
225 226 227
            dimSize[i] = input.dimSize[i + 1];
    }

228 229
    float dr = (!input.isSparse) ? 1.0F : input.denseRatio;
    XTensor output(order, dimSize, input.dataType, dr, input.devID, input.mem);
230
    output.SetTMPFlag();
231 232 233 234 235 236

    /* call _ReduceSum function */
    _ReduceSum(&input, &output, dim, &shift, power, isExp);
            
    /* tensor connection */
    XLink::MakeLink(&input, &shift, &output, REDUCE_REDUCESUM);
237
    XLink::AddParamToHeadInt(&output, dim);
238
    XLink::AddParamToHead(&output, power);
239 240 241 242 243 244 245 246 247
    XLink::AddParamToHeadBool(&output, isExp);

    /* destroy variables */
    delete[] dimSize;

    return output;
}

/* 
xiaotong committed
248
sum the items along a dimension of the tensor (return an XTensor structure)
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
make a new tensor to keep the result and return it

For a 1-dimensional data array a,
sum = \sum_i (a_i)^power if isExp == false
sum = \sum_i exp((a_i)^power) if isExp == true

>> input - the input tensor
>> dim - the dimension where the reduction is performed on
>> ieExp - specify if the exp() is performed
>> power - we perform pow(item_i, power) on each item in the array
<< return - the sum along a dimension of the tensor
*/
XTensor ReduceSum(const XTensor &input, int dim, DTYPE power, bool isExp)
{
    CheckNTErrors(dim >= 0 && dim < input.order, "Illegal dimension to reduce!");
	
    int order = input.order - 1;
    int * dimSize = new int[order];
    for(int i = 0; i < order; i++){
        if(i < dim)
            dimSize[i] = input.dimSize[i];
        else if(i >= dim)
            dimSize[i] = input.dimSize[i + 1];
    }

    float dr = (!input.isSparse) ? 1.0F : input.denseRatio;
    XTensor output(order, dimSize, input.dataType, dr, input.devID, input.mem);
276
    output.SetTMPFlag();
277 278 279 280 281 282 283 284 285

    /* call _ReduceSum function */
    _ReduceSum(&input, &output, dim, NULL, power, isExp);
            
    /* tensor connection */
    XLink::MakeLink(&input, NULL, &output, REDUCE_REDUCESUM);
    XLink::AddParamToHeadInt(&output, dim);
    XLink::AddParamToHead(&output, power);
    XLink::AddParamToHeadBool(&output, isExp);
286 287

    /* destroy variables */
xiaotong committed
288
    delete[] dimSize;
289 290 291 292

    return output;
}

xiaotong committed
293
} // namespace nts(NiuTrans.Tensor)