Skip to content

Commit

Permalink
corrected bug when using lrate DevideAndRecover
Browse files Browse the repository at this point in the history
  • Loading branch information
hschwenk committed Jul 10, 2015
1 parent cbcc372 commit 9134273
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Lrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ bool LrateDivideAndRecover::UpdateLrateOnDev(REAL rErrDev, REAL rBestErrDev, con
printf("error: %s\n", strerror(errno));
else {
// reload previous best machine parameters
Mach::SetShareOffs(random()); // use a new shareOffs since we have one globale table
Mach* pPrevMach = Mach::Read(ifs);
Mach::SetShareOffs(0); // reset
if ( (pMach->GetNbForw () >= pPrevMach->GetNbForw ())
&& (pMach->GetNbBackw() >= pPrevMach->GetNbBackw())
&& pMach->CopyParams(pPrevMach) )
Expand Down
29 changes: 17 additions & 12 deletions Mach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ using namespace std;
vector<Mach*> signal_mach;
int Mach::fileid=-1;
std::map<int, Mach *> prSharedMachines; // to store Mach pointers for sharing using clone() function
int shareOffs=0; // used to separate several machine loaded by multiple calls of Mach::Read()

#ifdef BLAS_CUDA
# include "Blas.h"
Expand Down Expand Up @@ -262,6 +263,7 @@ Mach *Mach::Read(istream &inpf, int bs)
char header[file_header_size], h[file_header_size];
int v;

debug1("###### READ with shareOffs %d\n",shareOffs);
inpf.read(header,file_header_size);
if (sscanf(header,"%s %d",h,&v) != 2) {
ErrorN("format of machine file not recognised: %s", header);
Expand Down Expand Up @@ -338,28 +340,30 @@ Mach *Mach::Read(istream &inpf, int bs)
// if version > 3 then check share-id
if(Mach::fileid >= file_header_version3){
m->ReadData(inpf, s, bs);
if(prSharedMachines[mt->GetShareId()] == NULL){
//fprintf(stderr, " ... new primary MachTab with share-id %d\n", mt->GetShareId());
prSharedMachines[mt->GetShareId()] = mt;
int shID = shareOffs+mt->GetShareId();
if(prSharedMachines[shID] == NULL){
//fprintf(stderr, " ... new primary MachTab with share-id %d\n", shID);
prSharedMachines[shID] = mt;
if(mt->GetTabAdr() == NULL) {
Error("Mach::Read: machine should have its weights allocated!\n");
}
} else {
//fprintf(stderr, " ... cloning secondary MachTab with share-id %d\n", mt->GetShareId());
m = prSharedMachines[mt->GetShareId()]->Clone();
//fprintf(stderr, " ... cloning secondary MachTab with share-id %d\n", shID);
m = prSharedMachines[shID]->Clone();
}

} else { // before file_header_version3, all MachTab in a MachPar share the weights

if(prSharedMachines[-1] == NULL ){
int shID = shareOffs + -1;
if(prSharedMachines[shID] == NULL ){
if(mt->bExternal==0) m->ReadData(inpf, s, bs); //read the data for the first MachTab
else{
Error("The first MachTab should have its own data but is set to have external data\n");
}
debug2("Storing address (%p) of machine %d\n",mt->GetTabAdr(),m);
prSharedMachines[-1]=m;
prSharedMachines[shID]=m;
} else {
m = prSharedMachines[-1]->Clone();
m = prSharedMachines[shID]->Clone();
debug1(" cloning MachTab, address = %p\n", mt->GetTabAdr());
//fprintf(stderr, " cloning MachTab, address = %p\n", mt->GetTabAdr());
}
Expand All @@ -368,18 +372,19 @@ Mach *Mach::Read(istream &inpf, int bs)
else if(Mach::fileid >= file_header_version4 && Mach::canShare(mtype)) {
//fprintf(stderr, "Shareable machine mtype = %d\n", mtype);
Shareable* sharem = dynamic_cast<Shareable*>(m);
int shID = shareOffs + sharem->GetShareId();
//fprintf(stderr, "Shareable: external=%d share-id=%d\n", sharem->HasExternalData(), sharem->GetShareId());
if(sharem->HasExternalData()){
if(prSharedMachines[sharem->GetShareId()] != NULL){
if(prSharedMachines[shID] != NULL){
//fprintf(stderr, " ... secondary machine with share-id %d -> cloning primary machine\n", sharem->GetShareId());
m = (MachLin*)prSharedMachines[sharem->GetShareId()]->Clone();
m = (MachLin*)prSharedMachines[shID]->Clone();
} else {
ErrorN("Found a secondary machine with shareid=%d, but the primary machine is not yet created\n", sharem->GetShareId());
}
} else {
if(sharem->GetShareId() != -1){
if(sharem->GetShareId() != shareOffs + -1){
//fprintf(stderr, " ... new primary machine with share-id %d\n", sharem->GetShareId());
prSharedMachines[sharem->GetShareId()] = m;
prSharedMachines[shID] = m;
}
//else { fprintf(stderr, " ... new primary machine with no sharing\n"); }
m->ReadData(inpf, s, bs);
Expand Down
9 changes: 7 additions & 2 deletions Mach.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#define file_header_mtype_max 32
#define file_header_mtype_avr 33

extern int shareOffs;
class Mach
{
private:
Expand Down Expand Up @@ -152,8 +153,12 @@ class Mach

static int GetFileId(){ return fileid;}
static void SetFileId(int id){ fileid = id;}
static bool canShare(int mtype){ return (mtype>=1 && mtype<=6) || (mtype>=8 && mtype<=10); }

static bool canShare(int mtype) {
return (mtype != file_header_mtype_base
&& mtype != file_header_mtype_stab
&& mtype <= file_header_mtype_softmax_class);
}
static void SetShareOffs(int val) { shareOffs = val; }
};

void GpuUnlock();
Expand Down
2 changes: 2 additions & 0 deletions MachLin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,12 @@ bool MachLin::CopyParams(Mach* mach)
memcpy(this->b, machlin->b, odim * sizeof(REAL));
memcpy(this->w, machlin->w, idim * odim * sizeof(REAL));
#endif
/*
if(Mach::fileid >= file_header_version4) {
this->bExternal = machlin->bExternal;
this->iShareId = machlin->iShareId;
}
*/
return true;
}
else
Expand Down

0 comments on commit 9134273

Please sign in to comment.