TSum.cpp 14.9 KB
Newer Older
huchi committed
1
/* NiuTrans.Tensor - an open-source tensor library
hello committed
2
* Copyright (C) 2017, Natural Language Processing Lab, Northeastern University.
huchi committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
* 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: LI Yinqiao (li.yin.qiao.2012@hotmail.com) 2018-04-30
 */

22
#include "../core/utilities/CheckData.h"
huchi committed
23 24 25 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
#include "TSum.h"

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

/* case 1: tensor summation c = a + b * \beta */
bool TestSum1()
{
    /* a tensor of size (2, 4) */
    int order = 2;
    int * dimSize = new int[order];
    dimSize[0] = 2;
    dimSize[1] = 4;

    int unitNum = 1;
    for (int i = 0; i < order; i++)
        unitNum *= dimSize[i];

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

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
51 52 53 54
    XTensor * a = NewTensorV2(order, dimSize);
    XTensor * b = NewTensorV2(order, dimSize);
    XTensor * c = NewTensorV2(order, dimSize);
    XTensor * cMe = NewTensorV2(order, dimSize);
huchi committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    XTensor cUser;

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

    /* call Sum function */
    _Sum(a, b, c);
    _SumMe(cMe, b);
    cUser = Sum(*a, *b);

    /* check results */
hello committed
69 70 71
    cpuTest = _CheckData(c, answer, unitNum, 1e-4F) &&
              _CheckData(cMe, answer, unitNum, 1e-4F) &&
              _CheckData(&cUser, answer, unitNum, 1e-4F);
huchi committed
72 73 74 75 76 77

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

    /* create tensor */
78 79 80 81
    XTensor * aGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * cGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * cMeGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
huchi committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
    XTensor cUserGPU;

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

    /* call Sum function */
    _Sum(aGPU, bGPU, cGPU);
    _SumMe(cMeGPU, bGPU);
    cUserGPU = Sum(*aGPU, *bGPU);

    /* check results */
hello committed
96 97 98
    gpuTest = _CheckData(cGPU, answer, unitNum, 1e-4F) &&
              _CheckData(cMeGPU, answer, unitNum, 1e-4F) &&
              _CheckData(&cUserGPU, answer, unitNum, 1e-4F);
huchi committed
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

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

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

    return cpuTest;
#endif // USE_CUDA
}

/* case 2: tensor summation c = a + b * \beta */
bool TestSum2()
{
    /* a tensor of size (2, 4) */
    int order = 2;
    int * dimSize = new int[order];
    dimSize[0] = 2;
    dimSize[1] = 4;

    int unitNum = 1;
    for (int i = 0; i < order; i++) {
        unitNum *= dimSize[i];
    }
    DTYPE aData[2][4] = { {0.0F, 1.0F, 2.0F, 3.0F},
                          {4.0F, 5.0F, 6.0F, 7.0F} };
    DTYPE bData[2][4] = { {1.0F, -1.0F, -3.0F, -5.0F}, 
                          {-7.0F, -9.0F, -11.0F, -13.0F} };
    DTYPE answer[2][4] = { {0.5F, 0.5F, 0.5F, 0.5F},
                           {0.5F, 0.5F, 0.5F, 0.5F} };
    float beta = 0.5F;

    /* CPU test */
    bool cpuTest = true;

    /* create tensor */
149 150 151 152
    XTensor * a = NewTensorV2(order, dimSize);
    XTensor * b = NewTensorV2(order, dimSize);
    XTensor * c = NewTensorV2(order, dimSize);
    XTensor * cMe = NewTensorV2(order, dimSize);
huchi committed
153 154 155 156 157 158 159 160 161 162 163
    XTensor cUser;

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

    /* call Sum function */
    _Sum(a, b, c, beta);
    _SumMe(cMe, b, beta);
hello committed
164
    cUser = Sum(*a, *b, false, beta);
huchi committed
165 166

    /* check results */
hello committed
167 168 169
    cpuTest = _CheckData(c, answer, unitNum, 1e-4F) &&
              _CheckData(cMe, answer, unitNum, 1e-4F) &&
              _CheckData(&cUser, answer, unitNum, 1e-4F);
huchi committed
170 171 172 173 174 175

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

    /* create tensor */
176 177 178 179
    XTensor * aGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * cGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * cMeGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
huchi committed
180 181 182 183 184 185 186 187 188 189 190
    XTensor cUserGPU;

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

    /* call Sum function */
    _Sum(aGPU, bGPU, cGPU, beta);
    _SumMe(cMeGPU, bGPU, beta);
hello committed
191
    cUserGPU = Sum(*aGPU, *bGPU, false, beta);
huchi committed
192 193

    /* check results */
hello committed
194 195 196
    gpuTest = _CheckData(cGPU, answer, unitNum, 1e-4F) &&
              _CheckData(cMeGPU, answer, unitNum, 1e-4F) &&
              _CheckData(&cUserGPU, answer, unitNum, 1e-4F);
huchi committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

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

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

    return cpuTest;
#endif // USE_CUDA
}

hello committed
222 223 224 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/* case 3: tensor summation c = a + b * \beta, which b is a scalar tensor */
bool TestSum3()
{
    /* 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 scalar */
    int bOrder = 0;
    int * bDimSize = new int[MAX_TENSOR_DIM_NUM];
    int bUnitNum = 1;


    /* a tensor of size (2, 4) */
    int cOrder = 2;
    int * cDimSize = new int[cOrder];
    cDimSize[0] = 2;
    cDimSize[1] = 4;

    int cUnitNum = 1;
    for (int i = 0; i < cOrder; i++)
        cUnitNum *= cDimSize[i];

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

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * a = NewTensorV2(aOrder, aDimSize);
    XTensor * b = NewTensorV2(bOrder, bDimSize);
    XTensor cUser;

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

    /* call Sum function */
hello committed
271
    cUser = Sum(*a, *b, false, beta);
hello committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    /* check results */
    cpuTest = _CheckData(&cUser, answer, cUnitNum, 1e-4F);

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

    /* create tensor */
    XTensor * aGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(bOrder, bDimSize, X_FLOAT, 1.0F, 0);
    XTensor cUserGPU;

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

    /* call Sum function */
hello committed
290
    cUserGPU = Sum(*aGPU, *bGPU, false, beta);
hello committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

    /* check results */
    gpuTest = _CheckData(&cUserGPU, answer, cUnitNum, 1e-4F);

    /* destroy variables */
    delete a;
    delete b;
    delete aGPU;
    delete bGPU;
    delete[] aDimSize;
    delete[] bDimSize;
    delete[] cDimSize;

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

    return cpuTest;
#endif // USE_CUDA
}

/* case 4: tensor summation c = a + b * \beta, which b is a 1d tensor */
bool TestSum4()
{
    /* a tensor of size (3, 4, 2) */
    int aOrder = 3;
    int * aDimSize = new int[aOrder];
    aDimSize[0] = 3;
    aDimSize[1] = 4;
    aDimSize[2] = 2;

    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];

    /* a tensor of size (3, 4, 2) */
    int cOrder = 3;
    int * cDimSize = new int[cOrder];
    cDimSize[0] = 3;
    cDimSize[1] = 4;
    cDimSize[2] = 2;

    int cUnitNum = 1;
    for (int i = 0; i < cOrder; i++)
        cUnitNum *= cDimSize[i];

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

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * a = NewTensorV2(aOrder, aDimSize);
    XTensor * b = NewTensorV2(bOrder, bDimSize);
    XTensor cUser;

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

    /* call Sum function */
hello committed
373
    cUser = Sum(*a, *b, false, beta);
hello committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

    /* check results */
    cpuTest = _CheckData(&cUser, answer, cUnitNum, 1e-4F);

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

    /* create tensor */
    XTensor * aGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(bOrder, bDimSize, X_FLOAT, 1.0F, 0);
    XTensor cUserGPU;

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

    /* call Sum function */
hello committed
392
    cUserGPU = Sum(*aGPU, *bGPU, false, beta);
hello committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

    /* check results */
    gpuTest = _CheckData(&cUserGPU, answer, cUnitNum, 1e-4F);

    /* destroy variables */
    delete a;
    delete b;
    delete aGPU;
    delete bGPU;
    delete[] aDimSize;
    delete[] bDimSize;
    delete[] cDimSize;

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

    return cpuTest;
#endif // USE_CUDA
}

/* case 5: tensor summation c = a + b * \beta, which b is a 1d tensor */
bool TestSum5()
{
    /* a tensor of size (4, 4) */
    int aOrder = 2;
    int * aDimSize = new int[aOrder];
    aDimSize[0] = 4;
    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];

    /* a tensor of size (4, 4) */
    int cOrder = 2;
    int * cDimSize = new int[cOrder];
    cDimSize[0] = 4;
    cDimSize[1] = 4;

    int cUnitNum = 1;
    for (int i = 0; i < cOrder; i++)
        cUnitNum *= cDimSize[i];

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

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * a = NewTensorV2(aOrder, aDimSize);
    XTensor * b = NewTensorV2(bOrder, bDimSize);
    XTensor cUser;

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

    /* call Sum function */
hello committed
475
    cUser = Sum(*a, *b, false, beta);
hello committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

    /* check results */
    cpuTest = _CheckData(&cUser, answer, cUnitNum, 1e-4F);

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

    /* create tensor */
    XTensor * aGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(bOrder, bDimSize, X_FLOAT, 1.0F, 0);
    XTensor cUserGPU;

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

    /* call Sum function */
hello committed
494
    cUserGPU = Sum(*aGPU, *bGPU, false, beta);
hello committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

    /* check results */
    gpuTest = _CheckData(&cUserGPU, answer, cUnitNum, 1e-4F);

    /* destroy variables */
    delete a;
    delete b;
    delete aGPU;
    delete bGPU;
    delete[] aDimSize;
    delete[] bDimSize;
    delete[] cDimSize;

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

    return cpuTest;
#endif // USE_CUDA
}

huchi committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
/* other cases */
/*
    TODO!!
*/

/* test for Sum Function */
bool TestSum()
{
    XPRINT(0, stdout, "[TEST SUM] tensor summation c = a + b * beta\n");
    bool returnFlag = true, caseFlag = true;

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

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

hello committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    /* case 3 test */
    caseFlag = TestSum3();
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 3 failed!\n");
    }
    else
    XPRINT(0, stdout, ">> case 3 passed!\n");

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

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

huchi committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    /* other cases test */
    /*
        TODO!!
    */

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

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

    return returnFlag;
}

} // namespace nts(NiuTrans.Tensor)