-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMachCopy.cpp
138 lines (114 loc) · 4.08 KB
/
MachCopy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* This file is part of the continuous space language and translation model toolkit
* for statistical machine translation and large vocabulary speech recognition.
*
* Copyright 2015, Holger Schwenk, LIUM, University of Le Mans, France
*
* The CSLM toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
using namespace std;
#include <iostream>
#include "Tools.h"
#include "MachCopy.h"
#ifdef CUDA
# include "Gpu.cuh"
#endif
MachCopy::MachCopy(const int p_idim, const int p_odim, const int p_bsize, const ulong p_nbfw, const ulong p_nbbw)
: Mach(p_idim, p_odim, p_bsize, p_nbfw, p_nbbw)
{
#ifdef BLAS_CUDA
debug3("*** CUDA constructor MachCopy %d x %d on GPU %d\n", idim,odim,Gpu::GetCudaDevice(Gpu::GetDevice(gpu_conf)));
#else
debug2("*** constructor MachCopy %d x %d\n", idim,odim);
if (odim != idim) {
Error ("The input size should be equal the output size for copy machine");
}
#endif
}
MachCopy::MachCopy(const MachCopy &m)
: Mach(m)
{
debug0("*** copy constructor MachCopy\n");
}
/*******************************************
*
********************************************/
void MachCopy::Info(bool detailed, char *txt)
{
if (detailed) {
cout << "Information on copy machine" << endl;
Mach::Info(detailed,txt);
}
else {
printf("%sMachCopy %d-%d, bs=%d, passes=%lu/%lu", txt, idim, odim, bsize, nb_forw, nb_backw);
#ifdef BLAS_CUDA
printf(", on GPU %d", Gpu::GetCudaDevice(Gpu::GetDevice(gpu_conf)));
#endif
tm.disp(", ");
tm.newline();
#ifdef BLAS_CUDA
debug5("*** %s cuda data: %p -> %p, grad %p <- %p\n", txt, (void*)data_in, (void*)data_out, (void*)grad_in, (void*)grad_out);
#else
debug5("*** %s data: %p -> %p, grad %p <- %p\n", txt, (void*)data_in, (void*)data_out, (void*)grad_in, (void*)grad_out);
#endif
}
}
//-----------------------------------------------
// File input
//-----------------------------------------------
void MachCopy::ReadData(istream &inpf, size_t s, int bs)
{
debug0("*** read data of MachCopy\n");
if (0 != s)
ErrorN("data block of copy machine has %zu elements (0 were expected)", s);
Mach::ReadData(inpf, 0, bs);
}
//-----------------------------------------------
// Training
//-----------------------------------------------
void MachCopy::Forw(int eff_bsize, bool in_train)
{
debug1("*** MachCopy Forw %p\n", (void*)this);
tm.start();
if (!data_in)
Error("MachCopy::Forw(): input data is not set");
if (eff_bsize<=0) eff_bsize=bsize;
debugMachInp("MachCopy",data_in,idim,odim,eff_bsize);
#ifdef BLAS_CUDA
Gpu::MemcpyAsync(data_out, data_in, eff_bsize * odim * sizeof(REAL), cudaMemcpyDeviceToDevice);
debug4("*** CUDA: MachCopy::Forw %p[%d] -> %p[%d] \n",data_in,idim,data_out,odim);
#else
memcpy(data_out, data_in, eff_bsize * odim * sizeof(REAL));
#endif
nb_forw += eff_bsize;
tm.stop();
debugMachOutp("MachCopy",data_out,idim,odim,eff_bsize);
}
void MachCopy::Backw(const float lrate, const float wdecay, int eff_bsize)
{
debug2("*** MachCopy Backw %p <- %p\n",(void*)grad_in,(void*)grad_out);
if (eff_bsize<=0) eff_bsize=bsize;
if (!grad_out)
Error("MachCopy::Backw(): output gradient is not set");
debugMachOutp("MachCopy Grad",grad_out,idim,odim,eff_bsize);
tm.start();
#ifdef BLAS_CUDA
Gpu::MemcpyAsync(grad_in, grad_out, eff_bsize * odim * sizeof(REAL), cudaMemcpyDeviceToDevice);
#else
memcpy(grad_in, grad_out, eff_bsize * odim * sizeof(REAL));
#endif
nb_backw += eff_bsize;
tm.stop();
debugMachInp("MachCopy Grad",grad_in,idim,odim,eff_bsize);
}