You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
13 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "gru_layer.h"
  2. #include "connected_layer.h"
  3. #include "utils.h"
  4. #include "dn_cuda.h"
  5. #include "blas.h"
  6. #include "gemm.h"
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. namespace DN{
  12. static void increment_layer(layer *l, int steps)
  13. {
  14. int num = l->outputs*l->batch*steps;
  15. l->output += num;
  16. l->delta += num;
  17. l->x += num;
  18. l->x_norm += num;
  19. #ifdef GPU
  20. l->output_gpu += num;
  21. l->delta_gpu += num;
  22. l->x_gpu += num;
  23. l->x_norm_gpu += num;
  24. #endif
  25. }
  26. layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam)
  27. {
  28. fprintf(stderr, "GRU Layer: %d inputs, %d outputs\n", inputs, outputs);
  29. batch = batch / steps;
  30. layer l;
  31. memset(&l, 0, sizeof(layer));
  32. l.batch = batch;
  33. l.type = GRU;
  34. l.steps = steps;
  35. l.inputs = inputs;
  36. l.uz = (layer*)malloc(sizeof(layer));
  37. fprintf(stderr, "\t\t");
  38. *(l.uz) = make_connected_layer(batch*steps, inputs, outputs, LINEAR, batch_normalize, adam);
  39. l.uz->batch = batch;
  40. l.wz = (layer*)malloc(sizeof(layer));
  41. fprintf(stderr, "\t\t");
  42. *(l.wz) = make_connected_layer(batch*steps, outputs, outputs, LINEAR, batch_normalize, adam);
  43. l.wz->batch = batch;
  44. l.ur = (layer*)malloc(sizeof(layer));
  45. fprintf(stderr, "\t\t");
  46. *(l.ur) = make_connected_layer(batch*steps, inputs, outputs, LINEAR, batch_normalize, adam);
  47. l.ur->batch = batch;
  48. l.wr = (layer*)malloc(sizeof(layer));
  49. fprintf(stderr, "\t\t");
  50. *(l.wr) = make_connected_layer(batch*steps, outputs, outputs, LINEAR, batch_normalize, adam);
  51. l.wr->batch = batch;
  52. l.uh = (layer*)malloc(sizeof(layer));
  53. fprintf(stderr, "\t\t");
  54. *(l.uh) = make_connected_layer(batch*steps, inputs, outputs, LINEAR, batch_normalize, adam);
  55. l.uh->batch = batch;
  56. l.wh = (layer*)malloc(sizeof(layer));
  57. fprintf(stderr, "\t\t");
  58. *(l.wh) = make_connected_layer(batch*steps, outputs, outputs, LINEAR, batch_normalize, adam);
  59. l.wh->batch = batch;
  60. l.batch_normalize = batch_normalize;
  61. l.outputs = outputs;
  62. l.output = (float*)calloc(outputs*batch*steps, sizeof(float));
  63. l.delta = (float*)calloc(outputs*batch*steps, sizeof(float));
  64. l.state = (float*)calloc(outputs*batch, sizeof(float));
  65. l.prev_state = (float*)calloc(outputs*batch, sizeof(float));
  66. l.forgot_state = (float*)calloc(outputs*batch, sizeof(float));
  67. l.forgot_delta = (float*)calloc(outputs*batch, sizeof(float));
  68. l.r_cpu = (float*)calloc(outputs*batch, sizeof(float));
  69. l.z_cpu = (float*)calloc(outputs*batch, sizeof(float));
  70. l.h_cpu = (float*)calloc(outputs*batch, sizeof(float));
  71. l.forward = forward_gru_layer;
  72. l.backward = backward_gru_layer;
  73. l.update = update_gru_layer;
  74. #ifdef GPU
  75. l.forward_gpu = forward_gru_layer_gpu;
  76. l.backward_gpu = backward_gru_layer_gpu;
  77. l.update_gpu = update_gru_layer_gpu;
  78. l.forgot_state_gpu = cuda_make_array(0, batch*outputs);
  79. l.forgot_delta_gpu = cuda_make_array(0, batch*outputs);
  80. l.prev_state_gpu = cuda_make_array(0, batch*outputs);
  81. l.state_gpu = cuda_make_array(0, batch*outputs);
  82. l.output_gpu = cuda_make_array(0, batch*outputs*steps);
  83. l.delta_gpu = cuda_make_array(0, batch*outputs*steps);
  84. l.r_gpu = cuda_make_array(0, batch*outputs);
  85. l.z_gpu = cuda_make_array(0, batch*outputs);
  86. l.h_gpu = cuda_make_array(0, batch*outputs);
  87. #ifdef CUDNN
  88. cudnnSetTensor4dDescriptor(l.uz->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.uz->out_c, l.uz->out_h, l.uz->out_w);
  89. cudnnSetTensor4dDescriptor(l.uh->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.uh->out_c, l.uh->out_h, l.uh->out_w);
  90. cudnnSetTensor4dDescriptor(l.ur->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.ur->out_c, l.ur->out_h, l.ur->out_w);
  91. cudnnSetTensor4dDescriptor(l.wz->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.wz->out_c, l.wz->out_h, l.wz->out_w);
  92. cudnnSetTensor4dDescriptor(l.wh->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.wh->out_c, l.wh->out_h, l.wh->out_w);
  93. cudnnSetTensor4dDescriptor(l.wr->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, batch, l.wr->out_c, l.wr->out_h, l.wr->out_w);
  94. #endif
  95. #endif
  96. return l;
  97. }
  98. void update_gru_layer(layer l, update_args a)
  99. {
  100. update_connected_layer(*(l.ur), a);
  101. update_connected_layer(*(l.uz), a);
  102. update_connected_layer(*(l.uh), a);
  103. update_connected_layer(*(l.wr), a);
  104. update_connected_layer(*(l.wz), a);
  105. update_connected_layer(*(l.wh), a);
  106. }
  107. void forward_gru_layer(layer l, network net)
  108. {
  109. network s = net;
  110. s.train = net.train;
  111. int i;
  112. layer uz = *(l.uz);
  113. layer ur = *(l.ur);
  114. layer uh = *(l.uh);
  115. layer wz = *(l.wz);
  116. layer wr = *(l.wr);
  117. layer wh = *(l.wh);
  118. fill_cpu(l.outputs * l.batch * l.steps, 0, uz.delta, 1);
  119. fill_cpu(l.outputs * l.batch * l.steps, 0, ur.delta, 1);
  120. fill_cpu(l.outputs * l.batch * l.steps, 0, uh.delta, 1);
  121. fill_cpu(l.outputs * l.batch * l.steps, 0, wz.delta, 1);
  122. fill_cpu(l.outputs * l.batch * l.steps, 0, wr.delta, 1);
  123. fill_cpu(l.outputs * l.batch * l.steps, 0, wh.delta, 1);
  124. if (net.train) {
  125. fill_cpu(l.outputs * l.batch * l.steps, 0, l.delta, 1);
  126. copy_cpu(l.outputs*l.batch, l.state, 1, l.prev_state, 1);
  127. }
  128. for (i = 0; i < l.steps; ++i) {
  129. s.input = l.state;
  130. forward_connected_layer(wz, s);
  131. forward_connected_layer(wr, s);
  132. s.input = net.input;
  133. forward_connected_layer(uz, s);
  134. forward_connected_layer(ur, s);
  135. forward_connected_layer(uh, s);
  136. copy_cpu(l.outputs*l.batch, uz.output, 1, l.z_cpu, 1);
  137. axpy_cpu(l.outputs*l.batch, 1, wz.output, 1, l.z_cpu, 1);
  138. copy_cpu(l.outputs*l.batch, ur.output, 1, l.r_cpu, 1);
  139. axpy_cpu(l.outputs*l.batch, 1, wr.output, 1, l.r_cpu, 1);
  140. activate_array(l.z_cpu, l.outputs*l.batch, LOGISTIC);
  141. activate_array(l.r_cpu, l.outputs*l.batch, LOGISTIC);
  142. copy_cpu(l.outputs*l.batch, l.state, 1, l.forgot_state, 1);
  143. mul_cpu(l.outputs*l.batch, l.r_cpu, 1, l.forgot_state, 1);
  144. s.input = l.forgot_state;
  145. forward_connected_layer(wh, s);
  146. copy_cpu(l.outputs*l.batch, uh.output, 1, l.h_cpu, 1);
  147. axpy_cpu(l.outputs*l.batch, 1, wh.output, 1, l.h_cpu, 1);
  148. if (l.tanh){
  149. activate_array(l.h_cpu, l.outputs*l.batch, TANH);
  150. }
  151. else {
  152. activate_array(l.h_cpu, l.outputs*l.batch, LOGISTIC);
  153. }
  154. weighted_sum_cpu(l.state, l.h_cpu, l.z_cpu, l.outputs*l.batch, l.output);
  155. copy_cpu(l.outputs*l.batch, l.output, 1, l.state, 1);
  156. net.input += l.inputs*l.batch;
  157. l.output += l.outputs*l.batch;
  158. increment_layer(&uz, 1);
  159. increment_layer(&ur, 1);
  160. increment_layer(&uh, 1);
  161. increment_layer(&wz, 1);
  162. increment_layer(&wr, 1);
  163. increment_layer(&wh, 1);
  164. }
  165. }
  166. void backward_gru_layer(layer l, network net)
  167. {
  168. }
  169. #ifdef GPU
  170. void pull_gru_layer(layer l)
  171. {
  172. }
  173. void push_gru_layer(layer l)
  174. {
  175. }
  176. void update_gru_layer_gpu(layer l, update_args a)
  177. {
  178. update_connected_layer_gpu(*(l.ur), a);
  179. update_connected_layer_gpu(*(l.uz), a);
  180. update_connected_layer_gpu(*(l.uh), a);
  181. update_connected_layer_gpu(*(l.wr), a);
  182. update_connected_layer_gpu(*(l.wz), a);
  183. update_connected_layer_gpu(*(l.wh), a);
  184. }
  185. void forward_gru_layer_gpu(layer l, network net)
  186. {
  187. network s = {0};
  188. s.train = net.train;
  189. int i;
  190. layer uz = *(l.uz);
  191. layer ur = *(l.ur);
  192. layer uh = *(l.uh);
  193. layer wz = *(l.wz);
  194. layer wr = *(l.wr);
  195. layer wh = *(l.wh);
  196. fill_gpu(l.outputs * l.batch * l.steps, 0, uz.delta_gpu, 1);
  197. fill_gpu(l.outputs * l.batch * l.steps, 0, ur.delta_gpu, 1);
  198. fill_gpu(l.outputs * l.batch * l.steps, 0, uh.delta_gpu, 1);
  199. fill_gpu(l.outputs * l.batch * l.steps, 0, wz.delta_gpu, 1);
  200. fill_gpu(l.outputs * l.batch * l.steps, 0, wr.delta_gpu, 1);
  201. fill_gpu(l.outputs * l.batch * l.steps, 0, wh.delta_gpu, 1);
  202. if(net.train) {
  203. fill_gpu(l.outputs * l.batch * l.steps, 0, l.delta_gpu, 1);
  204. copy_gpu(l.outputs*l.batch, l.state_gpu, 1, l.prev_state_gpu, 1);
  205. }
  206. for (i = 0; i < l.steps; ++i) {
  207. s.input_gpu = l.state_gpu;
  208. forward_connected_layer_gpu(wz, s);
  209. forward_connected_layer_gpu(wr, s);
  210. s.input_gpu = net.input_gpu;
  211. forward_connected_layer_gpu(uz, s);
  212. forward_connected_layer_gpu(ur, s);
  213. forward_connected_layer_gpu(uh, s);
  214. copy_gpu(l.outputs*l.batch, uz.output_gpu, 1, l.z_gpu, 1);
  215. axpy_gpu(l.outputs*l.batch, 1, wz.output_gpu, 1, l.z_gpu, 1);
  216. copy_gpu(l.outputs*l.batch, ur.output_gpu, 1, l.r_gpu, 1);
  217. axpy_gpu(l.outputs*l.batch, 1, wr.output_gpu, 1, l.r_gpu, 1);
  218. activate_array_gpu(l.z_gpu, l.outputs*l.batch, LOGISTIC);
  219. activate_array_gpu(l.r_gpu, l.outputs*l.batch, LOGISTIC);
  220. copy_gpu(l.outputs*l.batch, l.state_gpu, 1, l.forgot_state_gpu, 1);
  221. mul_gpu(l.outputs*l.batch, l.r_gpu, 1, l.forgot_state_gpu, 1);
  222. s.input_gpu = l.forgot_state_gpu;
  223. forward_connected_layer_gpu(wh, s);
  224. copy_gpu(l.outputs*l.batch, uh.output_gpu, 1, l.h_gpu, 1);
  225. axpy_gpu(l.outputs*l.batch, 1, wh.output_gpu, 1, l.h_gpu, 1);
  226. if(l.tanh){
  227. activate_array_gpu(l.h_gpu, l.outputs*l.batch, TANH);
  228. } else {
  229. activate_array_gpu(l.h_gpu, l.outputs*l.batch, LOGISTIC);
  230. }
  231. weighted_sum_gpu(l.state_gpu, l.h_gpu, l.z_gpu, l.outputs*l.batch, l.output_gpu);
  232. copy_gpu(l.outputs*l.batch, l.output_gpu, 1, l.state_gpu, 1);
  233. net.input_gpu += l.inputs*l.batch;
  234. l.output_gpu += l.outputs*l.batch;
  235. increment_layer(&uz, 1);
  236. increment_layer(&ur, 1);
  237. increment_layer(&uh, 1);
  238. increment_layer(&wz, 1);
  239. increment_layer(&wr, 1);
  240. increment_layer(&wh, 1);
  241. }
  242. }
  243. void backward_gru_layer_gpu(layer l, network net)
  244. {
  245. network s = {0};
  246. s.train = net.train;
  247. int i;
  248. layer uz = *(l.uz);
  249. layer ur = *(l.ur);
  250. layer uh = *(l.uh);
  251. layer wz = *(l.wz);
  252. layer wr = *(l.wr);
  253. layer wh = *(l.wh);
  254. increment_layer(&uz, l.steps - 1);
  255. increment_layer(&ur, l.steps - 1);
  256. increment_layer(&uh, l.steps - 1);
  257. increment_layer(&wz, l.steps - 1);
  258. increment_layer(&wr, l.steps - 1);
  259. increment_layer(&wh, l.steps - 1);
  260. net.input_gpu += l.inputs*l.batch*(l.steps-1);
  261. if(net.delta_gpu) net.delta_gpu += l.inputs*l.batch*(l.steps-1);
  262. l.output_gpu += l.outputs*l.batch*(l.steps-1);
  263. l.delta_gpu += l.outputs*l.batch*(l.steps-1);
  264. float *end_state = l.output_gpu;
  265. for (i = l.steps-1; i >= 0; --i) {
  266. if(i != 0) copy_gpu(l.outputs*l.batch, l.output_gpu - l.outputs*l.batch, 1, l.state_gpu, 1);
  267. else copy_gpu(l.outputs*l.batch, l.prev_state_gpu, 1, l.state_gpu, 1);
  268. float *prev_delta_gpu = (i == 0) ? 0 : l.delta_gpu - l.outputs*l.batch;
  269. copy_gpu(l.outputs*l.batch, uz.output_gpu, 1, l.z_gpu, 1);
  270. axpy_gpu(l.outputs*l.batch, 1, wz.output_gpu, 1, l.z_gpu, 1);
  271. copy_gpu(l.outputs*l.batch, ur.output_gpu, 1, l.r_gpu, 1);
  272. axpy_gpu(l.outputs*l.batch, 1, wr.output_gpu, 1, l.r_gpu, 1);
  273. activate_array_gpu(l.z_gpu, l.outputs*l.batch, LOGISTIC);
  274. activate_array_gpu(l.r_gpu, l.outputs*l.batch, LOGISTIC);
  275. copy_gpu(l.outputs*l.batch, uh.output_gpu, 1, l.h_gpu, 1);
  276. axpy_gpu(l.outputs*l.batch, 1, wh.output_gpu, 1, l.h_gpu, 1);
  277. if(l.tanh){
  278. activate_array_gpu(l.h_gpu, l.outputs*l.batch, TANH);
  279. } else {
  280. activate_array_gpu(l.h_gpu, l.outputs*l.batch, LOGISTIC);
  281. }
  282. weighted_delta_gpu(l.state_gpu, l.h_gpu, l.z_gpu, prev_delta_gpu, uh.delta_gpu, uz.delta_gpu, l.outputs*l.batch, l.delta_gpu);
  283. if(l.tanh){
  284. gradient_array_gpu(l.h_gpu, l.outputs*l.batch, TANH, uh.delta_gpu);
  285. } else {
  286. gradient_array_gpu(l.h_gpu, l.outputs*l.batch, LOGISTIC, uh.delta_gpu);
  287. }
  288. copy_gpu(l.outputs*l.batch, uh.delta_gpu, 1, wh.delta_gpu, 1);
  289. copy_gpu(l.outputs*l.batch, l.state_gpu, 1, l.forgot_state_gpu, 1);
  290. mul_gpu(l.outputs*l.batch, l.r_gpu, 1, l.forgot_state_gpu, 1);
  291. fill_gpu(l.outputs*l.batch, 0, l.forgot_delta_gpu, 1);
  292. s.input_gpu = l.forgot_state_gpu;
  293. s.delta_gpu = l.forgot_delta_gpu;
  294. backward_connected_layer_gpu(wh, s);
  295. if(prev_delta_gpu) mult_add_into_gpu(l.outputs*l.batch, l.forgot_delta_gpu, l.r_gpu, prev_delta_gpu);
  296. mult_add_into_gpu(l.outputs*l.batch, l.forgot_delta_gpu, l.state_gpu, ur.delta_gpu);
  297. gradient_array_gpu(l.r_gpu, l.outputs*l.batch, LOGISTIC, ur.delta_gpu);
  298. copy_gpu(l.outputs*l.batch, ur.delta_gpu, 1, wr.delta_gpu, 1);
  299. gradient_array_gpu(l.z_gpu, l.outputs*l.batch, LOGISTIC, uz.delta_gpu);
  300. copy_gpu(l.outputs*l.batch, uz.delta_gpu, 1, wz.delta_gpu, 1);
  301. s.input_gpu = l.state_gpu;
  302. s.delta_gpu = prev_delta_gpu;
  303. backward_connected_layer_gpu(wr, s);
  304. backward_connected_layer_gpu(wz, s);
  305. s.input_gpu = net.input_gpu;
  306. s.delta_gpu = net.delta_gpu;
  307. backward_connected_layer_gpu(uh, s);
  308. backward_connected_layer_gpu(ur, s);
  309. backward_connected_layer_gpu(uz, s);
  310. net.input_gpu -= l.inputs*l.batch;
  311. if(net.delta_gpu) net.delta_gpu -= l.inputs*l.batch;
  312. l.output_gpu -= l.outputs*l.batch;
  313. l.delta_gpu -= l.outputs*l.batch;
  314. increment_layer(&uz, -1);
  315. increment_layer(&ur, -1);
  316. increment_layer(&uh, -1);
  317. increment_layer(&wz, -1);
  318. increment_layer(&wr, -1);
  319. increment_layer(&wh, -1);
  320. }
  321. copy_gpu(l.outputs*l.batch, end_state, 1, l.state_gpu, 1);
  322. }
  323. #endif
  324. };