mlp.c (4181B)
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 8 - Redistributions of source code must retain the above copyright 9 notice, this list of conditions and the following disclaimer. 10 11 - Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifdef HAVE_CONFIG_H 29 #include "config.h" 30 #endif 31 32 #include <math.h> 33 #include "opus_types.h" 34 #include "opus_defines.h" 35 #include "arch.h" 36 #include "mlp.h" 37 38 #define fmadd(a, b, c) ((a)*(b)+(c)) 39 static OPUS_INLINE float tansig_approx(float x) 40 { 41 const float N0 = 952.52801514f; 42 const float N1 = 96.39235687f; 43 const float N2 = 0.60863042f; 44 const float D0 = 952.72399902f; 45 const float D1 = 413.36801147f; 46 const float D2 = 11.88600922f; 47 float X2, num, den; 48 X2 = x*x; 49 num = fmadd(fmadd(N2, X2, N1), X2, N0); 50 den = fmadd(fmadd(D2, X2, D1), X2, D0); 51 num = num*x/den; 52 return MAX32(-1.f, MIN32(1.f, num)); 53 } 54 55 static OPUS_INLINE float sigmoid_approx(float x) 56 { 57 return .5f + .5f*tansig_approx(.5f*x); 58 } 59 60 static void gemm_accum(float *out, const opus_int8 *weights, int rows, int cols, int col_stride, const float *x) 61 { 62 int i, j; 63 for (i=0;i<rows;i++) 64 { 65 for (j=0;j<cols;j++) 66 out[i] += weights[j*col_stride + i]*x[j]; 67 } 68 } 69 70 void analysis_compute_dense(const AnalysisDenseLayer *layer, float *output, const float *input) 71 { 72 int i; 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 output[i] = layer->bias[i]; 80 gemm_accum(output, layer->input_weights, N, M, stride, input); 81 for (i=0;i<N;i++) 82 output[i] *= WEIGHTS_SCALE; 83 if (layer->sigmoid) { 84 for (i=0;i<N;i++) 85 output[i] = sigmoid_approx(output[i]); 86 } else { 87 for (i=0;i<N;i++) 88 output[i] = tansig_approx(output[i]); 89 } 90 } 91 92 void analysis_compute_gru(const AnalysisGRULayer *gru, float *state, const float *input) 93 { 94 int i; 95 int N, M; 96 int stride; 97 float tmp[MAX_NEURONS]; 98 float z[MAX_NEURONS]; 99 float r[MAX_NEURONS]; 100 float h[MAX_NEURONS]; 101 M = gru->nb_inputs; 102 N = gru->nb_neurons; 103 stride = 3*N; 104 /* Compute update gate. */ 105 for (i=0;i<N;i++) 106 z[i] = gru->bias[i]; 107 gemm_accum(z, gru->input_weights, N, M, stride, input); 108 gemm_accum(z, gru->recurrent_weights, N, N, stride, state); 109 for (i=0;i<N;i++) 110 z[i] = sigmoid_approx(WEIGHTS_SCALE*z[i]); 111 112 /* Compute reset gate. */ 113 for (i=0;i<N;i++) 114 r[i] = gru->bias[N + i]; 115 gemm_accum(r, &gru->input_weights[N], N, M, stride, input); 116 gemm_accum(r, &gru->recurrent_weights[N], N, N, stride, state); 117 for (i=0;i<N;i++) 118 r[i] = sigmoid_approx(WEIGHTS_SCALE*r[i]); 119 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 }