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
e3455593
Commit
e3455593
authored
Mar 01, 2021
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new udpates and reorganizing XModel
parent
923af6c2
显示空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
146 行增加
和
80 行删除
+146
-80
source/train/XBaseTemplate.cpp
+1
-36
source/train/XBaseTemplate.h
+1
-24
source/train/XLeader.cpp
+30
-0
source/train/XLeader.h
+17
-2
source/train/XModel.cpp
+36
-0
source/train/XModel.h
+21
-1
source/train/XTrainer.cpp
+3
-5
source/train/XTrainer.h
+2
-1
source/train/XWorkerJob.cpp
+23
-9
source/train/XWorkerJob.h
+12
-2
没有找到文件。
source/train/X
Net
Template.cpp
→
source/train/X
Base
Template.cpp
查看文件 @
e3455593
...
@@ -26,7 +26,7 @@
...
@@ -26,7 +26,7 @@
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2021-02-25
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2021-02-25
*/
*/
#include "X
Net
Template.h"
#include "X
Base
Template.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
...
@@ -79,39 +79,4 @@ bool DataDistributeBase::GetBatchSafe(XList * args)
...
@@ -79,39 +79,4 @@ bool DataDistributeBase::GetBatchSafe(XList * args)
return
r
;
return
r
;
}
}
/*******************************
* neural network template
*******************************/
/* constructor */
NetBase
::
NetBase
()
{
MUTEX_INIT
(
netMutex
);
}
/* de-constructor */
NetBase
::~
NetBase
()
{
MUTEX_DELE
(
netMutex
);
}
/* run the net */
bool
NetBase
::
Run
(
XList
*
args
)
{
ShowNTErrors
(
"NetBase::Run must be overloaded!"
);
return
true
;
}
/* run the net (for multi-threading */
bool
NetBase
::
RunSafe
(
XList
*
args
)
{
bool
r
;
MUTEX_LOCK
(
netMutex
);
r
=
Run
(
args
);
MUTEX_UNLOCK
(
netMutex
);
return
r
;
}
}
}
source/train/X
Net
Template.h
→
source/train/X
Base
Template.h
查看文件 @
e3455593
...
@@ -36,7 +36,7 @@
...
@@ -36,7 +36,7 @@
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/*
/*
data distributor template. It distribute batches of data to workers.
data distributor template. It distribute
s
batches of data to workers.
The use of data distributor follows:
The use of data distributor follows:
Start() -> GetBatch() -> ... -> GetBatch() -> End()
Start() -> GetBatch() -> ... -> GetBatch() -> End()
...
@@ -76,29 +76,6 @@ protected:
...
@@ -76,29 +76,6 @@ protected:
bool
GetBatchSafe
(
XList
*
args
);
bool
GetBatchSafe
(
XList
*
args
);
};
};
/* neural network template */
class
NetBase
{
protected
:
/* mutex of the net */
MUTEX_HANDLE
netMutex
;
public
:
/* constructor */
NetBase
();
/* de-constructor */
~
NetBase
();
/* run the net */
virtual
bool
Run
(
XList
*
args
);
protected
:
/* run the net (for multi-threading */
bool
RunSafe
(
XList
*
args
);
};
}
}
#endif // __XNETTEMPLATE_H__
#endif // __XNETTEMPLATE_H__
...
...
source/train/XLeader.cpp
查看文件 @
e3455593
...
@@ -47,6 +47,15 @@ XLeader::~XLeader()
...
@@ -47,6 +47,15 @@ XLeader::~XLeader()
{
{
}
}
/* intialize the leader */
void
XLeader
::
Init
()
{
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
{
delete
(
XWorkerJob
*
)
jworkers
.
GetItem
(
i
);
}
jworkers
.
Clear
();
}
/* set id */
/* set id */
void
XLeader
::
SetID
(
int
myID
)
void
XLeader
::
SetID
(
int
myID
)
{
{
...
@@ -68,4 +77,25 @@ void XLeader::SetMode(XLEADER_MODE myMode)
...
@@ -68,4 +77,25 @@ void XLeader::SetMode(XLEADER_MODE myMode)
mode
=
myMode
;
mode
=
myMode
;
}
}
/*
add a number of job workers (given their device ids)
>> model - the neural network
>> ids - the array of device ids
*/
void
XLeader
::
AddJobWorker
(
XModel
*
model
,
int
*
ids
)
{
}
/*
run the model (for one time)
>> config - the configuration
>> dataDistributor - data distributor
>> model - the neural network that we want to run
>> optimizer - the optimization method
*/
void
XLeader
::
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
,
XOptimizer
*
optimizer
)
{
}
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
source/train/XLeader.h
查看文件 @
e3455593
...
@@ -36,11 +36,16 @@
...
@@ -36,11 +36,16 @@
#define __XLEADER_H__
#define __XLEADER_H__
#include "XModel.h"
#include "XModel.h"
#include "XNetTemplate.h"
#include "XOptimizer.h"
#include "XBaseTemplate.h"
#include "XWorkerJob.h"
#include "../tensor/XConfig.h"
#include "../tensor/XConfig.h"
#include "../tensor/XList.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
#define MAX_NUM_OF_WORKERS 1024
/*
/*
conmmunication mode of a leader. This offers a way of organizing a hierachy of the work
conmmunication mode of a leader. This offers a way of organizing a hierachy of the work
1) run as a standalone program
1) run as a standalone program
...
@@ -60,6 +65,9 @@ protected:
...
@@ -60,6 +65,9 @@ protected:
/* communication mode */
/* communication mode */
XLEADER_MODE
mode
;
XLEADER_MODE
mode
;
/* job workers of the leader */
XList
jworkers
;
public
:
public
:
/* constructor */
/* constructor */
XLeader
();
XLeader
();
...
@@ -67,6 +75,9 @@ public:
...
@@ -67,6 +75,9 @@ public:
/* de-constructor */
/* de-constructor */
~
XLeader
();
~
XLeader
();
/* intialize the leader */
void
Init
();
/* set id */
/* set id */
void
SetID
(
int
myID
);
void
SetID
(
int
myID
);
...
@@ -76,8 +87,12 @@ public:
...
@@ -76,8 +87,12 @@ public:
/* set the communication mode */
/* set the communication mode */
void
SetMode
(
XLEADER_MODE
myMode
);
void
SetMode
(
XLEADER_MODE
myMode
);
/* add a number of job workers (given their device ids) */
void
AddJobWorker
(
XModel
*
model
,
int
*
ids
);
/* run the model (for one time) */
/* run the model (for one time) */
void
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
modelParams
,
NetBase
*
net
);
void
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
,
XOptimizer
*
optimizer
);
};
};
}
}
...
...
source/train/XModel.cpp
查看文件 @
e3455593
...
@@ -36,11 +36,14 @@ namespace nts {
...
@@ -36,11 +36,14 @@ namespace nts {
/* constructor */
/* constructor */
XModel
::
XModel
()
XModel
::
XModel
()
{
{
MUTEX_INIT
(
modelMutex
);
}
}
/* de-constructor */
/* de-constructor */
XModel
::~
XModel
()
XModel
::~
XModel
()
{
{
Clear
();
MUTEX_DELE
(
modelMutex
);
}
}
/* clear the model */
/* clear the model */
...
@@ -49,6 +52,27 @@ void XModel::Clear()
...
@@ -49,6 +52,27 @@ void XModel::Clear()
params
.
Clear
();
params
.
Clear
();
}
}
/*
clone the model (would be overloaded)
>> devID - the device on that we keep the model
<< return - a cloned model
*/
XModel
*
XModel
::
Clone
(
int
devID
)
{
ShowNTErrors
(
"XModel::Clone() should be overloaded!"
);
return
NULL
;
}
/*
run the neural network
>> args - the arguments
*/
bool
XModel
::
Run
(
XList
*
args
)
{
ShowNTErrors
(
"NetBase::Run must be overloaded!"
);
return
true
;
}
/* reset the flag of parameters (the flag is used in data transfer) */
/* reset the flag of parameters (the flag is used in data transfer) */
void
XModel
::
RefreshMe
()
void
XModel
::
RefreshMe
()
{
{
...
@@ -68,4 +92,16 @@ void XModel::Refresh(XList * args)
...
@@ -68,4 +92,16 @@ void XModel::Refresh(XList * args)
model
->
RefreshMe
();
model
->
RefreshMe
();
}
}
/* run the neural network (for multi-threading */
bool
XModel
::
RunSafe
(
XList
*
args
)
{
bool
r
;
MUTEX_LOCK
(
netMutex
);
r
=
Run
(
args
);
MUTEX_UNLOCK
(
netMutex
);
return
r
;
}
}
/* end of the nts (NiuTrans.Tensor) namespace */
}
/* end of the nts (NiuTrans.Tensor) namespace */
source/train/XModel.h
查看文件 @
e3455593
...
@@ -41,6 +41,10 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
...
@@ -41,6 +41,10 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
/* a model template for training */
/* a model template for training */
class
XModel
class
XModel
{
{
protected
:
/* mutex of the model */
MUTEX_HANDLE
modelMutex
;
public
:
public
:
/* the list of model parameters (pointers to the parameter tensor) */
/* the list of model parameters (pointers to the parameter tensor) */
TensorList
params
;
TensorList
params
;
...
@@ -53,15 +57,31 @@ public:
...
@@ -53,15 +57,31 @@ public:
/* de-constructor */
/* de-constructor */
~
XModel
();
~
XModel
();
/* clear the model */
/* clear the model (would be overloaded) */
virtual
void
Clear
();
void
Clear
();
/* clone the model (would be overloaded) */
virtual
XModel
*
Clone
(
int
devID
);
/* run the neural network (would be overloaded) */
virtual
bool
Run
(
XList
*
args
);
public
:
/* reset the flag of parameters (the flag is used in data transfer) */
/* reset the flag of parameters (the flag is used in data transfer) */
void
RefreshMe
();
void
RefreshMe
();
/* wrapper of RefreshMe */
/* wrapper of RefreshMe */
static
static
void
Refresh
(
XList
*
args
);
void
Refresh
(
XList
*
args
);
protected
:
/* run the neural network (for multi-threading) */
bool
RunSafe
(
XList
*
args
);
};
};
}
}
...
...
source/train/XTrainer.cpp
查看文件 @
e3455593
...
@@ -44,16 +44,14 @@ XTrainer::~XTrainer()
...
@@ -44,16 +44,14 @@ XTrainer::~XTrainer()
run the trainer (this is the core process)
run the trainer (this is the core process)
>> config - configuration
>> config - configuration
>> dataDistributor - the data distributor that generates an input for the net each time
>> dataDistributor - the data distributor that generates an input for the net each time
>> modelParams - the parameter keeper
>> model - the neural network
>> net - the neural network
*/
*/
void
XTrainer
::
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
void
XTrainer
::
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
Params
,
NetBase
*
net
)
XModel
*
model
)
{
{
CheckNTErrors
(
config
!=
NULL
,
"No input config!"
);
CheckNTErrors
(
config
!=
NULL
,
"No input config!"
);
CheckNTErrors
(
dataDistributor
!=
NULL
,
"No input data distributor!"
);
CheckNTErrors
(
dataDistributor
!=
NULL
,
"No input data distributor!"
);
CheckNTErrors
(
modelParams
!=
NULL
,
"No input model parameter keeper!"
);
CheckNTErrors
(
model
!=
NULL
,
"No input neural network!"
);
CheckNTErrors
(
net
!=
NULL
,
"No input neural network!"
);
int
nepoch
=
config
->
GetInt
(
"nepoch"
,
50
);
int
nepoch
=
config
->
GetInt
(
"nepoch"
,
50
);
int
nstep
=
config
->
GetInt
(
"nstep"
,
100000
);
int
nstep
=
config
->
GetInt
(
"nstep"
,
100000
);
...
...
source/train/XTrainer.h
查看文件 @
e3455593
...
@@ -70,7 +70,7 @@ public:
...
@@ -70,7 +70,7 @@ public:
/* run the leader (this is the core process) */
/* run the leader (this is the core process) */
virtual
virtual
void
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
Params
,
NetBase
*
net
);
void
Run
(
XConfig
*
config
,
DataDistributeBase
*
dataDistributor
,
XModel
*
model
);
};
};
}
}
#endif // __XTRAINER_H__
#endif // __XTRAINER_H__
\ No newline at end of file
source/train/XWorkerJob.cpp
查看文件 @
e3455593
...
@@ -26,12 +26,14 @@
...
@@ -26,12 +26,14 @@
*/
*/
#include "XWorkerJob.h"
#include "XWorkerJob.h"
#include "../tensor/XList.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/* constructor */
/* constructor */
XWorkerJob
::
XWorkerJob
()
XWorkerJob
::
XWorkerJob
()
{
{
}
}
/* de-constructor */
/* de-constructor */
...
@@ -39,19 +41,31 @@ XWorkerJob::~XWorkerJob()
...
@@ -39,19 +41,31 @@ XWorkerJob::~XWorkerJob()
{
{
}
}
/* set the model */
void
XWorkerJob
::
SetModel
(
XModel
*
myModel
)
{
model
=
myModel
;
}
/* get the model */
XModel
*
XWorkerJob
::
GetModel
()
{
return
model
;
}
/*
/*
add a new job of model refreshment
add a new job of model refreshment
>>
paramKeeper - keeper of the model parameters
>>
myModel - the model
<< return - succeeded or not
<< return - succeeded or not
*/
*/
bool
XWorkerJob
::
AddJobRefresh
(
XModel
*
paramKeeper
)
bool
XWorkerJob
::
AddJobRefresh
(
XModel
*
myModel
)
{
{
CheckNTErrors
(
paramKeeper
!=
NULL
,
"no parameter keeper!"
);
CheckNTErrors
(
myModel
!=
NULL
,
"no parameter keeper!"
);
XList
args
(
1
);
XList
args
(
1
);
args
.
Add
(
paramKeeper
);
args
.
Add
(
myModel
);
queue
.
EnqueueJob
((
void
*
)
&
paramKeeper
->
Refresh
,
&
args
);
queue
.
EnqueueJob
((
void
*
)
&
myModel
->
Refresh
,
&
args
);
return
true
;
return
true
;
}
}
...
@@ -59,20 +73,20 @@ bool XWorkerJob::AddJobRefresh(XModel * paramKeeper)
...
@@ -59,20 +73,20 @@ bool XWorkerJob::AddJobRefresh(XModel * paramKeeper)
/*
/*
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
>> func - the function that calls the run of the neural network
>>
net - the neural network
>>
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
,
void
*
net
,
XList
*
inputs
,
XList
*
outputs
)
bool
XWorkerJob
::
AddJobNeuralNet
(
void
*
func
,
XModel
*
myModel
,
XList
*
inputs
,
XList
*
outputs
)
{
{
CheckNTErrors
(
func
!=
NULL
,
"no input function!"
);
CheckNTErrors
(
func
!=
NULL
,
"no input function!"
);
CheckNTErrors
(
net
!=
NULL
,
"no input neural network!"
);
CheckNTErrors
(
myModel
!=
NULL
,
"no input neural network!"
);
XList
args
;
XList
args
;
args
.
AddList
(
inputs
);
args
.
AddList
(
inputs
);
args
.
AddList
(
outputs
);
args
.
AddList
(
outputs
);
args
.
Add
(
net
);
args
.
Add
(
myModel
);
queue
.
EnqueueJob
(
func
,
&
args
);
queue
.
EnqueueJob
(
func
,
&
args
);
...
...
source/train/XWorkerJob.h
查看文件 @
e3455593
...
@@ -31,6 +31,8 @@
...
@@ -31,6 +31,8 @@
#include "XWorker.h"
#include "XWorker.h"
#include "XModel.h"
#include "XModel.h"
#include "XBaseTemplate.h"
#include "../tensor/XList.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
...
@@ -38,6 +40,8 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
...
@@ -38,6 +40,8 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
class
XWorkerJob
:
public
XWorker
class
XWorkerJob
:
public
XWorker
{
{
protected
:
protected
:
/* the model */
XModel
*
model
;
public
:
public
:
...
@@ -47,11 +51,17 @@ public:
...
@@ -47,11 +51,17 @@ public:
/* de-constructor */
/* de-constructor */
~
XWorkerJob
();
~
XWorkerJob
();
/* set the parameter keeper */
void
SetModel
(
XModel
*
myModel
);
/* get the parameter keeper */
XModel
*
GetModel
();
/* add a new job of model refreshment */
/* add a new job of model refreshment */
bool
AddJobRefresh
(
XModel
*
paramKeeper
);
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
,
void
*
net
,
XList
*
inputs
,
XList
*
outputs
);
bool
AddJobNeuralNet
(
void
*
func
,
XModel
*
myModel
,
XList
*
inputs
,
XList
*
outputs
);
};
};
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论