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
6bc6a058
Commit
6bc6a058
authored
Feb 18, 2020
by
huchi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update xtensor function call
parent
c177e3c5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
18 行增加
和
17 行删除
+18
-17
source/sample/transformer/T2TModel.cpp
+16
-15
source/sample/transformer/T2TSearch.cpp
+2
-2
没有找到文件。
source/sample/transformer/T2TModel.cpp
查看文件 @
6bc6a058
...
@@ -216,8 +216,8 @@ make the mask for training MT models
...
@@ -216,8 +216,8 @@ make the mask for training MT models
>> maksEncDec - mask of the decoder enc-dec attention
>> maksEncDec - mask of the decoder enc-dec attention
*/
*/
void
T2TModel
::
MakeMTMask
(
XTensor
&
inputEnc
,
XTensor
&
inputDec
,
void
T2TModel
::
MakeMTMask
(
XTensor
&
inputEnc
,
XTensor
&
inputDec
,
XTensor
&
paddingEnc
,
XTensor
&
paddingDec
,
XTensor
&
paddingEnc
,
XTensor
&
paddingDec
,
XTensor
&
maskEnc
,
XTensor
&
maskDec
,
XTensor
&
maskEncDec
)
XTensor
&
maskEnc
,
XTensor
&
maskDec
,
XTensor
&
maskEncDec
)
{
{
int
len
=
inputDec
.
GetDim
(
inputDec
.
order
-
1
);
int
len
=
inputDec
.
GetDim
(
inputDec
.
order
-
1
);
int
*
dims
=
new
int
[
inputDec
.
order
+
2
];
int
*
dims
=
new
int
[
inputDec
.
order
+
2
];
...
@@ -225,7 +225,7 @@ void T2TModel::MakeMTMask(XTensor& inputEnc, XTensor& inputDec,
...
@@ -225,7 +225,7 @@ void T2TModel::MakeMTMask(XTensor& inputEnc, XTensor& inputDec,
dims
[
i
+
1
]
=
inputDec
.
GetDim
(
i
);
dims
[
i
+
1
]
=
inputDec
.
GetDim
(
i
);
dims
[
0
]
=
nhead
;
dims
[
0
]
=
nhead
;
dims
[
inputDec
.
order
+
1
]
=
len
;
dims
[
inputDec
.
order
+
1
]
=
len
;
InitTensor
(
&
maskDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
paddingDec
.
devID
);
InitTensor
V2
(
&
maskDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
1.0
F
,
paddingDec
.
devID
);
/* an upper triangular matrix where the cells of the upper triangular are set to -1e-9.
/* an upper triangular matrix where the cells of the upper triangular are set to -1e-9.
this matrix can be used to prevent the attention to current or following words in
this matrix can be used to prevent the attention to current or following words in
...
@@ -235,10 +235,10 @@ void T2TModel::MakeMTMask(XTensor& inputEnc, XTensor& inputDec,
...
@@ -235,10 +235,10 @@ void T2TModel::MakeMTMask(XTensor& inputEnc, XTensor& inputDec,
/* encoder-decoder mask that prevents the attention to padding dummy words */
/* encoder-decoder mask that prevents the attention to padding dummy words */
dims
[
inputDec
.
order
+
1
]
=
inputEnc
.
GetDim
(
inputEnc
.
order
-
1
);
dims
[
inputDec
.
order
+
1
]
=
inputEnc
.
GetDim
(
inputEnc
.
order
-
1
);
InitTensor
(
&
maskEncDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
1.0
F
,
paddingEnc
.
devID
);
InitTensor
V2
(
&
maskEncDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
1.0
F
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPEnc
=
NewTensorBuf
(
paddingEnc
.
order
+
1
,
dims
+
1
,
paddingEnc
.
dataType
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPEnc
=
NewTensorBuf
V2
(
paddingEnc
.
order
+
1
,
dims
+
1
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPDec
=
NewTensorBuf
(
maskEncDecTMPEnc
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPDec
=
NewTensorBuf
V2
(
paddingEnc
.
order
+
1
,
dims
+
1
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
_Unsqueeze
(
&
paddingEnc
,
maskEncDecTMPEnc
,
paddingEnc
.
order
-
1
,
paddingDec
.
GetDim
(
-
1
));
_Unsqueeze
(
&
paddingEnc
,
maskEncDecTMPEnc
,
paddingEnc
.
order
-
1
,
paddingDec
.
GetDim
(
-
1
));
_ScaleAndShiftMe
(
maskEncDecTMPEnc
,
1e9
F
,
-
1e9
F
);
_ScaleAndShiftMe
(
maskEncDecTMPEnc
,
1e9
F
,
-
1e9
F
);
...
@@ -254,13 +254,13 @@ void T2TModel::MakeMTMask(XTensor& inputEnc, XTensor& inputDec,
...
@@ -254,13 +254,13 @@ void T2TModel::MakeMTMask(XTensor& inputEnc, XTensor& inputDec,
dimsPadding
[
paddingEnc
.
order
-
1
]
=
paddingEnc
.
GetDim
(
-
1
);
dimsPadding
[
paddingEnc
.
order
-
1
]
=
paddingEnc
.
GetDim
(
-
1
);
dimsPadding
[
paddingEnc
.
order
]
=
paddingEnc
.
GetDim
(
-
1
);
dimsPadding
[
paddingEnc
.
order
]
=
paddingEnc
.
GetDim
(
-
1
);
XTensor
*
padding2
=
NewTensorBuf
(
paddingEnc
.
order
+
1
,
dimsPadding
,
paddingEnc
.
dataType
,
paddingEnc
.
devID
);
XTensor
*
padding2
=
NewTensorBuf
V2
(
paddingEnc
.
order
+
1
,
dimsPadding
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
for
(
int
i
=
0
;
i
<
padding2
->
order
;
i
++
)
for
(
int
i
=
0
;
i
<
padding2
->
order
;
i
++
)
dimsPadding
[
i
+
1
]
=
padding2
->
GetDim
(
i
);
dimsPadding
[
i
+
1
]
=
padding2
->
GetDim
(
i
);
dimsPadding
[
0
]
=
nhead
;
dimsPadding
[
0
]
=
nhead
;
XTensor
*
padding3
=
NewTensorBuf
(
paddingEnc
.
order
+
2
,
dimsPadding
,
paddingEnc
.
dataType
,
paddingEnc
.
devID
);
XTensor
*
padding3
=
NewTensorBuf
V2
(
paddingEnc
.
order
+
2
,
dimsPadding
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
/* mask of the padding */
/* mask of the padding */
_Unsqueeze
(
&
paddingEnc
,
padding2
,
paddingEnc
.
order
-
1
,
paddingEnc
.
GetDim
(
-
1
));
_Unsqueeze
(
&
paddingEnc
,
padding2
,
paddingEnc
.
order
-
1
,
paddingEnc
.
GetDim
(
-
1
));
...
@@ -296,13 +296,13 @@ void T2TModel::MakeMTMaskEnc(XTensor& inputEnc, XTensor& paddingEnc, XTensor& ma
...
@@ -296,13 +296,13 @@ void T2TModel::MakeMTMaskEnc(XTensor& inputEnc, XTensor& paddingEnc, XTensor& ma
dimsPadding
[
paddingEnc
.
order
-
1
]
=
paddingEnc
.
GetDim
(
-
1
);
dimsPadding
[
paddingEnc
.
order
-
1
]
=
paddingEnc
.
GetDim
(
-
1
);
dimsPadding
[
paddingEnc
.
order
]
=
paddingEnc
.
GetDim
(
-
1
);
dimsPadding
[
paddingEnc
.
order
]
=
paddingEnc
.
GetDim
(
-
1
);
XTensor
*
padding2
=
NewTensorBuf
(
paddingEnc
.
order
+
1
,
dimsPadding
,
paddingEnc
.
dataType
,
paddingEnc
.
devID
);
XTensor
*
padding2
=
NewTensorBuf
V2
(
paddingEnc
.
order
+
1
,
dimsPadding
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
for
(
int
i
=
0
;
i
<
padding2
->
order
;
i
++
)
for
(
int
i
=
0
;
i
<
padding2
->
order
;
i
++
)
dimsPadding
[
i
+
1
]
=
padding2
->
GetDim
(
i
);
dimsPadding
[
i
+
1
]
=
padding2
->
GetDim
(
i
);
dimsPadding
[
0
]
=
nhead
;
dimsPadding
[
0
]
=
nhead
;
XTensor
*
padding3
=
NewTensorBuf
(
paddingEnc
.
order
+
2
,
dimsPadding
,
paddingEnc
.
dataType
,
paddingEnc
.
devID
);
XTensor
*
padding3
=
NewTensorBuf
V2
(
paddingEnc
.
order
+
2
,
dimsPadding
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
/* mask of the padding */
/* mask of the padding */
_Unsqueeze
(
&
paddingEnc
,
padding2
,
paddingEnc
.
order
-
1
,
paddingEnc
.
GetDim
(
-
1
));
_Unsqueeze
(
&
paddingEnc
,
padding2
,
paddingEnc
.
order
-
1
,
paddingEnc
.
GetDim
(
-
1
));
...
@@ -340,7 +340,7 @@ void T2TModel::MakeMTMaskDec(XTensor &inputEnc, XTensor &inputDec,
...
@@ -340,7 +340,7 @@ void T2TModel::MakeMTMaskDec(XTensor &inputEnc, XTensor &inputDec,
dims
[
i
+
1
]
=
inputDec
.
GetDim
(
i
);
dims
[
i
+
1
]
=
inputDec
.
GetDim
(
i
);
dims
[
0
]
=
nhead
;
dims
[
0
]
=
nhead
;
dims
[
inputDec
.
order
+
1
]
=
len
;
dims
[
inputDec
.
order
+
1
]
=
len
;
InitTensor
(
&
maskDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
paddingDec
.
devID
);
InitTensor
V2
(
&
maskDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
1.0
F
,
paddingDec
.
devID
);
/* An upper triangular matrix where the cells of the upper triangular are set to -1e-9.
/* An upper triangular matrix where the cells of the upper triangular are set to -1e-9.
This matrix can be used to block the attention to current or following words in
This matrix can be used to block the attention to current or following words in
...
@@ -355,11 +355,12 @@ void T2TModel::MakeMTMaskDec(XTensor &inputEnc, XTensor &inputDec,
...
@@ -355,11 +355,12 @@ void T2TModel::MakeMTMaskDec(XTensor &inputEnc, XTensor &inputDec,
/* encoder-decoder mask that prevents the attention to padding dummy words */
/* encoder-decoder mask that prevents the attention to padding dummy words */
dims
[
inputDec
.
order
+
1
]
=
inputEnc
.
GetDim
(
inputEnc
.
order
-
1
);
dims
[
inputDec
.
order
+
1
]
=
inputEnc
.
GetDim
(
inputEnc
.
order
-
1
);
InitTensor
(
&
maskEncDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
paddingEnc
.
devID
);
InitTensor
V2
(
&
maskEncDec
,
inputDec
.
order
+
2
,
dims
,
X_FLOAT
,
1.0
F
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPEnc
=
NewTensorBuf
(
paddingEnc
.
order
+
1
,
dims
+
1
,
paddingEnc
.
dataType
,
XTensor
*
maskEncDecTMPEnc
=
NewTensorBufV2
(
paddingEnc
.
order
+
1
,
dims
+
1
,
paddingEnc
.
devID
);
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPDec
=
NewTensorBuf
(
maskEncDecTMPEnc
,
paddingEnc
.
devID
);
XTensor
*
maskEncDecTMPDec
=
NewTensorBufV2
(
paddingEnc
.
order
+
1
,
dims
+
1
,
paddingEnc
.
dataType
,
1.0
F
,
paddingEnc
.
devID
);
_Unsqueeze
(
&
paddingEnc
,
maskEncDecTMPEnc
,
paddingEnc
.
order
-
1
,
paddingDec
.
GetDim
(
-
1
));
_Unsqueeze
(
&
paddingEnc
,
maskEncDecTMPEnc
,
paddingEnc
.
order
-
1
,
paddingDec
.
GetDim
(
-
1
));
...
...
source/sample/transformer/T2TSearch.cpp
查看文件 @
6bc6a058
...
@@ -294,14 +294,14 @@ void T2TSearch::Generate(T2TStateBundle* beam)
...
@@ -294,14 +294,14 @@ void T2TSearch::Generate(T2TStateBundle* beam)
InitTensorV2
(
&
indexCPU
,
order
,
dimsTopK
,
X_INT
,
1.0
F
,
-
1
);
InitTensorV2
(
&
indexCPU
,
order
,
dimsTopK
,
X_INT
,
1.0
F
,
-
1
);
/* TODO: check the mask - mask the first and the padding id */
/* TODO: check the mask - mask the first and the padding id */
/*
int dimMask[]{ score.GetDim(-1) };
int
dimMask
[]{
score
.
GetDim
(
-
1
)
};
XTensor
mask
;
XTensor
mask
;
InitTensorV2
(
&
mask
,
1
,
dimMask
,
X_FLOAT
,
1.0
F
,
-
1
);
InitTensorV2
(
&
mask
,
1
,
dimMask
,
X_FLOAT
,
1.0
F
,
-
1
);
mask
.
SetZeroAll
();
mask
.
SetZeroAll
();
mask
.
Set1D
(
-
1e9
F
,
0
);
mask
.
Set1D
(
-
1e9
F
,
0
);
mask
.
Set1D
(
-
1e9
F
,
1
);
mask
.
Set1D
(
-
1e9
F
,
1
);
mask
.
SetDevice
(
score
.
devID
);
mask
.
SetDevice
(
score
.
devID
);
_SumDim(&score, &mask, 2);
*/
_SumDim
(
&
score
,
&
mask
,
2
);
score
.
Reshape
(
order
,
dimsBeam
);
score
.
Reshape
(
order
,
dimsBeam
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论