Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
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
杨迪
NiuTrans.Tensor
Commits
137df5d9
Commit
137df5d9
authored
Mar 12, 2020
by
liyinqiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge with HU Chi branch.
1. Offer global setting for enableGrad. 2. Update XList. 3. Minor error fixed.
parent
b3dfbdcd
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
260 行增加
和
280 行删除
+260
-280
source/tensor/XCall.h
+20
-17
source/tensor/XList.cpp
+2
-22
source/tensor/XList.h
+11
-14
source/tensor/XTensor.cpp
+227
-227
没有找到文件。
source/tensor/XCall.h
查看文件 @
137df5d9
...
@@ -26,6 +26,9 @@
...
@@ -26,6 +26,9 @@
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/* default settings */
#define X_ENABLE_GRAD false
/*
/*
* we define the "new and delete" functions below
* we define the "new and delete" functions below
*/
*/
...
@@ -38,13 +41,13 @@ void InitTensorV2(XTensor * tensor,
...
@@ -38,13 +41,13 @@ void InitTensorV2(XTensor * tensor,
/* initialize a dense XTensor */
/* initialize a dense XTensor */
void
InitTensor
(
XTensor
*
tensor
,
void
InitTensor
(
XTensor
*
tensor
,
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a scalar V2 */
/* initialize a scalar V2 */
void
InitTensor0DV2
(
XTensor
*
tensor
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
void
InitTensor0DV2
(
XTensor
*
tensor
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
/* initialize a scalar */
/* initialize a scalar */
void
InitTensor0D
(
XTensor
*
tensor
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
void
InitTensor0D
(
XTensor
*
tensor
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a dense vector V2 */
/* initialize a dense vector V2 */
void
InitTensor1DV2
(
XTensor
*
tensor
,
const
int
num
,
void
InitTensor1DV2
(
XTensor
*
tensor
,
const
int
num
,
...
@@ -52,7 +55,7 @@ void InitTensor1DV2(XTensor * tensor, const int num,
...
@@ -52,7 +55,7 @@ void InitTensor1DV2(XTensor * tensor, const int num,
/* initialize a dense vector */
/* initialize a dense vector */
void
InitTensor1D
(
XTensor
*
tensor
,
const
int
num
,
void
InitTensor1D
(
XTensor
*
tensor
,
const
int
num
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a dense matrix V2 */
/* initialize a dense matrix V2 */
void
InitTensor2DV2
(
XTensor
*
tensor
,
const
int
rowNum
,
const
int
colNum
,
void
InitTensor2DV2
(
XTensor
*
tensor
,
const
int
rowNum
,
const
int
colNum
,
...
@@ -60,7 +63,7 @@ void InitTensor2DV2(XTensor * tensor, const int rowNum, const int colNum,
...
@@ -60,7 +63,7 @@ void InitTensor2DV2(XTensor * tensor, const int rowNum, const int colNum,
/* initialize a dense matrix */
/* initialize a dense matrix */
void
InitTensor2D
(
XTensor
*
tensor
,
const
int
rowNum
,
const
int
colNum
,
void
InitTensor2D
(
XTensor
*
tensor
,
const
int
rowNum
,
const
int
colNum
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a dense 3d tensor V2 */
/* initialize a dense 3d tensor V2 */
void
InitTensor3DV2
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
void
InitTensor3DV2
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
...
@@ -68,7 +71,7 @@ void InitTensor3DV2(XTensor * tensor, const int d0, const int d1, const int d2,
...
@@ -68,7 +71,7 @@ void InitTensor3DV2(XTensor * tensor, const int d0, const int d1, const int d2,
/* initialize a dense 3d tensor */
/* initialize a dense 3d tensor */
void
InitTensor3D
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
void
InitTensor3D
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a dense 4d tensor V2 */
/* initialize a dense 4d tensor V2 */
void
InitTensor4DV2
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
void
InitTensor4DV2
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
...
@@ -76,7 +79,7 @@ void InitTensor4DV2(XTensor * tensor, const int d0, const int d1, const int d2,
...
@@ -76,7 +79,7 @@ void InitTensor4DV2(XTensor * tensor, const int d0, const int d1, const int d2,
/* initialize a dense 4d tensor */
/* initialize a dense 4d tensor */
void
InitTensor4D
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
void
InitTensor4D
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a dense 5d tensor V2 */
/* initialize a dense 5d tensor V2 */
void
InitTensor5DV2
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
void
InitTensor5DV2
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
...
@@ -84,7 +87,7 @@ void InitTensor5DV2(XTensor * tensor, const int d0, const int d1, const int d2,
...
@@ -84,7 +87,7 @@ void InitTensor5DV2(XTensor * tensor, const int d0, const int d1, const int d2,
/* initialize a dense 5d tensor */
/* initialize a dense 5d tensor */
void
InitTensor5D
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
void
InitTensor5D
(
XTensor
*
tensor
,
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* initialize a tensor with a reference tensor V2 */
/* initialize a tensor with a reference tensor V2 */
void
InitTensorV2
(
XTensor
*
tensor
,
const
XTensor
*
reference
);
void
InitTensorV2
(
XTensor
*
tensor
,
const
XTensor
*
reference
);
...
@@ -104,7 +107,7 @@ XTensor * NewTensorV2(const int myOrder, const int * myDimSize, const TENSOR_DAT
...
@@ -104,7 +107,7 @@ XTensor * NewTensorV2(const int myOrder, const int * myDimSize, const TENSOR_DAT
/* generate a dense XTensor */
/* generate a dense XTensor */
XTensor
*
NewTensor
(
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
XTensor
*
NewTensor
(
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a XTensor which allocates data on the buffer V2 */
/* generate a XTensor which allocates data on the buffer V2 */
XTensor
*
NewTensorBufV2
(
const
int
myOrder
,
const
int
*
myDimSize
,
XTensor
*
NewTensorBufV2
(
const
int
myOrder
,
const
int
*
myDimSize
,
...
@@ -113,26 +116,26 @@ XTensor * NewTensorBufV2(const int myOrder, const int * myDimSize,
...
@@ -113,26 +116,26 @@ XTensor * NewTensorBufV2(const int myOrder, const int * myDimSize,
/* generate a dense XTensor which allocates data on the buffer */
/* generate a dense XTensor which allocates data on the buffer */
XTensor
*
NewTensorBuf
(
const
int
myOrder
,
const
int
*
myDimSize
,
XTensor
*
NewTensorBuf
(
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a XTensor which allocates data on the buffer V2 */
/* generate a XTensor which allocates data on the buffer V2 */
XTensor
*
NewTensorBufV2
(
const
XTensor
*
reference
,
int
devID
,
XMem
*
myMem
);
XTensor
*
NewTensorBufV2
(
const
XTensor
*
reference
,
int
devID
,
XMem
*
myMem
);
/* generate a XTensor which allocates data on the buffer */
/* generate a XTensor which allocates data on the buffer */
XTensor
*
NewTensorBuf
(
const
XTensor
*
reference
,
int
devID
,
const
bool
isEnableGrad
=
true
);
XTensor
*
NewTensorBuf
(
const
XTensor
*
reference
,
int
devID
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a scalar V2 */
/* generate a scalar V2 */
XTensor
*
NewTensor0DV2
(
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
XTensor
*
NewTensor0DV2
(
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
/* generate a scalar */
/* generate a scalar */
XTensor
*
NewTensor0D
(
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
XTensor
*
NewTensor0D
(
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a dense vector V2 */
/* generate a dense vector V2 */
XTensor
*
NewTensor1DV2
(
const
int
num
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
XTensor
*
NewTensor1DV2
(
const
int
num
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
XMem
*
myMem
=
NULL
);
/* generate a dense vector */
/* generate a dense vector */
XTensor
*
NewTensor1D
(
const
int
num
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
XTensor
*
NewTensor1D
(
const
int
num
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a dense matrix V2 */
/* generate a dense matrix V2 */
XTensor
*
NewTensor2DV2
(
const
int
rowNum
,
const
int
colNum
,
XTensor
*
NewTensor2DV2
(
const
int
rowNum
,
const
int
colNum
,
...
@@ -142,7 +145,7 @@ XTensor * NewTensor2DV2(const int rowNum, const int colNum,
...
@@ -142,7 +145,7 @@ XTensor * NewTensor2DV2(const int rowNum, const int colNum,
/* generate a dense matrix */
/* generate a dense matrix */
XTensor
*
NewTensor2D
(
const
int
rowNum
,
const
int
colNum
,
XTensor
*
NewTensor2D
(
const
int
rowNum
,
const
int
colNum
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a dense 3d tensor V2 */
/* generate a dense 3d tensor V2 */
XTensor
*
NewTensor3DV2
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
XTensor
*
NewTensor3DV2
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
...
@@ -152,7 +155,7 @@ XTensor * NewTensor3DV2(const int d0, const int d1, const int d2,
...
@@ -152,7 +155,7 @@ XTensor * NewTensor3DV2(const int d0, const int d1, const int d2,
/* generate a dense 3d tensor */
/* generate a dense 3d tensor */
XTensor
*
NewTensor3D
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
XTensor
*
NewTensor3D
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a dense 4d tensor V2 */
/* generate a dense 4d tensor V2 */
XTensor
*
NewTensor4DV2
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
XTensor
*
NewTensor4DV2
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
...
@@ -162,7 +165,7 @@ XTensor * NewTensor4DV2(const int d0, const int d1, const int d2, const int d3,
...
@@ -162,7 +165,7 @@ XTensor * NewTensor4DV2(const int d0, const int d1, const int d2, const int d3,
/* generate a dense 4d tensor */
/* generate a dense 4d tensor */
XTensor
*
NewTensor4D
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
XTensor
*
NewTensor4D
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a dense 5d tensor V2 */
/* generate a dense 5d tensor V2 */
XTensor
*
NewTensor5DV2
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
XTensor
*
NewTensor5DV2
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
...
@@ -172,10 +175,10 @@ XTensor * NewTensor5DV2(const int d0, const int d1, const int d2, const int d3,
...
@@ -172,10 +175,10 @@ XTensor * NewTensor5DV2(const int d0, const int d1, const int d2, const int d3,
/* generate a dense 5d tensor */
/* generate a dense 5d tensor */
XTensor
*
NewTensor5D
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
XTensor
*
NewTensor5D
(
const
int
d0
,
const
int
d1
,
const
int
d2
,
const
int
d3
,
const
int
d4
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a dense vector by range */
/* generate a dense vector by range */
XTensor
*
NewTensorRange
(
int
lower
,
int
upper
,
int
step
,
const
TENSOR_DATA_TYPE
myDataType
=
X_INT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
XTensor
*
NewTensorRange
(
int
lower
,
int
upper
,
int
step
,
const
TENSOR_DATA_TYPE
myDataType
=
X_INT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
X_ENABLE_GRAD
);
/* generate a copy of XTensor (with a reference to a given tensor) */
/* generate a copy of XTensor (with a reference to a given tensor) */
XTensor
*
NewTensor
(
const
XTensor
*
a
,
bool
isFilledData
=
true
);
XTensor
*
NewTensor
(
const
XTensor
*
a
,
bool
isFilledData
=
true
);
...
...
source/tensor/XList.cpp
查看文件 @
137df5d9
...
@@ -249,26 +249,6 @@ inline int TensorListBase<T>::FindFirst(const T& item)
...
@@ -249,26 +249,6 @@ inline int TensorListBase<T>::FindFirst(const T& item)
return
-
1
;
return
-
1
;
}
}
template
<>
inline
int
TensorListBase
<
Example
>::
FindFirst
(
const
Example
&
item
)
{
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
if
(
item
.
id
==
items
[
i
].
id
)
return
i
;
}
return
-
1
;
}
template
<>
inline
int
TensorListBase
<
Result
>::
FindFirst
(
const
Result
&
item
)
{
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
if
(
item
.
id
==
items
[
i
].
id
)
return
i
;
}
return
-
1
;
}
/* clear the data array */
/* clear the data array */
template
<
typename
T
>
template
<
typename
T
>
void
TensorListBase
<
T
>::
Clear
()
void
TensorListBase
<
T
>::
Clear
()
...
@@ -383,8 +363,7 @@ template struct TensorListBase<long>;
...
@@ -383,8 +363,7 @@ template struct TensorListBase<long>;
template
struct
TensorListBase
<
float
>
;
template
struct
TensorListBase
<
float
>
;
template
struct
TensorListBase
<
short
>
;
template
struct
TensorListBase
<
short
>
;
template
struct
TensorListBase
<
XTensor
*>
;
template
struct
TensorListBase
<
XTensor
*>
;
template
struct
TensorListBase
<
Result
>
;
template
struct
TensorListBase
<
uint64_t
>
;
template
struct
TensorListBase
<
Example
>
;
template
struct
TensorListBase
<
void
*>
;
template
struct
TensorListBase
<
void
*>
;
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
\ No newline at end of file
source/tensor/XList.h
查看文件 @
137df5d9
...
@@ -26,6 +26,8 @@
...
@@ -26,6 +26,8 @@
#include "XMem.h"
#include "XMem.h"
#include "XGlobal.h"
#include "XGlobal.h"
#include <cstdint>
#ifndef __TensorList_H__
#ifndef __TensorList_H__
#define __TensorList_H__
#define __TensorList_H__
...
@@ -118,7 +120,14 @@ public:
...
@@ -118,7 +120,14 @@ public:
void
Shuffle
(
int
nround
=
10
,
int
beg
=
-
1
,
int
len
=
0
);
void
Shuffle
(
int
nround
=
10
,
int
beg
=
-
1
,
int
len
=
0
);
/* short */
/* short */
T
&
operator
[]
(
int
i
)
{
return
GetItem
(
i
);
};
T
&
operator
[]
(
int
i
)
{
CheckNTErrors
(
i
>=
-
count
&&
i
<
count
,
"Index of a list item is out of scope!"
);
CheckNTErrors
(
count
>
0
,
"Cannt index the item in an empty list!"
);
if
(
i
<
0
)
return
items
[
count
+
i
];
else
return
items
[
i
];
};
T
&
Get
(
int
i
)
{
return
GetItem
(
i
);
};
T
&
Get
(
int
i
)
{
return
GetItem
(
i
);
};
void
Set
(
int
i
,
T
item
)
{
SetItem
(
i
,
item
);
};
void
Set
(
int
i
,
T
item
)
{
SetItem
(
i
,
item
);
};
};
};
...
@@ -132,19 +141,7 @@ typedef TensorListBase<char*> StrList;
...
@@ -132,19 +141,7 @@ typedef TensorListBase<char*> StrList;
typedef
TensorListBase
<
long
>
LongList
;
typedef
TensorListBase
<
long
>
LongList
;
typedef
TensorListBase
<
float
>
FloatList
;
typedef
TensorListBase
<
float
>
FloatList
;
typedef
TensorListBase
<
short
>
ShortList
;
typedef
TensorListBase
<
short
>
ShortList
;
typedef
TensorListBase
<
uint64_t
>
UInt64List
;
struct
Example
{
int
id
;
IntList
data
;
};
struct
Result
{
int
id
;
IntList
data
;
};
typedef
TensorListBase
<
Result
>
ResultList
;
typedef
TensorListBase
<
Example
>
ExampleList
;
typedef
TensorListBase
<
XTensor
*>
TensorList
;
typedef
TensorListBase
<
XTensor
*>
TensorList
;
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
...
...
source/tensor/XTensor.cpp
查看文件 @
137df5d9
...
@@ -64,7 +64,7 @@
...
@@ -64,7 +64,7 @@
#endif
#endif
/* the nts (NiuTrans.Tensor) namespace */
/* the nts (NiuTrans.Tensor) namespace */
namespace
nts
{
namespace
nts
{
int
tensorIDGlobal
=
0
;
int
tensorIDGlobal
=
0
;
MUTEX_HANDLE
tensorMutex
;
MUTEX_HANDLE
tensorMutex
;
...
@@ -73,11 +73,11 @@ XTensor NULLTensor;
...
@@ -73,11 +73,11 @@ XTensor NULLTensor;
/* generate a tensor id */
/* generate a tensor id */
int
MakeTensorID
()
int
MakeTensorID
()
{
{
if
(
tensorIDGlobal
==
0
)
if
(
tensorIDGlobal
==
0
)
MUTEX_INIT
(
tensorMutex
);
MUTEX_INIT
(
tensorMutex
);
MUTEX_LOCK
(
tensorMutex
);
MUTEX_LOCK
(
tensorMutex
);
int
id
=
tensorIDGlobal
++
;
int
id
=
tensorIDGlobal
++
;
MUTEX_UNLOCK
(
tensorMutex
);
MUTEX_UNLOCK
(
tensorMutex
);
return
id
;
return
id
;
...
@@ -91,13 +91,13 @@ XTensor::XTensor()
...
@@ -91,13 +91,13 @@ XTensor::XTensor()
id
=
MakeTensorID
();
id
=
MakeTensorID
();
isDefaultDType
=
true
;
isDefaultDType
=
true
;
isInGlobalMem
=
false
;
isInGlobalMem
=
false
;
isInit
=
false
;
isInit
=
false
;
isTmp
=
false
;
isTmp
=
false
;
}
}
/* constructor */
/* constructor */
XTensor
::
XTensor
(
const
XTensor
*
reference
)
XTensor
::
XTensor
(
const
XTensor
*
reference
)
{
{
Init
();
Init
();
SetDataPointer
();
SetDataPointer
();
...
@@ -112,7 +112,7 @@ constructor
...
@@ -112,7 +112,7 @@ constructor
>> myDevID - device id
>> myDevID - device id
>> myMem - memory pool used to allocating the data array
>> myMem - memory pool used to allocating the data array
*/
*/
XTensor
::
XTensor
(
const
int
myOrder
,
int
myDevID
,
XMem
*
myMem
)
XTensor
::
XTensor
(
const
int
myOrder
,
int
myDevID
,
XMem
*
myMem
)
{
{
CheckNTErrors
((
myOrder
>=
0
),
"Illegal tensor order!"
);
CheckNTErrors
((
myOrder
>=
0
),
"Illegal tensor order!"
);
...
@@ -134,8 +134,8 @@ constructor
...
@@ -134,8 +134,8 @@ constructor
>> myDevID - device id
>> myDevID - device id
>> myMem - memory pool used to allocating the data array
>> myMem - memory pool used to allocating the data array
*/
*/
XTensor
::
XTensor
(
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
,
XTensor
::
XTensor
(
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
,
const
float
myDenseRatio
,
int
myDevID
,
XMem
*
myMem
)
const
float
myDenseRatio
,
int
myDevID
,
XMem
*
myMem
)
{
{
Init
();
Init
();
SetDataPointer
();
SetDataPointer
();
...
@@ -145,12 +145,12 @@ XTensor::XTensor(const int myOrder, const int * myDimSize, const TENSOR_DATA_TYP
...
@@ -145,12 +145,12 @@ XTensor::XTensor(const int myOrder, const int * myDimSize, const TENSOR_DATA_TYP
mem
=
myMem
;
mem
=
myMem
;
devID
=
myMem
!=
NULL
?
myMem
->
devID
:
myDevID
;
devID
=
myMem
!=
NULL
?
myMem
->
devID
:
myDevID
;
if
(
order
>=
0
)
if
(
order
>=
0
)
Resize
(
myOrder
,
myDimSize
,
myDataType
,
myDenseRatio
);
Resize
(
myOrder
,
myDimSize
,
myDataType
,
myDenseRatio
);
}
}
/* copy constructor */
/* copy constructor */
XTensor
::
XTensor
(
const
XTensor
&
reference
)
XTensor
::
XTensor
(
const
XTensor
&
reference
)
{
{
Init
();
Init
();
SetDataPointer
();
SetDataPointer
();
...
@@ -158,8 +158,8 @@ XTensor::XTensor(const XTensor &reference)
...
@@ -158,8 +158,8 @@ XTensor::XTensor(const XTensor &reference)
ShallowCopy
(
reference
);
ShallowCopy
(
reference
);
data
=
NULL
;
data
=
NULL
;
dataHost
=
NULL
;
dataHost
=
NULL
;
if
(
reference
.
isTmp
)
{
if
(
reference
.
isTmp
)
{
devID
=
reference
.
devID
;
devID
=
reference
.
devID
;
mem
=
reference
.
mem
;
mem
=
reference
.
mem
;
data
=
reference
.
data
;
data
=
reference
.
data
;
...
@@ -172,26 +172,26 @@ XTensor::XTensor(const XTensor &reference)
...
@@ -172,26 +172,26 @@ XTensor::XTensor(const XTensor &reference)
This is VERY tricky and there might be better solutions :) */
This is VERY tricky and there might be better solutions :) */
*
reference
.
dataP
=
NULL
;
*
reference
.
dataP
=
NULL
;
}
}
else
{
else
{
devID
=
reference
.
devID
;
devID
=
reference
.
devID
;
mem
=
reference
.
mem
;
mem
=
reference
.
mem
;
InitTensorV2
(
this
,
&
reference
);
InitTensorV2
(
this
,
&
reference
);
_CopyValues
(
&
reference
,
this
);
_CopyValues
(
&
reference
,
this
);
}
}
if
(
reference
.
isTmp
)
if
(
reference
.
isTmp
)
XLink
::
Replace
(
&
reference
,
this
);
XLink
::
Replace
(
&
reference
,
this
);
else
{
else
{
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"The node has outgoing edge to other nodes!"
);
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"The node has outgoing edge to other nodes!"
);
XLink
::
CopyIncoming
(
&
reference
,
this
);
XLink
::
CopyIncoming
(
&
reference
,
this
);
}
}
isInit
=
true
;
isInit
=
true
;
isTmp
=
reference
.
isTmp
;
isTmp
=
reference
.
isTmp
;
}
}
/* copy constructor (with right value reference) */
/* copy constructor (with right value reference) */
XTensor
::
XTensor
(
const
XTensor
&&
reference
)
XTensor
::
XTensor
(
const
XTensor
&&
reference
)
{
{
Init
();
Init
();
SetDataPointer
();
SetDataPointer
();
...
@@ -215,7 +215,7 @@ XTensor::XTensor(const XTensor &&reference)
...
@@ -215,7 +215,7 @@ XTensor::XTensor(const XTensor &&reference)
XLink
::
Replace
(
&
reference
,
this
);
XLink
::
Replace
(
&
reference
,
this
);
isInit
=
true
;
isInit
=
true
;
isTmp
=
reference
.
isTmp
;
isTmp
=
reference
.
isTmp
;
}
}
/* de-constructor */
/* de-constructor */
...
@@ -225,12 +225,12 @@ XTensor::~XTensor()
...
@@ -225,12 +225,12 @@ XTensor::~XTensor()
the connectivity of the graph. To kill memory
the connectivity of the graph. To kill memory
leak, we release the data of the new tensor
leak, we release the data of the new tensor
when its parent is deleted (see ClearIncoming). */
when its parent is deleted (see ClearIncoming). */
if
(
outgo
.
tailNum
>
0
)
{
if
(
outgo
.
tailNum
>
0
)
{
int
dims
[
MAX_TENSOR_DIM_NUM
];
int
dims
[
MAX_TENSOR_DIM_NUM
];
memcpy
(
dims
,
dimSize
,
order
*
sizeof
(
int
));
memcpy
(
dims
,
dimSize
,
order
*
sizeof
(
int
));
dims
[
0
]
=
-
dims
[
0
];
dims
[
0
]
=
-
dims
[
0
];
XTensor
*
newTensor
=
new
XTensor
(
order
,
dims
,
dataType
,
denseRatio
,
devID
,
mem
);
XTensor
*
newTensor
=
new
XTensor
(
order
,
dims
,
dataType
,
denseRatio
,
devID
,
mem
);
newTensor
->
SetTMPFlag
();
newTensor
->
SetTMPFlag
();
newTensor
->
data
=
data
;
newTensor
->
data
=
data
;
data
=
NULL
;
data
=
NULL
;
...
@@ -243,12 +243,12 @@ XTensor::~XTensor()
...
@@ -243,12 +243,12 @@ XTensor::~XTensor()
DestroyData
();
DestroyData
();
if
(
grad
!=
NULL
)
if
(
grad
!=
NULL
)
delete
grad
;
delete
grad
;
}
}
/* set the name of the tensor */
/* set the name of the tensor */
void
XTensor
::
SetName
(
const
char
*
myName
)
void
XTensor
::
SetName
(
const
char
*
myName
)
{
{
strcpy
(
name
,
myName
);
strcpy
(
name
,
myName
);
}
}
...
@@ -277,10 +277,10 @@ void XTensor::Init()
...
@@ -277,10 +277,10 @@ void XTensor::Init()
isInGlobalMem
=
false
;
isInGlobalMem
=
false
;
memset
(
isAllValued
,
0
,
sizeof
(
bool
)
*
MAX_TENSOR_DIM_NUM
);
memset
(
isAllValued
,
0
,
sizeof
(
bool
)
*
MAX_TENSOR_DIM_NUM
);
isInit
=
false
;
isInit
=
false
;
isTmp
=
false
;
isTmp
=
false
;
isGrad
=
false
;
isGrad
=
false
;
isVar
=
false
;
isVar
=
false
;
enableGrad
=
true
;
enableGrad
=
X_ENABLE_GRAD
;
visitMark
=
0
;
visitMark
=
0
;
grad
=
NULL
;
grad
=
NULL
;
}
}
...
@@ -288,16 +288,16 @@ void XTensor::Init()
...
@@ -288,16 +288,16 @@ void XTensor::Init()
/* delete data arrays */
/* delete data arrays */
void
XTensor
::
DestroyData
()
void
XTensor
::
DestroyData
()
{
{
if
(
data
!=
NULL
&&
mem
==
NULL
&&
!
isShared
)
if
(
data
!=
NULL
&&
mem
==
NULL
&&
!
isShared
)
XMemFree
(
devID
,
data
);
XMemFree
(
devID
,
data
);
else
if
(
data
!=
NULL
&&
isInGlobalMem
)
else
if
(
data
!=
NULL
&&
isInGlobalMem
)
FreeData
(
this
,
mem
);
FreeData
(
this
,
mem
);
else
if
(
data
!=
NULL
)
else
if
(
data
!=
NULL
)
mem
->
Release
(
data
,
GetDataSizeInChar
(),
signature
);
mem
->
Release
(
data
,
GetDataSizeInChar
(),
signature
);
data
=
NULL
;
data
=
NULL
;
if
(
dataHost
!=
NULL
)
if
(
dataHost
!=
NULL
)
delete
[]
(
char
*
)
dataHost
;
delete
[]
(
char
*
)
dataHost
;
dataHost
=
NULL
;
dataHost
=
NULL
;
}
}
...
@@ -307,7 +307,7 @@ shallow copy of the tensor
...
@@ -307,7 +307,7 @@ shallow copy of the tensor
Note that we do not copy data array here
Note that we do not copy data array here
>> tensor - the source tensor
>> tensor - the source tensor
*/
*/
void
XTensor
::
ShallowCopy
(
const
XTensor
&
tensor
)
void
XTensor
::
ShallowCopy
(
const
XTensor
&
tensor
)
{
{
strcpy
(
name
,
tensor
.
name
);
strcpy
(
name
,
tensor
.
name
);
order
=
tensor
.
order
;
order
=
tensor
.
order
;
...
@@ -318,7 +318,7 @@ void XTensor::ShallowCopy(const XTensor &tensor)
...
@@ -318,7 +318,7 @@ void XTensor::ShallowCopy(const XTensor &tensor)
unitNum
=
tensor
.
unitNum
;
unitNum
=
tensor
.
unitNum
;
isSparse
=
tensor
.
isSparse
;
isSparse
=
tensor
.
isSparse
;
unitNumNonZero
=
tensor
.
unitNumNonZero
;
unitNumNonZero
=
tensor
.
unitNumNonZero
;
denseRatio
=
tensor
.
denseRatio
;
denseRatio
=
tensor
.
denseRatio
;
isShared
=
tensor
.
isShared
;
isShared
=
tensor
.
isShared
;
isDefaultDType
=
tensor
.
isDefaultDType
;
isDefaultDType
=
tensor
.
isDefaultDType
;
isInGlobalMem
=
tensor
.
isInGlobalMem
;
isInGlobalMem
=
tensor
.
isInGlobalMem
;
...
@@ -330,12 +330,12 @@ XTensor& XTensor::operator= (const XTensor& tensor)
...
@@ -330,12 +330,12 @@ XTensor& XTensor::operator= (const XTensor& tensor)
{
{
/* we must make a hard copy of the tensor if it is the input
/* we must make a hard copy of the tensor if it is the input
of another node. */
of another node. */
if
(
outgo
.
tailNum
>
0
)
{
if
(
outgo
.
tailNum
>
0
)
{
int
dims
[
MAX_TENSOR_DIM_NUM
];
int
dims
[
MAX_TENSOR_DIM_NUM
];
memcpy
(
dims
,
dimSize
,
order
*
sizeof
(
int
));
memcpy
(
dims
,
dimSize
,
order
*
sizeof
(
int
));
dims
[
0
]
=
-
dims
[
0
];
dims
[
0
]
=
-
dims
[
0
];
XTensor
*
newTensor
=
new
XTensor
(
order
,
dims
,
dataType
,
denseRatio
,
devID
,
mem
);
XTensor
*
newTensor
=
new
XTensor
(
order
,
dims
,
dataType
,
denseRatio
,
devID
,
mem
);
newTensor
->
SetTMPFlag
();
newTensor
->
SetTMPFlag
();
newTensor
->
data
=
data
;
newTensor
->
data
=
data
;
newTensor
->
dataHost
=
dataHost
;
newTensor
->
dataHost
=
dataHost
;
...
@@ -350,35 +350,35 @@ XTensor& XTensor::operator= (const XTensor& tensor)
...
@@ -350,35 +350,35 @@ XTensor& XTensor::operator= (const XTensor& tensor)
dataHost
=
NULL
;
dataHost
=
NULL
;
}
}
if
(
false
&&
!
tensor
.
isTmp
)
{
if
(
false
&&
!
tensor
.
isTmp
)
{
/* NOTE: this might lead to additional data copy by Mac LLVM compilers */
/* NOTE: this might lead to additional data copy by Mac LLVM compilers */
/* we make an identity transformation here */
/* we make an identity transformation here */
if
(
outgo
.
tailNum
>
0
)
if
(
outgo
.
tailNum
>
0
)
XLink
::
ClearOutgoing
(
this
);
XLink
::
ClearOutgoing
(
this
);
XLink
::
ClearIncoming
(
this
);
XLink
::
ClearIncoming
(
this
);
if
(
!
_IsSameShaped
(
this
,
&
tensor
))
if
(
!
_IsSameShaped
(
this
,
&
tensor
))
Resize
(
tensor
.
order
,
tensor
.
dimSize
,
tensor
.
dataType
,
tensor
.
denseRatio
);
Resize
(
tensor
.
order
,
tensor
.
dimSize
,
tensor
.
dataType
,
tensor
.
denseRatio
);
_Identity
(
&
tensor
,
this
);
_Identity
(
&
tensor
,
this
);
XLink
::
MakeLink
(
&
tensor
,
NULL
,
this
,
FUNC_IDENTITY
);
XLink
::
MakeLink
(
&
tensor
,
NULL
,
this
,
FUNC_IDENTITY
);
}
}
else
{
else
{
/* hard copy of the data array */
/* hard copy of the data array */
int
size
=
unitNum
*
unitSize
;
int
size
=
unitNum
*
unitSize
;
if
(
isInit
&&
!
isSparse
&&
!
tensor
.
isSparse
&&
if
(
isInit
&&
!
isSparse
&&
!
tensor
.
isSparse
&&
size
==
tensor
.
unitNum
*
tensor
.
unitSize
&&
size
==
tensor
.
unitNum
*
tensor
.
unitSize
&&
((
devID
<
0
&&
tensor
.
devID
<
0
)
&&
devID
==
tensor
.
devID
)
&&
((
devID
<
0
&&
tensor
.
devID
<
0
)
&&
devID
==
tensor
.
devID
)
&&
data
!=
NULL
)
data
!=
NULL
)
{
{
XMemCopy
(
data
,
devID
,
tensor
.
data
,
tensor
.
devID
,
size
);
XMemCopy
(
data
,
devID
,
tensor
.
data
,
tensor
.
devID
,
size
);
if
(
dataHost
!=
NULL
&&
tensor
.
dataHost
!=
NULL
)
if
(
dataHost
!=
NULL
&&
tensor
.
dataHost
!=
NULL
)
XMemCopy
(
dataHost
,
-
1
,
tensor
.
dataHost
,
tensor
.
devID
,
size
);
XMemCopy
(
dataHost
,
-
1
,
tensor
.
dataHost
,
tensor
.
devID
,
size
);
}
}
else
{
else
{
DestroyData
();
DestroyData
();
if
(
!
isInit
)
{
if
(
!
isInit
)
{
devID
=
tensor
.
devID
;
devID
=
tensor
.
devID
;
mem
=
tensor
.
mem
;
mem
=
tensor
.
mem
;
}
}
...
@@ -391,7 +391,7 @@ XTensor& XTensor::operator= (const XTensor& tensor)
...
@@ -391,7 +391,7 @@ XTensor& XTensor::operator= (const XTensor& tensor)
ShallowCopy
(
tensor
);
ShallowCopy
(
tensor
);
isInit
=
true
;
isInit
=
true
;
isTmp
=
false
;
isTmp
=
false
;
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"The node has outgoing edge to other nodes!"
);
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"The node has outgoing edge to other nodes!"
);
...
@@ -407,12 +407,12 @@ XTensor& XTensor::operator= (const XTensor&& tensor)
...
@@ -407,12 +407,12 @@ XTensor& XTensor::operator= (const XTensor&& tensor)
{
{
/* we must make a hard copy of the tensor if it is the input
/* we must make a hard copy of the tensor if it is the input
of another node. */
of another node. */
if
(
outgo
.
tailNum
>
0
)
{
if
(
outgo
.
tailNum
>
0
)
{
int
dims
[
MAX_TENSOR_DIM_NUM
];
int
dims
[
MAX_TENSOR_DIM_NUM
];
memcpy
(
dims
,
dimSize
,
order
*
sizeof
(
int
));
memcpy
(
dims
,
dimSize
,
order
*
sizeof
(
int
));
dims
[
0
]
=
-
dims
[
0
];
dims
[
0
]
=
-
dims
[
0
];
XTensor
*
newTensor
=
new
XTensor
(
order
,
dims
,
dataType
,
denseRatio
,
devID
,
mem
);
XTensor
*
newTensor
=
new
XTensor
(
order
,
dims
,
dataType
,
denseRatio
,
devID
,
mem
);
newTensor
->
SetTMPFlag
();
newTensor
->
SetTMPFlag
();
newTensor
->
data
=
data
;
newTensor
->
data
=
data
;
newTensor
->
dataHost
=
dataHost
;
newTensor
->
dataHost
=
dataHost
;
...
@@ -433,7 +433,7 @@ XTensor& XTensor::operator= (const XTensor&& tensor)
...
@@ -433,7 +433,7 @@ XTensor& XTensor::operator= (const XTensor&& tensor)
isInit
=
true
;
isInit
=
true
;
devID
=
tensor
.
devID
;
devID
=
tensor
.
devID
;
mem
=
tensor
.
mem
;
mem
=
tensor
.
mem
;
data
=
tensor
.
data
;
data
=
tensor
.
data
;
signature
=
tensor
.
signature
;
signature
=
tensor
.
signature
;
...
@@ -456,7 +456,7 @@ XTensor XTensor::operator+ (const XTensor& tensor) const
...
@@ -456,7 +456,7 @@ XTensor XTensor::operator+ (const XTensor& tensor) const
}
}
/* overloading of the plus-sign */
/* overloading of the plus-sign */
XTensor
XTensor
::
operator
+
(
const
DTYPE
shift
)
const
XTensor
XTensor
::
operator
+
(
const
DTYPE
shift
)
const
{
{
return
ScaleAndShift
(
*
this
,
1
,
shift
);
return
ScaleAndShift
(
*
this
,
1
,
shift
);
}
}
...
@@ -500,7 +500,7 @@ XTensor XTensor::operator/ (const XTensor& tensor) const
...
@@ -500,7 +500,7 @@ XTensor XTensor::operator/ (const XTensor& tensor) const
/* overloading of the division-sign */
/* overloading of the division-sign */
XTensor
XTensor
::
operator
/
(
const
DTYPE
scale
)
const
XTensor
XTensor
::
operator
/
(
const
DTYPE
scale
)
const
{
{
return
ScaleAndShift
(
*
this
,
(
DTYPE
)
1
/
scale
,
0
);
return
ScaleAndShift
(
*
this
,
(
DTYPE
)
1
.0
F
/
scale
,
0
);
}
}
/*
/*
...
@@ -518,7 +518,7 @@ relocate the data on the target device
...
@@ -518,7 +518,7 @@ relocate the data on the target device
>> myDevId - target device id
>> myDevId - target device id
>> myMem - memory pool on the target device
>> myMem - memory pool on the target device
*/
*/
void
XTensor
::
SetDevice
(
int
myDevId
,
XMem
*
myMem
)
void
XTensor
::
SetDevice
(
int
myDevId
,
XMem
*
myMem
)
{
{
if
(
myMem
==
NULL
)
{
if
(
myMem
==
NULL
)
{
myMem
=
GMems
.
GetMem
(
myDevId
);
myMem
=
GMems
.
GetMem
(
myDevId
);
...
@@ -527,9 +527,9 @@ void XTensor::SetDevice(int myDevId, XMem * myMem)
...
@@ -527,9 +527,9 @@ void XTensor::SetDevice(int myDevId, XMem * myMem)
isInGlobalMem
=
false
;
isInGlobalMem
=
false
;
}
}
bool
XTensor
::
IsReduceShaped
(
const
XTensor
*
a
,
const
XTensor
*
b
,
int
dim
)
bool
XTensor
::
IsReduceShaped
(
const
XTensor
*
a
,
const
XTensor
*
b
,
int
dim
)
{
{
if
(
a
==
NULL
||
b
==
NULL
)
if
(
a
==
NULL
||
b
==
NULL
)
return
false
;
return
false
;
if
((
a
->
order
-
1
)
!=
b
->
order
)
if
((
a
->
order
-
1
)
!=
b
->
order
)
...
@@ -541,18 +541,18 @@ bool XTensor::IsReduceShaped(const XTensor * a, const XTensor * b, int dim)
...
@@ -541,18 +541,18 @@ bool XTensor::IsReduceShaped(const XTensor * a, const XTensor * b, int dim)
return
false
;
return
false
;
}
}
else
if
(
i
>=
dim
)
{
else
if
(
i
>=
dim
)
{
if
(
a
->
dimSize
[
i
+
1
]
!=
b
->
dimSize
[
i
])
if
(
a
->
dimSize
[
i
+
1
]
!=
b
->
dimSize
[
i
])
return
false
;
return
false
;
}
}
}
}
if
(
a
->
dataType
!=
b
->
dataType
)
if
(
a
->
dataType
!=
b
->
dataType
)
return
false
;
return
false
;
if
(
a
->
denseRatio
!=
b
->
denseRatio
)
if
(
a
->
denseRatio
!=
b
->
denseRatio
)
return
false
;
return
false
;
if
(
a
->
isSparse
!=
b
->
isSparse
)
if
(
a
->
isSparse
!=
b
->
isSparse
)
return
false
;
return
false
;
return
true
;
return
true
;
...
@@ -562,7 +562,7 @@ bool XTensor::IsReduceShaped(const XTensor * a, const XTensor * b, int dim)
...
@@ -562,7 +562,7 @@ bool XTensor::IsReduceShaped(const XTensor * a, const XTensor * b, int dim)
set the size of each dimension
set the size of each dimension
>> myDimSize - size of each dimension
>> myDimSize - size of each dimension
*/
*/
void
XTensor
::
SetDim
(
int
*
myDimSize
)
void
XTensor
::
SetDim
(
int
*
myDimSize
)
{
{
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
dimSize
[
i
]
=
myDimSize
[
i
];
dimSize
[
i
]
=
myDimSize
[
i
];
...
@@ -579,7 +579,7 @@ int XTensor::GetDim(const int dim) const
...
@@ -579,7 +579,7 @@ int XTensor::GetDim(const int dim) const
CheckNTErrors
(
dim
>=
-
order
,
"dimenision is out of range!"
);
CheckNTErrors
(
dim
>=
-
order
,
"dimenision is out of range!"
);
int
d
=
dim
;
int
d
=
dim
;
if
(
dim
<
0
)
if
(
dim
<
0
)
d
=
order
+
dim
;
d
=
order
+
dim
;
return
dimSize
[
d
];
return
dimSize
[
d
];
...
@@ -590,12 +590,12 @@ reshape the tensor
...
@@ -590,12 +590,12 @@ reshape the tensor
>> myOrder - order of the tensor
>> myOrder - order of the tensor
>> myDimSize - size of each dimension
>> myDimSize - size of each dimension
*/
*/
void
XTensor
::
Reshape
(
const
int
myOrder
,
const
int
*
myDimSize
)
void
XTensor
::
Reshape
(
const
int
myOrder
,
const
int
*
myDimSize
)
{
{
int
dims
[
MAX_TENSOR_DIM_NUM
];
int
dims
[
MAX_TENSOR_DIM_NUM
];
int
num
=
1
;
int
num
=
1
;
for
(
int
i
=
0
;
i
<
myOrder
;
i
++
)
{
for
(
int
i
=
0
;
i
<
myOrder
;
i
++
)
{
num
*=
myDimSize
[
i
];
num
*=
myDimSize
[
i
];
dims
[
i
]
=
abs
(
myDimSize
[
i
]);
dims
[
i
]
=
abs
(
myDimSize
[
i
]);
}
}
...
@@ -663,7 +663,7 @@ XTensor XTensor::TypeAs(const XTensor input)
...
@@ -663,7 +663,7 @@ XTensor XTensor::TypeAs(const XTensor input)
/* get the number of items in the data array */
/* get the number of items in the data array */
int
XTensor
::
GetSize
()
const
int
XTensor
::
GetSize
()
const
{
{
if
(
isSparse
)
if
(
isSparse
)
return
unitNumNonZero
;
return
unitNumNonZero
;
else
else
return
unitNum
;
return
unitNum
;
...
@@ -672,13 +672,13 @@ int XTensor::GetSize() const
...
@@ -672,13 +672,13 @@ int XTensor::GetSize() const
/* get the size of the memory space used */
/* get the size of the memory space used */
int
XTensor
::
GetDataSizeInChar
()
const
int
XTensor
::
GetDataSizeInChar
()
const
{
{
if
(
isSparse
)
{
if
(
isSparse
)
{
int
num
=
int
(
unitNum
*
denseRatio
+
1
);
int
num
=
int
(
unitNum
*
denseRatio
+
1
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
size
=
sizeof
(
int
)
+
tupleSize
*
(
num
);
int
size
=
sizeof
(
int
)
+
tupleSize
*
(
num
);
return
size
;
return
size
;
}
}
else
{
else
{
return
unitNum
*
unitSize
;
return
unitNum
*
unitSize
;
}
}
}
}
...
@@ -690,15 +690,15 @@ get unit size in terms of "dataType"
...
@@ -690,15 +690,15 @@ get unit size in terms of "dataType"
*/
*/
int
XTensor
::
GetUnitSize
(
TENSOR_DATA_TYPE
myDataType
)
const
int
XTensor
::
GetUnitSize
(
TENSOR_DATA_TYPE
myDataType
)
const
{
{
if
(
myDataType
==
X_INT
)
if
(
myDataType
==
X_INT
)
return
sizeof
(
int
);
return
sizeof
(
int
);
else
if
(
myDataType
==
X_FLOAT
)
else
if
(
myDataType
==
X_FLOAT
)
return
sizeof
(
float
);
return
sizeof
(
float
);
else
if
(
myDataType
==
X_DOUBLE
)
else
if
(
myDataType
==
X_DOUBLE
)
return
sizeof
(
double
);
return
sizeof
(
double
);
else
if
(
myDataType
==
X_INT8
)
else
if
(
myDataType
==
X_INT8
)
return
1
;
return
1
;
else
if
(
myDataType
==
X_FLOAT16
)
else
if
(
myDataType
==
X_FLOAT16
)
return
2
;
return
2
;
return
sizeof
(
float
);
return
sizeof
(
float
);
}
}
...
@@ -737,21 +737,21 @@ MTYPE XTensor::GetOffset3D(int d0, int d1, int d2) const
...
@@ -737,21 +737,21 @@ MTYPE XTensor::GetOffset3D(int d0, int d1, int d2) const
a vector with all entries of 0
a vector with all entries of 0
>> stream - stream for the job pipeline
>> stream - stream for the job pipeline
*/
*/
void
XTensor
::
SetZeroAll
(
XStream
*
stream
)
void
XTensor
::
SetZeroAll
(
XStream
*
stream
)
{
{
if
(
data
==
NULL
)
if
(
data
==
NULL
)
return
;
return
;
if
(
isSparse
)
{
if
(
isSparse
)
{
if
(
devID
>=
0
)
{
if
(
devID
>=
0
)
{
#ifdef USE_CUDA
#ifdef USE_CUDA
int
size
=
sizeof
(
int
)
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
unitNumNonZero
;
int
size
=
sizeof
(
int
)
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
unitNumNonZero
;
int
devIDBackup
=
0
;
int
devIDBackup
=
0
;
cudaGetDevice
(
&
devIDBackup
);
cudaGetDevice
(
&
devIDBackup
);
cudaSetDevice
(
devID
);
cudaSetDevice
(
devID
);
if
(
stream
==
NULL
)
if
(
stream
==
NULL
)
cudaMemset
(
data
,
0
,
size
);
cudaMemset
(
data
,
0
,
size
);
else
else
cudaMemsetAsync
(
data
,
0
,
size
,
stream
->
stream
);
cudaMemsetAsync
(
data
,
0
,
size
,
stream
->
stream
);
...
@@ -764,14 +764,14 @@ void XTensor::SetZeroAll(XStream * stream)
...
@@ -764,14 +764,14 @@ void XTensor::SetZeroAll(XStream * stream)
unitNumNonZero
=
0
;
unitNumNonZero
=
0
;
}
}
else
{
else
{
if
(
devID
>=
0
)
{
if
(
devID
>=
0
)
{
#ifdef USE_CUDA
#ifdef USE_CUDA
int
devIDBackup
=
0
;
int
devIDBackup
=
0
;
cudaGetDevice
(
&
devIDBackup
);
cudaGetDevice
(
&
devIDBackup
);
cudaSetDevice
(
devID
);
cudaSetDevice
(
devID
);
if
(
stream
==
NULL
)
if
(
stream
==
NULL
)
cudaMemset
(
data
,
0
,
unitNum
*
unitSize
);
cudaMemset
(
data
,
0
,
unitNum
*
unitSize
);
else
else
cudaMemsetAsync
(
data
,
0
,
unitNum
*
unitSize
,
stream
->
stream
);
cudaMemsetAsync
(
data
,
0
,
unitNum
*
unitSize
,
stream
->
stream
);
...
@@ -789,9 +789,9 @@ void XTensor::SetZeroAll(XStream * stream)
...
@@ -789,9 +789,9 @@ void XTensor::SetZeroAll(XStream * stream)
>> num - number of data items
>> num - number of data items
>> beg - where we start the data copy in the data array of the tensor
>> beg - where we start the data copy in the data array of the tensor
*/
*/
void
XTensor
::
SetData
(
const
void
*
d
,
int
num
,
int
beg
)
void
XTensor
::
SetData
(
const
void
*
d
,
int
num
,
int
beg
)
{
{
if
(
data
==
NULL
||
d
==
NULL
)
if
(
data
==
NULL
||
d
==
NULL
)
return
;
return
;
CheckNTErrors
(
!
isSparse
,
"TODO"
);
CheckNTErrors
(
!
isSparse
,
"TODO"
);
...
@@ -830,7 +830,7 @@ void XTensor::SetDataRand(DTYPE lower, DTYPE upper)
...
@@ -830,7 +830,7 @@ void XTensor::SetDataRand(DTYPE lower, DTYPE upper)
// srand((unsigned)time(0));
// srand((unsigned)time(0));
DTYPE
variance
=
upper
-
lower
;
DTYPE
variance
=
upper
-
lower
;
void
*
d
=
NULL
;
void
*
d
=
NULL
;
if
(
dataType
==
X_FLOAT
)
{
if
(
dataType
==
X_FLOAT
)
{
d
=
new
float
[
unitNum
];
d
=
new
float
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
...
@@ -868,12 +868,12 @@ double GaussRand(DTYPE mean, DTYPE standardDeviation)
...
@@ -868,12 +868,12 @@ double GaussRand(DTYPE mean, DTYPE standardDeviation)
double
z
;
double
z
;
double
pi
=
3.141592654
;
double
pi
=
3.141592654
;
if
(
phase
==
0
){
if
(
phase
==
0
)
{
u
=
(
rand
()
+
1.0
)
/
(
RAND_MAX
+
1.0
);
u
=
(
rand
()
+
1.0
)
/
(
RAND_MAX
+
1.0
);
v
=
(
rand
()
+
1.0
)
/
(
RAND_MAX
+
1.0
);
v
=
(
rand
()
+
1.0
)
/
(
RAND_MAX
+
1.0
);
z
=
sqrt
(
-
2.0
*
log
(
u
))
*
sin
(
2.0
*
pi
*
v
);
z
=
sqrt
(
-
2.0
*
log
(
u
))
*
sin
(
2.0
*
pi
*
v
);
}
}
else
{
else
{
z
=
sqrt
(
-
2.0
*
log
(
u
))
*
cos
(
2.0
*
pi
*
v
);
z
=
sqrt
(
-
2.0
*
log
(
u
))
*
cos
(
2.0
*
pi
*
v
);
}
}
...
@@ -894,7 +894,7 @@ void XTensor::SetDataRandn(DTYPE mean, DTYPE standardDeviation)
...
@@ -894,7 +894,7 @@ void XTensor::SetDataRandn(DTYPE mean, DTYPE standardDeviation)
return
;
return
;
// srand((unsigned)time(0));
// srand((unsigned)time(0));
void
*
d
=
NULL
;
void
*
d
=
NULL
;
if
(
dataType
==
X_FLOAT
)
{
if
(
dataType
==
X_FLOAT
)
{
d
=
new
float
[
unitNum
];
d
=
new
float
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
...
@@ -927,7 +927,7 @@ set tensor items with an array of offsets
...
@@ -927,7 +927,7 @@ set tensor items with an array of offsets
>> value - value for the data items
>> value - value for the data items
>> num - number of the data items
>> num - number of the data items
*/
*/
void
XTensor
::
SetDataBatched
(
MTYPE
*
offsets
,
DTYPE
value
,
int
num
)
void
XTensor
::
SetDataBatched
(
MTYPE
*
offsets
,
DTYPE
value
,
int
num
)
{
{
_SetDataWithOffset
(
this
,
offsets
,
value
,
num
);
_SetDataWithOffset
(
this
,
offsets
,
value
,
num
);
}
}
...
@@ -938,7 +938,7 @@ set tensor items with an array of values
...
@@ -938,7 +938,7 @@ set tensor items with an array of values
>> values - value for each data item
>> values - value for each data item
>> num - number of the data items
>> num - number of the data items
*/
*/
void
XTensor
::
SetDataBatchedWithValues
(
MTYPE
*
offsets
,
void
*
values
,
int
num
)
void
XTensor
::
SetDataBatchedWithValues
(
MTYPE
*
offsets
,
void
*
values
,
int
num
)
{
{
_SetDataWithOffsetAndValue
(
this
,
offsets
,
values
,
num
);
_SetDataWithOffsetAndValue
(
this
,
offsets
,
values
,
num
);
}
}
...
@@ -973,9 +973,9 @@ DTYPE XTensor::Get(int offset) const
...
@@ -973,9 +973,9 @@ DTYPE XTensor::Get(int offset) const
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
CheckNTErrors
(
denseRatio
==
1.0
F
,
"Only dense tensors are supported in Get(offset)."
);
CheckNTErrors
(
denseRatio
==
1.0
F
,
"Only dense tensors are supported in Get(offset)."
);
DTYPE
*
address
=
(
DTYPE
*
)
data
+
offset
;
DTYPE
*
address
=
(
DTYPE
*
)
data
+
offset
;
return
ToCPU
(
devID
,
address
);
return
ToCPU
(
devID
,
address
);
}
}
...
@@ -985,25 +985,25 @@ get the pointer to a cell
...
@@ -985,25 +985,25 @@ get the pointer to a cell
>> size - size of index
>> size - size of index
<< return - pointer to the cell
<< return - pointer to the cell
*/
*/
void
*
XTensor
::
GetCell
(
int
index
[],
int
size
)
const
void
*
XTensor
::
GetCell
(
int
index
[],
int
size
)
const
{
{
CheckNTErrors
((
size
==
order
),
"Illegal index!"
);
CheckNTErrors
((
size
==
order
),
"Illegal index!"
);
int
offset
=
index
[
0
];
int
offset
=
index
[
0
];
for
(
int
i
=
1
;
i
<
size
;
++
i
)
{
for
(
int
i
=
1
;
i
<
size
;
++
i
)
{
CheckNTErrors
((
index
[
i
]
<
dimSize
[
i
]),
"Index is out of range!"
);
CheckNTErrors
((
index
[
i
]
<
dimSize
[
i
]),
"Index is out of range!"
);
offset
=
offset
*
dimSize
[
i
]
+
index
[
i
];
offset
=
offset
*
dimSize
[
i
]
+
index
[
i
];
}
}
if
(
isSparse
)
{
if
(
isSparse
)
{
DTYPE
value
;
DTYPE
value
;
void
*
p
;
void
*
p
;
if
(
BinarySearch
(
offset
,
value
,
p
))
if
(
BinarySearch
(
offset
,
value
,
p
))
return
(
char
*
)
p
+
sizeof
(
int
);
return
(
char
*
)
p
+
sizeof
(
int
);
else
else
return
NULL
;
return
NULL
;
}
}
else
{
else
{
return
((
char
*
)
data
)
+
offset
*
unitSize
;
return
((
char
*
)
data
)
+
offset
*
unitSize
;
}
}
}
}
...
@@ -1018,7 +1018,7 @@ DTYPE XTensor::Get0D() const
...
@@ -1018,7 +1018,7 @@ DTYPE XTensor::Get0D() const
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
int
dims
[
1
]
=
{
0
};
int
dims
[
1
]
=
{
0
};
void
*
value
=
GetCell
(
dims
,
0
);
void
*
value
=
GetCell
(
dims
,
0
);
return
ToCPU
(
devID
,
value
);
return
ToCPU
(
devID
,
value
);
}
}
...
@@ -1035,7 +1035,7 @@ DTYPE XTensor::Get1D(int i) const
...
@@ -1035,7 +1035,7 @@ DTYPE XTensor::Get1D(int i) const
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
int
dims
[
1
]
=
{
i
};
int
dims
[
1
]
=
{
i
};
void
*
value
=
GetCell
(
dims
,
1
);
void
*
value
=
GetCell
(
dims
,
1
);
return
ToCPU
(
devID
,
value
);
return
ToCPU
(
devID
,
value
);
}
}
...
@@ -1054,7 +1054,7 @@ DTYPE XTensor::Get2D(int ni, int mi) const
...
@@ -1054,7 +1054,7 @@ DTYPE XTensor::Get2D(int ni, int mi) const
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
int
dims
[
2
]
=
{
ni
,
mi
};
int
dims
[
2
]
=
{
ni
,
mi
};
void
*
value
=
GetCell
(
dims
,
2
);
void
*
value
=
GetCell
(
dims
,
2
);
return
ToCPU
(
devID
,
value
);
return
ToCPU
(
devID
,
value
);
}
}
...
@@ -1067,14 +1067,14 @@ get the value of a cell in a 3d tensor
...
@@ -1067,14 +1067,14 @@ get the value of a cell in a 3d tensor
*/
*/
DTYPE
XTensor
::
Get3D
(
int
d0
,
int
d1
,
int
d2
)
const
DTYPE
XTensor
::
Get3D
(
int
d0
,
int
d1
,
int
d2
)
const
{
{
CheckNTErrors
((
order
==
3
),
"Cannot get a
2
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
((
order
==
3
),
"Cannot get a
3
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
((
d0
>=
0
&&
d0
<
dimSize
[
0
]),
"dimension 0 is out of range!"
);
CheckNTErrors
((
d0
>=
0
&&
d0
<
dimSize
[
0
]),
"dimension 0 is out of range!"
);
CheckNTErrors
((
d1
>=
0
&&
d1
<
dimSize
[
1
]),
"dimension 1 is out of range!"
);
CheckNTErrors
((
d1
>=
0
&&
d1
<
dimSize
[
1
]),
"dimension 1 is out of range!"
);
CheckNTErrors
((
d2
>=
0
&&
d2
<
dimSize
[
2
]),
"dimension 2 is out of range!"
);
CheckNTErrors
((
d2
>=
0
&&
d2
<
dimSize
[
2
]),
"dimension 2 is out of range!"
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in default type."
);
int
dims
[
3
]
=
{
d0
,
d1
,
d2
};
int
dims
[
3
]
=
{
d0
,
d1
,
d2
};
void
*
value
=
GetCell
(
dims
,
3
);
void
*
value
=
GetCell
(
dims
,
3
);
return
ToCPU
(
devID
,
value
);
return
ToCPU
(
devID
,
value
);
}
}
...
@@ -1089,9 +1089,9 @@ int XTensor::GetInt(int offset) const
...
@@ -1089,9 +1089,9 @@ int XTensor::GetInt(int offset) const
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
CheckNTErrors
(
denseRatio
==
1.0
F
,
"Only dense tensors are supported in Get(offset)."
);
CheckNTErrors
(
denseRatio
==
1.0
F
,
"Only dense tensors are supported in Get(offset)."
);
int
*
address
=
(
int
*
)
data
+
offset
;
int
*
address
=
(
int
*
)
data
+
offset
;
return
ToCPUInt
(
devID
,
address
);
return
ToCPUInt
(
devID
,
address
);
}
}
...
@@ -1105,7 +1105,7 @@ int XTensor::Get0DInt() const
...
@@ -1105,7 +1105,7 @@ int XTensor::Get0DInt() const
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in int type."
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in int type."
);
int
dims
[
1
]
=
{
0
};
int
dims
[
1
]
=
{
0
};
void
*
value
=
GetCell
(
dims
,
0
);
void
*
value
=
GetCell
(
dims
,
0
);
return
ToCPUInt
(
devID
,
value
);
return
ToCPUInt
(
devID
,
value
);
}
}
...
@@ -1122,7 +1122,7 @@ int XTensor::Get1DInt(int i) const
...
@@ -1122,7 +1122,7 @@ int XTensor::Get1DInt(int i) const
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in int type."
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in int type."
);
int
dims
[
1
]
=
{
i
};
int
dims
[
1
]
=
{
i
};
void
*
value
=
GetCell
(
dims
,
1
);
void
*
value
=
GetCell
(
dims
,
1
);
return
ToCPUInt
(
devID
,
value
);
return
ToCPUInt
(
devID
,
value
);
}
}
...
@@ -1133,7 +1133,7 @@ get the value of a cell in a 2d tensor in int type
...
@@ -1133,7 +1133,7 @@ get the value of a cell in a 2d tensor in int type
>> mi - column index
>> mi - column index
<< return - value of cell(ni, mi) in int
<< return - value of cell(ni, mi) in int
*/
*/
int
XTensor
::
Get2DInt
(
int
ni
,
int
mi
)
const
int
XTensor
::
Get2DInt
(
int
ni
,
int
mi
)
const
{
{
CheckNTErrors
(
order
==
2
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
order
==
2
,
"Cannot get a 2d cell for a tensor whose order is not 2!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
...
@@ -1141,7 +1141,7 @@ get the value of a cell in a 2d tensor in int type
...
@@ -1141,7 +1141,7 @@ get the value of a cell in a 2d tensor in int type
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in default type."
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in default type."
);
int
dims
[
2
]
=
{
ni
,
mi
};
int
dims
[
2
]
=
{
ni
,
mi
};
void
*
value
=
GetCell
(
dims
,
2
);
void
*
value
=
GetCell
(
dims
,
2
);
return
ToCPUInt
(
devID
,
value
);
return
ToCPUInt
(
devID
,
value
);
}
}
...
@@ -1155,14 +1155,14 @@ get the value of a cell in a 3d tensor in int type
...
@@ -1155,14 +1155,14 @@ get the value of a cell in a 3d tensor in int type
*/
*/
int
XTensor
::
Get3DInt
(
int
d0
,
int
d1
,
int
d2
)
const
int
XTensor
::
Get3DInt
(
int
d0
,
int
d1
,
int
d2
)
const
{
{
CheckNTErrors
(
order
==
3
,
"Cannot get a
2
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
(
order
==
3
,
"Cannot get a
3
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
],
"dimension 1 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
],
"dimension 1 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
],
"dimension 2 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
],
"dimension 2 is out of range!"
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in default type."
);
CheckNTErrors
(
dataType
==
X_INT
,
"The tensor is not in default type."
);
int
dims
[
3
]
=
{
d0
,
d1
,
d2
};
int
dims
[
3
]
=
{
d0
,
d1
,
d2
};
void
*
value
=
GetCell
(
dims
,
3
);
void
*
value
=
GetCell
(
dims
,
3
);
return
ToCPUInt
(
devID
,
value
);
return
ToCPUInt
(
devID
,
value
);
}
}
...
@@ -1177,8 +1177,8 @@ DTYPE XTensor::GetInSparse(int i) const
...
@@ -1177,8 +1177,8 @@ DTYPE XTensor::GetInSparse(int i) const
CheckNTErrors
(
i
>=
0
&&
i
<
unitNum
,
"Index is out of range!"
);
CheckNTErrors
(
i
>=
0
&&
i
<
unitNum
,
"Index is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
char
*
d
=
(
char
*
)
data
+
sizeof
(
int
);
char
*
d
=
(
char
*
)
data
+
sizeof
(
int
);
DTYPE
*
value
=
(
DTYPE
*
)(
d
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
i
+
sizeof
(
int
));
DTYPE
*
value
=
(
DTYPE
*
)(
d
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
i
+
sizeof
(
int
));
return
ToCPU
(
devID
,
value
);
return
ToCPU
(
devID
,
value
);
}
}
...
@@ -1193,9 +1193,9 @@ int XTensor::GetKeyInSparse(int i) const
...
@@ -1193,9 +1193,9 @@ int XTensor::GetKeyInSparse(int i) const
CheckNTErrors
(
i
>=
0
&&
i
<
unitNum
,
"Index is out of range!"
);
CheckNTErrors
(
i
>=
0
&&
i
<
unitNum
,
"Index is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
char
*
d
=
(
char
*
)
data
+
sizeof
(
int
);
char
*
d
=
(
char
*
)
data
+
sizeof
(
int
);
int
*
key
=
(
int
*
)(
d
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
i
);
int
*
key
=
(
int
*
)(
d
+
(
sizeof
(
int
)
+
sizeof
(
DTYPE
))
*
i
);
return
ToCPUInt
(
devID
,
key
);
return
ToCPUInt
(
devID
,
key
);
}
}
...
@@ -1222,7 +1222,7 @@ bool XTensor::Set(DTYPE value, int offset)
...
@@ -1222,7 +1222,7 @@ bool XTensor::Set(DTYPE value, int offset)
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
DTYPE
*
d
=
(
DTYPE
*
)
data
+
offset
;
DTYPE
*
d
=
(
DTYPE
*
)
data
+
offset
;
return
SetToDevice
(
devID
,
d
,
value
);
return
SetToDevice
(
devID
,
d
,
value
);
}
}
...
@@ -1288,7 +1288,7 @@ set the value of a cell in a 3d tensor in default type
...
@@ -1288,7 +1288,7 @@ set the value of a cell in a 3d tensor in default type
*/
*/
bool
XTensor
::
Set3D
(
DTYPE
value
,
int
d0
,
int
d1
,
int
d2
)
bool
XTensor
::
Set3D
(
DTYPE
value
,
int
d0
,
int
d1
,
int
d2
)
{
{
CheckNTErrors
(
order
==
3
,
"Cannot get a
2
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
(
order
==
3
,
"Cannot get a
3
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
],
"dimension 1 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
],
"dimension 1 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
],
"dimension 2 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
],
"dimension 2 is out of range!"
);
...
@@ -1308,9 +1308,9 @@ bool XTensor::SetInt(int value, int offset)
...
@@ -1308,9 +1308,9 @@ bool XTensor::SetInt(int value, int offset)
{
{
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
offset
>=
0
&&
offset
<
unitNum
,
"Invalid index!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
CheckNTErrors
(
data
!=
NULL
,
"Cannot use an uninitialized tensor!"
);
int
*
d
=
(
int
*
)
data
+
offset
;
int
*
d
=
(
int
*
)
data
+
offset
;
return
SetToDeviceInt
(
devID
,
d
,
value
);
return
SetToDeviceInt
(
devID
,
d
,
value
);
}
}
...
@@ -1390,7 +1390,7 @@ set the integer value of a cell in a 3d tensor in default type
...
@@ -1390,7 +1390,7 @@ set the integer value of a cell in a 3d tensor in default type
*/
*/
bool
XTensor
::
Set3DInt
(
int
value
,
int
d0
,
int
d1
,
int
d2
)
bool
XTensor
::
Set3DInt
(
int
value
,
int
d0
,
int
d1
,
int
d2
)
{
{
CheckNTErrors
(
order
==
3
,
"Cannot get a
2
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
(
order
==
3
,
"Cannot get a
3
d cell for a tensor whose order is not 3!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
d0
>=
0
&&
d0
<
dimSize
[
0
],
"dimension 0 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
],
"dimension 1 is out of range!"
);
CheckNTErrors
(
d1
>=
0
&&
d1
<
dimSize
[
1
],
"dimension 1 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
],
"dimension 2 is out of range!"
);
CheckNTErrors
(
d2
>=
0
&&
d2
<
dimSize
[
2
],
"dimension 2 is out of range!"
);
...
@@ -1408,23 +1408,23 @@ increase the value of a cell in a 2d tensor
...
@@ -1408,23 +1408,23 @@ increase the value of a cell in a 2d tensor
>> mi - column index
>> mi - column index
<< return - succeeded or not
<< return - succeeded or not
*/
*/
bool
XTensor
::
Add2D
(
DTYPE
value
,
int
ni
,
int
mi
)
bool
XTensor
::
Add2D
(
DTYPE
value
,
int
ni
,
int
mi
)
{
{
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
],
"the row index is out of range!"
);
CheckNTErrors
(
ni
>=
0
&&
ni
<
dimSize
[
0
],
"the row index is out of range!"
);
CheckNTErrors
(
mi
>=
0
&&
mi
<
dimSize
[
1
],
"the column index is out of range!"
);
CheckNTErrors
(
mi
>=
0
&&
mi
<
dimSize
[
1
],
"the column index is out of range!"
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
CheckNTErrors
(
dataType
==
DEFAULT_DTYPE
,
"The tensor is not in default type."
);
CheckNTErrors
(
isSparse
==
false
,
"TODO!"
);
CheckNTErrors
(
isSparse
==
false
,
"TODO!"
);
if
(
devID
<
0
)
{
if
(
devID
<
0
)
{
DTYPE
*
p
=
(
DTYPE
*
)
data
+
ni
*
dimSize
[
1
]
+
mi
;
DTYPE
*
p
=
(
DTYPE
*
)
data
+
ni
*
dimSize
[
1
]
+
mi
;
CheckNTErrors
((
p
!=
NULL
),
"No data array is found!"
);
CheckNTErrors
((
p
!=
NULL
),
"No data array is found!"
);
*
p
=
*
p
+
value
;
*
p
=
*
p
+
value
;
return
true
;
return
true
;
}
}
else
{
else
{
int
dims
[
2
]
=
{
ni
,
mi
};
int
dims
[
2
]
=
{
ni
,
mi
};
return
SetToDevice
(
devID
,
GetCell
(
dims
,
2
),
Get2D
(
ni
,
mi
)
+
value
);
return
SetToDevice
(
devID
,
GetCell
(
dims
,
2
),
Get2D
(
ni
,
mi
)
+
value
);
}
}
...
@@ -1433,24 +1433,24 @@ increase the value of a cell in a 2d tensor
...
@@ -1433,24 +1433,24 @@ increase the value of a cell in a 2d tensor
/* get the number of non-zero elements (in a sparse tensor) */
/* get the number of non-zero elements (in a sparse tensor) */
int
XTensor
::
GetNonzeroSize
()
const
int
XTensor
::
GetNonzeroSize
()
const
{
{
if
(
!
isSparse
)
{
if
(
!
isSparse
)
{
XPRINT
(
1
,
stderr
,
"WARNING! Counting non-zero elements in a dense tensor might be slow!
\n
"
);
XPRINT
(
1
,
stderr
,
"WARNING! Counting non-zero elements in a dense tensor might be slow!
\n
"
);
CheckNTErrors
(
devID
<
0
,
"TODO"
);
CheckNTErrors
(
devID
<
0
,
"TODO"
);
if
(
dataType
==
DEFAULT_DTYPE
)
{
if
(
dataType
==
DEFAULT_DTYPE
)
{
int
count
=
0
;
int
count
=
0
;
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
DTYPE
value
=
*
(
DTYPE
*
)((
char
*
)
data
+
i
*
sizeof
(
DTYPE
));
DTYPE
value
=
*
(
DTYPE
*
)((
char
*
)
data
+
i
*
sizeof
(
DTYPE
));
if
(
value
==
0
)
if
(
value
==
0
)
count
++
;
count
++
;
}
}
return
count
;
return
count
;
}
}
else
{
else
{
ShowNTErrors
(
"TODO!"
);
ShowNTErrors
(
"TODO!"
);
return
-
1
;
return
-
1
;
}
}
}
}
else
{
else
{
/* return the head of the tuple list */
/* return the head of the tuple list */
return
unitNumNonZero
;
return
unitNumNonZero
;
}
}
...
@@ -1481,7 +1481,7 @@ set the tensor as "variable"
...
@@ -1481,7 +1481,7 @@ set the tensor as "variable"
void
XTensor
::
SetVarFlag
(
bool
myIsVar
)
void
XTensor
::
SetVarFlag
(
bool
myIsVar
)
{
{
isVar
=
myIsVar
;
isVar
=
myIsVar
;
if
(
isVar
)
if
(
isVar
)
SetGradFlag
(
true
);
SetGradFlag
(
true
);
}
}
...
@@ -1493,11 +1493,11 @@ resize a tensor with a specified tensor size
...
@@ -1493,11 +1493,11 @@ resize a tensor with a specified tensor size
>> myDenseRatio - how often an element has non-zero value
>> myDenseRatio - how often an element has non-zero value
<< return - succeeded or not
<< return - succeeded or not
*/
*/
bool
XTensor
::
Resize
(
const
int
myOrder
,
const
int
*
myDimSize
,
bool
XTensor
::
Resize
(
const
int
myOrder
,
const
int
*
myDimSize
,
const
TENSOR_DATA_TYPE
myDataType
,
const
float
myDenseRatio
)
const
TENSOR_DATA_TYPE
myDataType
,
const
float
myDenseRatio
)
{
{
/* free old mem */
/* free old mem */
if
(
data
!=
NULL
)
{
if
(
data
!=
NULL
)
{
if
(
mem
==
NULL
)
if
(
mem
==
NULL
)
XMemFree
(
devID
,
data
);
XMemFree
(
devID
,
data
);
else
else
...
@@ -1513,11 +1513,11 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
...
@@ -1513,11 +1513,11 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
bool
filledData
=
true
;
bool
filledData
=
true
;
bool
zeroData
=
false
;
bool
zeroData
=
false
;
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
dimSize
[
i
]
=
abs
(
myDimSize
[
i
]);
dimSize
[
i
]
=
abs
(
myDimSize
[
i
]);
if
(
myDimSize
[
i
]
<
0
)
if
(
myDimSize
[
i
]
<
0
)
filledData
=
false
;
filledData
=
false
;
if
(
myDimSize
[
i
]
==
0
)
if
(
myDimSize
[
i
]
==
0
)
zeroData
=
true
;
zeroData
=
true
;
unitNum
*=
dimSize
[
i
];
unitNum
*=
dimSize
[
i
];
}
}
...
@@ -1528,17 +1528,17 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
...
@@ -1528,17 +1528,17 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
dataType
=
myDataType
;
dataType
=
myDataType
;
unitSize
=
GetUnitSize
(
dataType
);
unitSize
=
GetUnitSize
(
dataType
);
if
(
myDataType
!=
DEFAULT_DTYPE
)
if
(
myDataType
!=
DEFAULT_DTYPE
)
isDefaultDType
=
false
;
isDefaultDType
=
false
;
else
else
isDefaultDType
=
true
;
isDefaultDType
=
true
;
if
(
zeroData
)
{
if
(
zeroData
)
{
unitNum
=
0
;
unitNum
=
0
;
return
false
;
return
false
;
}
}
if
(
isSparse
)
{
if
(
isSparse
)
{
/*
/*
for sparse matrices, we use a list of tuple (key, value),
for sparse matrices, we use a list of tuple (key, value),
ordered by key. Take a (2-dimensional) matrix as an example,
ordered by key. Take a (2-dimensional) matrix as an example,
...
@@ -1557,21 +1557,21 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
...
@@ -1557,21 +1557,21 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
*/
*/
int
num
=
int
(
unitNum
*
denseRatio
+
1
);
int
num
=
int
(
unitNum
*
denseRatio
+
1
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
size
=
sizeof
(
int
)
+
tupleSize
*
(
num
);
int
size
=
sizeof
(
int
)
+
tupleSize
*
(
num
);
if
(
filledData
){
int
*
d
=
NULL
;
if
(
mem
==
NULL
){
if
(
filledData
)
{
int
*
d
=
NULL
;
if
(
mem
==
NULL
)
{
d
=
new
int
[
size
];
d
=
new
int
[
size
];
memset
(
d
,
0
,
size
);
memset
(
d
,
0
,
size
);
}
}
else
{
else
{
d
=
(
int
*
)
mem
->
Alloc
(
mem
->
devID
,
size
);
d
=
(
int
*
)
mem
->
Alloc
(
mem
->
devID
,
size
);
}
}
if
(
d
==
NULL
)
if
(
d
==
NULL
)
return
false
;
return
false
;
#if !defined(UNSAFE_BUT_FAST_MEM)
#if !defined(UNSAFE_BUT_FAST_MEM)
...
@@ -1581,11 +1581,11 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
...
@@ -1581,11 +1581,11 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
}
}
return
true
;
return
true
;
}
}
else
{
else
{
if
(
filledData
)
{
if
(
filledData
)
{
/* allocate the new one */
/* allocate the new one */
if
(
mem
==
NULL
)
{
if
(
mem
==
NULL
)
{
data
=
XMemAlloc
(
devID
,
unitNum
*
unitSize
);
data
=
XMemAlloc
(
devID
,
unitNum
*
unitSize
);
#if defined(UNSAFE_BUT_FAST_MEM)
#if defined(UNSAFE_BUT_FAST_MEM)
XMemSet
(
devID
,
data
,
0
,
unitNum
*
unitSize
);
XMemSet
(
devID
,
data
,
0
,
unitNum
*
unitSize
);
#endif
#endif
...
@@ -1593,12 +1593,12 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
...
@@ -1593,12 +1593,12 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
else
else
data
=
(
void
*
)
mem
->
Alloc
(
mem
->
devID
,
unitNum
*
unitSize
);
data
=
(
void
*
)
mem
->
Alloc
(
mem
->
devID
,
unitNum
*
unitSize
);
if
(
data
==
NULL
)
if
(
data
==
NULL
)
return
false
;
return
false
;
}
}
#if !defined(UNSAFE_BUT_FAST_MEM)
#if !defined(UNSAFE_BUT_FAST_MEM)
if
(
data
!=
NULL
)
if
(
data
!=
NULL
)
XMem
::
SetZero
(
data
,
unitNum
*
unitSize
,
mem
);
XMem
::
SetZero
(
data
,
unitNum
*
unitSize
,
mem
);
#endif
#endif
return
true
;
return
true
;
...
@@ -1609,12 +1609,12 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
...
@@ -1609,12 +1609,12 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
resize a tensor by another
resize a tensor by another
>> myTensor - tensor for reference
>> myTensor - tensor for reference
*/
*/
bool
XTensor
::
Resize
(
const
XTensor
*
myTensor
)
bool
XTensor
::
Resize
(
const
XTensor
*
myTensor
)
{
{
denseRatio
=
myTensor
->
denseRatio
;
denseRatio
=
myTensor
->
denseRatio
;
TENSOR_DATA_TYPE
myDataType
=
myTensor
->
dataType
;
TENSOR_DATA_TYPE
myDataType
=
myTensor
->
dataType
;
if
(
myDataType
!=
DEFAULT_DTYPE
)
if
(
myDataType
!=
DEFAULT_DTYPE
)
isDefaultDType
=
false
;
isDefaultDType
=
false
;
else
else
isDefaultDType
=
true
;
isDefaultDType
=
true
;
...
@@ -1630,54 +1630,54 @@ binary search to find an element in a sparse tensor
...
@@ -1630,54 +1630,54 @@ binary search to find an element in a sparse tensor
it is the previous one if there is no hit
it is the previous one if there is no hit
<< return - found it or not?
<< return - found it or not?
*/
*/
bool
XTensor
::
BinarySearch
(
int
key
,
DTYPE
&
value
,
void
*
&
position
)
const
bool
XTensor
::
BinarySearch
(
int
key
,
DTYPE
&
value
,
void
*&
position
)
const
{
{
CheckNTErrors
((
isSparse
),
"A sparse tensor is required!"
);
CheckNTErrors
((
isSparse
),
"A sparse tensor is required!"
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in the default type."
);
CheckNTErrors
((
dataType
==
DEFAULT_DTYPE
),
"The tensor is not in the default type."
);
int
*
d
=
(
int
*
)
data
;
int
*
d
=
(
int
*
)
data
;
if
(
key
<
0
||
*
d
==
0
)
{
if
(
key
<
0
||
*
d
==
0
)
{
value
=
0
;
value
=
0
;
position
=
NULL
;
position
=
NULL
;
return
false
;
return
false
;
}
}
int
low
=
0
;
int
low
=
0
;
int
high
=
*
d
-
1
;
int
high
=
*
d
-
1
;
int
last
=
-
1
;
int
last
=
-
1
;
bool
ok
=
false
;
bool
ok
=
false
;
int
*
k
=
NULL
;
int
*
k
=
NULL
;
int
headSize
=
sizeof
(
int
);
int
headSize
=
sizeof
(
int
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
char
*
p
=
(
char
*
)
data
+
headSize
;
char
*
p
=
(
char
*
)
data
+
headSize
;
while
(
low
<=
high
)
{
while
(
low
<=
high
)
{
int
mid
=
low
+
(
high
-
low
)
/
2
;
int
mid
=
low
+
(
high
-
low
)
/
2
;
k
=
(
int
*
)(
p
+
tupleSize
*
mid
);
k
=
(
int
*
)(
p
+
tupleSize
*
mid
);
if
(
*
k
==
key
){
if
(
*
k
==
key
)
{
ok
=
true
;
ok
=
true
;
high
=
mid
-
1
;
high
=
mid
-
1
;
break
;
break
;
}
else
if
(
*
k
>
key
){
high
=
mid
-
1
;
}
}
else
{
else
if
(
*
k
>
key
)
{
low
=
mid
+
1
;
high
=
mid
-
1
;
}
else
{
low
=
mid
+
1
;
last
=
mid
;
last
=
mid
;
}
}
}
}
if
(
ok
)
{
if
(
ok
)
{
DTYPE
*
p
=
(
DTYPE
*
)((
char
*
)
k
+
sizeof
(
int
));
DTYPE
*
p
=
(
DTYPE
*
)((
char
*
)
k
+
sizeof
(
int
));
value
=
*
p
;
value
=
*
p
;
position
=
k
;
position
=
k
;
return
true
;
return
true
;
}
}
else
{
else
{
value
=
0
;
value
=
0
;
if
(
last
==
-
1
)
if
(
last
==
-
1
)
position
=
NULL
;
position
=
NULL
;
else
else
position
=
(
char
*
)
data
+
headSize
+
tupleSize
*
last
;
position
=
(
char
*
)
data
+
headSize
+
tupleSize
*
last
;
...
@@ -1693,12 +1693,12 @@ dump data to a file
...
@@ -1693,12 +1693,12 @@ dump data to a file
>> beg - the first item id
>> beg - the first item id
>> verbose - verbose level
>> verbose - verbose level
*/
*/
void
XTensor
::
Dump
(
FILE
*
file
,
const
char
*
label
,
const
int
n
,
const
int
beg
,
const
int
verbose
)
void
XTensor
::
Dump
(
FILE
*
file
,
const
char
*
label
,
const
int
n
,
const
int
beg
,
const
int
verbose
)
{
{
if
(
verbose
>
verboseLevel
)
if
(
verbose
>
verboseLevel
)
return
;
return
;
void
*
d
=
data
;
void
*
d
=
data
;
bool
isNewData
=
false
;
bool
isNewData
=
false
;
#ifdef USE_CUDA
#ifdef USE_CUDA
...
@@ -1716,7 +1716,7 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
...
@@ -1716,7 +1716,7 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
num
*=
dimSize
[
i
];
num
*=
dimSize
[
i
];
num
=
int
(
num
*
denseRatio
+
1
);
num
=
int
(
num
*
denseRatio
+
1
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
tupleSize
=
sizeof
(
int
)
+
sizeof
(
DTYPE
);
int
size
=
sizeof
(
int
)
+
tupleSize
*
(
num
);
int
size
=
sizeof
(
int
)
+
tupleSize
*
(
num
);
d
=
new
char
[
size
];
d
=
new
char
[
size
];
memset
(
d
,
0
,
size
);
memset
(
d
,
0
,
size
);
...
@@ -1730,8 +1730,8 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
...
@@ -1730,8 +1730,8 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
if
(
label
!=
NULL
)
if
(
label
!=
NULL
)
fprintf
(
file
,
"%s "
,
label
);
fprintf
(
file
,
"%s "
,
label
);
if
(
isInit
)
{
if
(
isInit
)
{
fprintf
(
file
,
"order=%d dimsize="
,
order
);
fprintf
(
file
,
"order=%d dimsize="
,
order
);
if
(
order
==
0
)
{
if
(
order
==
0
)
{
fprintf
(
file
,
"%d,"
,
dimSize
[
0
]);
fprintf
(
file
,
"%d,"
,
dimSize
[
0
]);
...
@@ -1742,21 +1742,21 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
...
@@ -1742,21 +1742,21 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
fprintf
(
file
,
","
);
fprintf
(
file
,
","
);
}
}
}
}
else
{
else
{
fprintf
(
file
,
"order=-1 dimsize=-1"
);
fprintf
(
file
,
"order=-1 dimsize=-1"
);
}
}
fprintf
(
file
,
" dtype=%s dense=%f
\n
"
,
GetDataTypeName
(
dataType
),
denseRatio
);
fprintf
(
file
,
" dtype=%s dense=%f
\n
"
,
GetDataTypeName
(
dataType
),
denseRatio
);
if
(
!
isInit
)
{
if
(
!
isInit
)
{
fprintf
(
file
,
"NULL"
);
fprintf
(
file
,
"NULL"
);
}
}
if
(
!
isSparse
)
{
if
(
!
isSparse
)
{
if
(
dataType
==
DEFAULT_DTYPE
)
{
if
(
dataType
==
DEFAULT_DTYPE
)
{
int
end
=
MIN
(
n
>
0
?
beg
+
n
:
beg
+
unitNum
,
unitNum
);
int
end
=
MIN
(
n
>
0
?
beg
+
n
:
beg
+
unitNum
,
unitNum
);
for
(
int
i
=
beg
;
i
<
end
;
i
++
)
{
for
(
int
i
=
beg
;
i
<
end
;
i
++
)
{
DTYPE
f
=
((
DTYPE
*
)
d
)[
i
];
DTYPE
f
=
((
DTYPE
*
)
d
)[
i
];
if
(
i
==
beg
)
if
(
i
==
beg
)
fprintf
(
file
,
"%e"
,
f
);
fprintf
(
file
,
"%e"
,
f
);
else
else
fprintf
(
file
,
" %e"
,
f
);
fprintf
(
file
,
" %e"
,
f
);
...
@@ -1765,9 +1765,9 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
...
@@ -1765,9 +1765,9 @@ void XTensor::Dump(FILE * file, const char * label, const int n, const int beg,
}
}
else
if
(
dataType
==
X_INT
)
{
else
if
(
dataType
==
X_INT
)
{
int
end
=
MIN
(
n
>
0
?
beg
+
n
:
beg
+
unitNum
,
unitNum
);
int
end
=
MIN
(
n
>
0
?
beg
+
n
:
beg
+
unitNum
,
unitNum
);
for
(
int
i
=
beg
;
i
<
end
;
i
++
)
{
for
(
int
i
=
beg
;
i
<
end
;
i
++
)
{
int
f
=
((
int
*
)
d
)[
i
];
int
f
=
((
int
*
)
d
)[
i
];
if
(
i
==
beg
)
if
(
i
==
beg
)
fprintf
(
file
,
"%d"
,
f
);
fprintf
(
file
,
"%d"
,
f
);
else
else
fprintf
(
file
,
" %d"
,
f
);
fprintf
(
file
,
" %d"
,
f
);
...
@@ -1807,7 +1807,7 @@ dump data to a file
...
@@ -1807,7 +1807,7 @@ dump data to a file
>> beg - the first item id
>> beg - the first item id
>> verbose - verbose level
>> verbose - verbose level
*/
*/
void
XTensor
::
Dump
(
const
XTensor
*
tensor
,
FILE
*
file
,
const
char
*
label
,
const
int
n
,
const
int
beg
,
const
int
verbose
)
void
XTensor
::
Dump
(
const
XTensor
*
tensor
,
FILE
*
file
,
const
char
*
label
,
const
int
n
,
const
int
beg
,
const
int
verbose
)
{
{
XTensor
a
(
tensor
->
order
,
tensor
->
dimSize
,
tensor
->
dataType
,
tensor
->
denseRatio
,
tensor
->
devID
,
tensor
->
mem
);
XTensor
a
(
tensor
->
order
,
tensor
->
dimSize
,
tensor
->
dataType
,
tensor
->
denseRatio
,
tensor
->
devID
,
tensor
->
mem
);
_CopyValues
(
tensor
,
&
a
);
_CopyValues
(
tensor
,
&
a
);
...
@@ -1839,7 +1839,7 @@ read data from a file
...
@@ -1839,7 +1839,7 @@ read data from a file
>> file - where to load the data
>> file - where to load the data
>> label - label of the tensor
>> label - label of the tensor
*/
*/
void
XTensor
::
Read
(
FILE
*
file
,
const
char
*
label
)
void
XTensor
::
Read
(
FILE
*
file
,
const
char
*
label
)
{
{
char
typeName
[
32
]
=
""
;
char
typeName
[
32
]
=
""
;
char
dimSizeName
[
128
]
=
""
;
char
dimSizeName
[
128
]
=
""
;
...
@@ -1858,7 +1858,7 @@ void XTensor::Read(FILE * file, const char * label)
...
@@ -1858,7 +1858,7 @@ void XTensor::Read(FILE * file, const char * label)
fgetc
(
file
);
fgetc
(
file
);
if
(
fscanf
(
file
,
"order=%d dimsize=%s dtype=%s dense=%f"
,
if
(
fscanf
(
file
,
"order=%d dimsize=%s dtype=%s dense=%f"
,
&
dimNum
,
dimSizeName
,
typeName
,
&
dRatio
)
<
4
)
{
&
dimNum
,
dimSizeName
,
typeName
,
&
dRatio
)
<
4
)
{
ShowNTErrors
(
"Incorrect format when reading the tensor!"
);
ShowNTErrors
(
"Incorrect format when reading the tensor!"
);
}
}
...
@@ -1872,7 +1872,7 @@ void XTensor::Read(FILE * file, const char * label)
...
@@ -1872,7 +1872,7 @@ void XTensor::Read(FILE * file, const char * label)
int
o
=
0
;
int
o
=
0
;
bool
sameSize
=
true
;
bool
sameSize
=
true
;
char
*
p
=
dimSizeName
;
char
*
p
=
dimSizeName
;
while
(
*
p
!=
0
)
{
while
(
*
p
!=
0
)
{
while
(
*
p
==
' '
||
*
p
==
'\t'
)
while
(
*
p
==
' '
||
*
p
==
'\t'
)
p
++
;
p
++
;
...
@@ -1896,14 +1896,14 @@ void XTensor::Read(FILE * file, const char * label)
...
@@ -1896,14 +1896,14 @@ void XTensor::Read(FILE * file, const char * label)
if
(
!
sameSize
||
dRatio
>
denseRatio
||
GetDataType
(
typeName
)
!=
dataType
)
if
(
!
sameSize
||
dRatio
>
denseRatio
||
GetDataType
(
typeName
)
!=
dataType
)
Resize
(
dimNum
,
dims
,
GetDataType
(
typeName
),
dRatio
);
Resize
(
dimNum
,
dims
,
GetDataType
(
typeName
),
dRatio
);
void
*
dataBuf
=
XMemAlloc
(
-
1
,
GetDataSizeInChar
());
void
*
dataBuf
=
XMemAlloc
(
-
1
,
GetDataSizeInChar
());
void
*
dataBackup
=
data
;
void
*
dataBackup
=
data
;
data
=
dataBuf
;
data
=
dataBuf
;
if
(
!
isSparse
)
{
if
(
!
isSparse
)
{
if
(
dataType
==
DEFAULT_DTYPE
)
{
if
(
dataType
==
DEFAULT_DTYPE
)
{
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
DTYPE
*
f
=
((
DTYPE
*
)
data
)
+
i
;
DTYPE
*
f
=
((
DTYPE
*
)
data
)
+
i
;
if
(
fscanf
(
file
,
"%e"
,
f
)
<
1
)
{
if
(
fscanf
(
file
,
"%e"
,
f
)
<
1
)
{
ShowNTErrors
(
"Incorrect tensor format!"
);
ShowNTErrors
(
"Incorrect tensor format!"
);
}
}
...
@@ -1956,13 +1956,13 @@ void XTensor::BinaryRead(FILE* file, size_t offset)
...
@@ -1956,13 +1956,13 @@ void XTensor::BinaryRead(FILE* file, size_t offset)
fseek
(
file
,
offset
,
0
);
fseek
(
file
,
offset
,
0
);
switch
(
dataType
)
{
switch
(
dataType
)
{
case
X_INT
:
{
case
X_INT
:
{
int
*
d
=
new
int
[
unitNum
];
int
*
d
=
new
int
[
unitNum
];
fread
(
d
,
sizeof
(
int
),
unitNum
,
file
);
fread
(
d
,
sizeof
(
int
),
unitNum
,
file
);
SetData
(
d
,
unitNum
);
SetData
(
d
,
unitNum
);
delete
[]
d
;
delete
[]
d
;
}
}
default
:
{
default
:
{
float
*
d
=
new
float
[
unitNum
];
float
*
d
=
new
float
[
unitNum
];
fread
(
d
,
sizeof
(
float
),
unitNum
,
file
);
fread
(
d
,
sizeof
(
float
),
unitNum
,
file
);
SetData
(
d
,
unitNum
);
SetData
(
d
,
unitNum
);
delete
[]
d
;
delete
[]
d
;
...
@@ -1974,7 +1974,7 @@ void XTensor::BinaryRead(FILE* file, size_t offset)
...
@@ -1974,7 +1974,7 @@ void XTensor::BinaryRead(FILE* file, size_t offset)
flush the data to the target device
flush the data to the target device
>> targetMem - memory pool on the target device
>> targetMem - memory pool on the target device
*/
*/
void
XTensor
::
FlushToMem
(
XMem
*
targetMem
)
void
XTensor
::
FlushToMem
(
XMem
*
targetMem
)
{
{
if
(
targetMem
==
NULL
)
if
(
targetMem
==
NULL
)
return
;
return
;
...
@@ -1987,7 +1987,7 @@ void XTensor::FlushToMem(XMem * targetMem)
...
@@ -1987,7 +1987,7 @@ void XTensor::FlushToMem(XMem * targetMem)
CudaCPUToGPUFlush
(
&
l
,
targetMem
->
devID
,
targetMem
);
CudaCPUToGPUFlush
(
&
l
,
targetMem
->
devID
,
targetMem
);
}
}
else
if
(
mem
!=
targetMem
)
{
else
if
(
mem
!=
targetMem
)
{
void
*
tmpData
=
targetMem
->
Alloc
(
targetMem
->
devID
,
GetDataSizeInChar
());
void
*
tmpData
=
targetMem
->
Alloc
(
targetMem
->
devID
,
GetDataSizeInChar
());
XMemCopy
(
tmpData
,
targetMem
->
devID
,
data
,
devID
,
GetDataSizeInChar
());
XMemCopy
(
tmpData
,
targetMem
->
devID
,
data
,
devID
,
GetDataSizeInChar
());
data
=
tmpData
;
data
=
tmpData
;
mem
=
targetMem
;
mem
=
targetMem
;
...
@@ -2016,24 +2016,24 @@ allocate the memory space of the tensor (in the global memory)
...
@@ -2016,24 +2016,24 @@ allocate the memory space of the tensor (in the global memory)
>> myMem - the memory pool we are using
>> myMem - the memory pool we are using
>> useBuf - indicates whether we use the buffer in the memory pool
>> useBuf - indicates whether we use the buffer in the memory pool
*/
*/
void
XTensor
::
AllocateData
(
XTensor
*
tensor
,
XMem
*
myMem
,
bool
useBuf
)
void
XTensor
::
AllocateData
(
XTensor
*
tensor
,
XMem
*
myMem
,
bool
useBuf
)
{
{
if
(
tensor
==
NULL
)
if
(
tensor
==
NULL
)
return
;
return
;
if
(
myMem
==
NULL
)
{
if
(
myMem
==
NULL
)
{
if
(
tensor
->
data
!=
NULL
)
if
(
tensor
->
data
!=
NULL
)
FreeData
(
tensor
,
NULL
,
false
);
FreeData
(
tensor
,
NULL
,
false
);
tensor
->
data
=
XMemAlloc
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
tensor
->
data
=
XMemAlloc
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
tensor
->
isInGlobalMem
=
true
;
tensor
->
isInGlobalMem
=
true
;
}
}
else
{
else
{
CheckNTErrors
((
tensor
->
data
==
NULL
),
"Cannot renew the space for the tensor"
);
CheckNTErrors
((
tensor
->
data
==
NULL
),
"Cannot renew the space for the tensor"
);
if
(
useBuf
)
{
if
(
useBuf
)
{
tensor
->
data
=
myMem
->
AllocBuf
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
tensor
->
data
=
myMem
->
AllocBuf
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
tensor
->
isInGlobalMem
=
false
;
tensor
->
isInGlobalMem
=
false
;
}
}
else
{
else
{
tensor
->
data
=
myMem
->
AllocGlobal
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
tensor
->
data
=
myMem
->
AllocGlobal
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
tensor
->
isInGlobalMem
=
true
;
tensor
->
isInGlobalMem
=
true
;
}
}
...
@@ -2048,16 +2048,16 @@ free the memory space of the tensor (in the global memory)
...
@@ -2048,16 +2048,16 @@ free the memory space of the tensor (in the global memory)
>> myMem - the memory pool we are using
>> myMem - the memory pool we are using
>> useBuf - indicates whether we use the buffer in the memory pool
>> useBuf - indicates whether we use the buffer in the memory pool
*/
*/
void
XTensor
::
FreeData
(
XTensor
*
tensor
,
XMem
*
myMem
,
bool
useBuf
)
void
XTensor
::
FreeData
(
XTensor
*
tensor
,
XMem
*
myMem
,
bool
useBuf
)
{
{
if
(
tensor
==
NULL
)
if
(
tensor
==
NULL
)
return
;
return
;
if
(
myMem
==
NULL
)
{
if
(
myMem
==
NULL
)
{
XMemFree
(
tensor
->
devID
,
tensor
->
data
);
XMemFree
(
tensor
->
devID
,
tensor
->
data
);
}
}
else
{
else
{
if
(
tensor
->
isInGlobalMem
)
if
(
tensor
->
isInGlobalMem
)
myMem
->
ReleaseGlobal
(
tensor
->
devID
,
tensor
->
data
);
myMem
->
ReleaseGlobal
(
tensor
->
devID
,
tensor
->
data
);
else
else
myMem
->
ReleaseBuf
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
myMem
->
ReleaseBuf
(
tensor
->
devID
,
tensor
->
GetDataSizeInChar
());
...
@@ -2068,27 +2068,27 @@ void XTensor::FreeData(XTensor * tensor, XMem * myMem, bool useBuf)
...
@@ -2068,27 +2068,27 @@ void XTensor::FreeData(XTensor * tensor, XMem * myMem, bool useBuf)
}
}
/* overloading of the plus-sign */
/* overloading of the plus-sign */
XTensor
operator
+
(
const
DTYPE
shift
,
const
XTensor
&
tensor
)
XTensor
operator
+
(
const
DTYPE
shift
,
const
XTensor
&
tensor
)
{
{
return
ScaleAndShift
(
tensor
,
1
,
shift
);
return
ScaleAndShift
(
tensor
,
1
,
shift
);
}
}
/* overloading of the minus-sign */
/* overloading of the minus-sign */
XTensor
operator
-
(
const
DTYPE
shift
,
const
XTensor
&
tensor
)
XTensor
operator
-
(
const
DTYPE
shift
,
const
XTensor
&
tensor
)
{
{
return
ScaleAndShift
(
tensor
,
1
,
-
shift
);
return
ScaleAndShift
(
tensor
,
1
,
-
shift
);
}
}
/* overloading of the multiply-sign */
/* overloading of the multiply-sign */
XTensor
operator
*
(
const
DTYPE
scale
,
const
XTensor
&
tensor
)
XTensor
operator
*
(
const
DTYPE
scale
,
const
XTensor
&
tensor
)
{
{
return
ScaleAndShift
(
tensor
,
scale
,
0
);
return
ScaleAndShift
(
tensor
,
scale
,
0
);
}
}
/* overloading of the division-sign */
/* overloading of the division-sign */
XTensor
operator
/
(
const
DTYPE
scale
,
const
XTensor
&
tensor
)
XTensor
operator
/
(
const
DTYPE
scale
,
const
XTensor
&
tensor
)
{
{
return
ScaleAndShift
(
tensor
,
(
DTYPE
)
1
/
scale
,
0
);
return
ScaleAndShift
(
tensor
,
(
DTYPE
)
1
.0
F
/
scale
,
0
);
}
}
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论