-
Notifications
You must be signed in to change notification settings - Fork 9
/
los.cc
239 lines (194 loc) · 5.78 KB
/
los.cc
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <malloc.h>
#include <fenv.h>
#include <random>
#include <chrono>
#include <fstream>
#include "fvector.hh"
#include <iostream>
#include <array>
#include "gru.hh"
#include "textsupport.hh"
std::ofstream g_tree;//("./tree.part");
#include "tracked.hh"
#include "misc.hh"
#include <initializer_list>
using namespace std;
template<typename T>
struct GRUModel
{
struct State
{
GRULayer<T, 98, 250> gm1;
GRULayer<T, 250, 250> gm2;
Linear<T, 250, 98> fc;
void zeroGrad()
{
gm1.zeroGrad();
gm2.zeroGrad();
fc.zeroGrad();
}
void save(std::ostream& out) const
{
gm1.save(out);
gm2.save(out);
fc.save(out);
}
void load(std::istream& in)
{
gm1.load(in);
gm2.load(in);
fc.load(in);
}
template<typename W>
void makeProj(const W& w)
{
gm1.makeProj(w);
gm2.makeProj(w);
fc.makeProj(w);
}
template<typename W>
void projForward(W& w) const
{
gm1.projForward(w);
gm2.projForward(w);
fc.projForward(w);
}
template<typename W>
void projBackGrad(const W& w)
{
gm1.projBackGrad(w);
gm2.projBackGrad(w);
fc.projBackGrad(w);
}
};
vector<NNArray<T, 98, 1>> invec;
vector<NNArray<T, 1, 98>> expvec;
vector<NNArray<T, 98, 1>> scorevec;
TrackedNumber<T> totloss;
void unroll(State& s, unsigned int choplen)
{
cout<<"Unrolling the GRU";
totloss = TrackedNumber<T>(0.0);
for(size_t i = 0 ; i < choplen; ++i) {
cout<<"."; cout.flush();
NNArray<T, 98, 1> in;
NNArray<T, 1, 98> expected;
in.zero();
expected.zero();
invec.push_back(in);
expvec.push_back(expected);
auto res1 = s.fc.forward(s.gm2.forward(s.gm1.forward(in)));
auto score = res1.logSoftMax();
scorevec.push_back(score);
auto loss = TrackedNumber<T>(0.0) - (expected*score)(0,0);
totloss = totloss + loss;
}
totloss = totloss/TrackedNumber<T>(choplen);
cout<<"\n";
}
};
int main(int argc, char **argv)
{
BiMapper bm("corpus.txt", 98);
constexpr int choplen= 75;
vector<string> sentences=textChopper("corpus.txt", choplen, 10);
cout<<"Got "<<sentences.size()<<" sentences"<<endl;
Batcher batcher(sentences.size());
auto grum = make_unique<GRUModel<float>>();
GRUModel<float>::State s;
if(argc > 1) {
cout<<"Loading model state from "<<argv[1]<<endl;
loadModelState(s, argv[1]);
}
grum->unroll(s, choplen - 1);
constexpr int batchsize = 64;
cout<<"\nDoing topo.."; cout.flush();
auto topo = grum->totloss.getTopo();
cout<<" "<< topo.size() <<" entries"<<endl;
vector<std::array<unsigned int, 98>> invecprojarray, expvecprojarray, scorevecprojarray;
cout<<"Making float worker"<<endl;
for(auto& x : grum->scorevec) {
x.setVariable();
}
for(auto& x : grum->invec) {
x.setVariable();
}
for(auto& x : grum->expvec) {
x.setVariable();
}
auto w = grum->totloss.getWork<float>(topo);
for(const auto& x : grum->scorevec) {
scorevecprojarray.push_back(makeProj(x, w));
}
for(const auto& x : grum->invec)
invecprojarray.push_back(makeProj(x, w));
for(const auto& x : grum->expvec)
expvecprojarray.push_back(makeProj(x, w));
s.makeProj(w);
cout<<"Resetting GRU"<<endl;
cout<<TrackedNumberImp<float>::getCount()<<" instances before clean"<<endl;
grum.reset();
cout<<TrackedNumberImp<float>::getCount()<<" instances after clean"<<endl;
vector<NNArray<fvector<8>, 98, 1>> invec(choplen);
vector<NNArray<fvector<8>, 1, 98>> expvec(choplen);
vector<NNArray<fvector<8>, 98, 1>> scorevec(choplen);
cout<<"Making AVX2 worker"<<endl;
auto w8 = w.convert<fvector<8>>();
cout<<"Starting the work"<<endl;
for(;;) { // the batch loop
cout<<TrackedNumberImp<float>::getCount()<<" instances"<<endl;
double batchloss = 0;
w8.zeroGrad();
s.zeroGrad();
s.projForward(w8);
for(unsigned int minibatchno = 0 ; minibatchno < batchsize/8; ++minibatchno) {
s.gm1.d_prevh.reset();
s.gm2.d_prevh.reset();
auto minibatch = batcher.getBatch(8);
if(minibatch.size() != 8)
goto batcherEmpty;
for(size_t pos = 0 ; pos < choplen - 1; ++pos) {
invec[pos].zero();
expvec[pos].zero();
}
int avxindex =0;
for(const auto& idx : minibatch) {
string input = sentences[idx];
std::string output;
for(size_t pos = 0 ; pos < input.size() - 1; ++pos) {
cout<<input.at(pos);
invec[pos](bm.c2i(input.at(pos)), 0).impl->d_val.v[avxindex] = 1.0;
expvec[pos](0, bm.c2i(input.at(pos+1))).impl->d_val.v[avxindex] = 1.0;
}
cout<<endl;
avxindex++;
}
for(unsigned int projpos = 0; projpos < scorevecprojarray.size(); ++projpos) {
projForward(invecprojarray[projpos], invec[projpos], w8);
projForward(expvecprojarray[projpos], expvec[projpos], w8);
}
auto numloss = w8.getResult().sum()/8; // this triggers the whole calculation
for(unsigned int projpos = 0; projpos < scorevecprojarray.size(); ++projpos)
projBack(scorevecprojarray[projpos], w8, scorevec[projpos]);
for(int t =0 ; t< 8; ++t) {
for(size_t pos = 0 ; pos < choplen - 1; ++pos) {
cout<< bm.i2c(scorevec[pos].getUnparallel(t).maxValueIndexOfColumn(0));
}
cout<<"\n";
}
batchloss += numloss;
cout<<"\naverage loss: "<<numloss<<endl;
s.projBackGrad(w8);
}
// done with all the minibatches, have a batch
batchloss /= batchsize;
float lr=0.01/batchsize;
cout<<"Average batch loss: "<<batchloss<<endl;
s.gm1.learn(lr);
s.gm2.learn(lr);
s.fc.learn(lr);
// cout<<"\n\n";
saveModelState(s, "los-worker.state");
}
batcherEmpty:;
}