TClip.cpp 3.86 KB
Newer Older
1
/* NiuTrans.Tensor - an open-source tensor library
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * 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.
 */
17 18

/*
19 20
 * $Created by: Lin Ye (email: linye2015@outlook.com) 2018-08-03
 */
21 22

#include "../XTensor.h"
23
#include "../core/math/Clip.h"
liyinqiao committed
24
#include "../core/utilities/CheckData.h"
25 26 27 28 29 30 31 32 33 34
#include "TClip.h"

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

/*
case 1: test Clip function.
Set every entry to its clip value.
*/
bool TestClip1()
{
liyinqiao committed
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 65 66 67 68 69 70
    /* a tensor of size (3, 2) */
    int aOrder = 2;
    int * aDimSize = new int[aOrder];
    aDimSize[0] = 3;
    aDimSize[1] = 2;

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

    DTYPE aData[3][2] = { {1.0F, -2.0F},
                          {0.0F, 4.0F},
                          {5.0F, -6.0F} };
    DTYPE answer[3][2] = { {1.0F, -1.0F},
                           {0.0F, 1.0F},
                              {1.0F, -1.0F} };

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * a = NewTensor(aOrder, aDimSize);
    XTensor * b = NewTensor(aOrder, aDimSize);
    XTensor * aMe = NewTensor(aOrder, aDimSize);
    XTensor bUser;

    /* initialize variables */
    a->SetData(aData, aUnitNum);
    aMe->SetData(aData, aUnitNum);

    /* call Clip function */
    _Clip(a, b, -1.0, 1.0);
    _ClipMe(aMe, -1.0, 1.0);
    bUser = Clip(*a, -1.0, 1.0);

    /* check results */
liyinqiao committed
71 72 73
    cpuTest = CheckData(b, answer, aUnitNum, 1e-4F) && 
              CheckData(aMe, answer, aUnitNum, 1e-4F) &&
              CheckData(&bUser, answer, aUnitNum, 1e-4F);
74 75

#ifdef USE_CUDA
liyinqiao committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    /* GPU test */
    bool gpuTest = true;

    /* create tensor */
    XTensor * aGPU = NewTensor(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensor(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * aMeGPU = NewTensor(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor bUserGPU;

    /* Initialize variables */
    aGPU->SetData(aData, aUnitNum);
    aMeGPU->SetData(aData, aUnitNum);

    /* call Clip function */
    _Clip(aGPU, bGPU, -1.0, 1.0);
    _ClipMe(aMeGPU, -1.0, 1.0);
    bUserGPU = Clip(*aGPU, -1.0, 1.0);

    /* check results */
liyinqiao committed
95 96 97
    gpuTest = CheckData(bGPU, answer, aUnitNum, 1e-4F) &&
              CheckData(aMeGPU, answer, aUnitNum, 1e-4F) &&
              CheckData(&bUserGPU, answer, aUnitNum, 1e-4F);
98

liyinqiao committed
99 100 101 102 103 104 105 106
    /* destroy variables */
    delete a;
    delete b;
    delete aMe;
    delete aGPU;
    delete bGPU;
    delete aMeGPU;
    delete[] aDimSize;
107

liyinqiao committed
108
    return cpuTest && gpuTest;
109
#else
liyinqiao committed
110 111 112 113 114
    /* destroy variables */
    delete a;
    delete b;
    delete aMe;
    delete[] aDimSize;
115

liyinqiao committed
116
    return cpuTest;
117 118 119 120 121 122 123 124 125 126 127
#endif // USE_CUDA
}

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

/* test for Clip Function */
bool TestClip()
{
liyinqiao committed
128 129
    XPRINT(0, stdout, "[TEST Clip] set every entry to its clip value \n");
    bool returnFlag = true, caseFlag = true;
130

liyinqiao committed
131 132
    /* case 1 test */
    caseFlag = TestClip1();
133

liyinqiao committed
134 135 136 137 138 139
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 1 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 1 passed!\n");
140

liyinqiao committed
141 142 143 144
    /* other cases test */
    /*
    TODO!!
    */
145

liyinqiao committed
146 147 148 149 150
    if (returnFlag) {
        XPRINT(0, stdout, ">> All Passed!\n");
    }
    else
        XPRINT(0, stdout, ">> Failed!\n");
151

liyinqiao committed
152
    XPRINT(0, stdout, "\n");
153

liyinqiao committed
154
    return returnFlag;
155 156 157
}

} // namespace nts(NiuTrans.Tensor)