TRectify.cpp 5.97 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-14
*/

22
#include "../core/utilities/CheckData.h"
liyinqiao committed
23
#include "TRectify.h"
xiaotong committed
24 25

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

/* 
case 1: test rectify function
In this case, y = max(0, x) 
liyinqiao committed
30
*/
xiaotong committed
31 32
bool TestRectify1()
{
33 34 35 36 37 38 39 40 41
    /* a tensor of size (2, 3) */
    int order = 2;
    int * dimSize = new int[order];
    dimSize[0] = 2;
    dimSize[1] = 3;

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

liyinqiao committed
43 44 45 46
    DTYPE xData[2][3] = { {0.0F, -1.0F, 2.0F},
                          {3.0F, -4.0F, -5.0F} };
    DTYPE answer[2][3] = { {0.0F, 0.0F, 2.0F},
                           {3.0F, 0.0F, 0.0F} };
xiaotong committed
47 48 49 50 51

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
52 53
    XTensor * x = NewTensorV2(order, dimSize);
    XTensor * y = NewTensorV2(order, dimSize);
54
    XTensor yUser;
xiaotong committed
55 56

    /* initialize variables */
57
    x->SetData(xData, unitNum);
xiaotong committed
58 59
    y->SetZeroAll();

liyinqiao committed
60
    /* call Rectify function */
61
    _Rectify(x, y);
62
    yUser = Rectify(*x);
xiaotong committed
63 64

    /* check results */
65
    cpuTest = _CheckData(y, answer, unitNum, 1e-4F) && _CheckData(&yUser, answer, unitNum, 1e-4F);
xiaotong committed
66 67

#ifdef USE_CUDA
68 69
    /* GPU test */
    bool gpuTest = true;
xiaotong committed
70

71 72 73
    /* create tensor */
    XTensor * xGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * yGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
74
    XTensor yUserGPU;
xiaotong committed
75

76 77 78
    /* Initialize variables */
    xGPU->SetData(xData, unitNum);
    yGPU->SetZeroAll();
xiaotong committed
79

80 81
    /* call Rectify function */
    _Rectify(xGPU, yGPU);
82
    yUserGPU = Rectify(*xGPU);
xiaotong committed
83

84 85
    /* check results */
    gpuTest = _CheckData(yGPU, answer, unitNum, 1e-4F) && _CheckData(&yUserGPU, answer, unitNum, 1e-4F);
xiaotong committed
86

87 88
    /* destroy variables */
    delete x;
liyinqiao committed
89 90 91
    delete y;
    delete xGPU;
    delete yGPU;
92
    delete[] dimSize;
xiaotong committed
93

94
    return cpuTest && gpuTest;
xiaotong committed
95
#else
96 97
    /* destroy variables */
    delete x;
liyinqiao committed
98
    delete y;
99
    delete[] dimSize;
xiaotong committed
100

101
    return cpuTest;
xiaotong committed
102 103 104
#endif // USE_CUDA
}

liyinqiao committed
105 106 107 108 109
/* 
case 2: backward computation 
dE/dx = dE/dy * dy/dx 
rectified: y = max(0, x) 
In this case, lossName=CROSSENTROPY.
xiaotong committed
110 111 112
*/
bool TestRectify2()
{
113 114 115 116 117
    /* a tensor of size (2, 3) */
    int order = 2;
    int * dimSize = new int[order];
    dimSize[0] = 2;
    dimSize[1] = 3;
xiaotong committed
118

119 120 121
    int unitNum = 1;
    for (int i = 0; i < order; i++)
        unitNum *= dimSize[i];
xiaotong committed
122

123 124 125 126 127 128 129 130
	DTYPE xData[2][3] = { {-1.0F, 1.0F, 2.0F},
	                      {-2.0F, 4.0F, 5.0F} };
    DTYPE yData[2][3] = { {0.0F, 1.0F, 2.0F},
	                      {0.0F, 4.0F, 5.0F} };
	DTYPE dedyData[2][3] = { {-0.5F, -0.5F, -0.25F},
	                         {-0.25F, -0.125F, -0.1F} };
	DTYPE dedxAnswer[2][3] = { {0.0F, -0.5F, -0.25F},
	                           {0.0F, -0.125F, -0.1F} };
xiaotong committed
131

132 133
    /* CPU test */
    bool cpuTest = true;
xiaotong committed
134

135 136 137 138 139
    /* create tensors */
    XTensor * x = NewTensorV2(order, dimSize);
    XTensor * y = NewTensorV2(order, dimSize);
    XTensor * dedy = NewTensorV2(order, dimSize);
    XTensor * dedx = NewTensorV2(order, dimSize);
xiaotong committed
140

141 142
    /* initialize variables */
    x->SetData(xData, unitNum);
143 144
	y->SetData(yData, unitNum);
	dedy->SetData(dedyData, unitNum);
145 146 147
    dedx->SetZeroAll();

    /* call Rectify function */
xiaotong committed
148

149
    /* call RectifyBackward function */
150
	_RectifyBackward(y, x, dedy, dedx);
xiaotong committed
151

152 153
    /* check results */
    cpuTest = _CheckData(dedx, dedxAnswer, unitNum, 1e-4F);
xiaotong committed
154 155

#ifdef USE_CUDA
156 157
    /* GPU test */
    bool gpuTest = true;
xiaotong committed
158

159 160 161 162 163
    /* create tensors */
    XTensor * xGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * yGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * dedyGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * dedxGPU = NewTensorV2(order, dimSize, X_FLOAT, 1.0F, 0);
xiaotong committed
164

165 166
    /* initialize variables */
    xGPU->SetData(xData, unitNum);
167 168
	yGPU->SetData(yData, unitNum);
	dedyGPU->SetData(dedyData, unitNum);
169 170 171
    dedxGPU->SetZeroAll();
    
    /* call Rectify function */
xiaotong committed
172

173
    /* call rectifybackward function */
174
	_RectifyBackward(yGPU, xGPU, dedyGPU, dedxGPU);
liyinqiao committed
175
    
176 177
    /* check results */
    gpuTest = _CheckData(dedxGPU, dedxAnswer, unitNum, 1e-4F);
xiaotong committed
178

179
    /* destroy variables */
liyinqiao committed
180 181 182 183 184 185 186 187
    delete x;
    delete y;
    delete dedy;
    delete dedx;
    delete xGPU;
    delete yGPU;
    delete dedyGPU;
    delete dedxGPU;
188
    delete[] dimSize;
xiaotong committed
189

190
    return cpuTest && gpuTest;
xiaotong committed
191
#else
192
    /* destroy variables */
liyinqiao committed
193 194 195 196
    delete x;
    delete y;
    delete dedy;
    delete dedx;
197
	delete[] dimSize;
xiaotong committed
198

199
    return cpuTest;
xiaotong committed
200 201 202 203 204 205 206 207 208 209 210
#endif // USE_CUDA
}

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

/* test for Rectify Function */
bool TestRectify()
{
211
    XPRINT(0, stdout, "[TEST RECTIFY] rectify function and its backward computation \n");
xiaotong committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    bool returnFlag = true, caseFlag = true;

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

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

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

227 228 229 230 231 232
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 2 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 2 passed!\n");
xiaotong committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    /* 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)