| 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 "tansig_table.h" |
| 37 | #include "mlp.h" |
| 38 |
| 39 | static OPUS_INLINE float tansig_approx(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_isnan(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_table[i]; |
| 62 | dy = 1-y*y; |
| 63 | y = y + x*dy*(1 - y*x); |
| 64 | return sign*y; |
| 65 | } |
| 66 |
| 67 | static OPUS_INLINE float sigmoid_approx(float x) |
| 68 | { |
| 69 | return .5f + .5f*tansig_approx(.5f*x); |
| 70 | } |
| 71 |
| 72 | void compute_dense(const DenseLayer *layer, float *output, const float *input) |
| 73 | { |
| 74 | int i, j; |
| 75 | int N, M; |
| 76 | int stride; |
| 77 | M = layer->nb_inputs; |
| 78 | N = layer->nb_neurons; |
| 79 | stride = N; |
| 80 | for (i=0;i<N;i++) |
| 81 | { |
| 82 | /* Compute update gate. */ |
| 83 | float sum = layer->bias[i]; |
| 84 | for (j=0;j<M;j++) |
| 85 | sum += layer->input_weights[j*stride + i]*input[j]; |
| 86 | output[i] = WEIGHTS_SCALE*sum; |
| 87 | } |
| 88 | if (layer->sigmoid) { |
| 89 | for (i=0;i<N;i++) |
| 90 | output[i] = sigmoid_approx(output[i]); |
| 91 | } else { |
| 92 | for (i=0;i<N;i++) |
| 93 | output[i] = tansig_approx(output[i]); |
| 94 | } |
| 95 | } |
| 96 |
| 97 | void compute_gru(const GRULayer *gru, float *state, const float *input) |
| 98 | { |
| 99 | int i, j; |
| 100 | int N, M; |
| 101 | int stride; |
| 102 | float z[MAX_NEURONS]; |
| 103 | float r[MAX_NEURONS]; |
| 104 | float h[MAX_NEURONS]; |
| 105 | M = gru->nb_inputs; |
| 106 | N = gru->nb_neurons; |
| 107 | stride = 3*N; |
| 108 | for (i=0;i<N;i++) |
| 109 | { |
| 110 | /* Compute update gate. */ |
| 111 | float sum = gru->bias[i]; |
| 112 | for (j=0;j<M;j++) |
| 113 | sum += gru->input_weights[j*stride + i]*input[j]; |
| 114 | for (j=0;j<N;j++) |
| 115 | sum += gru->recurrent_weights[j*stride + i]*state[j]; |
| 116 | z[i] = sigmoid_approx(WEIGHTS_SCALE*sum); |
| 117 | } |
| 118 | for (i=0;i<N;i++) |
| 119 | { |
| 120 | /* Compute reset gate. */ |
| 121 | float sum = gru->bias[N + i]; |
| 122 | for (j=0;j<M;j++) |
| 123 | sum += gru->input_weights[N + j*stride + i]*input[j]; |
| 124 | for (j=0;j<N;j++) |
| 125 | sum += gru->recurrent_weights[N + j*stride + i]*state[j]; |
| 126 | r[i] = sigmoid_approx(WEIGHTS_SCALE*sum); |
| 127 | } |
| 128 | for (i=0;i<N;i++) |
| 129 | { |
| 130 | /* Compute output. */ |
| 131 | float sum = gru->bias[2*N + i]; |
| 132 | for (j=0;j<M;j++) |
| 133 | sum += gru->input_weights[2*N + j*stride + i]*input[j]; |
| 134 | for (j=0;j<N;j++) |
| 135 | sum += gru->recurrent_weights[2*N + j*stride + i]*state[j]*r[j]; |
| 136 | h[i] = z[i]*state[i] + (1-z[i])*tansig_approx(WEIGHTS_SCALE*sum); |
| 137 | } |
| 138 | for (i=0;i<N;i++) |
| 139 | state[i] = h[i]; |
| 140 | } |
| 141 |
| 142 |