Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
T
Tensor.LowPrecision
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
linye
Tensor.LowPrecision
Commits
01747e0d
Commit
01747e0d
authored
Aug 02, 2019
by
linye
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update test of multiply, softmax, unsqueeze, crossentropy
parent
4c0fafc0
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
801 行增加
和
3 行删除
+801
-3
source/tensor/test/TCrossEntropy.cpp
+360
-0
source/tensor/test/TMultiply.cpp
+123
-0
source/tensor/test/TSoftmax.cpp
+182
-0
source/tensor/test/TUnsqueeze.cpp
+133
-0
source/tensor/test/Test.cpp
+3
-3
没有找到文件。
source/tensor/test/TCrossEntropy.cpp
查看文件 @
01747e0d
差异被折叠。
点击展开。
source/tensor/test/TMultiply.cpp
查看文件 @
01747e0d
...
...
@@ -20,6 +20,7 @@
*/
#include "TMultiply.h"
#include "../core/getandset/ConvertDataType.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
...
...
@@ -148,6 +149,118 @@ bool TestMultiply1()
#endif // USE_CUDA
}
/*
case 2: float16 element-wise product of two tensors
c(i) = a(i)*b(i) + \alpha * c(i)
In this case, (2, 2) (2, 2) -> (2, 2), leadingDim=0, alpha=0.
*/
bool
TestMultiply2
()
{
/* a source tensor of size (2, 2) */
int
sOrder1
=
2
;
int
*
sDimSize1
=
new
int
[
sOrder1
];
sDimSize1
[
0
]
=
2
;
sDimSize1
[
1
]
=
2
;
int
sUnitNum1
=
1
;
for
(
int
i
=
0
;
i
<
sOrder1
;
i
++
)
sUnitNum1
*=
sDimSize1
[
i
];
/* a source tensor of size (2, 2) */
int
sOrder2
=
2
;
int
*
sDimSize2
=
new
int
[
sOrder2
];
sDimSize2
[
0
]
=
2
;
sDimSize2
[
1
]
=
2
;
int
sUnitNum2
=
1
;
for
(
int
i
=
0
;
i
<
sOrder2
;
i
++
)
sUnitNum2
*=
sDimSize2
[
i
];
/* a target tensor of size (2, 2) */
int
tOrder
=
2
;
int
*
tDimSize
=
new
int
[
tOrder
];
tDimSize
[
0
]
=
2
;
tDimSize
[
1
]
=
2
;
int
tUnitNum
=
1
;
for
(
int
i
=
0
;
i
<
tOrder
;
i
++
)
tUnitNum
*=
tDimSize
[
i
];
DTYPE
sData1
[
2
][
2
]
=
{
{
0.0
F
,
1.0
F
},
{
2.0
F
,
3.0
F
}
};
DTYPE
sData2
[
2
][
2
]
=
{
{
0.0
F
,
1.0
F
},
{
2.0
F
,
3.0
F
}
};
DTYPE
answer
[
2
][
2
]
=
{
{
0.0
F
,
1.0
F
},
{
4.0
F
,
9.0
F
}
};
/* CPU test */
bool
cpuTest
=
true
;
#ifdef USE_CUDA
/* GPU test */
bool
gpuTest
=
true
;
/* create tensor */
XTensor
*
sGPU1
=
NewTensor
(
sOrder1
,
sDimSize1
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
sGPU2
=
NewTensor
(
sOrder2
,
sDimSize2
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
tGPU
=
NewTensor
(
tOrder
,
tDimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
tMeGPU
=
NewTensor
(
tOrder
,
tDimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
tUserGPU
;
/* create float16 tensor */
XTensor
sHalfGPU1
;
XTensor
sHalfGPU2
;
XTensor
tHalfGPU
;
XTensor
tMeHalfGPU
;
XTensor
tUserHalfGPU
;
/* Initialize variables */
sGPU1
->
SetData
(
sData1
,
sUnitNum1
);
tMeGPU
->
SetData
(
sData1
,
sUnitNum1
);
sGPU2
->
SetData
(
sData2
,
sUnitNum2
);
tGPU
->
SetZeroAll
();
/* convert data type from float to float16 */
sHalfGPU1
=
ConvertDataType
(
*
sGPU1
,
X_FLOAT16
);
sHalfGPU2
=
ConvertDataType
(
*
sGPU2
,
X_FLOAT16
);
tHalfGPU
=
ConvertDataType
(
*
tGPU
,
X_FLOAT16
);
tMeHalfGPU
=
ConvertDataType
(
*
tMeGPU
,
X_FLOAT16
);
/* call multiply function */
_Multiply
(
&
sHalfGPU1
,
&
sHalfGPU2
,
&
tHalfGPU
,
0
,
0
);
_MultiplyMe
(
&
tMeHalfGPU
,
&
sHalfGPU2
,
0
,
0
);
tUserHalfGPU
=
Multiply
(
sHalfGPU1
,
sHalfGPU2
,
0
);
/* convert data type from float16 to float */
_ConvertDataType
(
&
tHalfGPU
,
tGPU
);
_ConvertDataType
(
&
tMeHalfGPU
,
tMeGPU
);
tUserGPU
=
ConvertDataType
(
tUserHalfGPU
,
X_FLOAT
);
/* check results */
gpuTest
=
tGPU
->
CheckData
(
answer
,
tUnitNum
)
&&
tMeGPU
->
CheckData
(
answer
,
tUnitNum
)
&&
tUserGPU
.
CheckData
(
answer
,
tUnitNum
);
/* destroy variables */
delete
sGPU1
;
delete
sGPU2
;
delete
tGPU
;
delete
tMeGPU
;
delete
[]
sDimSize1
;
delete
[]
sDimSize2
;
delete
[]
tDimSize
;
return
cpuTest
&&
gpuTest
;
#else
/* destroy variables */
delete
[]
sDimSize1
;
delete
[]
sDimSize2
;
delete
[]
tDimSize
;
return
cpuTest
;
#endif // USE_CUDA
}
/* other cases */
/*
TODO!!
...
...
@@ -169,6 +282,16 @@ bool TestMultiply()
else
XPRINT
(
0
,
stdout
,
">> case 1 passed!
\n
"
);
/* case 2 test */
caseFlag
=
TestMultiply2
();
if
(
!
caseFlag
)
{
returnFlag
=
false
;
XPRINT
(
0
,
stdout
,
">> case 2 failed!
\n
"
);
}
else
XPRINT
(
0
,
stdout
,
">> case 2 passed!
\n
"
);
/* other cases test */
/*
TODO!!
...
...
source/tensor/test/TSoftmax.cpp
查看文件 @
01747e0d
...
...
@@ -22,6 +22,7 @@
#include "../XTensor.h"
#include "../XUtility.h"
#include "TSoftmax.h"
#include "../core/getandset/ConvertDataType.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
...
...
@@ -207,6 +208,167 @@ bool TestSoftmax2()
#endif // USE_CUDA
}
/*
case 3: float16 test Softmax function.
softmax function: y = e^x / \sum_{i} e^{x_i}
*/
bool
TestSoftmax3
()
{
/* 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
];
DTYPE
xData
[
2
][
3
]
=
{
{
0.0
F
,
1.0
F
,
2.0
F
},
{
0.5
F
,
0.7
F
,
1.4
F
}
};
DTYPE
answer
[
2
][
3
]
=
{
{
0.0900
F
,
0.2447
F
,
0.6652
F
},
{
0.2136
F
,
0.2609
F
,
0.5254
F
}
};
/* CPU test */
bool
cpuTest
=
true
;
#ifdef USE_CUDA
/* GPU test */
bool
gpuTest
=
true
;
/* create tensors */
XTensor
*
xGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
yGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
yUserGPU
;
/* create float16 tensors */
XTensor
xHalfGPU
;
XTensor
yHalfGPU
;
XTensor
yUserHalfGPU
;
/* initialize variables */
xGPU
->
SetData
(
xData
,
unitNum
);
yGPU
->
SetZeroAll
();
/* convert data type from float to float16 */
xHalfGPU
=
ConvertDataType
(
*
xGPU
,
X_FLOAT16
);
yHalfGPU
=
ConvertDataType
(
*
yGPU
,
X_FLOAT16
);
/* call softmax function */
_Softmax
(
&
xHalfGPU
,
&
yHalfGPU
,
1
);
yUserHalfGPU
=
Softmax
(
xHalfGPU
,
1
);
/* convert data type from float16 to float */
_ConvertDataType
(
&
yHalfGPU
,
yGPU
);
yUserGPU
=
ConvertDataType
(
yUserHalfGPU
,
X_FLOAT
);
/* check result */
gpuTest
=
yGPU
->
CheckData
(
answer
,
unitNum
,
1e-3
F
)
&&
yUserGPU
.
CheckData
(
answer
,
unitNum
,
1e-3
F
);
/* destroy variables */
delete
xGPU
;
delete
yGPU
;
delete
[]
dimSize
;
return
cpuTest
&&
gpuTest
;
#else
/* destroy variables */
delete
[]
dimSize
;
return
cpuTest
;
#endif // USE_CUDA
}
/*
case 4: float16 test SoftmaxBackward function.
SoftmaxBackward function: dE/dx_j = -gold_j + y_j
In this case, LossName=CROSSENTROPY.
*/
bool
TestSoftmax4
()
{
/* a input tensor of size (2, 3) */
int
order
=
2
;
int
*
dimSize
=
new
int
[
order
];
dimSize
[
0
]
=
1
;
dimSize
[
1
]
=
3
;
int
unitNum
=
1
;
for
(
int
i
=
0
;
i
<
order
;
i
++
)
unitNum
*=
dimSize
[
i
];
DTYPE
xData
[
1
][
3
]
=
{
{
0.0
F
,
1.0
F
,
2.0
F
}
};
DTYPE
gData
[
1
][
3
]
=
{
{
0.0
F
,
0.0
F
,
1.0
F
}
};
DTYPE
yAnswer
[
1
][
3
]
=
{
{
0.0900
F
,
0.2447
F
,
0.6652
F
}
};
DTYPE
dedxAnswer
[
1
][
3
]
=
{
0.0900
F
,
0.2447
F
,
-
0.3347
F
};
/* CPU test */
bool
cpuTest
=
true
;
#ifdef USE_CUDA
/* GPU test */
bool
gpuTest
=
true
;
/* create tensors */
XTensor
*
xGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
yGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
gGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
dedyGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
dedxGPU
=
NewTensor
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
/* create float16 tensors */
XTensor
xHalfGPU
;
XTensor
yHalfGPU
;
XTensor
gHalfGPU
;
XTensor
dedyHalfGPU
;
XTensor
dedxHalfGPU
;
/* initialize variables */
xGPU
->
SetData
(
xData
,
unitNum
);
gGPU
->
SetData
(
gData
,
unitNum
);
yGPU
->
SetZeroAll
();
dedxGPU
->
SetZeroAll
();
dedyGPU
->
SetZeroAll
();
/* convert data type from float to float16 */
xHalfGPU
=
ConvertDataType
(
*
xGPU
,
X_FLOAT16
);
yHalfGPU
=
ConvertDataType
(
*
yGPU
,
X_FLOAT16
);
gHalfGPU
=
ConvertDataType
(
*
gGPU
,
X_FLOAT16
);
dedxHalfGPU
=
ConvertDataType
(
*
dedxGPU
,
X_FLOAT16
);
dedyHalfGPU
=
ConvertDataType
(
*
dedyGPU
,
X_FLOAT16
);
/* call softmax function */
_Softmax
(
&
xHalfGPU
,
&
yHalfGPU
,
1
);
/* call SoftmaxBackward function */
_SoftmaxBackward
(
&
gHalfGPU
,
&
yHalfGPU
,
&
xHalfGPU
,
&
dedyHalfGPU
,
&
dedxHalfGPU
,
NULL
,
1
,
CROSSENTROPY
);
/* convert data type from float to float16 */
_ConvertDataType
(
&
yHalfGPU
,
yGPU
);
_ConvertDataType
(
&
dedxHalfGPU
,
dedxGPU
);
/* check result */
gpuTest
=
yGPU
->
CheckData
(
yAnswer
,
unitNum
,
1e-3
F
)
&&
dedxGPU
->
CheckData
(
dedxAnswer
,
unitNum
,
1e-3
F
);
/* destroy variables */
delete
xGPU
;
delete
yGPU
;
delete
gGPU
;
delete
dedxGPU
;
delete
dedyGPU
;
delete
[]
dimSize
;
return
cpuTest
&&
gpuTest
;
#else
/* destroy variables */
delete
[]
dimSize
;
return
cpuTest
;
#endif // USE_CUDA
}
/* other cases */
/*
TODO!!
...
...
@@ -238,6 +400,26 @@ bool TestSoftmax()
else
XPRINT
(
0
,
stdout
,
">> case 2 passed!
\n
"
);
/* case 3 test */
caseFlag
=
TestSoftmax3
();
if
(
!
caseFlag
)
{
returnFlag
=
false
;
XPRINT
(
0
,
stdout
,
">> case 3 failed!
\n
"
);
}
else
XPRINT
(
0
,
stdout
,
">> case 3 passed!
\n
"
);
/* case 4 test */
caseFlag
=
TestSoftmax4
();
if
(
!
caseFlag
)
{
returnFlag
=
false
;
XPRINT
(
0
,
stdout
,
">> case 4 failed!
\n
"
);
}
else
XPRINT
(
0
,
stdout
,
">> case 4 passed!
\n
"
);
/* other cases test */
/*
TODO!!
...
...
source/tensor/test/TUnsqueeze.cpp
查看文件 @
01747e0d
...
...
@@ -21,6 +21,7 @@
#include "../XList.h"
#include "TUnsqueeze.h"
#include "../core/getandset/ConvertDataType.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
...
...
@@ -153,6 +154,128 @@ bool TestUnsqueeze1()
#endif // USE_CUDA
}
/*
case 2: float16 insert a dimension by copying the blocks for x times (where x is the size of the inerted dimension)
In this case,
(2, 3) -> (2, 2, 3), dim=1, dSize=2
(2, 3) -> (2, 3, 2), dim=2, dSize=2
*/
bool
TestUnsqueeze2
()
{
/* a source tensor of size (2, 3) */
int
sOrder
=
2
;
int
*
sDimSize
=
new
int
[
sOrder
];
sDimSize
[
0
]
=
2
;
sDimSize
[
1
]
=
3
;
int
sUnitNum
=
1
;
for
(
int
i
=
0
;
i
<
sOrder
;
i
++
)
sUnitNum
*=
sDimSize
[
i
];
/* a target tensor of size (2, 2, 3) */
int
tOrder1
=
3
;
int
*
tDimSize1
=
new
int
[
tOrder1
];
tDimSize1
[
0
]
=
2
;
tDimSize1
[
1
]
=
2
;
tDimSize1
[
2
]
=
3
;
int
tUnitNum1
=
1
;
for
(
int
i
=
0
;
i
<
tOrder1
;
i
++
)
tUnitNum1
*=
tDimSize1
[
i
];
/* a target tensor of size (2, 3, 2) */
int
tOrder2
=
3
;
int
*
tDimSize2
=
new
int
[
tOrder2
];
tDimSize2
[
0
]
=
2
;
tDimSize2
[
1
]
=
3
;
tDimSize2
[
2
]
=
2
;
int
tUnitNum2
=
1
;
for
(
int
i
=
0
;
i
<
tOrder2
;
i
++
)
tUnitNum2
*=
tDimSize2
[
i
];
DTYPE
sData
[
2
][
3
]
=
{
{
0.0
F
,
1.0
F
,
2.0
F
},
{
3.0
F
,
4.0
F
,
5.0
F
}
};
DTYPE
answer1
[
2
][
2
][
3
]
=
{
{
{
0.0
F
,
1.0
F
,
2.0
F
},
{
0.0
F
,
1.0
F
,
2.0
F
}
},
{
{
3.0
F
,
4.0
F
,
5.0
F
},
{
3.0
F
,
4.0
F
,
5.0
F
}
}
};
DTYPE
answer2
[
2
][
3
][
2
]
=
{
{
{
0.0
F
,
0.0
F
},
{
1.0
F
,
1.0
F
},
{
2.0
F
,
2.0
F
}
},
{
{
3.0
F
,
3.0
F
},
{
4.0
F
,
4.0
F
},
{
5.0
F
,
5.0
F
}
}
};
/* CPU test */
bool
cpuTest
=
true
;
#ifdef USE_CUDA
/* GPU test */
bool
gpuTest
=
true
;
/* create tensor */
XTensor
*
sGPU
=
NewTensor
(
sOrder
,
sDimSize
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
tGPU1
=
NewTensor
(
tOrder1
,
tDimSize1
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
*
tGPU2
=
NewTensor
(
tOrder2
,
tDimSize2
,
X_FLOAT
,
1.0
F
,
0
);
XTensor
tUserGPU1
;
XTensor
tUserGPU2
;
/* create float16 tensor */
XTensor
sHalfGPU
;
XTensor
tHalfGPU1
;
XTensor
tHalfGPU2
;
XTensor
tUserHalfGPU1
;
XTensor
tUserHalfGPU2
;
/* Initialize variables */
sGPU
->
SetData
(
sData
,
sUnitNum
);
tGPU1
->
SetZeroAll
();
tGPU2
->
SetZeroAll
();
/* convert data type from float to float16 */
sHalfGPU
=
ConvertDataType
(
*
sGPU
,
X_FLOAT16
);
tHalfGPU1
=
ConvertDataType
(
*
tGPU1
,
X_FLOAT16
);
tHalfGPU2
=
ConvertDataType
(
*
tGPU2
,
X_FLOAT16
);
/* call unsqueeze function */
_Unsqueeze
(
&
sHalfGPU
,
&
tHalfGPU1
,
1
,
2
);
_Unsqueeze
(
&
sHalfGPU
,
&
tHalfGPU2
,
2
,
2
);
tUserHalfGPU1
=
Unsqueeze
(
sHalfGPU
,
1
,
2
);
tUserHalfGPU2
=
Unsqueeze
(
sHalfGPU
,
2
,
2
);
/* convert data type from float16 to float */
_ConvertDataType
(
&
tHalfGPU1
,
tGPU1
);
_ConvertDataType
(
&
tHalfGPU2
,
tGPU2
);
tUserGPU1
=
ConvertDataType
(
tUserHalfGPU1
,
X_FLOAT
);
tUserGPU2
=
ConvertDataType
(
tUserHalfGPU2
,
X_FLOAT
);
/* check results */
gpuTest
=
tGPU1
->
CheckData
(
answer1
,
tUnitNum1
)
&&
tUserGPU1
.
CheckData
(
answer1
,
tUnitNum1
)
&&
tGPU2
->
CheckData
(
answer2
,
tUnitNum2
)
&&
tUserGPU2
.
CheckData
(
answer2
,
tUnitNum2
);
/* destroy variables */
delete
sGPU
;
delete
tGPU1
;
delete
tGPU2
;
delete
[]
sDimSize
;
delete
[]
tDimSize1
;
delete
[]
tDimSize2
;
return
cpuTest
&&
gpuTest
;
#else
/* destroy variables */
delete
[]
sDimSize
;
delete
[]
tDimSize1
;
delete
[]
tDimSize2
;
return
cpuTest
;
#endif // USE_CUDA
}
/* other cases */
/*
TODO!!
...
...
@@ -174,6 +297,16 @@ bool TestUnsqueeze()
else
XPRINT
(
0
,
stdout
,
">> case 1 passed!
\n
"
);
/* case 2 test */
caseFlag
=
TestUnsqueeze2
();
if
(
!
caseFlag
)
{
returnFlag
=
false
;
XPRINT
(
0
,
stdout
,
">> case 2 failed!
\n
"
);
}
else
XPRINT
(
0
,
stdout
,
">> case 2 passed!
\n
"
);
/* other cases test */
/*
TODO!!
...
...
source/tensor/test/Test.cpp
查看文件 @
01747e0d
...
...
@@ -76,10 +76,10 @@ bool Test()
//wrong = !TestTan() || wrong;
//wrong = !TestTranspose() || wrong;
//wrong = !TestTopK() || wrong;
//
wrong = !TestUnsqueeze() || wrong;
wrong
=
!
TestUnsqueeze
()
||
wrong
;
//wrong = !TestXMem() || wrong;
//
//
wrong = !TestCrossEntropy() || wrong;
wrong
=
!
TestCrossEntropy
()
||
wrong
;
//wrong = !TestDropout() || wrong;
//wrong = !TestHardTanH() || wrong;
//wrong = !TestIdentity() || wrong;
...
...
@@ -87,7 +87,7 @@ bool Test()
//wrong = !TestLoss() || wrong;
//wrong = !TestRectify() || wrong;
//wrong = !TestSigmoid() || wrong;
//
wrong = !TestSoftmax() || wrong;
wrong
=
!
TestSoftmax
()
||
wrong
;
/* other test */
/*
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论