THardTanH.cpp 6.38 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-20
*/

22
#include "../XTensor.h"
liyinqiao committed
23
#include "THardTanH.h"
xiaotong committed
24 25

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

27 28 29 30 31 32
/* 
case 1: test HardTanH function 
y =  1    if x > 1
     x    if -1 <= x <= 1
    -1    if x < -1
*/
xiaotong committed
33 34
bool TestHardTanH1()
{
35 36 37 38 39 40 41 42 43
	/* 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
44

liyinqiao committed
45 46 47 48
	DTYPE xData[2][3] = { {0.5F, -1.0F, 2.0F},
	                      {3.5F, -4.5F, 1.0F} };
	DTYPE answer[2][3] = { {0.5F, -1.0F, 1.0F},
	                       {1.0F, -1.0F, 1.0F} };
xiaotong committed
49 50 51 52 53

	/* CPU test */
	bool cpuTest = true;

	/* create tensors */
54 55
	XTensor * x = NewTensor(order, dimSize);
	XTensor * y = NewTensor(order, dimSize);
56
    XTensor yUser;
xiaotong committed
57 58

	/* initialize variables */
59
	x->SetData(xData, unitNum);
xiaotong committed
60 61 62
	y->SetZeroAll();

	/* call hardtanh function */
63
	_HardTanH(x, y);
64
    yUser = HardTanH(*x);
xiaotong committed
65 66

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

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

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

	/* Initialize variables */
79
	xGPU->SetData(xData, unitNum);
xiaotong committed
80 81 82
	yGPU->SetZeroAll();

	/* call hardtanh function */
83
	_HardTanH(xGPU, yGPU);
84
    yUserGPU = HardTanH(*xGPU);
xiaotong committed
85 86

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

	/* destroy variables */
liyinqiao committed
90 91 92 93
	delete x;
    delete y;
    delete xGPU;
    delete yGPU;
94
	delete[] dimSize;
xiaotong committed
95 96 97 98

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

	return cpuTest;
#endif // USE_CUDA
}

liyinqiao committed
107
/*
108 109 110 111 112 113 114 115 116
case 2: test backward computation of HardTanH function.
dE/dx = dE/dy * dy/dx
hard tanh: y =  1    if x > 1
                x    if -1 <= x <= 1
               -1    if x< -1

   and dy/dx =  1    if -1 <= x <= 1
                0    otherwise
In this case, lossName=SQUAREDERROR.
xiaotong committed
117 118 119
*/
bool TestHardTanH2()
{
120 121 122 123 124
	/* a tensor of size (2, 3) */
	int order = 2;
	int * dimSize = new int[order];
	dimSize[0] = 2;
	dimSize[1] = 3;
xiaotong committed
125

126 127 128
	int unitNum = 1;
	for (int i = 0; i < order; i++)
		unitNum *= dimSize[i];
xiaotong committed
129

liyinqiao committed
130 131 132 133
	DTYPE xData[2][3] = { {0.5F, -1.0F, 2.0F},
	                      {3.5F, -4.5F, 1.0F} };
	DTYPE goldData[2][3] = { {1.0F, 1.0F, 1.0F},
	                         {1.0F, 1.0F, 1.0F} };
134 135 136 137 138 139
	DTYPE yAnswer[2][3] = { {0.5F, -1.0F, 1.0F},
	                        {1.0F, -1.0F, 1.0F} };
    DTYPE dedyAnswer[2][3] = { {-0.5F, -2.0F, 0.0F},
	                           {0.0F, -2.0F, 0.0F} };
	DTYPE dedxAnswer[2][3] = { {-0.5F, -2.0F, 0.0F},
	                           {0.0F, 0.0F, -0.0F} };
xiaotong committed
140 141 142 143 144

	/* CPU test */
	bool cpuTest = true;

	/* create tensors */
145 146 147 148 149
	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
150 151

	/* initialize variables */
152 153 154 155
	x->SetData(xData, unitNum);
	gold->SetData(goldData, unitNum);
    y->SetZeroAll();
    dedy->SetZeroAll();
xiaotong committed
156 157
	dedx->SetZeroAll();

158
    /* call HardTanH function */
159
    _HardTanH(x, y);
160 161

	/* call HardTanHBackward function */
162
	_HardTanHBackward(gold, y, x, dedy, dedx, SQUAREDERROR);
xiaotong committed
163 164

	/* check results */
165 166 167
	cpuTest = y->CheckData(yAnswer, unitNum, 1e-4F) 
              && dedx->CheckData(dedxAnswer, unitNum, 1e-4F)
              && dedy->CheckData(dedyAnswer, unitNum, 1e-4F);
xiaotong committed
168 169 170 171 172 173

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

	/* create tensors */
174 175 176 177 178
	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
179 180

	/* initialize variables */
181 182 183 184
	xGPU->SetData(xData, unitNum);
	goldGPU->SetData(goldData, unitNum);
    yGPU->SetZeroAll();
    dedyGPU->SetZeroAll();
xiaotong committed
185 186
	dedxGPU->SetZeroAll();

187
    /* call HardTanH function */
188
    _HardTanH(xGPU, yGPU);
xiaotong committed
189 190

	/* call hardtanhbackward function */
191
	_HardTanHBackward(goldGPU, yGPU, xGPU, dedyGPU, dedxGPU, SQUAREDERROR);
xiaotong committed
192 193

	/* check results */
194 195 196
	gpuTest = y->CheckData(yAnswer, unitNum, 1e-4F) 
              && dedxGPU->CheckData(dedxAnswer, unitNum, 1e-4F)
              && dedyGPU->CheckData(dedyAnswer, unitNum, 1e-4F);
xiaotong committed
197 198

	/* destroy variables */
199 200 201 202 203 204 205 206 207 208 209
    delete x;
    delete y;
    delete gold;
    delete dedx;
    delete dedy;
    delete xGPU;
    delete yGPU;
    delete goldGPU;
    delete dedxGPU;
    delete dedyGPU;
    delete[] dimSize;
xiaotong committed
210

211
    return cpuTest && gpuTest;
xiaotong committed
212
#else
213 214 215 216 217 218 219
    /* destroy variables */
    delete x;
    delete y;
    delete gold;
    delete dedx;
    delete dedy;
    delete[] dimSize;
xiaotong committed
220 221 222 223 224 225 226 227 228 229 230 231 232

	return cpuTest;
#endif // USE_CUDA
}

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

/* test for HardTanH Function */
bool TestHardTanH()
{
liyinqiao committed
233
	XPRINT(0, stdout, "[TEST HARDTANH] test hardtanh and its backward computation \n");
xiaotong committed
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 271 272
	bool returnFlag = true, caseFlag = true;

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

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

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

	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)