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
a4e6ab64
Commit
a4e6ab64
authored
Jul 13, 2019
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix the memory leak bug
parent
06053721
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
57 行增加
和
11 行删除
+57
-11
source/network/Main.cpp
+2
-3
source/sample/transformer/T2TDecoder.cpp
+8
-0
source/sample/transformer/T2TPredictor.cpp
+1
-1
source/sample/transformer/T2TSearch.cpp
+43
-7
source/sample/transformer/T2TSearch.h
+3
-0
没有找到文件。
source/network/Main.cpp
查看文件 @
a4e6ab64
...
@@ -42,9 +42,8 @@ using namespace transformer;
...
@@ -42,9 +42,8 @@ using namespace transformer;
int
main
(
int
argc
,
const
char
**
argv
)
int
main
(
int
argc
,
const
char
**
argv
)
{
{
//_CrtSetBreakAlloc(896);
//_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
//BackwardTest();
//_CrtSetBreakAlloc(2708);
//return 0;
if
(
argc
>
1
&&
!
strcmp
(
argv
[
1
],
"-fnnlm"
))
if
(
argc
>
1
&&
!
strcmp
(
argv
[
1
],
"-fnnlm"
))
FNNLMMain
(
argc
-
1
,
argv
+
1
);
FNNLMMain
(
argc
-
1
,
argv
+
1
);
...
...
source/sample/transformer/T2TDecoder.cpp
查看文件 @
a4e6ab64
...
@@ -31,6 +31,10 @@ namespace transformer
...
@@ -31,6 +31,10 @@ namespace transformer
/* constructor */
/* constructor */
AttDecoder
::
AttDecoder
()
AttDecoder
::
AttDecoder
()
{
{
attentions
=
NULL
;
fnns
=
NULL
;
attLayerNorms
=
NULL
;
fnnLayerNorms
=
NULL
;
attentionsEnde
=
NULL
;
attentionsEnde
=
NULL
;
attEndeLayerNorms
=
NULL
;
attEndeLayerNorms
=
NULL
;
}
}
...
@@ -38,6 +42,10 @@ AttDecoder::AttDecoder()
...
@@ -38,6 +42,10 @@ AttDecoder::AttDecoder()
/* de-constructor */
/* de-constructor */
AttDecoder
::~
AttDecoder
()
AttDecoder
::~
AttDecoder
()
{
{
delete
[]
attentions
;
delete
[]
fnns
;
delete
[]
attLayerNorms
;
delete
[]
fnnLayerNorms
;
delete
[]
attentionsEnde
;
delete
[]
attentionsEnde
;
delete
[]
attEndeLayerNorms
;
delete
[]
attEndeLayerNorms
;
}
}
...
...
source/sample/transformer/T2TPredictor.cpp
查看文件 @
a4e6ab64
...
@@ -159,7 +159,7 @@ void T2TPredictor::Predict(T2TStateBundle * next, XTensor * encoding,
...
@@ -159,7 +159,7 @@ void T2TPredictor::Predict(T2TStateBundle * next, XTensor * encoding,
XTensor
*
inputLast
=
(
XTensor
*
)
s
->
layersDec
.
GetItem
(
0
);
XTensor
*
inputLast
=
(
XTensor
*
)
s
->
layersDec
.
GetItem
(
0
);
/* word indices of positions up to next state */
/* word indices of positions up to next state */
XTensor
&
inputDec
=
*
NewTensor
()
;
XTensor
inputDec
;
/* the first token */
/* the first token */
XTensor
first
;
XTensor
first
;
...
...
source/sample/transformer/T2TSearch.cpp
查看文件 @
a4e6ab64
...
@@ -218,6 +218,14 @@ void T2TSearch::Score(T2TStateBundle * prev, T2TStateBundle * beam)
...
@@ -218,6 +218,14 @@ void T2TSearch::Score(T2TStateBundle * prev, T2TStateBundle * beam)
/* score = log-prob/lp */
/* score = log-prob/lp */
_DivDim
(
&
probPath
,
&
lp
,
&
score
,
0
);
_DivDim
(
&
probPath
,
&
lp
,
&
score
,
0
);
if
(
prev
->
isStart
)
{
XTensor
firstMask
=
MakeFirstMask
(
beam
);
firstMask
.
Reshape
(
firstMask
.
unitNum
);
/* mask the hypotheses in the beam expect the first one */
_SumDim
(
&
score
,
&
firstMask
,
&
score
,
0
);
}
InitTensor
(
&
mask
,
InitTensor
(
&
mask
,
prev
->
endMark
.
order
,
prev
->
endMark
.
dimSize
,
X_FLOAT
,
1.0
F
,
prev
->
endMark
.
order
,
prev
->
endMark
.
dimSize
,
X_FLOAT
,
1.0
F
,
prev
->
endMark
.
devID
,
prev
->
endMark
.
mem
);
prev
->
endMark
.
devID
,
prev
->
endMark
.
mem
);
...
@@ -390,9 +398,10 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
...
@@ -390,9 +398,10 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
it needs much more coding work and the speed-up is not obvious. */
it needs much more coding work and the speed-up is not obvious. */
for
(
int
i
=
0
;
i
<
beam
->
stateNum
;
i
+=
beamSize
){
for
(
int
i
=
0
;
i
<
beam
->
stateNum
;
i
+=
beamSize
){
for
(
int
j
=
0
;
j
<
beamSize
;
j
++
){
for
(
int
j
=
0
;
j
<
beamSize
;
j
++
){
T2TState
&
state
=
states
[
i
+
j
];
int
k
=
i
+
j
;
T2TState
&
state
=
states
[
k
];
int
offset
=
id
.
GetInt
(
i
+
j
);
int
offset
=
id
.
GetInt
(
k
);
int
pid
=
i
/
beamSize
;
int
pid
=
i
/
beamSize
;
T2TState
*
last
=
prev
->
states
+
pid
*
beamSize
+
offset
;
T2TState
*
last
=
prev
->
states
+
pid
*
beamSize
+
offset
;
...
@@ -412,12 +421,12 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
...
@@ -412,12 +421,12 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
}
}
/* scores */
/* scores */
state
.
modelScore
=
modelScore
.
Get
(
i
);
state
.
modelScore
=
modelScore
.
Get
(
k
);
state
.
prob
=
prob
.
Get
(
i
);
state
.
prob
=
prob
.
Get
(
k
);
state
.
probPath
=
probPath
.
Get
(
i
);
state
.
probPath
=
probPath
.
Get
(
k
);
/* prediction */
/* prediction */
state
.
prediction
=
prediction
.
GetInt
(
i
);
state
.
prediction
=
prediction
.
GetInt
(
k
);
CheckNTErrors
(
state
.
prediction
>=
0
,
"Illegal prediction!"
);
CheckNTErrors
(
state
.
prediction
>=
0
,
"Illegal prediction!"
);
...
@@ -425,7 +434,7 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
...
@@ -425,7 +434,7 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
state
.
isEnd
=
IsEnd
(
state
.
prediction
);
state
.
isEnd
=
IsEnd
(
state
.
prediction
);
/* set the ending mark */
/* set the ending mark */
endMarkCPU
.
SetInt
(
state
.
isEnd
,
i
);
endMarkCPU
.
SetInt
(
state
.
isEnd
,
k
);
}
}
}
}
...
@@ -554,4 +563,31 @@ void T2TSearch::SetEnd(const int * tokens, const int tokenNum)
...
@@ -554,4 +563,31 @@ void T2TSearch::SetEnd(const int * tokens, const int tokenNum)
endSymbolNum
=
tokenNum
;
endSymbolNum
=
tokenNum
;
}
}
/*
make a mask to prevent duplicated entries in beam expansion for the first position
>> beam - the beam that keeps the searching states
*/
XTensor
T2TSearch
::
MakeFirstMask
(
T2TStateBundle
*
beam
)
{
XTensor
&
prob
=
beam
->
prob
;
XTensor
mask
;
int
order
=
prob
.
order
;
int
dims
[
MAX_TENSOR_DIM_NUM
];
for
(
int
i
=
0
;
i
<
order
-
1
;
i
++
)
dims
[
i
]
=
prob
.
GetDim
(
i
);
InitTensor
(
&
mask
,
order
-
1
,
dims
,
X_FLOAT
);
mask
.
SetZeroAll
();
for
(
int
i
=
0
;
i
<
mask
.
unitNum
;
i
++
)
{
if
(
i
%
beamSize
!=
0
)
mask
.
Set
(
-
1e9
,
i
);
}
mask
.
SetDevice
(
prob
.
devID
,
prob
.
mem
);
return
mask
;
}
}
}
source/sample/transformer/T2TSearch.h
查看文件 @
a4e6ab64
...
@@ -101,6 +101,9 @@ public:
...
@@ -101,6 +101,9 @@ public:
/* set end symbols for search */
/* set end symbols for search */
void
SetEnd
(
const
int
*
tokens
,
const
int
tokenNum
);
void
SetEnd
(
const
int
*
tokens
,
const
int
tokenNum
);
/* make a mask to prevent duplicated entries in beam expansion for the first position */
XTensor
MakeFirstMask
(
T2TStateBundle
*
beam
);
};
};
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论