ReduceSumAll.cpp 2.8 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* 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-09-27
 */

#include "ReduceSumAll.h"
#include "ReduceSum.h"
#include "../movement/CopyValues.h"

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

int * getDimSize(const XTensor * tensor, int n)
{
    int order = tensor->order;
    int * dimSize = new int[order - 1];

    for (int i = 0; i < order; i++) {
        if(i < n)
            dimSize[i] = tensor->dimSize[i];
        else if(i > n)
            dimSize[i - 1] = tensor->dimSize[i];
    }
    return dimSize;
}

/*
sum all the items of the tensor (It should be optimized!)
>> source - the inpute tensor
<< return - the total summation
*/
47
DTYPE _ReduceSumAll(const XTensor * source)
48
{
xiaotong committed
49 50
    int dims[2] = {1, source->unitNum};
    int one = 1;
51

xiaotong committed
52 53
    XTensor * all = NewTensorBuf(2, dims, source->dataType, source->denseRatio, source->devID, source->mem);
    XTensor * result = NewTensorBuf(1, &one, source->dataType, 1.0F, source->devID, source->mem);
54
    
xiaotong committed
55 56
    _CopyValues(source, all);
    _ReduceSum(all, result, 1);
57
    
xiaotong committed
58
    DTYPE r = result->Get1D(0);
59
    
xiaotong committed
60 61
    DelTensorBuf(result);
    DelTensorBuf(all);
62
    
xiaotong committed
63 64
    return r;

65 66 67 68 69
    int order = source->order;
    DTYPE summation;

    XTensor * big = NewTensor(source);
    _CopyValues(source, big);
70 71 72
    for(int i = order - 1; i >= 0; i--) {
        if(i == 0)
            big->Reshape(1, big->unitNum);
73

74
        int leadingDim = big->order - 1;
75
        int * dimSize;
76 77 78
        dimSize = getDimSize(big, leadingDim);
        XTensor * little = NewTensor(big->order - 1, dimSize, source->dataType, source->denseRatio, 
                                     source->devID, source->mem);
79
        
80
        _ReduceSum(big, little, leadingDim);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        delete big;
        delete dimSize;

        big = NewTensor(little);
        _CopyValues(little, big);

        delete little;
    }
    summation = big->Get1D(0);
    delete big;

    return summation;
}

/*
sum all the items of the tensor
>> source - the inpute tensor
<< return - the total summation   
*/
101
DTYPE ReduceSumAll(const XTensor & source)
102 103 104 105 106
{
    return _ReduceSumAll(&source);
}

} // namespace nts(NiuTrans.Tensor)