Commit 7d8fedae by liyinqiao

Roll back some codes.

Find some fp16 bugs during decoding NMT system. We decide to roll back some codes to the last version. These codes need to be reviewed.
parent f451bc79
......@@ -60,4 +60,25 @@ TENSOR_DATA_TYPE GetDataType(const char * typeName)
}
}
/*
Below is for calling CPU BLAS for fast matrix operations
I'm not sure how fast it is. But it seems that other
guys are crazy about this. So I decided to have a try.
*/
/* float -> float16 */
_XINLINE_ unsigned short FloatToFloat16(float f)
{
unsigned int x = *((unsigned int*)&f);
unsigned short h = ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff);
return h;
}
/* float16 -> float */
_XINLINE_ float Float16ToFloat(unsigned short h)
{
float f = float(((h&0x8000)<<16) | (((h&0x7c00)+0x1C000)<<13) | ((h&0x03FF)<<13));
return f;
}
} /* end of the nts (NiuTrans.Tensor) namespace */
......@@ -46,6 +46,10 @@ 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);
#define CheckDataType(a, b) \
{ \
if(GetDataTypeName(a) != GetDataTypeName(a)){ \
......
......@@ -1739,13 +1739,12 @@ void XTensor::Dump(FILE* file, const char* label, const int n, const int beg, co
}
}
else if (dataType == X_FLOAT16) {
float16* f = (float16*)d;
for (int i = beg; i < end; i++) {
float v = f[i].Float();
if (i == beg)
fprintf(file, "%e", v);
for(int i = beg; i < end; i++){
DTYPE f = ((unsigned short*)d)[i];
if(i == beg)
fprintf(file, "%e", f);
else
fprintf(file, " %e", v);
fprintf(file, " %e", f);
}
}
else
......@@ -1805,7 +1804,7 @@ void XTensor::BinaryDump(FILE* file)
break;
}
case X_FLOAT16: {
fwrite(tmp.data, sizeof(float16), unitNum, file);
fwrite(tmp.data, sizeof(unsigned short), unitNum, file);
break;
}
default: {
......@@ -1943,8 +1942,8 @@ void XTensor::BinaryRead(FILE* file, size_t offset)
break;
}
case X_FLOAT16: {
float16* d = new float16[unitNum];
fread(d, sizeof(float16), unitNum, file);
unsigned short* d = new unsigned short[unitNum];
fread(d, sizeof(unsigned short), unitNum, file);
SetData(d, unitNum);
delete[] d;
break;
......
......@@ -91,7 +91,6 @@
#include "sort/TopK.h"
#include "utilities/CheckData.h"
#include "utilities/Float16.h"
#include "utilities/FlushToMem.h"
#include "utilities/SetAscendingOrder.h"
#include "utilities/XMatrixSegment.h"
......
......@@ -24,7 +24,6 @@
#include "ConvertDataType.h"
#include "ConvertDataType.cuh"
#include "../movement/CopyValues.h"
#include "../utilities/Float16.h"
namespace nts { // namespace nts(NiuTrans.Tensor)
......@@ -49,12 +48,12 @@ void ConvertDataType(int devID,
if(typeS == X_FLOAT && typeT == X_FLOAT16){
for(int i = 0; i < size; i++){
((float16*)t)[i] = float16(((float*)s)[i]);
((unsigned short*)t)[i] = FloatToFloat16(((float*)s)[i]);
}
}
else if(typeS == X_FLOAT16 && typeT == X_FLOAT){
for(int i = 0; i < size; i++){
((float*)t)[i] = ((float16*)s)[i].Float();
((float*)t)[i] = Float16ToFloat(((unsigned short*)s)[i]);
}
}
else{
......@@ -95,15 +94,15 @@ void _ConvertDataType(const XTensor * input, XTensor * output)
}
else if (input->dataType == X_FLOAT && output->dataType == X_FLOAT16) {
float* inputData = (float*)input->data;
float16* outputData = (float16*)output->data;
unsigned short* outputData = (unsigned short*)output->data;
for (int i = 0; i < input->unitNum; i++)
outputData[i] = (float16)inputData[i];
outputData[i] = (unsigned short)inputData[i];
}
else if (input->dataType == X_FLOAT16 && output->dataType == X_FLOAT) {
float16* inputData = (float16*)input->data;
unsigned short* inputData = (unsigned short*)input->data;
float* outputData = (float*)output->data;
for (int i = 0; i < input->unitNum; i++)
outputData[i] = inputData[i].Float();
outputData[i] = (float)inputData[i];
}
else
ShowNTErrors("Unsupported data types for conversion!");
......
......@@ -25,7 +25,6 @@
#include "SetData.cuh"
#include "../../XUtility.h"
#include "../movement/CopyValues.h"
#include "../utilities/Float16.h"
#if !defined( WIN32 ) && !defined( _WIN32 )
#include "sys/time.h"
......@@ -435,19 +434,19 @@ void _SetDataRand(XTensor * tensor, DTYPE lower, DTYPE upper)
if(tensor->dataType == X_FLOAT){
float * d = (float*)tensor->data;
for(int i = 0; i < tensor->unitNum; i++){
d[i] = ((float)rand()/RAND_MAX) * variance + lower;
d[i] = variance * ((float)rand()/RAND_MAX) + lower;
}
}
else if (tensor->dataType == X_FLOAT16) {
float16* d = (float16*)tensor->data;
unsigned short* d = (unsigned short*)tensor->data;
for (int i = 0; i < tensor->unitNum; i++) {
d[i] = ((float16)rand() / RAND_MAX) * variance + lower;
d[i] = variance * ((unsigned short)rand() / RAND_MAX) + lower;
}
}
else if(tensor->dataType == X_DOUBLE){
double * d = (double*)tensor->data;
for(int i = 0; i < tensor->unitNum; i++){
d[i] = ((double)rand()/RAND_MAX) * variance+ lower;
d[i] = variance * ((double)rand()/RAND_MAX) + lower;
}
}
else{
......
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2020, Natural Language Processing Lab, Northeastern 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.
*/
/*
* $Creted by: Guan Huhao 2020-02-05
* $Updated by: Xu Chen (email: hello_master1954@163.com) 2020-05-01
*/
#ifndef FLOAT16_H
#define FLOAT16_H
namespace nts { // namespace nts(NiuTrans.Tensor)
struct float16
{
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
*/
unsigned short data : 10;
unsigned short exp : 5;
unsigned short sign : 1;
// mask for calculate the highest 1
static unsigned int mask[32];
static unsigned int pow2[32];
//int FindHighOne(const int &num, int &l, int &r);
int AbsCompare(const float16 & a,const float16 & b);
public:
float16 SetOverFlow();
// judge whether overflow
int IsOverlFlow() const;
/* constructor by (sign, exp, data)
similar to ieee 32 floating point
sign: 1bit
exp: 5bit
data: 10bit */
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 float num
float16(const float& data);
// constructor by other datatype
template<class T> float16(const T& data);
void Dump();
// convert float16 to float and return
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 and double. */
float16 operator = (const float& data);
float16 operator = (const float16& data);
template<class T> float16 operator = (const T& data);
// overload operator (less than) 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) 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) a + b
float16 operator + (const float16& data);
template<class T> float16 operator + (const T& data);
// overload operator += (add) a += b
float16 operator += (const float16& data);
template<class T> float16 operator += (const T& data);
// overload operator -(negetive) -a
float16 operator - ();
// overload operator - (substraction) a - b
float16 operator - (const float16& data);
template<class T> float16 operator - (const T& data);
// overload operator -= (substraction) a -= b
float16 operator -= (const float16& data);
template<class T> float16 operator -= (const T& data);
// overload operator * (multiple) a * b
float16 operator * (const float16& data);
template<class T> float16 operator * (const T& data);
// overload operator *= (multiple) a *= b
float16 operator *= (const float16& data);
template<class T> float16 operator *= (const T& data);
// overload operator / (division) a / b
float16 GetInverse() const;
float16 operator / (const float16& data);
template<class T> float16 operator / (const T& data);
// overload operator /= (division) a /= b
float16 operator /= (const float16& data);
template<class T> float16 operator /= (const T& data);
};
} // namespace nts(NiuTrans.Tensor)
#endif /* FLOAT16_H */
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论