TSplit.cpp 10.5 KB
Newer Older
xiaotong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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: Lin Ye (email: linye2015@outlook.com) 2018-06-13
*/

张裕浩 committed
22
#include "../core/utilities/CheckData.h"
liyinqiao committed
23
#include "TSplit.h"
xiaotong committed
24 25

namespace nts { // namespace nt(NiuTrans.Tensor)
liyinqiao committed
26 27 28 29

/* 
case 1: transform a tensor by splitting it, e.g., (N, M) -> (N/3, M, 3)
In this case, (4, 3) -> (2, 2, 3), whereToSplit=0, splitNum=2.
xiaotong committed
30 31 32
*/
bool TestSplit1()
{
liyinqiao committed
33
    /* a source tensor of size (4, 3) */
xiaotong committed
34 35 36 37 38 39 40 41 42
    int sOrder = 2;
    int * sDimSize = new int[sOrder];
    sDimSize[0] = 4;
    sDimSize[1] = 3;

    int sUnitNum = 1;
    for (int i = 0; i < sOrder; i++)
        sUnitNum *= sDimSize[i];

liyinqiao committed
43
    /* a target tensor of size (2, 2, 3) */
xiaotong committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    int tOrder = 3;
    int * tDimSize = new int[tOrder];
    tDimSize[0] = 2;
    tDimSize[1] = 2;
    tDimSize[2] = 3;

    int tUnitNum = 1;
    for (int i = 0; i < tOrder; i++)
        tUnitNum *= tDimSize[i];

    DTYPE sData[4][3] = { {0.0F, 1.0F, 2.0F},
                          {3.0F, 4.0F, 5.0F},
                          {0.1F, 1.1F, 2.1F},
                          {3.1F, 4.1F, 5.1F} };
张裕浩 committed
58 59 60 61
    DTYPE answer[2][2][3] = { { {0.0F, 1.0F, 2.0F},
                                {3.0F, 4.0F, 5.0F} },
                              { {0.1F, 1.1F, 2.1F},
                                {3.1F, 4.1F, 5.1F} } };
xiaotong committed
62 63 64 65 66 67 68
  
    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * s = NewTensor(sOrder, sDimSize);
    XTensor * t = NewTensor(tOrder, tDimSize);
69
    XTensor tUser;
xiaotong committed
70 71 72 73 74 75

    /* initialize variables */
    s->SetData(sData, sUnitNum);
    t->SetZeroAll();

    /* call split function */
76
    _Split(s, t, 0, 2);
77
    tUser = Split(*s, 0, 2);
xiaotong committed
78 79

    /* check results */
张裕浩 committed
80
    cpuTest = _CheckData(t, answer, tUnitNum) && _CheckData(&tUser, answer, tUnitNum);
xiaotong committed
81 82 83 84 85 86 87

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

    /* create tensor */
    XTensor * sGPU = NewTensor(sOrder, sDimSize, X_FLOAT, 1.0F, 0);
张裕浩 committed
88
    XTensor * tGPU = NewTensor(tOrder, tDimSize, X_FLOAT, 1.0F, 0);
89
    XTensor tUserGPU;
xiaotong committed
90 91

    /* Initialize variables */
张裕浩 committed
92 93
    sGPU->SetData(sData, sUnitNum);
    tGPU->SetZeroAll();
xiaotong committed
94 95

    /* call sum function */
张裕浩 committed
96
    _Split(sGPU, tGPU, 0, 2);
97
    tUserGPU = Split(*sGPU, 0, 2);
xiaotong committed
98 99

    /* check results */
张裕浩 committed
100
    gpuTest = _CheckData(tGPU, answer, tUnitNum) && _CheckData(&tUserGPU, answer, tUnitNum);
xiaotong committed
101 102

    /* destroy variables */
张裕浩 committed
103
    delete s;
104 105 106
    delete t;
    delete sGPU;
    delete tGPU;
张裕浩 committed
107
    delete[] sDimSize;
108
    delete[] tDimSize;
xiaotong committed
109

张裕浩 committed
110
    return cpuTest && gpuTest;
xiaotong committed
111 112
#else
    /* destroy variables */
张裕浩 committed
113
    delete s;
114
    delete t;
张裕浩 committed
115
    delete[] sDimSize;
116
    delete[] tDimSize;
xiaotong committed
117

张裕浩 committed
118
    return cpuTest;
xiaotong committed
119 120 121
#endif // USE_CUDA
}

liyinqiao committed
122 123 124
/* 
case 2: transform a tensor by splitting it, e.g., (N, M) -> (N/3, M, 3)
In this case, (3, 4) -> (2, 3, 2), whereToSplit=1, splitNum=2.
xiaotong committed
125 126 127
*/
bool TestSplit2()
{
liyinqiao committed
128
    /* a source tensor of size (3, 4) */
xiaotong committed
129 130 131 132 133 134 135 136 137
    int sOrder = 2;
    int * sDimSize = new int[sOrder];
    sDimSize[0] = 3;
    sDimSize[1] = 4;

    int sUnitNum = 1;
    for (int i = 0; i < sOrder; i++)
        sUnitNum *= sDimSize[i];

liyinqiao committed
138
    /* a target tensor of size (2, 3, 2) */
xiaotong committed
139 140 141 142 143 144 145 146 147 148 149 150 151
    int tOrder = 3;
    int * tDimSize = new int[tOrder];
    tDimSize[0] = 2;
    tDimSize[1] = 3;
    tDimSize[2] = 2;

    int tUnitNum = 1;
    for (int i = 0; i < tOrder; i++)
        tUnitNum *= tDimSize[i];

    DTYPE sData[3][4] = { {0.0F, 1.0F, 2.0F, 3.0F},
                          {4.0F, 5.0F, 0.1F, 1.1F},
                          {2.1F, 3.1F, 4.1F, 5.1F} };
张裕浩 committed
152 153 154 155 156 157
    DTYPE answer[2][3][2] = { { {0.0F, 1.0F},
                                {4.0F, 5.0F},
                                {2.1F, 3.1F} },
                              { {2.0F, 3.0F},
                                {0.1F, 1.1F},
                                {4.1F, 5.1F} } };
xiaotong committed
158 159 160 161 162 163 164

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * s = NewTensor(sOrder, sDimSize);
    XTensor * t = NewTensor(tOrder, tDimSize);
165
    XTensor tUser;
xiaotong committed
166 167 168 169 170 171

    /* initialize variables */
    s->SetData(sData, sUnitNum);
    t->SetZeroAll();;

    /* call split function */
172
    _Split(s, t, 1, 2);
173
    tUser = Split(*s, 1, 2);
xiaotong committed
174 175

    /* check results */
张裕浩 committed
176
    cpuTest = _CheckData(t, answer, tUnitNum) && _CheckData(&tUser, answer, tUnitNum);
177

xiaotong committed
178 179 180 181 182 183

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

    /* create tensor */
张裕浩 committed
184 185
    XTensor * sGPU = NewTensor(sOrder, sDimSize, X_FLOAT, 1.0F, 0);
    XTensor * tGPU = NewTensor(tOrder, tDimSize, X_FLOAT, 1.0F, 0);
186
    XTensor tUserGPU;
xiaotong committed
187 188

    /* Initialize variables */
张裕浩 committed
189 190
    sGPU->SetData(sData, sUnitNum);
    tGPU->SetZeroAll();
xiaotong committed
191 192

    /* call sum function */
张裕浩 committed
193
    _Split(sGPU, tGPU, 1, 2);
194
    tUserGPU = Split(*sGPU, 1, 2);
xiaotong committed
195 196

    /* check results */
张裕浩 committed
197
    gpuTest = _CheckData(tGPU, answer, tUnitNum) && _CheckData(&tUserGPU, answer, tUnitNum);
xiaotong committed
198 199

    /* destroy variables */
张裕浩 committed
200
    delete s;
201 202 203
    delete t;
    delete sGPU;
    delete tGPU;
张裕浩 committed
204 205
    delete[] sDimSize;
    delete[] tDimSize;
xiaotong committed
206

张裕浩 committed
207
    return cpuTest && gpuTest;
xiaotong committed
208 209
#else
    /* destroy variables */
张裕浩 committed
210
    delete s;
211
    delete t;
张裕浩 committed
212 213
    delete[] sDimSize;
    delete[] tDimSize;
xiaotong committed
214

张裕浩 committed
215
    return cpuTest;
xiaotong committed
216 217 218
#endif // USE_CUDA
}

liyinqiao committed
219 220 221
/* 
case 3: split a big tensor into small tensors
In this case, (3, 4) -> 2 * (3, 2) , whereToSplit=1, splitNum=2.
xiaotong committed
222 223 224
*/
bool TestSplit3()
{
张裕浩 committed
225 226 227
    /* create list */
    TensorList * tList = new TensorList();
    TensorList tUserList;
xiaotong committed
228

liyinqiao committed
229
    /* a source tensor of size (3, 4) */
xiaotong committed
230 231 232 233 234 235 236 237 238
    int sOrder = 2;
    int * sDimSize = new int[sOrder];
    sDimSize[0] = 3;
    sDimSize[1] = 4;

    int sUnitNum = 1;
    for (int i = 0; i < sOrder; i++)
        sUnitNum *= sDimSize[i];

liyinqiao committed
239
    /* a target tensor of size (3, 2) */
xiaotong committed
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 271 272 273 274 275
    int tOrder1 = 2;
    int * tDimSize1 = new int[tOrder1];
    tDimSize1[0] = 3;
    tDimSize1[1] = 2;

    int tUnitNum1 = 1;
    for (int i = 0; i < tOrder1; i++)
        tUnitNum1 *= tDimSize1[i];

    /* a target tensor of size (3 * 2) */
    int tOrder2 = 2;
    int * tDimSize2 = new int[tOrder2];
    tDimSize2[0] = 3;
    tDimSize2[1] = 2;

    int tUnitNum2 = 1;
    for (int i = 0; i < tOrder2; i++)
        tUnitNum2 *= tDimSize2[i];

    DTYPE sData[3][4] = { {0.0F, 1.0F, 2.0F, 3.0F},
                          {4.0F, 5.0F, 0.1F, 1.1F},
                          {2.1F, 3.1F, 4.1F, 5.1F} };
    DTYPE answer1[3][2] = { {0.0F, 1.0F},
                            {4.0F, 5.0F},
                            {2.1F, 3.1F} };
    DTYPE answer2[3][2] = { {2.0F, 3.0F},
                            {0.1F, 1.1F},
                            {4.1F, 5.1F} };

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * s = NewTensor(sOrder, sDimSize);
    XTensor * t1 = NewTensor(tOrder1, tDimSize1);
    XTensor * t2 = NewTensor(tOrder2, tDimSize2);
276 277
    XTensor * t3 = NewTensor(tOrder2, tDimSize2);
    XTensor * t4 = NewTensor(tOrder2, tDimSize2);
xiaotong committed
278 279 280 281 282 283

    /* initialize variables */
    s->SetData(sData, sUnitNum);
    t1->SetZeroAll();
    t2->SetZeroAll();

张裕浩 committed
284
    /* add tensors to list */
285 286
    tList->Add(t1);
    tList->Add(t2);
287 288 289

    tUserList.Add(t3);
    tUserList.Add(t4);
xiaotong committed
290 291

    /* call split function */
292 293
    _Split(s, tList, 1, 2);
    Split(*s, tUserList, 1, 2);
xiaotong committed
294 295

    /* check results */
张裕浩 committed
296 297
    cpuTest = _CheckData(t1, answer1, tUnitNum1) && _CheckData((XTensor *)tUserList.Get(0), answer1, tUnitNum1) &&
              _CheckData(t2, answer2, tUnitNum2) && _CheckData((XTensor *)tUserList.Get(1), answer2, tUnitNum2);
xiaotong committed
298 299 300 301 302

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

张裕浩 committed
303 304
    /* clear list */
    tList->Clear();
305
    tUserList.Clear();
xiaotong committed
306 307

    /* create tensor */
张裕浩 committed
308 309 310 311 312
    XTensor * sGPU = NewTensor(sOrder, sDimSize, X_FLOAT, 1.0F, 0);
    XTensor * tGPU1 = NewTensor(tOrder1, tDimSize1, X_FLOAT, 1.0F, 0);
    XTensor * tGPU2 = NewTensor(tOrder2, tDimSize2, X_FLOAT, 1.0F, 0);
    XTensor * tGPU3 = NewTensor(tOrder2, tDimSize2, X_FLOAT, 1.0F, 0);
    XTensor * tGPU4 = NewTensor(tOrder2, tDimSize2, X_FLOAT, 1.0F, 0);
xiaotong committed
313 314

    /* Initialize variables */
张裕浩 committed
315 316 317
    sGPU->SetData(sData, sUnitNum);
    tGPU1->SetZeroAll();
    tGPU2->SetZeroAll();
xiaotong committed
318

张裕浩 committed
319 320 321
    /* add tensors to list */
    tList->Add(tGPU1);
    tList->Add(tGPU2);
322

张裕浩 committed
323 324
    tUserList.Add(tGPU3);
    tUserList.Add(tGPU4);
xiaotong committed
325

张裕浩 committed
326 327
    /* call Split function */
    _Split(sGPU, tList, 1, 2);
328
    Split(*sGPU, tUserList, 1, 2);
xiaotong committed
329 330

    /* check results */
张裕浩 committed
331 332
    gpuTest = _CheckData(tGPU1, answer1, tUnitNum1) && _CheckData((XTensor *)tUserList.Get(0), answer1, tUnitNum1) &&
              _CheckData(tGPU2, answer2, tUnitNum2) && _CheckData((XTensor *)tUserList.Get(1), answer2, tUnitNum2);
xiaotong committed
333 334

    /* destroy variables */
张裕浩 committed
335
    delete s;
336 337
    delete t1;
    delete t2;
338 339
    delete t3;
    delete t4;
340 341 342
    delete sGPU;
    delete tGPU1;
    delete tGPU2;
343 344
    delete tGPU3;
    delete tGPU4;
张裕浩 committed
345 346 347
    delete[] sDimSize;
    delete[] tDimSize1;
    delete[] tDimSize2;
348
    delete tList;
xiaotong committed
349

张裕浩 committed
350
    return cpuTest && gpuTest;
xiaotong committed
351 352
#else
    /* destroy variables */
张裕浩 committed
353
    delete s;
354 355
    delete t1;
    delete t2;
356 357
    delete t3;
    delete t4;
张裕浩 committed
358 359 360
    delete[] sDimSize;
    delete[] tDimSize1;
    delete[] tDimSize2;
361
    delete tList;
xiaotong committed
362

张裕浩 committed
363
    return cpuTest;
xiaotong committed
364 365 366 367 368 369 370 371 372
#endif // USE_CUDA
}

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

/* test for Split Function */
liyinqiao committed
373
bool TestSplit()
xiaotong committed
374
{
liyinqiao committed
375
    XPRINT(0, stdout, "[TEST SPLIT] split a big tensor into small tensors \n");
xiaotong committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 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
    bool returnFlag = true, caseFlag = true;

    /* case 1 test */
    caseFlag = TestSplit1();

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

    /* case 2 test */
    caseFlag = TestSplit2();

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

    /* case 3 test */
    caseFlag = TestSplit3();

    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 3 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 3 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;
}

} // namespace nts(NiuTrans.Tensor)