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
39eef110
Commit
39eef110
authored
Mar 05, 2021
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixes
parent
e9d68683
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
96 行增加
和
24 行删除
+96
-24
source/sample/transformer/translate/LengthPenalty.cpp
+1
-1
source/tensor/XUtility.cpp
+9
-10
source/train/TTrain.cpp
+24
-10
source/train/XLeader.cpp
+51
-1
source/train/XLeader.h
+4
-0
source/train/XModel.cpp
+4
-2
source/train/XWorkerJob.cpp
+3
-0
没有找到文件。
source/sample/transformer/translate/LengthPenalty.cpp
查看文件 @
39eef110
...
...
@@ -42,7 +42,7 @@ float LengthPenalizer::GNMT(float length, float alpha)
base
=
(
length
+
5.0
F
)
/
(
1.0
F
+
5.0
F
);
lp
=
pow
(
base
,
alpha
);
lp
=
(
float
)
pow
(
base
,
alpha
);
return
lp
;
}
...
...
source/tensor/XUtility.cpp
查看文件 @
39eef110
...
...
@@ -155,13 +155,13 @@ void XMemSet(int devID, void * p, int value, size_t size)
cudaMemcpyKind
GetMemcpyKind
(
int
devIDFrom
,
int
devIDTo
)
{
if
(
devIDFrom
<
0
&&
devIDTo
<
0
)
return
cudaMemcpyHostToHost
;
return
cudaMemcpy
Kind
::
cudaMemcpy
HostToHost
;
else
if
(
devIDFrom
<
0
&&
devIDTo
>=
0
)
return
cudaMemcpyHostToDevice
;
return
cudaMemcpy
Kind
::
cudaMemcpy
HostToDevice
;
else
if
(
devIDFrom
>=
0
&&
devIDTo
<
0
)
return
cudaMemcpyDeviceToHost
;
return
cudaMemcpy
Kind
::
cudaMemcpy
DeviceToHost
;
else
return
cudaMemcpyDeviceToDevice
;
return
cudaMemcpy
Kind
::
cudaMemcpy
DeviceToDevice
;
}
#endif
...
...
@@ -553,9 +553,9 @@ void XQSort(void * data, void * index, int num, int width, int stride, int (*com
stackptr
=
0
;
lo
=
(
char
*
)
data
;
hi
=
(
char
*
)
data
+
realStride
*
(
num
-
1
);
hi
=
(
char
*
)
data
+
(
long
)
realStride
*
(
num
-
1
);
indexlo
=
(
int
*
)
index
;
indexhi
=
index
!=
NULL
?
(
int
*
)
index
+
stride
*
(
num
-
1
)
:
NULL
;
indexhi
=
index
!=
NULL
?
(
int
*
)
index
+
(
long
)
stride
*
(
num
-
1
)
:
NULL
;
recurse:
...
...
@@ -565,8 +565,8 @@ recurse:
if
(
size
<=
MIN_QSORT_NUM
)
XShortSort
(
lo
,
hi
,
indexlo
,
indexhi
,
width
,
stride
,
comp
);
else
{
mid
=
lo
+
(
size
/
2
)
*
realStride
;
indexmid
=
indexlo
+
(
size
/
2
)
*
stride
;
mid
=
lo
+
(
long
)(
size
/
2
)
*
realStride
;
indexmid
=
indexlo
+
(
long
)(
size
/
2
)
*
stride
;
/* sort the first, last and middle elements into order */
if
(
comp
(
lo
,
mid
)
>
0
)
...
...
@@ -834,8 +834,7 @@ int SplitALine(char* inputString, const char* seperator, StrList* items)
return
0
;
if
(
sepLen
==
0
)
{
char
*
item
=
new
char
[
inputLen
+
1
];
char
*
item
=
new
char
[(
long
)
inputLen
+
1
];
strcpy
(
item
,
inputString
);
items
->
Add
(
item
);
}
...
...
source/train/TTrain.cpp
查看文件 @
39eef110
...
...
@@ -122,12 +122,12 @@ void TTDataLoader::SetBatchSize(int myBatchSize)
/* start the process */
bool
TTDataLoader
::
Start
()
{
file
=
fopen
(
fileName
,
"
w
b"
);
CheckNTErrors
(
file
,
"Cannot open the file"
);
file
=
fopen
(
fileName
,
"
r
b"
);
CheckNTErrors
(
file
!=
NULL
,
"Cannot open the file"
);
/* skip the first line */
char
*
line
=
new
char
[
MAX_SAMPLE_LINE_LENGTH
];
fgets
(
line
,
MAX_SAMPLE_LINE_LENGTH
-
1
,
file
);
fgets
(
line
,
MAX_SAMPLE_LINE_LENGTH
,
file
);
delete
[]
line
;
return
true
;
...
...
@@ -148,6 +148,7 @@ get a batch of samples
*/
bool
TTDataLoader
::
GetBatchSimple
(
XList
*
inputs
,
XList
*
golds
)
{
fprintf
(
stderr
,
"get batch 0
\n
"
);
CheckNTErrors
(
file
!=
NULL
,
"No input file specificed!"
);
CheckNTErrors
(
inputs
!=
NULL
&&
inputs
->
count
>=
1
,
"Wrong argument!"
);
CheckNTErrors
(
golds
!=
NULL
&&
golds
->
count
>=
1
,
"Wrong argument!"
);
...
...
@@ -162,9 +163,9 @@ bool TTDataLoader::GetBatchSimple(XList * inputs, XList * golds)
int
*
goldBatch
=
new
int
[
batchSize
];
int
A
,
B
,
C
,
D
;
while
(
fgets
(
line
,
MAX_SAMPLE_LINE_LENGTH
-
1
,
file
))
{
while
(
fgets
(
line
,
MAX_SAMPLE_LINE_LENGTH
,
file
))
{
if
(
count
++
==
batchSize
)
if
(
count
==
batchSize
)
break
;
if
(
sscanf
(
line
,
"%d %d %d %d"
,
&
A
,
&
B
,
&
C
,
&
D
)
<
4
)
{
...
...
@@ -175,19 +176,28 @@ bool TTDataLoader::GetBatchSimple(XList * inputs, XList * golds)
inputBatch
[
count
*
3
+
1
]
=
B
;
inputBatch
[
count
*
3
+
2
]
=
C
;
goldBatch
[
count
]
=
D
;
count
++
;
}
InitTensor2D
(
input
,
count
,
3
,
X_INT
);
InitTensor2D
(
gold
,
count
,
1
,
X_INT
);
if
(
count
>
0
)
{
InitTensor2D
(
input
,
count
,
3
,
X_INT
);
InitTensor2D
(
gold
,
count
,
1
,
X_INT
);
input
->
SetData
(
input
,
count
*
3
);
gold
->
SetData
(
gold
,
count
);
input
->
SetData
(
input
,
count
*
3
);
gold
->
SetData
(
gold
,
count
);
}
delete
[]
line
;
delete
[]
inputBatch
;
delete
[]
goldBatch
;
return
true
;
fprintf
(
stderr
,
"get batch 1
\n
"
);
if
(
count
>
0
)
return
true
;
else
return
false
;
}
/*****************************
...
...
@@ -233,6 +243,8 @@ void TTModel::Forward(int devID, XTensor * input, XTensor * output)
XTensor
embeddingCat
;
XTensor
hidden
;
fprintf
(
stderr
,
"forward 0
\n
"
);
/* [e_0, e_1, e_2] = w_e * input(one-hot) */
embedding
=
Gather
(
embeddingW
,
*
input
);
...
...
@@ -244,6 +256,8 @@ void TTModel::Forward(int devID, XTensor * input, XTensor * output)
/* output = Softmax(h) */
*
output
=
Softmax
(
hidden
,
0
);
fprintf
(
stderr
,
"forward 1
\n
"
);
}
/* clear the model */
...
...
source/train/XLeader.cpp
查看文件 @
39eef110
...
...
@@ -205,7 +205,7 @@ run the model (for one time)
bool
XLeader
::
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
,
XOptimizer
*
optimizer
)
{
bool
isDataOK
=
true
;
bool
isDataOK
=
true
;
/* Feed the input to each worker and geneate the output.
For each worker, we define a job queue and enqueue jobs
...
...
@@ -264,7 +264,57 @@ bool XLeader::Run(XConfig * config, DataDistributeBase * dataDistributor,
ShowNTErrors
(
"No data-broadcasting workers!"
);
}
WaitForFinishing
();
return
isDataOK
;
}
/* wait until all workers finish their job */
void
XLeader
::
WaitForFinishing
(
int
sleepTime
)
{
while
(
1
)
{
bool
finished
=
true
;
if
(
finished
)
{
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
jworkers
[
i
];
if
(
worker
->
GetJobNum
()
>
0
)
{
finished
=
false
;
break
;
}
}
}
if
(
finished
)
{
for
(
int
i
=
0
;
i
<
cworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
cworkers
[
i
];
if
(
worker
->
GetJobNum
()
>
0
)
{
finished
=
false
;
break
;
}
}
}
if
(
finished
)
{
for
(
int
i
=
0
;
i
<
uworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
uworkers
[
i
];
if
(
worker
->
GetJobNum
()
>
0
)
{
finished
=
false
;
break
;
}
}
}
if
(
finished
)
break
;
#ifdef _WIN32
Sleep
((
DWORD
)
sleepTime
);
#else
sleep
((
unsigned
)
sleepTime
/
1000
);
#endif
}
}
}
/* end of the nts (NiuTrans.Tensor) namespace */
source/train/XLeader.h
查看文件 @
39eef110
...
...
@@ -48,6 +48,7 @@
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
#define MAX_NUM_OF_WORKERS 1024
#define SLEEP_TIME_IN_WAITING_FOR_JOBS 10
/*
conmmunication mode of a leader. This offers a way of organizing a hierachy of the work
...
...
@@ -126,6 +127,9 @@ public:
/* run the model (for one time) */
bool
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
,
XOptimizer
*
optimizer
);
/* wait until all workers finish their job */
void
WaitForFinishing
(
int
sleepTime
=
SLEEP_TIME_IN_WAITING_FOR_JOBS
);
};
}
...
...
source/train/XModel.cpp
查看文件 @
39eef110
...
...
@@ -124,8 +124,10 @@ bool XModel::Run(XList * args)
XModel
*
model
=
(
XModel
*
)
args
->
GetItem
(
0
);
XList
newArgs
;
for
(
int
i
=
1
;
i
<
args
->
count
;
i
++
)
newArgs
.
Add
(
args
->
GetItem
(
i
));
for
(
int
i
=
1
;
i
<
args
->
count
;
i
++
)
{
void
*
arg
=
args
->
GetItem
(
i
);
newArgs
.
Add
(
arg
);
}
return
model
->
RunMe
(
&
newArgs
);
}
...
...
source/train/XWorkerJob.cpp
查看文件 @
39eef110
...
...
@@ -105,6 +105,7 @@ add a new job of model refreshment
*/
bool
XWorkerJob
::
AddJobRefresh
(
XModel
*
myModel
)
{
fprintf
(
stderr
,
"refresh 0
\n
"
);
CheckNTErrors
(
myModel
!=
NULL
,
"no parameter keeper!"
);
XList
args
(
1
);
...
...
@@ -112,6 +113,8 @@ bool XWorkerJob::AddJobRefresh(XModel * myModel)
queue
.
EnqueueJob
((
void
*
)(
char
*
)
XModel
::
Refresh
,
&
args
);
fprintf
(
stderr
,
"refresh 1
\n
"
);
return
true
;
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论