vunhucuongit commited on
Commit
0d87b37
1 Parent(s): fef93e0

Upload 2 files

Browse files
Files changed (2) hide show
  1. en/model_weights.js +48 -0
  2. en/script.js +226 -0
en/model_weights.js ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var oReq = new XMLHttpRequest();
2
+ oReq.open("GET", "weights.bin", true);
3
+ oReq.responseType = "arraybuffer";
4
+
5
+ var weights_meta={'rnn/~/conv1_d__b': [[0, 73], [73]], 'rnn/~/conv1_d__w': [[73, 26718], [5, 73, 73]], 'rnn/~/embed__embeddings': [[26718, 64094], [512, 73]], 'rnn/~/embed_1__embeddings': [[64094, 69423], [73, 73]], 'rnn/~/linear__b': [[69423, 69454], [31]], 'rnn/~/linear__w': [[69454, 85326], [512, 31]], 'rnn/~/lstm_attention_core/~/gru__b': [[85326, 86862], [1536]], 'rnn/~/lstm_attention_core/~/gru__w_h': [[86862, 873294], [512, 1536]], 'rnn/~/lstm_attention_core/~/gru__w_i': [[873294, 990030], [76, 1536]], 'rnn/~/lstm_attention_core/~/gru_1__b': [[990030, 991566], [1536]], 'rnn/~/lstm_attention_core/~/gru_1__w_h': [[991566, 1777998], [512, 1536]], 'rnn/~/lstm_attention_core/~/gru_1__w_i': [[1777998, 2681166], [588, 1536]], 'rnn/~/lstm_attention_core/~/linear__b': [[2681166, 2681169], [3]], 'rnn/~/lstm_attention_core/~/linear__w': [[2681169, 2682933], [588, 3]]};
6
+
7
+ var WEIGHTS = {};
8
+ var weight_buffer = null;
9
+ var W = null;
10
+ var w32 = null;
11
+ var w16 = null;
12
+
13
+ oReq.onload = function (oEvent) {
14
+ var arrayBuffer = oReq.response; // Note: not oReq.responseText
15
+ if (arrayBuffer) {
16
+ // convert bfloat16 to float32
17
+ // w16 = new Uint16Array(arrayBuffer)
18
+ // weight_buffer = new SharedArrayBuffer(2*arrayBuffer.byteLength);
19
+ // w32 = new Uint16Array(weight_buffer);
20
+ // for(var i=0; i < w16.length; i++) {
21
+ // w32[i * 2 + 1] = w16[i];
22
+ // }
23
+ W = new Float32Array(arrayBuffer);
24
+ document.getElementById("btn").innerText = "Buffer arrieved";
25
+
26
+ for(var k in weights_meta) {
27
+ info = weights_meta[k];
28
+ offset = info[0];
29
+ shape = info[1];
30
+ WEIGHTS[k] = tf.tensor(W.subarray(offset[0], offset[1]), shape);
31
+ }
32
+
33
+ document.getElementById("btn").disabled = false;
34
+ tf.engine().startScope();
35
+ setTimeout(function() {
36
+ cur_run = cur_run + 1;
37
+ dojob(cur_run);
38
+ }, 0);
39
+
40
+ document.getElementById("btn").innerText = "Generate";
41
+ }
42
+ };
43
+
44
+ tf.setBackend('wasm');
45
+ tf.ready().then( function() {
46
+ tf.enableProdMode();
47
+ oReq.send(null);
48
+ });
en/script.js ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var log = console.log;
2
+ var ctx = null;
3
+ var canvas = null;
4
+ var RNN_SIZE = 512;
5
+ var cur_run = 0;
6
+
7
+ var randn = function() {
8
+ // Standard Normal random variable using Box-Muller transform.
9
+ var u = Math.random() * 0.999 + 1e-5;
10
+ var v = Math.random() * 0.999 + 1e-5;
11
+ return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
12
+ }
13
+
14
+ var rand_truncated_normal = function(low, high) {
15
+ while (true) {
16
+ r = randn();
17
+ if (r >= low && r <= high)
18
+ break;
19
+ // rejection sampling.
20
+ }
21
+ return r;
22
+ }
23
+
24
+ var softplus = function(x) {
25
+ const m = tf.maximum(x, 0.0);
26
+ return tf.add(m, tf.log(tf.add(tf.exp(tf.neg(m)), tf.exp(tf.sub(x, m)))));
27
+ }
28
+
29
+
30
+ var char2idx = {'\x00': 0, ' ': 1, '!': 2, '"': 3, '#': 4, "'": 5, '(': 6, ')': 7, ',': 8, '-': 9, '.': 10, '0': 11, '1': 12, '2': 13, '3': 14, '4': 15, '5': 16, '6': 17, '7': 18, '8': 19, '9': 20, ':': 21, ';': 22, '?': 23, 'A': 24, 'B': 25, 'C': 26, 'D': 27, 'E': 28, 'F': 29, 'G': 30, 'H': 31, 'I': 32, 'J': 33, 'K': 34, 'L': 35, 'M': 36, 'N': 37, 'O': 38, 'P': 39, 'R': 40, 'S': 41, 'T': 42, 'U': 43, 'V': 44, 'W': 45, 'Y': 46, 'a': 47, 'b': 48, 'c': 49, 'd': 50, 'e': 51, 'f': 52, 'g': 53, 'h': 54, 'i': 55, 'j': 56, 'k': 57, 'l': 58, 'm': 59, 'n': 60, 'o': 61, 'p': 62, 'q': 63, 'r': 64, 's': 65, 't': 66, 'u': 67, 'v': 68, 'w': 69, 'x': 70, 'y': 71, 'z': 72};
31
+
32
+ var gru_core = function(input, weights, state, hidden_size) {
33
+ var [w_h,w_i,b] = weights;
34
+ var [w_h_z,w_h_a] = tf.split(w_h, [2 * hidden_size, hidden_size], 1);
35
+ var [b_z,b_a] = tf.split(b, [2 * hidden_size, hidden_size], 0);
36
+ gates_x = tf.matMul(input, w_i);
37
+ [zr_x,a_x] = tf.split(gates_x, [2 * hidden_size, hidden_size], 1);
38
+ zr_h = tf.matMul(state, w_h_z);
39
+ zr = tf.add(tf.add(zr_x, zr_h), b_z);
40
+ // fix this
41
+ [z,r] = tf.split(tf.sigmoid(zr), 2, 1);
42
+ a_h = tf.matMul(tf.mul(r, state), w_h_a);
43
+ a = tf.tanh(tf.add(tf.add(a_x, a_h), b_a));
44
+ next_state = tf.add(tf.mul(tf.sub(1., z), state), tf.mul(z, a));
45
+ return [next_state, next_state];
46
+ };
47
+
48
+
49
+ var generate = function() {
50
+ cur_run = cur_run + 1;
51
+ setTimeout(function() {
52
+ var counter = 2000;
53
+ tf.disposeVariables();
54
+
55
+ tf.engine().startScope();
56
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
57
+ ctx.beginPath();
58
+ dojob(cur_run);
59
+ }, 200);
60
+
61
+ return false;
62
+ }
63
+
64
+ var dojob = function(run_id) {
65
+ var text = document.getElementById("user-input").value;
66
+ if (text.length == 0) {
67
+ text = "The quick brown fox jumps over the lazy dog";
68
+ }
69
+
70
+ var cur_x = 50.;
71
+ var cur_y = 300.;
72
+
73
+
74
+ log(text);
75
+ original_text = text;
76
+ text = '' + text + ' ' + text;
77
+
78
+ text = Array.from(text).map(function(e) {
79
+ return char2idx[e]
80
+ })
81
+ var text_embed = WEIGHTS['rnn/~/embed_1__embeddings'];
82
+ indices = tf.tensor1d(text, 'int32');
83
+ text = text_embed.gather(indices);
84
+
85
+ filter = WEIGHTS['rnn/~/conv1_d__w'];
86
+ embed = tf.conv1d(text, filter, 1, 'same');
87
+ bias = tf.expandDims(WEIGHTS['rnn/~/conv1_d__b'], 0);
88
+ embed = tf.add(embed, bias);
89
+
90
+ var writer_embed = WEIGHTS['rnn/~/embed__embeddings'];
91
+ var e = document.getElementById("writers");
92
+ var wid = parseInt(e.value);
93
+ // log(wid);
94
+
95
+ wid = tf.tensor1d([wid], 'int32');
96
+ wid = writer_embed.gather(wid);
97
+ embed = tf.add(wid, embed);
98
+
99
+ // initial state
100
+ var gru0_hx = tf.zeros([1, RNN_SIZE]);
101
+ var gru1_hx = tf.zeros([1, RNN_SIZE]);
102
+ // var gru2_hx = tf.zeros([1, RNN_SIZE]);
103
+
104
+ var att_location = tf.zeros([1, 1]);
105
+ var att_context = tf.zeros([1, 73]);
106
+
107
+ var input = tf.tensor([[0., 0., 1.]]);
108
+
109
+ gru0_w_h = WEIGHTS['rnn/~/lstm_attention_core/~/gru__w_h'];
110
+ gru0_w_i = WEIGHTS['rnn/~/lstm_attention_core/~/gru__w_i'];
111
+ gru0_bias = WEIGHTS['rnn/~/lstm_attention_core/~/gru__b'];
112
+
113
+ gru1_w_h = WEIGHTS['rnn/~/lstm_attention_core/~/gru_1__w_h'];
114
+ gru1_w_i = WEIGHTS['rnn/~/lstm_attention_core/~/gru_1__w_i'];
115
+ gru1_bias = WEIGHTS['rnn/~/lstm_attention_core/~/gru_1__b'];
116
+ att_w = WEIGHTS['rnn/~/lstm_attention_core/~/linear__w'];
117
+ att_b = WEIGHTS['rnn/~/lstm_attention_core/~/linear__b'];
118
+ gmm_w = WEIGHTS['rnn/~/linear__w'];
119
+ gmm_b = WEIGHTS['rnn/~/linear__b'];
120
+
121
+ ruler = tf.tensor([...Array(text.shape[0]).keys()]);
122
+ var bias = parseInt(document.getElementById("bias").value) / 100 * 3;
123
+
124
+ cur_x = 50.;
125
+ cur_y = 400.;
126
+ var path = [];
127
+ var dx = 0.;
128
+ var dy = 0;
129
+ var eos = 1.;
130
+ var counter = 0;
131
+
132
+
133
+ function loop(my_run_id) {
134
+ if (my_run_id < cur_run) {
135
+ tf.disposeVariables();
136
+ tf.engine().endScope();
137
+ return;
138
+ }
139
+
140
+ counter++;
141
+ if (counter < 2000) {
142
+ [att_location,att_context,gru0_hx,gru1_hx,input] = tf.tidy(function() {
143
+ // Attention
144
+ const inp_0 = tf.concat([att_context, input], 1);
145
+ gru0_hx_ = gru0_hx;
146
+ [out_0,gru0_hx] = gru_core(inp_0, [gru0_w_h, gru0_w_i, gru0_bias], gru0_hx, RNN_SIZE);
147
+ tf.dispose(gru0_hx_);
148
+ const att_inp = tf.concat([att_context, input, out_0], 1);
149
+ const att_params = tf.add(tf.matMul(att_inp, att_w), att_b);
150
+ [alpha,beta,kappa] = tf.split(softplus(att_params), 3, 1);
151
+ att_location_ = att_location;
152
+ att_location = tf.add(att_location, tf.div(kappa, 25.));
153
+ tf.dispose(att_location_)
154
+
155
+ const phi = tf.mul(alpha, tf.exp(tf.div(tf.neg(tf.square(tf.sub(att_location, ruler))), beta)));
156
+ att_context_ = att_context;
157
+ att_context = tf.sum(tf.mul(tf.expandDims(phi, 2), tf.expandDims(embed, 0)), 1)
158
+ tf.dispose(att_context_);
159
+
160
+ const inp_1 = tf.concat([input, out_0, att_context], 1);
161
+ tf.dispose(input);
162
+ gru1_hx_ = gru1_hx;
163
+ [out_1,gru1_hx] = gru_core(inp_1, [gru1_w_h, gru1_w_i, gru1_bias], gru1_hx, RNN_SIZE);
164
+ tf.dispose(gru1_hx_);
165
+
166
+ // GMM
167
+ const gmm_params = tf.add(tf.matMul(out_1, gmm_w), gmm_b);
168
+ [x,y,logstdx,logstdy,angle,log_weight,eos_logit] = tf.split(gmm_params, [5, 5, 5, 5, 5, 5, 1], 1);
169
+ // log_weight = tf.softmax(log_weight, 1);
170
+ // log_weight = tf.log(log_weight);
171
+ // log_weight = tf.mul(log_weight, 1. + bias);
172
+ // const idx = tf.multinomial(log_weight, 1).dataSync()[0];
173
+ // log_weight = tf.softmax(log_weight, 1);
174
+ // log_weight = tf.log(log_weight);
175
+ // log_weight = tf.mul(log_weight, 1. + bias);
176
+ const idx = tf.argMax(log_weight, 1).dataSync()[0];
177
+
178
+ x = x.dataSync()[idx];
179
+ y = y.dataSync()[idx];
180
+ const stdx = tf.exp(tf.sub(logstdx, bias)).dataSync()[idx];
181
+ const stdy = tf.exp(tf.sub(logstdy, bias)).dataSync()[idx];
182
+ angle = angle.dataSync()[idx];
183
+ e = tf.sigmoid(tf.mul(eos_logit, (1. + 0.*bias))).dataSync()[0];
184
+ const rx = rand_truncated_normal(-5, 5) * stdx;
185
+ const ry = rand_truncated_normal(-5, 5) * stdy;
186
+ x = x + Math.cos(-angle) * rx - Math.sin(-angle) * ry;
187
+ y = y + Math.sin(-angle) * rx + Math.cos(-angle) * ry;
188
+ if (Math.random() < e) {
189
+ e = 1.;
190
+ } else {
191
+ e = 0.;
192
+ }
193
+ input = tf.tensor([[x, y, e]]);
194
+ return [att_location, att_context, gru0_hx, gru1_hx, input];
195
+ });
196
+
197
+ [dx,dy,eos_] = input.dataSync();
198
+ dy = -dy * 3;
199
+ dx = dx * 3;
200
+ if (eos == 0.) {
201
+ ctx.beginPath();
202
+ ctx.moveTo(cur_x, cur_y, 0, 0);
203
+ ctx.lineTo(cur_x + dx, cur_y + dy);
204
+ ctx.stroke();
205
+ }
206
+ eos = eos_;
207
+ cur_x = cur_x + dx;
208
+ cur_y = cur_y + dy;
209
+
210
+ if (att_location.dataSync()[0] < original_text.length + 2) {
211
+ setTimeout(function() {loop(my_run_id);}, 0);
212
+ }
213
+ }
214
+ }
215
+
216
+ loop(run_id);
217
+ }
218
+
219
+ window.onload = function(e) {
220
+ //Setting up canvas
221
+ canvas = document.getElementById("hw-canvas");
222
+ ctx = canvas.getContext("2d");
223
+ ctx.canvas.width = window.innerWidth- 50;
224
+ ctx.canvas.height = window.innerHeight - 50;
225
+
226
+ }