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
魏冰浩
Tensor.LowPrecision
Commits
3ad0e638
Commit
3ad0e638
authored
Jul 16, 2019
by
linye
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
int8 matrix bug fixed
parent
b5c4aa4e
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
27 行增加
和
169 行删除
+27
-169
source/tensor/core/arithmetic/XTensorBLAS.cu
+25
-53
source/tensor/test/TMatrixMul.cpp
+0
-114
source/tensor/test/Test.cpp
+2
-2
没有找到文件。
source/tensor/core/arithmetic/XTensorBLAS.cu
查看文件 @
3ad0e638
...
...
@@ -17,7 +17,7 @@
/*
* $Created by: XIAO Tong (email: xiaotong@mail.neu.edu.cn) 2018-04-24
* $Update by: Lin Ye (email: linye2015@outlook.com) 2019-07-
0
6 float16/int8 added
* $Update by: Lin Ye (email: linye2015@outlook.com) 2019-07-
1
6 float16/int8 added
*/
#include "../../XUtility.h"
...
...
@@ -94,25 +94,23 @@ void _CudaBLASMatrixMUL(cublasHandle_t * handle,
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else if (dataTypeA == X_INT8 && dataTypeB == X_INT8 && dataTypeC == X_FLOAT) {
int alpha2 = (int)alpha;
int beta2 = (int)beta;
/*
CUDA requires that the dimension of two tensor( lda, ldb ) should be multiples of 4.
details in https://devtalk.nvidia.com/default/topic/999101/about-cublasgemm-int8-support/
*/
if (mb % 4 != 0 || ma % 4 != 0) {
ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
return;
}
//
if (mb % 4 != 0 || ma % 4 != 0) {
//
ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
//
return;
//
}
cublasSetMathMode(*handle, CUBLAS_TENSOR_OP_MATH);
if (transposedA == X_NOTRANS && transposedB == X_NOTRANS)
cublasGemmEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_NOTRANS)
cublasGemmEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_NOTRANS && transposedB == X_TRANS)
cublasGemmEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_TRANS)
cublasGemmEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else {
...
...
@@ -183,25 +181,23 @@ void _CudaBLASMatrixMULBatched(cublasHandle_t * handle,
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else if (dataTypeA == X_INT8 && dataTypeB == X_INT8 && dataTypeC == X_FLOAT) {
int alpha2 = (int)alpha;
int beta2 = (int)beta;
/*
CUDA requires that the dimension of two tensor( lda, ldb ) should be multiples of 4.
details in https://devtalk.nvidia.com/default/topic/999101/about-cublasgemm-int8-support/
*/
if (mb % 4 != 0 || ma % 4 != 0) {
ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
return;
}
//
if (mb % 4 != 0 || ma % 4 != 0) {
//
ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
//
return;
//
}
cublasSetMathMode(*handle, CUBLAS_TENSOR_OP_MATH);
if (transposedA == X_NOTRANS && transposedB == X_NOTRANS)
cublasGemmBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_NOTRANS)
cublasGemmBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_NOTRANS && transposedB == X_TRANS)
cublasGemmBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_TRANS)
cublasGemmBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, (__int8*)&beta2
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, a, CUDA_R_8I, ma, &beta
, c, CUDA_R_32F, mc, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else {
...
...
@@ -270,47 +266,23 @@ void _CudaBLASMatrixMULBatchedStrided(cublasHandle_t * handle,
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else if (dataTypeA == X_INT8 && dataTypeB == X_INT8 && dataTypeC == X_FLOAT) {
int alpha2 = (int)alpha;
int beta2 = (int)beta;
/*
CUDA requires that the dimension of two tensor( lda, ldb ) should be multiples of 4.
details in https://devtalk.nvidia.com/default/topic/999101/about-cublasgemm-int8-support/
*/
if (mb % 4 != 0 || ma % 4 != 0) {
ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
return;
}
cublasSetMathMode(*handle, CUBLAS_TENSOR_OP_MATH);
if (transposedA == X_NOTRANS && transposedB == X_NOTRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma, (__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_NOTRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma, (__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_NOTRANS && transposedB == X_TRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma, (__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_TRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma, (__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else if (dataTypeA == X_INT8 && dataTypeB == X_INT8 && dataTypeC == X_INT) {
int alpha2 = (int)alpha;
int beta2 = (int)beta;
/*
CUDA requires that the dimension of two tensor( lda, ldb ) should be multiples of 4.
details in https://devtalk.nvidia.com/default/topic/999101/about-cublasgemm-int8-support/
*/
if (mb % 4 != 0 || ma % 4 != 0) {
ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
return;
}
//if (mb % 4 != 0 || ma % 4 != 0) {
// ShowNTErrors("mb, ma( lda, ldb ) should be multiples of 4!");
// return;
//}
cublasSetMathMode(*handle, CUBLAS_TENSOR_OP_MATH);
if (transposedA == X_NOTRANS && transposedB == X_NOTRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_C_32I, mc, strideC, count, CUDA_R_32I
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_N, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, &beta, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_NOTRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_C_32I, mc, strideC, count, CUDA_R_32I
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_N, CUBLAS_OP_T, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, &beta, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_NOTRANS && transposedB == X_TRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_C_32I, mc, strideC, count, CUDA_R_32I
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_N, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, &beta, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
else if (transposedA == X_TRANS && transposedB == X_TRANS)
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma,
(__int8*)&alpha2, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, (__int8*)&beta2, c, CUDA_C_32I, mc, strideC, count, CUDA_R_32I
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasGemmStridedBatchedEx(*handle, CUBLAS_OP_T, CUBLAS_OP_T, mc, nc, ma,
&alpha, b, CUDA_R_8I, mb, strideB, a, CUDA_R_8I, ma, strideA, &beta, c, CUDA_R_32F, mc, strideC, count, CUDA_R_32F
, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
cublasSetMathMode(*handle, CUBLAS_DEFAULT_MATH);
}
else {
...
...
source/tensor/test/TMatrixMul.cpp
查看文件 @
3ad0e638
...
...
@@ -807,111 +807,6 @@ bool TestMatrixMul7()
}
/*
case 8: int8 matrix multiplication.
In this case, int8 a=(2, 3), int8 b=(3, 2) -> int32 c=(2, 2),
transposedA=X_NOTRANS, transposedB=X_NOTRANS.
*/
bool
TestMatrixMul8
()
{
/* a source tensor of size (2, 3) */
int
sOrder1
=
2
;
int
*
sDimSize1
=
new
int
[
sOrder1
];
sDimSize1
[
0
]
=
2
;
sDimSize1
[
1
]
=
3
;
int
sUnitNum1
=
1
;
for
(
int
i
=
0
;
i
<
sOrder1
;
i
++
)
sUnitNum1
*=
sDimSize1
[
i
];
/* a source tensor of size (3, 2) */
int
sOrder2
=
2
;
int
*
sDimSize2
=
new
int
[
sOrder2
];
sDimSize2
[
0
]
=
3
;
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
][
3
]
=
{
{
1
,
2
,
3
},
{
-
4
,
5
,
6
}
};
DTYPE
sData2
[
3
][
2
]
=
{
{
0
,
-
1
},
{
1
,
2
},
{
2
,
1
}
};
DTYPE
answer
[
2
][
2
]
=
{
{
8
,
6
},
{
17
,
20
}
};
/* 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
*
intTGPU
=
NewTensor
(
tOrder
,
tDimSize
,
X_INT
,
1.0
F
,
0
);
XTensor
tUserGPU
;
XTensor
intTUserGPU
;
/* create int8 tensors */
XTensor
int8SGPU1
;
XTensor
int8SGPU2
;
/* Initialize variables */
sGPU1
->
SetData
(
sData1
,
sUnitNum1
);
sGPU2
->
SetData
(
sData2
,
sUnitNum2
);
tGPU
->
SetZeroAll
();
/* convert data type from float to int8 */
int8SGPU1
=
ConvertDataType
(
*
sGPU1
,
X_INT8
);
int8SGPU2
=
ConvertDataType
(
*
sGPU2
,
X_INT8
);
/* call MatrixMul function */
_MatrixMul
(
&
int8SGPU1
,
X_NOTRANS
,
&
int8SGPU2
,
X_NOTRANS
,
intTGPU
);
intTUserGPU
=
MatrixMul
(
int8SGPU1
,
X_NOTRANS
,
int8SGPU2
,
X_NOTRANS
,
X_INT
);
/* convert data type from int to float32 */
_ConvertDataType
(
intTGPU
,
tGPU
);
tUserGPU
=
ConvertDataType
(
intTUserGPU
,
X_FLOAT
);
/* check results */
gpuTest
=
tGPU
->
CheckData
(
answer
,
tUnitNum
)
&&
tUserGPU
.
CheckData
(
answer
,
tUnitNum
);
/* destroy variables */
delete
sGPU1
;
delete
sGPU2
;
delete
tGPU
;
delete
intTGPU
;
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!!
...
...
@@ -987,15 +882,6 @@ bool TestMatrixMul()
else
XPRINT
(
0
,
stdout
,
">> case 7 passed!
\n
"
);
/* case 8 test */
caseFlag
=
TestMatrixMul8
();
if
(
!
caseFlag
)
{
returnFlag
=
false
;
XPRINT
(
0
,
stdout
,
">> case 8 failed!
\n
"
);
}
else
XPRINT
(
0
,
stdout
,
">> case 8 passed!
\n
"
);
/* other cases test */
/*
TODO!!
...
...
source/tensor/test/Test.cpp
查看文件 @
3ad0e638
...
...
@@ -39,11 +39,11 @@ bool Test()
//wrong = !TestCopyIndexed() || wrong;
//wrong = !TestCopyValues() || wrong;
//wrong = !TestDiv() || wrong;
wrong
=
!
TestDivDim
()
||
wrong
;
//
wrong = !TestDivDim() || wrong;
//wrong = !TestExp() || wrong;
//wrong = !TestGather() || wrong;
//wrong = !TestLog() || wrong;
//
wrong = !TestMatrixMul() || wrong;
wrong
=
!
TestMatrixMul
()
||
wrong
;
//wrong = !TestMatrixMul2D() || wrong;
//wrong = !TestMatrixMul2DParallel() || wrong;
//wrong = !TestMatrixMulBatched() || wrong;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论