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
93bc3158
Commit
93bc3158
authored
Oct 13, 2019
by
xuchen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the stack function. I'm so hungry.
parent
bc49d32a
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
240 行增加
和
5 行删除
+240
-5
source/tensor/XName.cpp
+2
-0
source/tensor/XName.h
+2
-1
source/tensor/XTensor.h
+1
-1
source/tensor/core/CHeader.h
+1
-0
source/tensor/core/shape/Merge.cpp
+1
-1
source/tensor/core/shape/Stack.cpp
+189
-0
source/tensor/core/shape/Stack.h
+42
-0
source/tensor/test/TSetData.cpp
+2
-2
没有找到文件。
source/tensor/XName.cpp
查看文件 @
93bc3158
...
@@ -135,6 +135,8 @@ const char * GetOPName(int type)
...
@@ -135,6 +135,8 @@ const char * GetOPName(int type)
return
"S_SPLIT"
;
return
"S_SPLIT"
;
else
if
(
type
==
SHAPE_SPLIT_LIST
)
else
if
(
type
==
SHAPE_SPLIT_LIST
)
return
"S_SPLIT_LIST"
;
return
"S_SPLIT_LIST"
;
else
if
(
type
==
SHAPE_STACK
)
return
"S_SHAPE_STACK"
;
else
if
(
type
==
SHAPE_SQUEEZE
)
else
if
(
type
==
SHAPE_SQUEEZE
)
return
"S_SQUEEZE"
;
return
"S_SQUEEZE"
;
else
if
(
type
==
SHAPE_TRANSPOSE
)
else
if
(
type
==
SHAPE_TRANSPOSE
)
...
...
source/tensor/XName.h
查看文件 @
93bc3158
...
@@ -99,7 +99,8 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
...
@@ -99,7 +99,8 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
#define SHAPE_RESHAPE SHAPE_PERMUTE + 1
#define SHAPE_RESHAPE SHAPE_PERMUTE + 1
#define SHAPE_SPLIT SHAPE_RESHAPE + 1
#define SHAPE_SPLIT SHAPE_RESHAPE + 1
#define SHAPE_SPLIT_LIST SHAPE_SPLIT + 1
#define SHAPE_SPLIT_LIST SHAPE_SPLIT + 1
#define SHAPE_SQUEEZE SHAPE_SPLIT_LIST + 1
#define SHAPE_STACK SHAPE_SPLIT_LIST + 1
#define SHAPE_SQUEEZE SHAPE_STACK + 1
#define SHAPE_TRANSPOSE SHAPE_SQUEEZE + 1
#define SHAPE_TRANSPOSE SHAPE_SQUEEZE + 1
#define SHAPE_UNSQUEEZE SHAPE_TRANSPOSE + 1
#define SHAPE_UNSQUEEZE SHAPE_TRANSPOSE + 1
...
...
source/tensor/XTensor.h
查看文件 @
93bc3158
...
@@ -430,7 +430,7 @@ public:
...
@@ -430,7 +430,7 @@ public:
bool
BinarySearch
(
int
key
,
DTYPE
&
value
,
void
*
&
position
)
const
;
bool
BinarySearch
(
int
key
,
DTYPE
&
value
,
void
*
&
position
)
const
;
/* dump data to a file */
/* dump data to a file */
void
Dump
(
FILE
*
file
,
const
char
*
label
=
NULL
,
const
int
n
=
-
1
,
const
int
beg
=
0
,
const
int
verbose
=
0
);
void
Dump
(
FILE
*
file
=
stdout
,
const
char
*
label
=
NULL
,
const
int
n
=
-
1
,
const
int
beg
=
0
,
const
int
verbose
=
0
);
/* dump data to a file */
/* dump data to a file */
static
static
...
...
source/tensor/core/CHeader.h
查看文件 @
93bc3158
...
@@ -83,6 +83,7 @@
...
@@ -83,6 +83,7 @@
#include "shape/Permute.h"
#include "shape/Permute.h"
#include "shape/Split.h"
#include "shape/Split.h"
#include "shape/Squeeze.h"
#include "shape/Squeeze.h"
#include "shape/Stack.h"
#include "shape/Transpose.h"
#include "shape/Transpose.h"
#include "shape/Unsqueeze.h"
#include "shape/Unsqueeze.h"
...
...
source/tensor/core/shape/Merge.cpp
查看文件 @
93bc3158
...
@@ -284,7 +284,7 @@ void _Merge(const TensorList * smalls, XTensor * t, int whereToMerge)
...
@@ -284,7 +284,7 @@ void _Merge(const TensorList * smalls, XTensor * t, int whereToMerge)
CheckNTErrors
((
smalls
!=
NULL
),
"Invalid list!"
);
CheckNTErrors
((
smalls
!=
NULL
),
"Invalid list!"
);
CheckNTErrors
((
smalls
->
count
>
0
),
"Empty list!"
);
CheckNTErrors
((
smalls
->
count
>
0
),
"Empty list!"
);
CheckNTErrors
((
whereToMerge
>=
0
&&
whereToMerge
<
t
->
order
),
"Wrong range of
whereToMerge"
);
CheckNTErrors
((
whereToMerge
>=
0
&&
whereToMerge
<
t
->
order
),
"Wrong range of whereToMerge"
);
bool
uniform
=
true
;
bool
uniform
=
true
;
...
...
source/tensor/core/shape/Stack.cpp
0 → 100644
查看文件 @
93bc3158
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2017, Natural Language Processing Lab, Northestern 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.
*/
/*
* $Created by: Xu Chen (email: hello_master1954@163.com) 2019-10-13
*/
#include "Stack.h"
#include "../../XUtility.h"
#include "../../XName.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/* stack small tensors into a big tensor along with a dimension */
void
_Stack
(
const
TensorList
*
smalls
,
XTensor
*
t
,
int
dim
)
{
dim
=
(
dim
<
0
?
t
->
order
-
1
:
dim
);
int
count
=
smalls
->
count
;
CheckNTErrors
(
smalls
!=
NULL
,
"Invalid list!"
);
CheckNTErrors
(
count
>
0
,
"Empty list!"
);
CheckNTErrors
(
dim
>=
0
&&
dim
<
t
->
order
,
"Wrong range of dim"
);
for
(
int
i
=
1
;
i
<
count
;
i
++
)
{
XTensor
*
tmp1
=
smalls
->
GetItem
(
i
);
XTensor
*
tmp2
=
smalls
->
GetItem
(
i
-
1
);
CheckNTErrors
(
XTensor
::
IsSameShaped
(
tmp1
,
tmp2
),
"The input tensor must be same size!"
);
}
int
blockSize
=
1
;
int
blockNum
=
1
;
int
gridSize
=
1
;
int
gridNum
=
1
;
XTensor
*
smallsItem0
=
smalls
->
GetItem
(
0
);
int
unitNum
=
smallsItem0
->
unitNum
;
int
unitSize
=
smallsItem0
->
unitSize
;
int
itemSize
=
unitNum
*
unitSize
;
for
(
int
i
=
0
;
i
<
smallsItem0
->
order
;
i
++
)
{
if
(
i
>=
dim
)
blockSize
*=
smallsItem0
->
dimSize
[
i
];
else
blockNum
*=
smallsItem0
->
dimSize
[
i
];
}
/* merging with fewer data copy operations */
if
(
count
*
gridNum
<=
MIN_TENSOR_MERGE_LIST_NUM
)
{
int
sPitch
=
blockSize
*
unitSize
;
int
tPtich
=
blockSize
*
count
*
unitSize
;
int
mSize
=
blockSize
*
unitSize
;
int
n
=
blockNum
;
int
sStep
=
0
;
int
tStep
=
blockSize
*
unitSize
;
char
*
tData
=
(
char
*
)
t
->
data
;
for
(
int
k
=
0
;
k
<
count
;
k
++
)
{
XTensor
*
s
=
smalls
->
GetItem
(
k
);
char
*
sData
=
(
char
*
)
s
->
data
;
XMemCopy2D
(
tData
+
k
*
tStep
,
tPtich
,
t
->
devID
,
sData
+
k
*
sStep
,
sPitch
,
s
->
devID
,
mSize
,
n
);
}
}
else
{
ShowNTErrors
(
"TO DO!!!"
);
}
}
/* stack small tensors into a big tensor along with a dimension (return an XTensor structure) */
XTensor
Stack
(
const
TensorList
&
smalls
,
int
dim
)
{
int
count
=
smalls
.
count
;
CheckNTErrors
(
count
>
0
,
"Empty list!"
);
CheckNTErrors
(
dim
>=
0
,
"Illegal dimension to concatenate!"
);
XTensor
*
tensor
=
smalls
.
GetItem
(
0
);
int
order
=
tensor
->
order
+
1
;
int
*
dimSize
=
new
int
[
order
];
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
if
(
i
<
dim
)
dimSize
[
i
]
=
tensor
->
GetDim
(
i
);
else
if
(
i
>
dim
)
dimSize
[
i
]
=
tensor
->
GetDim
(
i
);
else
if
(
i
==
dim
)
dimSize
[
i
]
=
count
;
}
float
dr
=
(
!
tensor
->
isSparse
)
?
1.0
F
:
tensor
->
denseRatio
;
XTensor
t
(
order
,
dimSize
,
tensor
->
dataType
,
dr
,
tensor
->
devID
,
tensor
->
mem
);
t
.
SetTMPFlag
();
/* destroy variables */
delete
[]
dimSize
;
/* call _Stack function */
_Stack
(
&
smalls
,
&
t
,
dim
);
/* tensor connection */
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
XTensor
*
tmp
=
smalls
.
GetItem
(
i
);
if
(
tmp
->
enableGrad
==
false
)
return
t
;
}
XLink
::
MakeLink
(
&
smalls
,
&
t
,
SHAPE_STACK
);
XLink
::
AddParamToHeadInt
(
&
t
,
dim
);
return
t
;
}
/* check the shape of target tensor */
bool
CheckStackShape
(
const
TensorList
&
smalls
,
XTensor
&
t
,
int
dim
)
{
XTensor
*
tensor
=
(
XTensor
*
)
smalls
.
GetItem
(
0
);
int
order
=
tensor
->
order
;
for
(
int
i
=
0
;
i
<
tensor
->
order
;
i
++
)
{
if
(
i
<
dim
)
if
(
t
.
GetDim
(
i
)
!=
tensor
->
GetDim
(
i
))
return
false
;
else
if
(
i
>
dim
)
if
(
t
.
GetDim
(
i
)
!=
tensor
->
GetDim
(
i
-
1
))
return
false
;
else
if
(
i
==
dim
)
if
(
t
.
GetDim
(
i
)
!=
smalls
.
count
)
return
false
;
}
return
true
;
}
/* stack small tensors into a big tensor along with a dimension */
void
Stack
(
const
TensorList
&
smalls
,
XTensor
&
t
,
int
dim
)
{
int
count
=
smalls
.
count
;
CheckNTErrors
(
count
>
0
,
"Empty list!"
);
CheckNTErrors
(
dim
>=
0
,
"Illegal dimension to concatenate!"
);
if
(
!
t
.
isInit
||
!
CheckStackShape
(
smalls
,
t
,
dim
))
{
XTensor
*
tensor
=
smalls
.
GetItem
(
0
);
int
order
=
tensor
->
order
+
1
;
int
*
dimSize
=
new
int
[
order
];
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
if
(
i
<
dim
)
dimSize
[
i
]
=
tensor
->
GetDim
(
i
);
else
if
(
i
>
dim
)
dimSize
[
i
]
=
tensor
->
GetDim
(
i
-
1
);
else
if
(
i
==
dim
)
dimSize
[
i
]
=
count
;
}
float
dr
=
(
!
tensor
->
isSparse
)
?
1.0
F
:
tensor
->
denseRatio
;
InitTensor
(
&
t
,
order
,
dimSize
,
tensor
->
dataType
,
dr
,
tensor
->
devID
,
tensor
->
mem
);
/* destroy variables */
delete
[]
dimSize
;
}
/* call _Stack function */
_Stack
(
&
smalls
,
&
t
,
dim
);
/* tensor connection */
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
XTensor
*
tmp
=
smalls
.
GetItem
(
i
);
if
(
tmp
->
enableGrad
==
false
)
return
;
}
XLink
::
MakeLink
(
&
smalls
,
&
t
,
SHAPE_STACK
);
XLink
::
AddParamToHeadInt
(
&
t
,
dim
);
}
}
// namespace nts(NiuTrans.Tensor)
source/tensor/core/shape/Stack.h
0 → 100644
查看文件 @
93bc3158
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2017, Natural Language Processing Lab, Northestern 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.
*/
/*
* $Created by: Xu Chen (email: hello_master1954@163.com) 2019-10-13
* It's so cold outside. It's too hard for me to get out.
*/
#ifndef __STACK_H__
#define __STACK_H__
#include "../../XTensor.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/* stack small tensors into a big tensor along with a dimension */
void
_Stack
(
const
TensorList
*
smalls
,
XTensor
*
t
,
int
dim
);
/* stack small tensors into a big tensor along with a dimension (return an XTensor structure) */
XTensor
Stack
(
const
TensorList
&
list
,
int
leadingDim
);
/* stack small tensors into a big tensor along with a dimension */
void
Stack
(
const
TensorList
&
smalls
,
XTensor
&
t
,
int
dim
);
}
// namespace nts(NiuTrans.Tensor)
#endif // __STACK_H__
\ No newline at end of file
source/tensor/test/TSetData.cpp
查看文件 @
93bc3158
...
@@ -433,7 +433,7 @@ bool TestSetData6()
...
@@ -433,7 +433,7 @@ bool TestSetData6()
s
->
SetZeroAll
();
s
->
SetZeroAll
();
/* call _SetDataRange function */
/* call _SetDataRange function */
_SetDataRange
(
s
,
5.2
,
-
3.2
,
-
2
);
_SetDataRange
(
s
,
5.2
F
,
-
3.2
F
,
-
2.0
F
);
/* check results */
/* check results */
cpuTest
=
s
->
CheckData
(
answer
,
unitNum
,
1e-4
F
);
cpuTest
=
s
->
CheckData
(
answer
,
unitNum
,
1e-4
F
);
...
@@ -449,7 +449,7 @@ bool TestSetData6()
...
@@ -449,7 +449,7 @@ bool TestSetData6()
sGPU
->
SetZeroAll
();
sGPU
->
SetZeroAll
();
/* call _SetDataRange function */
/* call _SetDataRange function */
_SetDataRange
(
sGPU
,
5.2
,
-
3.2
,
-
2
);
_SetDataRange
(
sGPU
,
5.2
F
,
-
3.2
F
,
-
2.0
F
);
gpuTest
=
sGPU
->
CheckData
(
answer
,
unitNum
,
1e-4
F
);
gpuTest
=
sGPU
->
CheckData
(
answer
,
unitNum
,
1e-4
F
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论