Skip to content

Commit a674854

Browse files
committed
Pre-computing GRU_A's input contribution.
1 parent 3fcd879 commit a674854

File tree

4 files changed

+90
-13
lines changed

4 files changed

+90
-13
lines changed

src/dump_lpcnet.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,22 @@ def dump_gru_layer(self, f, hf):
9191
CuDNNGRU.dump_layer = dump_gru_layer
9292
GRU.dump_layer = dump_gru_layer
9393

94+
def dump_dense_layer_impl(name, weights, bias, activation, f, hf):
95+
printVector(f, weights, name + '_weights')
96+
printVector(f, bias, name + '_bias')
97+
f.write('const DenseLayer {} = {{\n {}_bias,\n {}_weights,\n {}, {}, ACTIVATION_{}\n}};\n\n'
98+
.format(name, name, name, weights.shape[0], weights.shape[1], activation))
99+
hf.write('#define {}_OUT_SIZE {}\n'.format(name.upper(), weights.shape[1]))
100+
hf.write('extern const DenseLayer {};\n\n'.format(name));
101+
94102
def dump_dense_layer(self, f, hf):
95103
name = self.name
96104
print("printing layer " + name + " of type " + self.__class__.__name__)
97105
weights = self.get_weights()
98-
printVector(f, weights[0], name + '_weights')
99-
printVector(f, weights[-1], name + '_bias')
100106
activation = self.activation.__name__.upper()
101-
f.write('const DenseLayer {} = {{\n {}_bias,\n {}_weights,\n {}, {}, ACTIVATION_{}\n}};\n\n'
102-
.format(name, name, name, weights[0].shape[0], weights[0].shape[1], activation))
103-
hf.write('#define {}_OUT_SIZE {}\n'.format(name.upper(), weights[0].shape[1]))
104-
hf.write('extern const DenseLayer {};\n\n'.format(name));
107+
dump_dense_layer_impl(name, weights[0], weights[1], activation, f, hf)
105108
return False
109+
106110
Dense.dump_layer = dump_dense_layer
107111

108112
def dump_mdense_layer(self, f, hf):
@@ -141,15 +145,18 @@ def dump_conv1d_layer(self, f, hf):
141145
Conv1D.dump_layer = dump_conv1d_layer
142146

143147

148+
def dump_embedding_layer_impl(name, weights, f, hf):
149+
printVector(f, weights, name + '_weights')
150+
f.write('const EmbeddingLayer {} = {{\n {}_weights,\n {}, {}\n}};\n\n'
151+
.format(name, name, weights.shape[0], weights.shape[1]))
152+
hf.write('#define {}_OUT_SIZE {}\n'.format(name.upper(), weights.shape[1]))
153+
hf.write('extern const EmbeddingLayer {};\n\n'.format(name));
154+
144155
def dump_embedding_layer(self, f, hf):
145156
name = self.name
146157
print("printing layer " + name + " of type " + self.__class__.__name__)
147-
weights = self.get_weights()
148-
printVector(f, weights[0], name + '_weights')
149-
f.write('const EmbeddingLayer {} = {{\n {}_weights,\n {}, {}\n}};\n\n'
150-
.format(name, name, weights[0].shape[0], weights[0].shape[1]))
151-
hf.write('#define {}_OUT_SIZE {}\n'.format(name.upper(), weights[0].shape[1]))
152-
hf.write('extern const EmbeddingLayer {};\n\n'.format(name));
158+
weights = self.get_weights()[0]
159+
dump_embedding_layer_impl(name, weights, f, hf)
153160
return False
154161
Embedding.dump_layer = dump_embedding_layer
155162

@@ -178,6 +185,21 @@ def dump_embedding_layer(self, f, hf):
178185
hf.write('/*This file is automatically generated from a Keras model*/\n\n')
179186
hf.write('#ifndef RNN_DATA_H\n#define RNN_DATA_H\n\n#include "nnet.h"\n\n')
180187

188+
embed_size = lpcnet.embed_size
189+
190+
E = model.get_layer('embed_sig').get_weights()[0]
191+
W = model.layers[18].get_weights()[0][:embed_size,:]
192+
dump_embedding_layer_impl('gru_a_embed_sig', np.dot(E, W), f, hf)
193+
W = model.layers[18].get_weights()[0][embed_size:2*embed_size,:]
194+
dump_embedding_layer_impl('gru_a_embed_pred', np.dot(E, W), f, hf)
195+
E = model.get_layer('embed_exc').get_weights()[0]
196+
W = model.layers[18].get_weights()[0][2*embed_size:3*embed_size,:]
197+
dump_embedding_layer_impl('gru_a_embed_exc', np.dot(E, W), f, hf)
198+
W = model.layers[18].get_weights()[0][3*embed_size:,:]
199+
#FIXME: dump only half the biases
200+
b = model.layers[18].get_weights()[2]
201+
dump_dense_layer_impl('gru_a_dense_feature', W, b, 'LINEAR', f, hf)
202+
181203
layer_list = []
182204
for i, layer in enumerate(model.layers):
183205
if layer.dump_layer(f, hf):

src/lpcnet.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,17 @@ void run_frame_network(LPCNetState *lpcnet, float *condition, const float *featu
116116
void run_sample_network(NNetState *net, float *pdf, const float *condition, int last_exc, int last_sig, int pred)
117117
{
118118
float in_a[SAMPLE_INPUT_SIZE];
119+
float gru_a_input[3*GRU_A_STATE_SIZE];
119120
float in_b[GRU_A_STATE_SIZE+FEATURE_DENSE2_OUT_SIZE];
120121
compute_embedding(&embed_sig, &in_a[0], last_sig);
121122
compute_embedding(&embed_sig, &in_a[EMBED_SIG_OUT_SIZE], pred);
122123
compute_embedding(&embed_exc, &in_a[2*EMBED_SIG_OUT_SIZE], last_exc);
123124
RNN_COPY(&in_a[2*EMBED_SIG_OUT_SIZE + EMBED_EXC_OUT_SIZE], condition, FEATURE_DENSE2_OUT_SIZE);
124-
compute_gru2(&gru_a, net->gru_a_state, in_a);
125+
compute_dense(&gru_a_dense_feature, gru_a_input, condition);
126+
accum_embedding(&gru_a_embed_sig, gru_a_input, last_sig);
127+
accum_embedding(&gru_a_embed_pred, gru_a_input, pred);
128+
accum_embedding(&gru_a_embed_exc, gru_a_input, last_exc);
129+
compute_gru3(&gru_a, net->gru_a_state, gru_a_input);
125130
RNN_COPY(in_b, net->gru_a_state, GRU_A_STATE_SIZE);
126131
RNN_COPY(&in_b[GRU_A_STATE_SIZE], condition, FEATURE_DENSE2_OUT_SIZE);
127132
compute_gru2(&gru_b, net->gru_b_state, in_b);

src/nnet.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,40 @@ void compute_gru2(const GRULayer *gru, float *state, const float *input)
256256
state[i] = h[i];
257257
}
258258

259+
void compute_gru3(const GRULayer *gru, float *state, const float *input)
260+
{
261+
int i;
262+
int N;
263+
int stride;
264+
float zrh[3*MAX_RNN_NEURONS];
265+
float recur[3*MAX_RNN_NEURONS];
266+
float *z;
267+
float *r;
268+
float *h;
269+
N = gru->nb_neurons;
270+
z = zrh;
271+
r = &zrh[N];
272+
h = &zrh[2*N];
273+
celt_assert(gru->nb_neurons <= MAX_RNN_NEURONS);
274+
celt_assert(input != state);
275+
celt_assert(gru->reset_after);
276+
stride = 3*N;
277+
RNN_COPY(zrh, input, 3*N);
278+
for (i=0;i<3*N;i++)
279+
recur[i] = gru->bias[3*N + i];
280+
gemm_accum(recur, gru->recurrent_weights, 3*N, N, stride, state);
281+
for (i=0;i<2*N;i++)
282+
zrh[i] += recur[i];
283+
compute_activation(zrh, zrh, 2*N, ACTIVATION_SIGMOID);
284+
for (i=0;i<N;i++)
285+
h[i] += recur[2*N+i]*r[i];
286+
compute_activation(h, h, N, gru->activation);
287+
for (i=0;i<N;i++)
288+
h[i] = z[i]*state[i] + (1-z[i])*h[i];
289+
for (i=0;i<N;i++)
290+
state[i] = h[i];
291+
}
292+
259293
void compute_conv1d(const Conv1DLayer *layer, float *output, float *mem, const float *input)
260294
{
261295
int i;
@@ -288,6 +322,18 @@ void compute_embedding(const EmbeddingLayer *layer, float *output, int input)
288322
}
289323
}
290324

325+
void accum_embedding(const EmbeddingLayer *layer, float *output, int input)
326+
{
327+
int i;
328+
celt_assert(input >= 0);
329+
celt_assert(input < layer->nb_inputs);
330+
/*if (layer->dim == 64) printf("%d\n", input);*/
331+
for (i=0;i<layer->dim;i++)
332+
{
333+
output[i] += layer->embedding_weights[input*layer->dim + i];
334+
}
335+
}
336+
291337
int sample_from_pdf(const float *pdf, int N, float exp_boost, float pdf_floor)
292338
{
293339
int i;

src/nnet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ void compute_gru(const GRULayer *gru, float *state, const float *input);
8787

8888
void compute_gru2(const GRULayer *gru, float *state, const float *input);
8989

90+
void compute_gru3(const GRULayer *gru, float *state, const float *input);
91+
9092
void compute_conv1d(const Conv1DLayer *layer, float *output, float *mem, const float *input);
9193

9294
void compute_embedding(const EmbeddingLayer *layer, float *output, int input);
9395

96+
void accum_embedding(const EmbeddingLayer *layer, float *output, int input);
97+
9498
int sample_from_pdf(const float *pdf, int N, float exp_boost, float pdf_floor);
9599

96100
#endif /* _MLP_H_ */

0 commit comments

Comments
 (0)