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
430f0dfc
Commit
430f0dfc
authored
Oct 09, 2018
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the decoder for transformer
parent
7250ec45
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
201 行增加
和
26 行删除
+201
-26
source/sample/transformer/T2TDecoder.cpp
+141
-0
source/sample/transformer/T2TDecoder.h
+20
-6
source/sample/transformer/T2TEncoder.cpp
+4
-0
source/sample/transformer/T2TModel.cpp
+29
-8
source/sample/transformer/T2TModel.h
+2
-2
source/sample/transformer/T2TTrainer.cpp
+5
-10
没有找到文件。
source/sample/transformer/T2TDecoder.cpp
查看文件 @
430f0dfc
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2018, 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: XIAO Tong (xiaotong@mail.neu.edu.cn) 2018-10-09
*/
#include <math.h>
#include "T2TDecoder.h"
#include "../../tensor/core/CHeader.h"
namespace
transformer
{
/* constructor */
AttDecoder
::
AttDecoder
()
{
attentionsEnde
=
NULL
;
attEndeLayerNorms
=
NULL
;
}
/* de-constructor */
AttDecoder
::~
AttDecoder
()
{
delete
[]
attentionsEnde
;
delete
[]
attEndeLayerNorms
;
}
/*
initialize the model
>> argc - number of arguments
>> argv - list of pointers to the arguments
>> myIsMasked - indicates whether the masked attention is employed
>> myIgnored - number of positions ignored in attention (from the start)
>> myDevID - device id
>> myMem - the memory pool
*/
void
AttDecoder
::
InitModel
(
int
argc
,
char
**
argv
,
bool
myIsMasked
,
int
myIgnored
,
int
myDevID
,
XMem
*
myMem
)
{
AttEncoder
::
InitModel
(
argc
,
argv
,
myIsMasked
,
myIgnored
,
myDevID
,
myMem
);
attentionsEnde
=
new
T2TAttention
[
nlayer
];
attEndeLayerNorms
=
new
T2TLN
[
nlayer
];
/* initialize the stacked layers */
for
(
int
i
=
0
;
i
<
nlayer
;
i
++
){
attentionsEnde
[
i
].
InitModel
(
argc
,
argv
,
myIsMasked
,
myIgnored
,
myDevID
,
myMem
);
attEndeLayerNorms
[
i
].
InitModel
(
argc
,
argv
,
myDevID
,
myMem
);
}
}
/*
make the decoding network
>> input - the input tensor of the decoder
>> encoderOutput - the output tensor of the encoder
>> mask - the mask that indicate each position is valid
>> isTraining - indicates whether the model is used for training
<< return - the output tensor of the encoder
*/
XTensor
AttDecoder
::
Make
(
XTensor
&
input
,
XTensor
&
encoderOutput
,
XTensor
&
mask
,
bool
isTraining
)
{
XTensor
x
;
x
=
embedder
.
Make
(
input
);
/* dropout */
if
(
isTraining
&&
dropoutP
>
0
)
x
=
Dropout
(
x
,
dropoutP
);
for
(
int
i
=
0
;
i
<
nlayer
;
i
++
){
XTensor
att
;
XTensor
ende
;
XTensor
ln
;
XTensor
fnn
;
XTensor
res
;
/******************/
/* self attention */
att
=
attentions
[
i
].
Make
(
x
,
x
,
x
,
mask
,
isTraining
);
/* dropout */
if
(
isTraining
&&
dropoutP
>
0
)
att
=
Dropout
(
att
,
dropoutP
);
/* residual connection */
res
=
Sum
(
att
,
x
);
/* layer normalization */
x
=
attLayerNorms
[
i
].
Make
(
res
);
/*****************************/
/* encoder-decoder attention */
ende
=
attentionsEnde
[
i
].
Make
(
encoderOutput
,
x
,
encoderOutput
,
mask
,
isTraining
);
/* dropout */
if
(
isTraining
&&
dropoutP
>
0
)
ende
=
Dropout
(
ende
,
dropoutP
);
/* residual connection */
res
=
Sum
(
ende
,
x
);
/* layer normalization */
x
=
attEndeLayerNorms
[
i
].
Make
(
res
);
/*******/
/* fnn */
fnn
=
fnns
[
i
].
Make
(
x
,
isTraining
);
/* dropout */
if
(
isTraining
&&
dropoutP
>
0
)
fnn
=
Dropout
(
fnn
,
dropoutP
);
/* residual connection */
res
=
Sum
(
fnn
,
x
);
/* layer normalization */
x
=
fnnLayerNorms
[
i
].
Make
(
res
);
}
return
x
;
}
}
\ No newline at end of file
source/sample/transformer/T2TDecoder.h
查看文件 @
430f0dfc
...
@@ -22,19 +22,33 @@
...
@@ -22,19 +22,33 @@
#ifndef __T2TDECODER_H__
#ifndef __T2TDECODER_H__
#define __T2TDECODER_H__
#define __T2TDECODER_H__
#include "T2TEncoder.h"
namespace
transformer
namespace
transformer
{
{
class
T2TDe
coder
class
AttDecoder
:
public
AttEn
coder
{
{
public
:
/* encoder-decoder attention model of each layer */
T2TAttention
*
attentionsEnde
;
};
/* layer normalization for encoder-decoder attention */
T2TLN
*
attEndeLayerNorms
;
class
AttDecoder
:
T2TDecoder
{
public
:
public
:
/* constructor */
AttDecoder
();
/* deconstructor */
~
AttDecoder
();
/* initialize the model */
/* initialize the model */
void
InitModel
(
int
argc
,
char
**
argv
);
void
InitModel
(
int
argc
,
char
**
argv
,
bool
myIsMasked
,
int
myIgnored
,
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
/* make the decoding network */
XTensor
Make
(
XTensor
&
input
,
XTensor
&
encoderOutput
,
XTensor
&
mask
,
bool
isTraining
);
};
};
}
}
...
...
source/sample/transformer/T2TEncoder.cpp
查看文件 @
430f0dfc
...
@@ -31,6 +31,10 @@ namespace transformer
...
@@ -31,6 +31,10 @@ namespace transformer
/* constructor */
/* constructor */
AttEncoder
::
AttEncoder
()
AttEncoder
::
AttEncoder
()
{
{
attentions
=
NULL
;
fnns
=
NULL
;
attLayerNorms
=
NULL
;
fnnLayerNorms
=
NULL
;
}
}
/* de-constructor */
/* de-constructor */
...
...
source/sample/transformer/T2TModel.cpp
查看文件 @
430f0dfc
...
@@ -71,6 +71,9 @@ void T2TModel::InitModel(int argc, char ** argv)
...
@@ -71,6 +71,9 @@ void T2TModel::InitModel(int argc, char ** argv)
encoder
.
InitModel
(
argc
,
argv
,
isLM
,
0
,
devID
,
mem
);
encoder
.
InitModel
(
argc
,
argv
,
isLM
,
0
,
devID
,
mem
);
outputLayer
.
InitModel
(
argc
,
argv
,
devID
,
mem
);
outputLayer
.
InitModel
(
argc
,
argv
,
devID
,
mem
);
if
(
isMT
)
decoder
.
InitModel
(
argc
,
argv
,
true
,
0
,
devID
,
mem
);
XList
params
(
10
);
XList
params
(
10
);
GetParams
(
params
);
GetParams
(
params
);
...
@@ -93,17 +96,16 @@ XTensor T2TModel::MakeEncoding(XTensor &input, XTensor &mask, bool isTraining)
...
@@ -93,17 +96,16 @@ XTensor T2TModel::MakeEncoding(XTensor &input, XTensor &mask, bool isTraining)
}
}
/*
/*
make the entire network (with the output softmax layer)
make the entire network
for language modeling
(with the output softmax layer)
>> input - input tensor
>> input - input tensor
>> output - output tensor (distribution)
>> output - output tensor (distribution)
>> padding - padding of the sequences
>> padding - padding of the sequences
>> isTraining - indicates whether the model is for training
>> isTraining - indicates whether the model is for training
*/
*/
void
T2TModel
::
Make
(
XTensor
&
input
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
)
void
T2TModel
::
Make
LM
(
XTensor
&
input
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
)
{
{
XTensor
encoding
;
XTensor
encoding
;
if
(
isLM
){
/* generate mask to see "previous" words only */
/* generate mask to see "previous" words only */
int
len
=
input
.
GetDim
(
input
.
order
-
2
);
int
len
=
input
.
GetDim
(
input
.
order
-
2
);
int
*
dims
=
new
int
[
input
.
order
+
1
];
int
*
dims
=
new
int
[
input
.
order
+
1
];
...
@@ -141,7 +143,7 @@ void T2TModel::Make(XTensor &input, XTensor &output, XTensor &padding, bool isTr
...
@@ -141,7 +143,7 @@ void T2TModel::Make(XTensor &input, XTensor &output, XTensor &padding, bool isTr
_ScaleAndShiftMe
(
padding3
,
1e9
F
,
-
1e9
F
);
_ScaleAndShiftMe
(
padding3
,
1e9
F
,
-
1e9
F
);
_Sum
(
&
mask
,
padding3
,
&
mask
);
//
_Sum(&mask, padding3, &mask);
encoding
=
MakeEncoding
(
input
,
mask
,
isTraining
);
encoding
=
MakeEncoding
(
input
,
mask
,
isTraining
);
outputLayer
.
Make
(
encoding
,
output
);
outputLayer
.
Make
(
encoding
,
output
);
...
@@ -151,10 +153,6 @@ void T2TModel::Make(XTensor &input, XTensor &output, XTensor &padding, bool isTr
...
@@ -151,10 +153,6 @@ void T2TModel::Make(XTensor &input, XTensor &output, XTensor &padding, bool isTr
DelTensorBuf
(
padding2
);
DelTensorBuf
(
padding2
);
DelTensorBuf
(
padding3
);
DelTensorBuf
(
padding3
);
}
else
{
ShowNTErrors
(
"TODO!"
);
}
}
}
/*
/*
...
@@ -181,6 +179,29 @@ void T2TModel::GetParams(XList &list)
...
@@ -181,6 +179,29 @@ void T2TModel::GetParams(XList &list)
list
.
Add
(
&
encoder
.
attLayerNorms
[
i
].
b
);
list
.
Add
(
&
encoder
.
attLayerNorms
[
i
].
b
);
}
}
if
(
isMT
){
for
(
int
i
=
0
;
i
<
decoder
.
nlayer
;
i
++
){
list
.
Add
(
&
decoder
.
fnns
[
i
].
w1
);
list
.
Add
(
&
decoder
.
fnns
[
i
].
b1
);
list
.
Add
(
&
decoder
.
fnns
[
i
].
w2
);
list
.
Add
(
&
decoder
.
fnns
[
i
].
b2
);
list
.
Add
(
&
decoder
.
attentionsEnde
[
i
].
wk
);
list
.
Add
(
&
decoder
.
attentionsEnde
[
i
].
wq
);
list
.
Add
(
&
decoder
.
attentionsEnde
[
i
].
wv
);
list
.
Add
(
&
decoder
.
attentionsEnde
[
i
].
wa
);
list
.
Add
(
&
decoder
.
attEndeLayerNorms
[
i
].
w
);
list
.
Add
(
&
decoder
.
attEndeLayerNorms
[
i
].
b
);
list
.
Add
(
&
decoder
.
attentions
[
i
].
wk
);
list
.
Add
(
&
decoder
.
attentions
[
i
].
wq
);
list
.
Add
(
&
decoder
.
attentions
[
i
].
wv
);
list
.
Add
(
&
decoder
.
attentions
[
i
].
wa
);
list
.
Add
(
&
decoder
.
fnnLayerNorms
[
i
].
w
);
list
.
Add
(
&
decoder
.
fnnLayerNorms
[
i
].
b
);
list
.
Add
(
&
decoder
.
attLayerNorms
[
i
].
w
);
list
.
Add
(
&
decoder
.
attLayerNorms
[
i
].
b
);
}
}
list
.
Add
(
&
encoder
.
embedder
.
w
);
list
.
Add
(
&
encoder
.
embedder
.
w
);
}
}
...
...
source/sample/transformer/T2TModel.h
查看文件 @
430f0dfc
...
@@ -71,8 +71,8 @@ public:
...
@@ -71,8 +71,8 @@ public:
/* make the encoding network */
/* make the encoding network */
XTensor
MakeEncoding
(
XTensor
&
input
,
XTensor
&
mask
,
bool
isTraining
);
XTensor
MakeEncoding
(
XTensor
&
input
,
XTensor
&
mask
,
bool
isTraining
);
/* make the entire network (with the output softmax layer) */
/* make the entire network
for langauge modeling
(with the output softmax layer) */
void
Make
(
XTensor
&
input
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
);
void
Make
LM
(
XTensor
&
input
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
);
/* get parameter matrics */
/* get parameter matrics */
void
GetParams
(
XList
&
list
);
void
GetParams
(
XList
&
list
);
...
...
source/sample/transformer/T2TTrainer.cpp
查看文件 @
430f0dfc
...
@@ -197,7 +197,7 @@ void T2TTrainer::Train(const char * fn, const char * validFN, const char * model
...
@@ -197,7 +197,7 @@ void T2TTrainer::Train(const char * fn, const char * validFN, const char * model
XTensor
output
;
XTensor
output
;
/* make the network */
/* make the network */
model
->
Make
(
batch
,
output
,
padding
,
true
);
model
->
Make
LM
(
batch
,
output
,
padding
,
true
);
/* back-propagation for obtaining gradients */
/* back-propagation for obtaining gradients */
if
(
labelSmoothingP
>
0
)
if
(
labelSmoothingP
>
0
)
...
@@ -343,7 +343,7 @@ void T2TTrainer::Test(const char * fn, const char * ofn, T2TModel * model)
...
@@ -343,7 +343,7 @@ void T2TTrainer::Test(const char * fn, const char * ofn, T2TModel * model)
XTensor
output
;
XTensor
output
;
/* make the network */
/* make the network */
model
->
Make
(
batch
,
output
,
padding
,
false
);
model
->
Make
LM
(
batch
,
output
,
padding
,
false
);
int
bSize
=
batch
.
GetDim
(
0
);
int
bSize
=
batch
.
GetDim
(
0
);
int
length
=
batch
.
GetDim
(
1
);
int
length
=
batch
.
GetDim
(
1
);
...
@@ -886,17 +886,12 @@ void T2TTrainer::RescaleOutput(XTensor * output, XTensor * gold, XTensor * paddi
...
@@ -886,17 +886,12 @@ void T2TTrainer::RescaleOutput(XTensor * output, XTensor * gold, XTensor * paddi
CheckNTErrors
(
output
->
order
==
3
,
"Wrong dimension number!"
);
CheckNTErrors
(
output
->
order
==
3
,
"Wrong dimension number!"
);
CheckNTErrors
(
gold
->
order
==
3
,
"Wrong dimension number!"
);
CheckNTErrors
(
gold
->
order
==
3
,
"Wrong dimension number!"
);
int
num
=
padding
->
GetDim
(
0
);
DTYPE
count
=
_ReduceSumAll
(
padding
);
XTensor
*
factor
=
NewTensorBuf
(
1
,
&
num
,
padding
->
dataType
,
1.0
F
,
padding
->
devID
,
padding
->
mem
);
_ReduceSum
(
padding
,
factor
,
padding
->
order
-
1
);
_ExpMe
(
output
);
_ExpMe
(
output
);
_
DivDim
(
output
,
factor
,
output
,
0
);
_
ScaleAndShiftMe
(
output
,
1
/
count
);
_LogMe
(
output
);
_LogMe
(
output
);
_DivDim
(
gold
,
factor
,
gold
,
0
);
_ScaleAndShiftMe
(
gold
,
1
/
count
);
DelTensorBuf
(
factor
);
}
}
/*
/*
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论