2017-06-02 06:31:13 +03:00
|
|
|
#include "rnn_layer.h"
|
|
|
|
#include "connected_layer.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "cuda.h"
|
|
|
|
#include "blas.h"
|
|
|
|
#include "gemm.h"
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-03-01 00:54:12 +03:00
|
|
|
static void increment_layer(layer *l, int steps)
|
2016-02-05 11:15:12 +03:00
|
|
|
{
|
|
|
|
int num = l->outputs*l->batch*steps;
|
|
|
|
l->output += num;
|
|
|
|
l->delta += num;
|
|
|
|
l->x += num;
|
|
|
|
l->x_norm += num;
|
|
|
|
|
2016-02-05 23:49:06 +03:00
|
|
|
#ifdef GPU
|
2016-02-05 11:15:12 +03:00
|
|
|
l->output_gpu += num;
|
|
|
|
l->delta_gpu += num;
|
|
|
|
l->x_gpu += num;
|
|
|
|
l->x_norm_gpu += num;
|
2016-02-05 23:49:06 +03:00
|
|
|
#endif
|
2016-02-05 11:15:12 +03:00
|
|
|
}
|
2016-01-28 23:30:38 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
layer make_rnn_layer(int batch, int inputs, int outputs, int steps, ACTIVATION activation, int batch_normalize, int adam)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2016-02-01 02:52:03 +03:00
|
|
|
fprintf(stderr, "RNN Layer: %d inputs, %d outputs\n", inputs, outputs);
|
2016-01-28 23:30:38 +03:00
|
|
|
batch = batch / steps;
|
|
|
|
layer l = {0};
|
|
|
|
l.batch = batch;
|
|
|
|
l.type = RNN;
|
|
|
|
l.steps = steps;
|
|
|
|
l.inputs = inputs;
|
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
l.state = calloc(batch*outputs, sizeof(float));
|
|
|
|
l.prev_state = calloc(batch*outputs, sizeof(float));
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
l.input_layer = malloc(sizeof(layer));
|
2016-02-01 02:52:03 +03:00
|
|
|
fprintf(stderr, "\t\t");
|
2017-06-18 23:05:37 +03:00
|
|
|
*(l.input_layer) = make_connected_layer(batch*steps, inputs, outputs, activation, batch_normalize, adam);
|
2016-01-28 23:30:38 +03:00
|
|
|
l.input_layer->batch = batch;
|
|
|
|
|
|
|
|
l.self_layer = malloc(sizeof(layer));
|
2016-02-01 02:52:03 +03:00
|
|
|
fprintf(stderr, "\t\t");
|
2017-06-18 23:05:37 +03:00
|
|
|
*(l.self_layer) = make_connected_layer(batch*steps, outputs, outputs, activation, batch_normalize, adam);
|
2016-01-28 23:30:38 +03:00
|
|
|
l.self_layer->batch = batch;
|
|
|
|
|
|
|
|
l.output_layer = malloc(sizeof(layer));
|
2016-02-01 02:52:03 +03:00
|
|
|
fprintf(stderr, "\t\t");
|
2017-06-18 23:05:37 +03:00
|
|
|
*(l.output_layer) = make_connected_layer(batch*steps, outputs, outputs, activation, batch_normalize, adam);
|
2016-01-28 23:30:38 +03:00
|
|
|
l.output_layer->batch = batch;
|
|
|
|
|
|
|
|
l.outputs = outputs;
|
|
|
|
l.output = l.output_layer->output;
|
|
|
|
l.delta = l.output_layer->delta;
|
|
|
|
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward = forward_rnn_layer;
|
|
|
|
l.backward = backward_rnn_layer;
|
|
|
|
l.update = update_rnn_layer;
|
2016-02-05 11:15:12 +03:00
|
|
|
#ifdef GPU
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward_gpu = forward_rnn_layer_gpu;
|
|
|
|
l.backward_gpu = backward_rnn_layer_gpu;
|
|
|
|
l.update_gpu = update_rnn_layer_gpu;
|
2017-06-18 23:05:37 +03:00
|
|
|
l.state_gpu = cuda_make_array(0, batch*outputs);
|
|
|
|
l.prev_state_gpu = cuda_make_array(0, batch*outputs);
|
2016-01-28 23:30:38 +03:00
|
|
|
l.output_gpu = l.output_layer->output_gpu;
|
|
|
|
l.delta_gpu = l.output_layer->delta_gpu;
|
2017-06-18 23:05:37 +03:00
|
|
|
#ifdef CUDNN
|
|
|
|
cudnnSetTensor4dDescriptor(l.input_layer->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.input_layer->out_c, l.input_layer->out_h, l.input_layer->out_w);
|
|
|
|
cudnnSetTensor4dDescriptor(l.self_layer->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.self_layer->out_c, l.self_layer->out_h, l.self_layer->out_w);
|
|
|
|
cudnnSetTensor4dDescriptor(l.output_layer->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.output_layer->out_c, l.output_layer->out_h, l.output_layer->out_w);
|
|
|
|
#endif
|
2016-02-05 11:15:12 +03:00
|
|
|
#endif
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:19:08 +03:00
|
|
|
void update_rnn_layer(layer l, update_args a)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2017-06-13 02:19:08 +03:00
|
|
|
update_connected_layer(*(l.input_layer), a);
|
|
|
|
update_connected_layer(*(l.self_layer), a);
|
|
|
|
update_connected_layer(*(l.output_layer), a);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_rnn_layer(layer l, network net)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2017-04-10 05:56:42 +03:00
|
|
|
network s = net;
|
|
|
|
s.train = net.train;
|
2016-01-28 23:30:38 +03:00
|
|
|
int i;
|
|
|
|
layer input_layer = *(l.input_layer);
|
|
|
|
layer self_layer = *(l.self_layer);
|
|
|
|
layer output_layer = *(l.output_layer);
|
|
|
|
|
|
|
|
fill_cpu(l.outputs * l.batch * l.steps, 0, output_layer.delta, 1);
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_cpu(l.outputs * l.batch * l.steps, 0, self_layer.delta, 1);
|
|
|
|
fill_cpu(l.outputs * l.batch * l.steps, 0, input_layer.delta, 1);
|
|
|
|
if(net.train) fill_cpu(l.outputs * l.batch, 0, l.state, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
for (i = 0; i < l.steps; ++i) {
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input = net.input;
|
2016-01-28 23:30:38 +03:00
|
|
|
forward_connected_layer(input_layer, s);
|
|
|
|
|
|
|
|
s.input = l.state;
|
|
|
|
forward_connected_layer(self_layer, s);
|
|
|
|
|
2016-02-05 11:15:12 +03:00
|
|
|
float *old_state = l.state;
|
2017-06-18 23:05:37 +03:00
|
|
|
if(net.train) l.state += l.outputs*l.batch;
|
2016-02-05 11:15:12 +03:00
|
|
|
if(l.shortcut){
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_cpu(l.outputs * l.batch, old_state, 1, l.state, 1);
|
2016-02-05 11:15:12 +03:00
|
|
|
}else{
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_cpu(l.outputs * l.batch, 0, l.state, 1);
|
2016-02-05 11:15:12 +03:00
|
|
|
}
|
2017-06-18 23:05:37 +03:00
|
|
|
axpy_cpu(l.outputs * l.batch, 1, input_layer.output, 1, l.state, 1);
|
|
|
|
axpy_cpu(l.outputs * l.batch, 1, self_layer.output, 1, l.state, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
s.input = l.state;
|
|
|
|
forward_connected_layer(output_layer, s);
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
net.input += l.inputs*l.batch;
|
2016-02-05 11:15:12 +03:00
|
|
|
increment_layer(&input_layer, 1);
|
|
|
|
increment_layer(&self_layer, 1);
|
|
|
|
increment_layer(&output_layer, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_rnn_layer(layer l, network net)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2017-04-10 05:56:42 +03:00
|
|
|
network s = net;
|
|
|
|
s.train = net.train;
|
2016-01-28 23:30:38 +03:00
|
|
|
int i;
|
|
|
|
layer input_layer = *(l.input_layer);
|
|
|
|
layer self_layer = *(l.self_layer);
|
|
|
|
layer output_layer = *(l.output_layer);
|
|
|
|
|
2016-02-05 11:15:12 +03:00
|
|
|
increment_layer(&input_layer, l.steps-1);
|
|
|
|
increment_layer(&self_layer, l.steps-1);
|
|
|
|
increment_layer(&output_layer, l.steps-1);
|
2016-01-28 23:30:38 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
l.state += l.outputs*l.batch*l.steps;
|
2016-01-28 23:30:38 +03:00
|
|
|
for (i = l.steps-1; i >= 0; --i) {
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_cpu(l.outputs * l.batch, input_layer.output, 1, l.state, 1);
|
|
|
|
axpy_cpu(l.outputs * l.batch, 1, self_layer.output, 1, l.state, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
s.input = l.state;
|
|
|
|
s.delta = self_layer.delta;
|
|
|
|
backward_connected_layer(output_layer, s);
|
2016-02-05 11:15:12 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
l.state -= l.outputs*l.batch;
|
2016-02-05 11:15:12 +03:00
|
|
|
/*
|
|
|
|
if(i > 0){
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_cpu(l.outputs * l.batch, input_layer.output - l.outputs*l.batch, 1, l.state, 1);
|
|
|
|
axpy_cpu(l.outputs * l.batch, 1, self_layer.output - l.outputs*l.batch, 1, l.state, 1);
|
2016-02-05 11:15:12 +03:00
|
|
|
}else{
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_cpu(l.outputs * l.batch, 0, l.state, 1);
|
2016-02-05 11:15:12 +03:00
|
|
|
}
|
|
|
|
*/
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
s.input = l.state;
|
2017-06-18 23:05:37 +03:00
|
|
|
s.delta = self_layer.delta - l.outputs*l.batch;
|
2016-01-28 23:30:38 +03:00
|
|
|
if (i == 0) s.delta = 0;
|
|
|
|
backward_connected_layer(self_layer, s);
|
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_cpu(l.outputs*l.batch, self_layer.delta, 1, input_layer.delta, 1);
|
|
|
|
if (i > 0 && l.shortcut) axpy_cpu(l.outputs*l.batch, 1, self_layer.delta, 1, self_layer.delta - l.outputs*l.batch, 1);
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input = net.input + i*l.inputs*l.batch;
|
|
|
|
if(net.delta) s.delta = net.delta + i*l.inputs*l.batch;
|
2016-01-28 23:30:38 +03:00
|
|
|
else s.delta = 0;
|
|
|
|
backward_connected_layer(input_layer, s);
|
|
|
|
|
2016-02-05 11:15:12 +03:00
|
|
|
increment_layer(&input_layer, -1);
|
|
|
|
increment_layer(&self_layer, -1);
|
|
|
|
increment_layer(&output_layer, -1);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GPU
|
|
|
|
|
|
|
|
void pull_rnn_layer(layer l)
|
|
|
|
{
|
|
|
|
pull_connected_layer(*(l.input_layer));
|
|
|
|
pull_connected_layer(*(l.self_layer));
|
|
|
|
pull_connected_layer(*(l.output_layer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_rnn_layer(layer l)
|
|
|
|
{
|
|
|
|
push_connected_layer(*(l.input_layer));
|
|
|
|
push_connected_layer(*(l.self_layer));
|
|
|
|
push_connected_layer(*(l.output_layer));
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:19:08 +03:00
|
|
|
void update_rnn_layer_gpu(layer l, update_args a)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2017-06-13 02:19:08 +03:00
|
|
|
update_connected_layer_gpu(*(l.input_layer), a);
|
|
|
|
update_connected_layer_gpu(*(l.self_layer), a);
|
|
|
|
update_connected_layer_gpu(*(l.output_layer), a);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_rnn_layer_gpu(layer l, network net)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2017-06-18 23:05:37 +03:00
|
|
|
network s = {0};
|
2017-04-10 05:56:42 +03:00
|
|
|
s.train = net.train;
|
2016-01-28 23:30:38 +03:00
|
|
|
int i;
|
|
|
|
layer input_layer = *(l.input_layer);
|
|
|
|
layer self_layer = *(l.self_layer);
|
|
|
|
layer output_layer = *(l.output_layer);
|
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_gpu(l.outputs * l.batch * l.steps, 0, output_layer.delta_gpu, 1);
|
|
|
|
fill_gpu(l.outputs * l.batch * l.steps, 0, self_layer.delta_gpu, 1);
|
|
|
|
fill_gpu(l.outputs * l.batch * l.steps, 0, input_layer.delta_gpu, 1);
|
|
|
|
|
|
|
|
if(net.train) {
|
|
|
|
fill_gpu(l.outputs * l.batch * l.steps, 0, l.delta_gpu, 1);
|
|
|
|
copy_gpu(l.outputs*l.batch, l.state_gpu, 1, l.prev_state_gpu, 1);
|
|
|
|
}
|
2016-01-28 23:30:38 +03:00
|
|
|
|
|
|
|
for (i = 0; i < l.steps; ++i) {
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input_gpu = net.input_gpu;
|
2016-01-28 23:30:38 +03:00
|
|
|
forward_connected_layer_gpu(input_layer, s);
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input_gpu = l.state_gpu;
|
2016-01-28 23:30:38 +03:00
|
|
|
forward_connected_layer_gpu(self_layer, s);
|
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_gpu(l.outputs * l.batch, 0, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, input_layer.output_gpu, 1, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input_gpu = l.state_gpu;
|
2016-01-28 23:30:38 +03:00
|
|
|
forward_connected_layer_gpu(output_layer, s);
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
net.input_gpu += l.inputs*l.batch;
|
2016-02-05 11:15:12 +03:00
|
|
|
increment_layer(&input_layer, 1);
|
|
|
|
increment_layer(&self_layer, 1);
|
|
|
|
increment_layer(&output_layer, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_rnn_layer_gpu(layer l, network net)
|
2016-01-28 23:30:38 +03:00
|
|
|
{
|
2017-06-18 23:05:37 +03:00
|
|
|
network s = {0};
|
2017-04-10 05:56:42 +03:00
|
|
|
s.train = net.train;
|
2016-01-28 23:30:38 +03:00
|
|
|
int i;
|
|
|
|
layer input_layer = *(l.input_layer);
|
|
|
|
layer self_layer = *(l.self_layer);
|
|
|
|
layer output_layer = *(l.output_layer);
|
2016-02-05 11:15:12 +03:00
|
|
|
increment_layer(&input_layer, l.steps - 1);
|
|
|
|
increment_layer(&self_layer, l.steps - 1);
|
|
|
|
increment_layer(&output_layer, l.steps - 1);
|
2017-06-18 23:05:37 +03:00
|
|
|
float *last_input = input_layer.output_gpu;
|
|
|
|
float *last_self = self_layer.output_gpu;
|
2016-01-28 23:30:38 +03:00
|
|
|
for (i = l.steps-1; i >= 0; --i) {
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_gpu(l.outputs * l.batch, 0, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, input_layer.output_gpu, 1, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, self_layer.output_gpu, 1, l.state_gpu, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input_gpu = l.state_gpu;
|
|
|
|
s.delta_gpu = self_layer.delta_gpu;
|
2016-01-28 23:30:38 +03:00
|
|
|
backward_connected_layer_gpu(output_layer, s);
|
2016-02-05 11:15:12 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
if(i != 0) {
|
|
|
|
fill_gpu(l.outputs * l.batch, 0, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, input_layer.output_gpu - l.outputs*l.batch, 1, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, self_layer.output_gpu - l.outputs*l.batch, 1, l.state_gpu, 1);
|
|
|
|
}else {
|
|
|
|
copy_gpu(l.outputs*l.batch, l.prev_state_gpu, 1, l.state_gpu, 1);
|
|
|
|
}
|
2016-01-28 23:30:38 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_gpu(l.outputs*l.batch, self_layer.delta_gpu, 1, input_layer.delta_gpu, 1);
|
2016-05-07 02:25:16 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input_gpu = l.state_gpu;
|
2017-06-18 23:05:37 +03:00
|
|
|
s.delta_gpu = (i > 0) ? self_layer.delta_gpu - l.outputs*l.batch : 0;
|
2017-04-10 05:56:42 +03:00
|
|
|
if (i == 0) s.delta_gpu = 0;
|
2016-01-28 23:30:38 +03:00
|
|
|
backward_connected_layer_gpu(self_layer, s);
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
s.input_gpu = net.input_gpu + i*l.inputs*l.batch;
|
|
|
|
if(net.delta_gpu) s.delta_gpu = net.delta_gpu + i*l.inputs*l.batch;
|
|
|
|
else s.delta_gpu = 0;
|
2016-01-28 23:30:38 +03:00
|
|
|
backward_connected_layer_gpu(input_layer, s);
|
|
|
|
|
2016-02-05 11:15:12 +03:00
|
|
|
increment_layer(&input_layer, -1);
|
|
|
|
increment_layer(&self_layer, -1);
|
|
|
|
increment_layer(&output_layer, -1);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_gpu(l.outputs * l.batch, 0, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, last_input, 1, l.state_gpu, 1);
|
|
|
|
axpy_gpu(l.outputs * l.batch, 1, last_self, 1, l.state_gpu, 1);
|
2016-01-28 23:30:38 +03:00
|
|
|
}
|
|
|
|
#endif
|