Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
8
Issues
8
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
NiuTrans
NiuTrans.Tensor
Commits
e193b1c2
Commit
e193b1c2
authored
Mar 25, 2020
by
xuchen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the CPU float16 datatype
parent
38bff350
全部展开
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
139 行增加
和
5 行删除
+139
-5
source/tensor/XDataType.h
+2
-0
source/tensor/core/getandset/ConvertDataType.cpp
+12
-0
source/tensor/core/utilities/Float16.cpp
+0
-0
source/tensor/core/utilities/Float16.h
+121
-0
source/tensor/test/TConvertDataType.cpp
+4
-5
没有找到文件。
source/tensor/XDataType.h
查看文件 @
e193b1c2
...
...
@@ -25,6 +25,7 @@
#define __XDATATYPE_H__
#include "XGlobal.h"
#include "core/utilities/Float16.h"
/* the nts (NiuTrans.Tensor) namespace */
namespace
nts
{
...
...
@@ -46,6 +47,7 @@ enum MATRIX_TRANS_TYPE{X_TRANS, X_NOTRANS};
extern
const
char
*
GetDataTypeName
(
TENSOR_DATA_TYPE
type
);
extern
TENSOR_DATA_TYPE
GetDataType
(
const
char
*
typeName
);
/* data conversion (for lower precision computation) */
unsigned
short
FloatToFloat16
(
float
f
);
float
Float16ToFloat
(
unsigned
short
h
);
...
...
source/tensor/core/getandset/ConvertDataType.cpp
查看文件 @
e193b1c2
...
...
@@ -92,6 +92,18 @@ void _ConvertDataType(const XTensor * input, XTensor * output)
for
(
int
i
=
0
;
i
<
input
->
unitNum
;
i
++
)
outputData
[
i
]
=
(
float
)
inputData
[
i
];
}
else
if
(
input
->
dataType
==
X_FLOAT
&&
output
->
dataType
==
X_FLOAT16
)
{
float
*
inputData
=
(
float
*
)
input
->
data
;
float16
*
outputData
=
(
float16
*
)
output
->
data
;
for
(
int
i
=
0
;
i
<
input
->
unitNum
;
i
++
)
outputData
[
i
]
=
(
float16
)
inputData
[
i
];
}
else
if
(
input
->
dataType
==
X_FLOAT16
&&
output
->
dataType
==
X_FLOAT
)
{
float16
*
inputData
=
(
float16
*
)
input
->
data
;
float
*
outputData
=
(
float
*
)
output
->
data
;
for
(
int
i
=
0
;
i
<
input
->
unitNum
;
i
++
)
outputData
[
i
]
=
inputData
[
i
].
Float
();
}
else
ShowNTErrors
(
"Unsupported data types for conversion!"
);
}
...
...
source/tensor/core/utilities/Float16.cpp
0 → 100644
查看文件 @
e193b1c2
差异被折叠。
点击展开。
source/tensor/core/utilities/Float16.h
0 → 100644
查看文件 @
e193b1c2
//
// float16.h
// 16bit
//
// Created by 管胡昊 on 2020/2/5.
// Copyright © 2020 管胡昊. All rights reserved.
//
#ifndef FLOAT16_H
#define FLOAT16_H
struct
float16
{
//private member variable
private:
/*
sign is the sign bit 1 means negative, 0 means positive
exp is the exponent with 16 offset
data is the data,similar to ieee-754,the highest is default 1 and ignored
*/
// mask for calculate the highest 1
static
unsigned
int
mask
[
32
];
static
unsigned
int
pow2
[
32
];
// private function
int
FindHighOne
(
const
int
&
num
,
int
&
l
,
int
&
r
);
int
AbsCompare
(
const
float16
&
a
,
const
float16
&
b
);
public
:
unsigned
short
data
:
10
;
unsigned
short
exp
:
5
;
unsigned
short
sign
:
1
;
float16
SetOverFlow
();
// judge whether overflow
int
IsOverlFlow
()
const
;
/* constructor by sign, exp, data
sign:1bit exp:5bit data:10bit similar to ieee 32 floating point */
float16
(
const
int
&
s
,
const
int
&
e
,
const
int
&
d
);
/* default constructor
This initializes the 16bit floating point to 0. */
float16
();
// constructor by a 32-bit floating point
float16
(
const
float
&
data
);
template
<
class
T
>
float16
(
const
T
&
data
);
// constructor by other datatype
//template<class T> float16(const T &data);
// change float16 to flaot as you can see the result is a 32-bit floating point
float
Float
();
/* assignment function and tempalte function
float assignment function is the basic function
template assignment function is force change other datetype to float
then call the float assignment function
template assignment function now support int, double */
float16
operator
=
(
const
float16
&
data
);
float16
operator
=
(
const
float
&
data
);
template
<
class
T
>
float16
operator
=
(
const
T
&
data
);
// overload operator (less than) eg. a<b
int
operator
<
(
const
float16
&
data
);
template
<
class
T
>
int
operator
<
(
const
T
&
data
);
// overload opertator <= (less or equal than) a<=b
int
operator
<=
(
const
float16
&
data
);
template
<
class
T
>
int
operator
<=
(
const
T
&
data
);
// overload operator (greater than) eg. a>b
int
operator
>
(
const
float16
&
data
);
template
<
class
T
>
int
operator
>
(
const
T
&
data
);
//overload opertator <= (greater or equal than) a>=b
int
operator
>=
(
const
float16
&
data
);
template
<
class
T
>
int
operator
>=
(
const
T
&
data
);
// overload operator + (add) eg. a+b
float16
operator
+
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
+
(
const
T
&
data
);
// overload operator += (add) eg. a+=b
float16
operator
+=
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
+=
(
const
T
&
data
);
// overload operator -(negetive) eg. -a
float16
operator
-
();
// overload operator - (substraction) eg. a-b
float16
operator
-
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
-
(
const
T
&
data
);
// overload operator -= (substraction) eg. a-=b
float16
operator
-=
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
-=
(
const
T
&
data
);
// overload operator * (multiple) eg. a*b
float16
operator
*
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
*
(
const
T
&
data
);
// overload operator *= (multiple) eg. a*=b
float16
operator
*=
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
*=
(
const
T
&
data
);
// overload operator / (division) eg. a/b
float16
GetInverse
()
const
;
float16
operator
/
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
/
(
const
T
&
data
);
// overload operator /= (division) eg. a/=b
float16
operator
/=
(
const
float16
&
data
);
template
<
class
T
>
float16
operator
/=
(
const
T
&
data
);
};
#endif
/* FLOAT16_H */
source/tensor/test/TConvertDataType.cpp
查看文件 @
e193b1c2
...
...
@@ -28,7 +28,6 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
/*
case 1: test ConvertDataType function.
In this case, the flaot32 data type is converted to int32 data type.
*/
bool
TestConvertDataType1
()
{
...
...
@@ -234,11 +233,11 @@ bool TestConvertDataType3()
a
->
SetData
(
data1
,
unitNum1
);
/* call ConvertDataType function (We have not implemented this yet...) */
//
_ConvertDataType(a, b);
//
_ConvertDataType(b, c);
_ConvertDataType
(
a
,
b
);
_ConvertDataType
(
b
,
c
);
/* check results */
//
cpuTest = _CheckData(a, data1, unitNum1, 1e-4F);
cpuTest
=
_CheckData
(
a
,
data1
,
unitNum1
,
1e-4
F
);
#ifdef USE_CUDA
/* GPU test */
...
...
@@ -264,7 +263,7 @@ bool TestConvertDataType3()
_ConvertDataType
(
eGPU
,
fGPU
);
/* check results */
gpuTest
=
_CheckData
(
fGPU
,
answer
,
unitNum3
,
1e-4
F
);
//
gpuTest = _CheckData(fGPU, answer, unitNum3, 1e-4F);
/* destroy variables */
delete
a
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论