TMultiplyDim.cpp 7.77 KB
Newer Older
1
/* NiuTrans.Tensor - an open-source tensor library
liyinqiao committed
2
 * Copyright (C) 2017, Natural Language Processing Lab, Northeastern University.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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-07-30
 */

#include "../core/arithmetic/MultiplyDim.h"
liyinqiao committed
23
#include "../core/utilities/CheckData.h"
24
#include "../XTensor.h"
liyinqiao committed
25
#include "TMultiplyDim.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

namespace nts { // namespace nts(NiuTrans.Tensor)
/*
case 1: tensor multiplication c = a * b + \alpha * c
where the size of b is equal to the n-th dimension of a,
i.e., a is multiplied with b by broadcasting
In this case, (2, 4) * (2) = (2, 4), n = 0.
*/
bool TestMultiplyDim1()
{
    /* a tensor of size (2, 4) */
    int aOrder = 2;
    int * aDimSize = new int[aOrder];
    aDimSize[0] = 2;
    aDimSize[1] = 4;

    int aUnitNum = 1;
    for (int i = 0; i < aOrder; i++)
        aUnitNum *= aDimSize[i];

    /* a tensor of size (2) */
    int bOrder = 1;
    int * bDimSize = new int[bOrder];
    bDimSize[0] = 2;

    int bUnitNum = 1;
    for (int i = 0; i < bOrder; i++)
        bUnitNum *= bDimSize[i];

    DTYPE aData[2][4] = { {0.0F, 1.0F, 2.0F, 3.0F},
                          {4.0F, 5.0F, 6.0F, 7.0F} };
    DTYPE bData[2] = {1.0F, -1.0F};
    DTYPE answer[2][4] = { {0.0F, 1.0F, 2.0F, 3.0F},
                           {-4.0F, -5.0F, -6.0F, -7.0F} };

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
liyinqiao committed
65 66 67 68
    XTensor * a = NewTensorV2(aOrder, aDimSize);
    XTensor * b = NewTensorV2(bOrder, bDimSize);
    XTensor * c = NewTensorV2(aOrder, aDimSize);
    XTensor * cMe = NewTensorV2(aOrder, aDimSize);
69 70 71 72 73 74 75 76 77 78 79 80 81 82
    XTensor cUser;

    /* initialize variables */
    a->SetData(aData, aUnitNum);
    cMe->SetData(aData, aUnitNum);
    b->SetData(bData, bUnitNum);
    c->SetZeroAll();

    /* call MultiplyDim function */
    _MultiplyDim(a, b, c, 0);
    _MultiplyDimMe(cMe, b, 0);
    cUser = MultiplyDim(*a, *b, 0);

    /* check results */
liyinqiao committed
83 84 85
    cpuTest = _CheckData(c, answer, aUnitNum, 1e-4F) &&
              _CheckData(cMe, answer, aUnitNum, 1e-4F) &&
              _CheckData(&cUser, answer, aUnitNum, 1e-4F);
86 87 88 89 90 91

#ifdef USE_CUDA
    /* GPU test */
    bool gpuTest = true;

    /* create tensor */
liyinqiao committed
92 93 94 95
    XTensor * aGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(bOrder, bDimSize, X_FLOAT, 1.0F, 0);
    XTensor * cGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * cMeGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    XTensor cUserGPU;

    /* Initialize variables */
    aGPU->SetData(aData, aUnitNum);
    cMeGPU->SetData(aData, aUnitNum);
    bGPU->SetData(bData, bUnitNum);
    cGPU->SetZeroAll();

    /* call MultiplyDim function */
    _MultiplyDim(aGPU, bGPU, cGPU, 0);
    _MultiplyDimMe(cMeGPU, bGPU, 0);
    cUserGPU = MultiplyDim(*aGPU, *bGPU, 0);

    /* check results */
liyinqiao committed
110 111 112
    gpuTest = _CheckData(cGPU, answer, aUnitNum, 1e-4F) &&
              _CheckData(cMeGPU, answer, aUnitNum, 1e-4F) &&
              _CheckData(&cUserGPU, answer, aUnitNum, 1e-4F);
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

    /* destroy variables */
    delete a;
    delete b;
    delete c;
    delete cMe;
    delete aGPU;
    delete bGPU;
    delete cGPU;
    delete cMeGPU;
    delete[] aDimSize;
    delete[] bDimSize;

    return cpuTest && gpuTest;
#else
    /* destroy variables */
    delete a;
    delete b;
    delete c;
    delete cMe;
    delete[] aDimSize;
    delete[] bDimSize;

    return cpuTest;
#endif // USE_CUDA
}

/* 
case 2: tensor multiplication c = a*b + \alpha * c
where the size of b is equal to the n-th dimension of a, 
i.e., a is multiplied with b by broadcasting.
In this case, (2, 4) * (4) = (2, 4), n = 1.
*/
bool TestMultiplyDim2()
{
    /* a tensor of size (2, 4) */
    int aOrder = 2;
    int * aDimSize = new int[aOrder];
    aDimSize[0] = 2;
    aDimSize[1] = 4;

    int aUnitNum = 1;
    for (int i = 0; i < aOrder; i++)
        aUnitNum *= aDimSize[i];

    /* a tensor of size (4) */
    int bOrder = 1;
    int * bDimSize = new int[bOrder];
    bDimSize[0] = 4;

    int bUnitNum = 1;
    for (int i = 0; i < bOrder; i++)
        bUnitNum *= bDimSize[i];

    DTYPE aData[2][4] = { {0.0F, 1.0F, 2.0F, 3.0F},
                          {4.0F, 5.0F, 6.0F, 7.0F} };
    DTYPE bData[4] = {1.0F, -1.0F , 1.0F, -1.0F};
    DTYPE answer[2][4] = { {0.0F, -1.0F, 2.0F, -3.0F},
                           {4.0F, -5.0F, 6.0F, -7.0F} };

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
liyinqiao committed
177 178 179 180
    XTensor * a = NewTensorV2(aOrder, aDimSize);
    XTensor * b = NewTensorV2(bOrder, bDimSize);
    XTensor * c = NewTensorV2(aOrder, aDimSize);
    XTensor * cMe = NewTensorV2(aOrder, aDimSize);
181 182 183 184 185 186 187 188 189 190 191 192 193 194
    XTensor cUser;

    /* initialize variables */
    a->SetData(aData, aUnitNum);
    cMe->SetData(aData, aUnitNum);
    b->SetData(bData, bUnitNum);
    c->SetZeroAll();

    /* call MultiplyDim function */
    _MultiplyDim(a, b, c, 1);
    _MultiplyDimMe(cMe, b, 1);
    cUser = MultiplyDim(*a, *b, 1);

    /* check results */
liyinqiao committed
195 196 197
    cpuTest = _CheckData(c, answer, aUnitNum, 1e-4F) &&
              _CheckData(cMe, answer, aUnitNum, 1e-4F) &&
              _CheckData(&cUser, answer, aUnitNum, 1e-4F);
198 199 200 201 202 203

#ifdef USE_CUDA
    /* GPU test */
    bool gpuTest = true;

    /* create tensor */
liyinqiao committed
204 205 206 207
    XTensor * aGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(bOrder, bDimSize, X_FLOAT, 1.0F, 0);
    XTensor * cGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * cMeGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
208 209 210 211 212 213 214 215 216 217 218 219 220 221
    XTensor cUserGPU;

    /* Initialize variables */
    aGPU->SetData(aData, aUnitNum);
    cMeGPU->SetData(aData, aUnitNum);
    bGPU->SetData(bData, bUnitNum);
    cGPU->SetZeroAll();

    /* call MultiplyDim function */
    _MultiplyDim(aGPU, bGPU, cGPU, 1);
    _MultiplyDimMe(cMeGPU, bGPU, 1);
    cUserGPU = MultiplyDim(*aGPU, *bGPU, 1);

    /* check results */
liyinqiao committed
222 223 224
    gpuTest = _CheckData(cGPU, answer, aUnitNum, 1e-4F) &&
              _CheckData(cMeGPU, answer, aUnitNum, 1e-4F) &&
              _CheckData(&cUserGPU, answer, aUnitNum, 1e-4F);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    /* destroy variables */
    delete a;
    delete b;
    delete c;
    delete cMe;
    delete aGPU;
    delete bGPU;
    delete cGPU;
    delete cMeGPU;
    delete[] aDimSize;
    delete[] bDimSize;

    return cpuTest && gpuTest;
#else
    /* destroy variables */
    delete a;
    delete b;
    delete c;
    delete cMe;
    delete[] aDimSize;
    delete[] bDimSize;

    return cpuTest;
#endif // USE_CUDA
}

/* test for MultiplyDim Function */
bool TestMultiplyDim()
{
liyinqiao committed
255
    XPRINT(0, stdout, "[TEST MULTIPLYDIM] tensor multiplication c = a * b + \\alpha * c by broadcasting\n");
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    bool returnFlag = true, caseFlag = true;

    /* case 1 test */
    caseFlag = TestMultiplyDim1(); 
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 1 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 1 passed!\n");

    /* case 2 test */
    caseFlag = TestMultiplyDim2();
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 2 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 2 passed!\n");

    /* other cases test */
    /*
    TODO!!
    */

    if (returnFlag) {
        XPRINT(0, stdout, ">> All Passed!\n");
    }
    else
        XPRINT(0, stdout, ">> Failed!\n");

    XPRINT(0, stdout, "\n");

    return returnFlag;
}



}