| 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 "opus__config.h"cxx . . $ `<file>/external/opusfile/opus/opus__config.h`/ |
| 30 | //#endif |
| 31 | |
| 32 | #include <math.h> |
| 33 | #include "opus_types.h"cxx . . $ `<file>/external/opusfile/opus/opus_types.h`/ |
| 34 | #include "opus_defines.h"cxx . . $ `<file>/external/opusfile/opus/opus_defines.h`/ |
| 35 | #include "arch.h"cxx . . $ `<file>/external/opusfile/opus/celt/arch.h`/ |
| 36 | #include "tansig_table.h"cxx . . $ `<file>/external/opusfile/opus/tansig_table.h`/ |
| 37 | #include "mlp.h"cxx . . $ `<file>/external/opusfile/opus/mlp.h`/ |
| 38 | |
| 39 | static OPUS_INLINEcxx . . $ `external/opusfile/opus/opus_defines.h:110:10`! float tansig_approxcxx . . $ tansig_approx(fbcf3211dfe1bfd1).(float x) |
| 40 | { |
| 41 | int i; |
| 42 | float y, dy; |
| 43 | float sign=1; |
| 44 | /* Tests are reversed to catch NaNs */ |
| 45 | if (!(x<8)) |
| 46 | return 1; |
| 47 | if (!(x>-8)) |
| 48 | return -1; |
| 49 | #ifndef FIXED_POINT |
| 50 | /* Another check in case of -ffast-math */ |
| 51 | if (celt_isnancxx . . $ `external/opusfile/opus/celt/arch.h:200:9`!(x)) |
| 52 | return 0; |
| 53 | #endif |
| 54 | if (x<0) |
| 55 | { |
| 56 | x=-x; |
| 57 | sign=-1; |
| 58 | } |
| 59 | i = (int)floor(.5f+25*x); |
| 60 | x -= .04f*i; |
| 61 | y = tansig_tablecxx . . $ tansig_table.[i]; |
| 62 | dy = 1-y*y; |
| 63 | y = y + x*dy*(1 - y*x); |
| 64 | return sign*y; |
| 65 | } |
| 66 | |
| 67 | static OPUS_INLINEcxx . . $ `external/opusfile/opus/opus_defines.h:110:10`! float sigmoid_approxcxx . . $ sigmoid_approx(fbcf3211dfe1bfd1).(float x) |
| 68 | { |
| 69 | return .5f + .5f*tansig_approxcxx . . $ tansig_approx(fbcf3211dfe1bfd1).(.5f*x); |
| 70 | } |
| 71 | |
| 72 | static void gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(float *out, const opus_int8cxx . . $ opus_int8# *weights, int rows, int cols, int col_stride, const float *x) |
| 73 | { |
| 74 | int i, j; |
| 75 | for (i=0;i<rows;i++) |
| 76 | { |
| 77 | for (j=0;j<cols;j++) |
| 78 | out[i] += weights[j*col_stride + i]*x[j]; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void compute_densecxx . . $ compute_dense(ca2ca097ee90386c).(const DenseLayercxx . . $ DenseLayer# *layer, float *output, const float *input) |
| 83 | { |
| 84 | int i; |
| 85 | int N, M; |
| 86 | int stride; |
| 87 | M = layer->nb_inputscxx . . $ $anonymous_type_498a7eee9f9b7fd7_0#nb_inputs.; |
| 88 | N = layer->nb_neuronscxx . . $ $anonymous_type_498a7eee9f9b7fd7_0#nb_neurons.; |
| 89 | stride = N; |
| 90 | for (i=0;i<N;i++) |
| 91 | output[i] = layer->biascxx . . $ $anonymous_type_498a7eee9f9b7fd7_0#bias.[i]; |
| 92 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(output, layer->input_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_0#input_weights., N, M, stride, input); |
| 93 | for (i=0;i<N;i++) |
| 94 | output[i] *= WEIGHTS_SCALEcxx . . $ `external/opusfile/opus/mlp.h:32:9`!; |
| 95 | if (layer->sigmoidcxx . . $ $anonymous_type_498a7eee9f9b7fd7_0#sigmoid.) { |
| 96 | for (i=0;i<N;i++) |
| 97 | output[i] = sigmoid_approxcxx . . $ sigmoid_approx(fbcf3211dfe1bfd1).(output[i]); |
| 98 | } else { |
| 99 | for (i=0;i<N;i++) |
| 100 | output[i] = tansig_approxcxx . . $ tansig_approx(fbcf3211dfe1bfd1).(output[i]); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void compute_grucxx . . $ compute_gru(f6728b7a28131e83).(const GRULayercxx . . $ GRULayer# *gru, float *state, const float *input) |
| 105 | { |
| 106 | int i; |
| 107 | int N, M; |
| 108 | int stride; |
| 109 | float tmp[MAX_NEURONScxx . . $ `external/opusfile/opus/mlp.h:34:9`!]; |
| 110 | float z[MAX_NEURONScxx . . $ `external/opusfile/opus/mlp.h:34:9`!]; |
| 111 | float r[MAX_NEURONScxx . . $ `external/opusfile/opus/mlp.h:34:9`!]; |
| 112 | float h[MAX_NEURONScxx . . $ `external/opusfile/opus/mlp.h:34:9`!]; |
| 113 | M = gru->nb_inputscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#nb_inputs.; |
| 114 | N = gru->nb_neuronscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#nb_neurons.; |
| 115 | stride = 3*N; |
| 116 | /* Compute update gate. */ |
| 117 | for (i=0;i<N;i++) |
| 118 | z[i] = gru->biascxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#bias.[i]; |
| 119 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(z, gru->input_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#input_weights., N, M, stride, input); |
| 120 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(z, gru->recurrent_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#recurrent_weights., N, N, stride, state); |
| 121 | for (i=0;i<N;i++) |
| 122 | z[i] = sigmoid_approxcxx . . $ sigmoid_approx(fbcf3211dfe1bfd1).(WEIGHTS_SCALEcxx . . $ `external/opusfile/opus/mlp.h:32:9`!*z[i]); |
| 123 | |
| 124 | /* Compute reset gate. */ |
| 125 | for (i=0;i<N;i++) |
| 126 | r[i] = gru->biascxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#bias.[N + i]; |
| 127 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(r, &gru->input_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#input_weights.[N], N, M, stride, input); |
| 128 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(r, &gru->recurrent_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#recurrent_weights.[N], N, N, stride, state); |
| 129 | for (i=0;i<N;i++) |
| 130 | r[i] = sigmoid_approxcxx . . $ sigmoid_approx(fbcf3211dfe1bfd1).(WEIGHTS_SCALEcxx . . $ `external/opusfile/opus/mlp.h:32:9`!*r[i]); |
| 131 | |
| 132 | /* Compute output. */ |
| 133 | for (i=0;i<N;i++) |
| 134 | h[i] = gru->biascxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#bias.[2*N + i]; |
| 135 | for (i=0;i<N;i++) |
| 136 | tmp[i] = state[i] * r[i]; |
| 137 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(h, &gru->input_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#input_weights.[2*N], N, M, stride, input); |
| 138 | gemm_accumcxx . . $ gemm_accum(3865e76c4745cc40).(h, &gru->recurrent_weightscxx . . $ $anonymous_type_498a7eee9f9b7fd7_1#recurrent_weights.[2*N], N, N, stride, tmp); |
| 139 | for (i=0;i<N;i++) |
| 140 | h[i] = z[i]*state[i] + (1-z[i])*tansig_approxcxx . . $ tansig_approx(fbcf3211dfe1bfd1).(WEIGHTS_SCALEcxx . . $ `external/opusfile/opus/mlp.h:32:9`!*h[i]); |
| 141 | for (i=0;i<N;i++) |
| 142 | state[i] = h[i]; |
| 143 | } |
| 144 |