mlp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "opus_defines.h"
  30. #include "arch.h"
  31. #include "tansig_table.h"
  32. #include "mlp.h"
  33. static OPUS_INLINE float tansig_approx(float x)
  34. {
  35. int i;
  36. float y, dy;
  37. float sign=1;
  38. /* Tests are reversed to catch NaNs */
  39. if (!(x<8))
  40. return 1;
  41. if (!(x>-8))
  42. return -1;
  43. #ifndef FIXED_POINT
  44. /* Another check in case of -ffast-math */
  45. if (celt_isnan(x))
  46. return 0;
  47. #endif
  48. if (x<0)
  49. {
  50. x=-x;
  51. sign=-1;
  52. }
  53. i = (int)floor(.5f+25*x);
  54. x -= .04f*i;
  55. y = tansig_table[i];
  56. dy = 1-y*y;
  57. y = y + x*dy*(1 - y*x);
  58. return sign*y;
  59. }
  60. static OPUS_INLINE float sigmoid_approx(float x)
  61. {
  62. return .5f + .5f*tansig_approx(.5f*x);
  63. }
  64. static void gemm_accum(float *out, const opus_int8 *weights, int rows, int cols, int col_stride, const float *x)
  65. {
  66. int i, j;
  67. for (i=0;i<rows;i++)
  68. {
  69. for (j=0;j<cols;j++)
  70. out[i] += weights[j*col_stride + i]*x[j];
  71. }
  72. }
  73. void compute_dense(const DenseLayer *layer, float *output, const float *input)
  74. {
  75. int i;
  76. int N, M;
  77. int stride;
  78. M = layer->nb_inputs;
  79. N = layer->nb_neurons;
  80. stride = N;
  81. for (i=0;i<N;i++)
  82. output[i] = layer->bias[i];
  83. gemm_accum(output, layer->input_weights, N, M, stride, input);
  84. for (i=0;i<N;i++)
  85. output[i] *= WEIGHTS_SCALE;
  86. if (layer->sigmoid) {
  87. for (i=0;i<N;i++)
  88. output[i] = sigmoid_approx(output[i]);
  89. } else {
  90. for (i=0;i<N;i++)
  91. output[i] = tansig_approx(output[i]);
  92. }
  93. }
  94. void compute_gru(const GRULayer *gru, float *state, const float *input)
  95. {
  96. int i;
  97. int N, M;
  98. int stride;
  99. float tmp[MAX_NEURONS];
  100. float z[MAX_NEURONS];
  101. float r[MAX_NEURONS];
  102. float h[MAX_NEURONS];
  103. M = gru->nb_inputs;
  104. N = gru->nb_neurons;
  105. stride = 3*N;
  106. /* Compute update gate. */
  107. for (i=0;i<N;i++)
  108. z[i] = gru->bias[i];
  109. gemm_accum(z, gru->input_weights, N, M, stride, input);
  110. gemm_accum(z, gru->recurrent_weights, N, N, stride, state);
  111. for (i=0;i<N;i++)
  112. z[i] = sigmoid_approx(WEIGHTS_SCALE*z[i]);
  113. /* Compute reset gate. */
  114. for (i=0;i<N;i++)
  115. r[i] = gru->bias[N + i];
  116. gemm_accum(r, &gru->input_weights[N], N, M, stride, input);
  117. gemm_accum(r, &gru->recurrent_weights[N], N, N, stride, state);
  118. for (i=0;i<N;i++)
  119. r[i] = sigmoid_approx(WEIGHTS_SCALE*r[i]);
  120. /* Compute output. */
  121. for (i=0;i<N;i++)
  122. h[i] = gru->bias[2*N + i];
  123. for (i=0;i<N;i++)
  124. tmp[i] = state[i] * r[i];
  125. gemm_accum(h, &gru->input_weights[2*N], N, M, stride, input);
  126. gemm_accum(h, &gru->recurrent_weights[2*N], N, N, stride, tmp);
  127. for (i=0;i<N;i++)
  128. h[i] = z[i]*state[i] + (1-z[i])*tansig_approx(WEIGHTS_SCALE*h[i]);
  129. for (i=0;i<N;i++)
  130. state[i] = h[i];
  131. }