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

liyinqiao committed
22
#include "TRectify.h"
xiaotong committed
23 24

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

/* 
case 1: test rectify function
In this case, y = max(0, x) 
liyinqiao committed
29
*/
xiaotong committed
30 31
bool TestRectify1()
{
32 33 34 35 36 37 38 39 40
    /* 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
41

liyinqiao committed
42 43 44 45
    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
46 47 48 49 50

    /* CPU test */
    bool cpuTest = true;

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

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

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

    /* check results */
64
	cpuTest = y->CheckData(answer, unitNum, 1e-4F) && yUser.CheckData(answer, unitNum, 1e-4F);
xiaotong committed
65 66 67 68 69 70

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

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

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

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

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

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

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

	return cpuTest;
#endif // USE_CUDA
}

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

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

liyinqiao committed
122 123 124 125
	DTYPE xData[2][3] = { {1.0F, 1.0F, 2.0F},
	                      {2.0F, 4.0F, 5.0F} };
	DTYPE goldData[2][3] = { {1.0F, 1.0F, 1.0F},
	                         {1.0F, 1.0F, 1.0F} };
126 127 128 129 130 131
    DTYPE yAnswer[2][3] = { {1.0F, 1.0F, 2.0F},
	                        {2.0F, 4.0F, 5.0F} };
	DTYPE dedyAnswer[2][3] = { {-1.0F, -1.0F, -0.5F},
	                           {-0.5F, -0.25F, -0.2F} };
	DTYPE dedxAnswer[2][3] = { {-1.0F, -1.0F, -0.5F},
	                           {-0.5F, -0.25F, -0.2F} };
xiaotong committed
132 133 134 135 136

	/* CPU test */
	bool cpuTest = true;

	/* create tensors */
137 138 139 140 141
	XTensor * x = NewTensor(order, dimSize);
	XTensor * y = NewTensor(order, dimSize);
	XTensor * gold = NewTensor(order, dimSize);
	XTensor * dedy = NewTensor(order, dimSize);
	XTensor * dedx = NewTensor(order, dimSize);
xiaotong committed
142 143

	/* initialize variables */
144 145 146 147
	x->SetData(xData, unitNum);
	gold->SetData(goldData, unitNum);
	y->SetZeroAll();
	dedy->SetZeroAll();
xiaotong committed
148 149
	dedx->SetZeroAll();

150
    /* call Rectify function */
151
    _Rectify(x, y);
152

liyinqiao committed
153
	/* call RectifyBackward function */
154
	_RectifyBackward(gold, y, x, dedy, dedx, CROSSENTROPY);
xiaotong committed
155 156

	/* check results */
157 158 159
    cpuTest = y->CheckData(yAnswer, unitNum, 1e-4F)
              && dedx->CheckData(dedxAnswer, unitNum, 1e-4F)
              && dedy->CheckData(dedyAnswer, unitNum, 1e-4F);
xiaotong committed
160 161 162 163 164 165

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

	/* create tensors */
166 167 168 169 170
	XTensor * xGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
	XTensor * yGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
	XTensor * goldGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
	XTensor * dedyGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
	XTensor * dedxGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
xiaotong committed
171 172

	/* initialize variables */
173 174 175 176
	xGPU->SetData(xData, unitNum);
	goldGPU->SetData(goldData, unitNum);
	yGPU->SetZeroAll();
	dedyGPU->SetZeroAll();
xiaotong committed
177
	dedxGPU->SetZeroAll();
178 179
    
    /* call Rectify function */
180
    _Rectify(xGPU, yGPU);
xiaotong committed
181 182

	/* call rectifybackward function */
183
	_RectifyBackward(goldGPU, yGPU, xGPU, dedyGPU, dedxGPU, CROSSENTROPY);
liyinqiao committed
184
    
xiaotong committed
185
	/* check results */
186 187 188
    gpuTest = yGPU->CheckData(yAnswer, unitNum, 1e-4F)
              && dedxGPU->CheckData(dedxAnswer, unitNum, 1e-4F)
              && dedyGPU->CheckData(dedyAnswer, unitNum, 1e-4F);
xiaotong committed
189 190

	/* destroy variables */
liyinqiao committed
191 192 193 194 195 196 197 198 199 200
    delete x;
    delete y;
    delete dedy;
    delete dedx;
    delete gold;
    delete xGPU;
    delete yGPU;
    delete dedyGPU;
    delete dedxGPU;
    delete goldGPU;
201
	delete[] dimSize;
xiaotong committed
202 203 204 205

	return cpuTest && gpuTest;
#else
	/* destroy variables */
liyinqiao committed
206 207 208 209 210
    delete x;
    delete y;
    delete dedy;
    delete dedx;
    delete gold;
211
	delete[] dimSize;
xiaotong committed
212 213 214 215 216 217 218 219 220 221 222 223 224

	return cpuTest;
#endif // USE_CUDA
}

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

/* test for Rectify Function */
bool TestRectify()
{
225
    XPRINT(0, stdout, "[TEST RECTIFY] rectify function and its backward computation \n");
xiaotong committed
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
    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();

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

} // namespace nts(NiuTrans.Tensor)