rnn.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (c) 2008-2011 Octasic Inc.
  2. 2012-2017 Jean-Marc Valin */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  16. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <math.h>
  28. #include "opus_types.h"
  29. #include "common.h"
  30. #include "arch.h"
  31. #include "tansig_table.h"
  32. #include "rnn.h"
  33. #include "rnn_data.h"
  34. #include <stdio.h>
  35. static OPUS_INLINE float tansig_approx(float x)
  36. {
  37. int i;
  38. float y, dy;
  39. float sign=1;
  40. /* Tests are reversed to catch NaNs */
  41. if (!(x<8))
  42. return 1;
  43. if (!(x>-8))
  44. return -1;
  45. #ifndef FIXED_POINT
  46. /* Another check in case of -ffast-math */
  47. if (celt_isnan(x))
  48. return 0;
  49. #endif
  50. if (x<0)
  51. {
  52. x=-x;
  53. sign=-1;
  54. }
  55. i = (int)floor(.5f+25*x);
  56. x -= .04f*i;
  57. y = tansig_table[i];
  58. dy = 1-y*y;
  59. y = y + x*dy*(1 - y*x);
  60. return sign*y;
  61. }
  62. static OPUS_INLINE float sigmoid_approx(float x)
  63. {
  64. return .5 + .5*tansig_approx(.5*x);
  65. }
  66. static OPUS_INLINE float relu(float x)
  67. {
  68. return x < 0 ? 0 : x;
  69. }
  70. void compute_dense(const DenseLayer *layer, float *output, const float *input)
  71. {
  72. int i, j;
  73. int N, M;
  74. int stride;
  75. M = layer->nb_inputs;
  76. N = layer->nb_neurons;
  77. stride = N;
  78. for (i=0;i<N;i++)
  79. {
  80. /* Compute update gate. */
  81. float sum = layer->bias[i];
  82. for (j=0;j<M;j++)
  83. sum += layer->input_weights[j*stride + i]*input[j];
  84. output[i] = WEIGHTS_SCALE*sum;
  85. }
  86. if (layer->activation == ACTIVATION_SIGMOID) {
  87. for (i=0;i<N;i++)
  88. output[i] = sigmoid_approx(output[i]);
  89. } else if (layer->activation == ACTIVATION_TANH) {
  90. for (i=0;i<N;i++)
  91. output[i] = tansig_approx(output[i]);
  92. } else if (layer->activation == ACTIVATION_RELU) {
  93. for (i=0;i<N;i++)
  94. output[i] = relu(output[i]);
  95. } else {
  96. *(int*)0=0;
  97. }
  98. }
  99. void compute_gru(const GRULayer *gru, float *state, const float *input)
  100. {
  101. int i, j;
  102. int N, M;
  103. int stride;
  104. float z[MAX_NEURONS];
  105. float r[MAX_NEURONS];
  106. float h[MAX_NEURONS];
  107. M = gru->nb_inputs;
  108. N = gru->nb_neurons;
  109. stride = 3*N;
  110. for (i=0;i<N;i++)
  111. {
  112. /* Compute update gate. */
  113. float sum = gru->bias[i];
  114. for (j=0;j<M;j++)
  115. sum += gru->input_weights[j*stride + i]*input[j];
  116. for (j=0;j<N;j++)
  117. sum += gru->recurrent_weights[j*stride + i]*state[j];
  118. z[i] = sigmoid_approx(WEIGHTS_SCALE*sum);
  119. }
  120. for (i=0;i<N;i++)
  121. {
  122. /* Compute reset gate. */
  123. float sum = gru->bias[N + i];
  124. for (j=0;j<M;j++)
  125. sum += gru->input_weights[N + j*stride + i]*input[j];
  126. for (j=0;j<N;j++)
  127. sum += gru->recurrent_weights[N + j*stride + i]*state[j];
  128. r[i] = sigmoid_approx(WEIGHTS_SCALE*sum);
  129. }
  130. for (i=0;i<N;i++)
  131. {
  132. /* Compute output. */
  133. float sum = gru->bias[2*N + i];
  134. for (j=0;j<M;j++)
  135. sum += gru->input_weights[2*N + j*stride + i]*input[j];
  136. for (j=0;j<N;j++)
  137. sum += gru->recurrent_weights[2*N + j*stride + i]*state[j]*r[j];
  138. if (gru->activation == ACTIVATION_SIGMOID) sum = sigmoid_approx(WEIGHTS_SCALE*sum);
  139. else if (gru->activation == ACTIVATION_TANH) sum = tansig_approx(WEIGHTS_SCALE*sum);
  140. else if (gru->activation == ACTIVATION_RELU) sum = relu(WEIGHTS_SCALE*sum);
  141. else *(int*)0=0;
  142. h[i] = z[i]*state[i] + (1-z[i])*sum;
  143. }
  144. for (i=0;i<N;i++)
  145. state[i] = h[i];
  146. }
  147. #define INPUT_SIZE 42
  148. void compute_rnn(RNNState *rnn, float *gains, float *vad, const float *input) {
  149. int i;
  150. float dense_out[MAX_NEURONS];
  151. float noise_input[MAX_NEURONS*3];
  152. float denoise_input[MAX_NEURONS*3];
  153. compute_dense(rnn->model->input_dense, dense_out, input);
  154. compute_gru(rnn->model->vad_gru, rnn->vad_gru_state, dense_out);
  155. compute_dense(rnn->model->vad_output, vad, rnn->vad_gru_state);
  156. for (i=0;i<rnn->model->input_dense_size;i++) noise_input[i] = dense_out[i];
  157. for (i=0;i<rnn->model->vad_gru_size;i++) noise_input[i+rnn->model->input_dense_size] = rnn->vad_gru_state[i];
  158. for (i=0;i<INPUT_SIZE;i++) noise_input[i+rnn->model->input_dense_size+rnn->model->vad_gru_size] = input[i];
  159. compute_gru(rnn->model->noise_gru, rnn->noise_gru_state, noise_input);
  160. for (i=0;i<rnn->model->vad_gru_size;i++) denoise_input[i] = rnn->vad_gru_state[i];
  161. for (i=0;i<rnn->model->noise_gru_size;i++) denoise_input[i+rnn->model->vad_gru_size] = rnn->noise_gru_state[i];
  162. for (i=0;i<INPUT_SIZE;i++) denoise_input[i+rnn->model->vad_gru_size+rnn->model->noise_gru_size] = input[i];
  163. compute_gru(rnn->model->denoise_gru, rnn->denoise_gru_state, denoise_input);
  164. compute_dense(rnn->model->denoise_output, gains, rnn->denoise_gru_state);
  165. }