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
9b3209d8
Commit
9b3209d8
authored
Mar 01, 2021
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updates of XWorker and XWorkerJob
parent
e3455593
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
91 行增加
和
29 行删除
+91
-29
source/train/XLeader.cpp
+30
-1
source/train/XLeader.h
+1
-1
source/train/XModel.cpp
+8
-13
source/train/XModel.h
+6
-7
source/train/XWorkerJob.cpp
+30
-6
source/train/XWorkerJob.h
+16
-1
没有找到文件。
source/train/XLeader.cpp
查看文件 @
9b3209d8
...
@@ -80,10 +80,20 @@ void XLeader::SetMode(XLEADER_MODE myMode)
...
@@ -80,10 +80,20 @@ void XLeader::SetMode(XLEADER_MODE myMode)
/*
/*
add a number of job workers (given their device ids)
add a number of job workers (given their device ids)
>> model - the neural network
>> model - the neural network
>> n - number of the models
>> ids - the array of device ids
>> ids - the array of device ids
*/
*/
void
XLeader
::
AddJobWorker
(
XModel
*
model
,
int
*
ids
)
void
XLeader
::
AddJobWorker
(
XModel
*
model
,
int
n
,
int
*
ids
)
{
{
/* we keep the input model */
if
(
n
>=
1
)
{
jworkers
.
Add
(
model
);
}
/* we clone the input model */
for
(
int
i
=
0
;
i
<
n
-
1
;
i
++
)
{
jworkers
.
Add
(
model
->
Clone
(
ids
[
i
]));
}
}
}
/*
/*
...
@@ -96,6 +106,25 @@ run the model (for one time)
...
@@ -96,6 +106,25 @@ run the model (for one time)
void
XLeader
::
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
void
XLeader
::
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
,
XOptimizer
*
optimizer
)
XModel
*
model
,
XOptimizer
*
optimizer
)
{
{
/* feed the input to each worker and geneate the output */
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
jworkers
[
i
];
XModel
*
model
=
worker
->
GetModel
();
/* get a batch of samples */
dataDistributor
->
GetBatch
(
worker
->
GetInput
());
/* job in the queue: refresh the model */
worker
->
AddJobRefresh
(
model
);
/* job in the queue: run the model */
worker
->
AddJobNeuralNet
(
model
,
worker
->
GetInput
(),
worker
->
GetOutput
());
/* clear it */
worker
->
Clear
();
}
/* collect the (gradient) data and update the model */
}
}
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
source/train/XLeader.h
查看文件 @
9b3209d8
...
@@ -88,7 +88,7 @@ public:
...
@@ -88,7 +88,7 @@ public:
void
SetMode
(
XLEADER_MODE
myMode
);
void
SetMode
(
XLEADER_MODE
myMode
);
/* add a number of job workers (given their device ids) */
/* add a number of job workers (given their device ids) */
void
AddJobWorker
(
XModel
*
model
,
int
*
ids
);
void
AddJobWorker
(
XModel
*
model
,
int
n
,
int
*
ids
);
/* run the model (for one time) */
/* run the model (for one time) */
void
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
void
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
...
...
source/train/XModel.cpp
查看文件 @
9b3209d8
...
@@ -67,13 +67,13 @@ XModel * XModel::Clone(int devID)
...
@@ -67,13 +67,13 @@ XModel * XModel::Clone(int devID)
run the neural network
run the neural network
>> args - the arguments
>> args - the arguments
*/
*/
bool
XModel
::
Run
(
XList
*
args
)
bool
XModel
::
Run
Me
(
XList
*
args
)
{
{
ShowNTErrors
(
"NetBase::Run must be overloaded!"
);
ShowNTErrors
(
"NetBase::Run must be overloaded!"
);
return
true
;
return
true
;
}
}
/* re
set the flag of parameters (the flag is used in data transfer)
*/
/* re
fresh the model
*/
void
XModel
::
RefreshMe
()
void
XModel
::
RefreshMe
()
{
{
for
(
int
i
=
0
;
i
<
params
.
count
;
i
++
)
{
for
(
int
i
=
0
;
i
<
params
.
count
;
i
++
)
{
...
@@ -85,23 +85,18 @@ void XModel::RefreshMe()
...
@@ -85,23 +85,18 @@ void XModel::RefreshMe()
/* wrapper of RefreshMe */
/* wrapper of RefreshMe */
void
XModel
::
Refresh
(
XList
*
args
)
void
XModel
::
Refresh
(
XList
*
args
)
{
{
CheckNTErrors
(
args
!=
NULL
,
"illegal arguments!"
);
CheckNTErrors
(
args
!=
NULL
||
args
->
count
==
0
,
"no arguments for XModel::Refresh"
);
CheckNTErrors
(
args
->
count
==
1
,
"The number of arguments must be 1!"
);
XModel
*
model
=
(
XModel
*
)
args
->
GetItem
(
0
);
XModel
*
model
=
(
XModel
*
)
args
->
GetItem
(
0
);
model
->
RefreshMe
();
model
->
RefreshMe
();
}
}
/*
run the neural network (for multi-threading
*/
/*
wrapper of Run()
*/
bool
XModel
::
Run
Safe
(
XList
*
args
)
bool
XModel
::
Run
(
XList
*
args
)
{
{
bool
r
;
CheckNTErrors
(
args
!=
NULL
||
args
->
count
==
0
,
"no arguments for XModel::Refresh"
);
XModel
*
model
=
(
XModel
*
)
args
->
GetItem
(
0
);
MUTEX_LOCK
(
netMutex
);
r
=
Run
(
args
);
MUTEX_UNLOCK
(
netMutex
);
return
r
;
return
model
->
Run
(
args
)
;
}
}
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
source/train/XModel.h
查看文件 @
9b3209d8
...
@@ -67,21 +67,20 @@ public:
...
@@ -67,21 +67,20 @@ public:
/* run the neural network (would be overloaded) */
/* run the neural network (would be overloaded) */
virtual
virtual
bool
Run
(
XList
*
args
);
bool
Run
Me
(
XList
*
args
);
public
:
public
:
/* re
set the flag of parameters (the flag is used in data transfer)
*/
/* re
fresh the model
*/
void
RefreshMe
();
void
RefreshMe
();
/* wrapper of RefreshMe */
/* wrapper of RefreshMe
()
*/
static
static
void
Refresh
(
XList
*
args
);
void
Refresh
(
XList
*
args
);
protected
:
/* wrapper of Run() */
static
/* run the neural network (for multi-threading) */
bool
Run
(
XList
*
args
);
bool
RunSafe
(
XList
*
args
);
};
};
}
}
...
...
source/train/XWorkerJob.cpp
查看文件 @
9b3209d8
...
@@ -53,6 +53,30 @@ XModel * XWorkerJob::GetModel()
...
@@ -53,6 +53,30 @@ XModel * XWorkerJob::GetModel()
return
model
;
return
model
;
}
}
/* clear the worker */
void
XWorkerJob
::
Clear
()
{
for
(
int
i
=
0
;
i
<
inputs
.
count
;
i
++
)
delete
(
XTensor
*
)
inputs
[
i
];
inputs
.
Clear
();
for
(
int
i
=
0
;
i
<
outputs
.
count
;
i
++
)
delete
(
XTensor
*
)
outputs
[
i
];
outputs
.
Clear
();
}
/* get the input list */
XList
*
XWorkerJob
::
GetInput
()
{
return
&
inputs
;
}
/* get the output list */
XList
*
XWorkerJob
::
GetOutput
()
{
return
&
outputs
;
}
/*
/*
add a new job of model refreshment
add a new job of model refreshment
>> myModel - the model
>> myModel - the model
...
@@ -65,30 +89,30 @@ bool XWorkerJob::AddJobRefresh(XModel * myModel)
...
@@ -65,30 +89,30 @@ bool XWorkerJob::AddJobRefresh(XModel * myModel)
XList
args
(
1
);
XList
args
(
1
);
args
.
Add
(
myModel
);
args
.
Add
(
myModel
);
queue
.
EnqueueJob
(
(
void
*
)
&
myModel
->
Refresh
,
&
args
);
queue
.
EnqueueJob
(
XModel
::
Refresh
,
&
args
);
return
true
;
return
true
;
}
}
/*
/*
add a new job of neural network forward and backward computation (with the input)
add a new job of neural network forward and backward computation (with the input)
>> func - the function that calls the run of the neural network
>> myModel - the model
>> myModel - the model
>> inputs - inputs of the neural network
>> inputs - inputs of the neural network
>> outputs - outputs of the neural network
>> outputs - outputs of the neural network
<< return - succeeded or not
<< return - succeeded or not
*/
*/
bool
XWorkerJob
::
AddJobNeuralNet
(
void
*
func
,
XModel
*
myModel
,
XList
*
inputs
,
XList
*
outputs
)
bool
XWorkerJob
::
AddJobNeuralNet
(
XModel
*
myModel
,
XList
*
inputs
,
XList
*
outputs
)
{
{
CheckNTErrors
(
func
!=
NULL
,
"no input function!"
);
CheckNTErrors
(
myModel
!=
NULL
,
"no input neural network!"
);
CheckNTErrors
(
myModel
!=
NULL
,
"no input neural network!"
);
CheckNTErrors
(
inputs
!=
NULL
,
"no inputs of the model!"
);
CheckNTErrors
(
outputs
!=
NULL
,
"no outputs of the model!"
);
XList
args
;
XList
args
;
args
.
Add
(
myModel
);
args
.
AddList
(
inputs
);
args
.
AddList
(
inputs
);
args
.
AddList
(
outputs
);
args
.
AddList
(
outputs
);
args
.
Add
(
myModel
);
queue
.
EnqueueJob
(
func
,
&
args
);
queue
.
EnqueueJob
(
XModel
::
Run
,
&
args
);
return
true
;
return
true
;
}
}
...
...
source/train/XWorkerJob.h
查看文件 @
9b3209d8
...
@@ -43,6 +43,12 @@ protected:
...
@@ -43,6 +43,12 @@ protected:
/* the model */
/* the model */
XModel
*
model
;
XModel
*
model
;
/* the input tensors of the model */
XList
inputs
;
/* the output tensors of the model */
XList
outputs
;
public
:
public
:
/* constructor */
/* constructor */
...
@@ -57,11 +63,20 @@ public:
...
@@ -57,11 +63,20 @@ public:
/* get the parameter keeper */
/* get the parameter keeper */
XModel
*
GetModel
();
XModel
*
GetModel
();
/* clear the worker */
void
Clear
();
/* get the input list */
XList
*
GetInput
();
/* get the output list */
XList
*
GetOutput
();
/* add a new job of model refreshment */
/* add a new job of model refreshment */
bool
AddJobRefresh
(
XModel
*
myModel
);
bool
AddJobRefresh
(
XModel
*
myModel
);
/* add a new job of neural network forward and backward computation (with the input) */
/* add a new job of neural network forward and backward computation (with the input) */
bool
AddJobNeuralNet
(
void
*
func
,
XModel
*
myModel
,
XList
*
inputs
,
XList
*
outputs
);
bool
AddJobNeuralNet
(
XModel
*
myModel
,
XList
*
inputs
,
XList
*
outputs
);
};
};
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论