|
| 1 | +/* Copyright (c) 2022 Amazon |
| 2 | + Written by Jan Buethe */ |
| 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 COPYRIGHT OWNER |
| 19 | + OR 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 | +#include <math.h> |
| 29 | + |
| 30 | +#ifdef HAVE_CONFIG_H |
| 31 | +#include "config.h" |
| 32 | +#endif |
| 33 | + |
| 34 | + |
| 35 | +#include "rade_enc.h" |
| 36 | +#include "os_support.h" |
| 37 | +#include "rade_constants.h" |
| 38 | + |
| 39 | +static void conv1_cond_init(float *mem, int len, int dilation, int *init) |
| 40 | +{ |
| 41 | + if (!*init) { |
| 42 | + int i; |
| 43 | + for (i=0;i<dilation;i++) OPUS_CLEAR(&mem[i*len], len); |
| 44 | + } |
| 45 | + *init = 1; |
| 46 | +} |
| 47 | + |
| 48 | +void rade_encode_dframe( |
| 49 | + RDOVAEEncState *enc_state, /* io: encoder state */ |
| 50 | + const RDOVAEEnc *model, |
| 51 | + const float *input, /* i: double feature frame (concatenated) */ |
| 52 | + int arch |
| 53 | + ) |
| 54 | +{ |
| 55 | + //float padded_latents[DRED_PADDED_LATENT_DIM]; |
| 56 | + //float padded_state[DRED_PADDED_STATE_DIM]; |
| 57 | + float buffer[ENC_DENSE1_OUT_SIZE + ENC_GRU1_OUT_SIZE + ENC_GRU2_OUT_SIZE + ENC_GRU3_OUT_SIZE + ENC_GRU4_OUT_SIZE + ENC_GRU5_OUT_SIZE |
| 58 | + + ENC_CONV1_OUT_SIZE + ENC_CONV2_OUT_SIZE + ENC_CONV3_OUT_SIZE + ENC_CONV4_OUT_SIZE + ENC_CONV5_OUT_SIZE]; |
| 59 | + float state_hidden[GDENSE1_OUT_SIZE]; |
| 60 | + int output_index = 0; |
| 61 | + |
| 62 | + /* run encoder stack and concatenate output in buffer*/ |
| 63 | + compute_generic_dense(&model->enc_dense1, &buffer[output_index], input, ACTIVATION_TANH, arch); |
| 64 | + output_index += ENC_DENSE1_OUT_SIZE; |
| 65 | + |
| 66 | + compute_generic_gru(&model->enc_gru1_input, &model->enc_gru1_recurrent, enc_state->gru1_state, buffer, arch); |
| 67 | + OPUS_COPY(&buffer[output_index], enc_state->gru1_state, ENC_GRU1_OUT_SIZE); |
| 68 | + output_index += ENC_GRU1_OUT_SIZE; |
| 69 | + conv1_cond_init(enc_state->conv1_state, output_index, 1, &enc_state->initialized); |
| 70 | + compute_generic_conv1d(&model->enc_conv1, &buffer[output_index], enc_state->conv1_state, buffer, output_index, ACTIVATION_TANH, arch); |
| 71 | + output_index += ENC_CONV1_OUT_SIZE; |
| 72 | + |
| 73 | + compute_generic_gru(&model->enc_gru2_input, &model->enc_gru2_recurrent, enc_state->gru2_state, buffer, arch); |
| 74 | + OPUS_COPY(&buffer[output_index], enc_state->gru2_state, ENC_GRU2_OUT_SIZE); |
| 75 | + output_index += ENC_GRU2_OUT_SIZE; |
| 76 | + conv1_cond_init(enc_state->conv2_state, output_index, 2, &enc_state->initialized); |
| 77 | + compute_generic_conv1d_dilation(&model->enc_conv2, &buffer[output_index], enc_state->conv2_state, buffer, output_index, 2, ACTIVATION_TANH, arch); |
| 78 | + output_index += ENC_CONV2_OUT_SIZE; |
| 79 | + |
| 80 | + compute_generic_gru(&model->enc_gru3_input, &model->enc_gru3_recurrent, enc_state->gru3_state, buffer, arch); |
| 81 | + OPUS_COPY(&buffer[output_index], enc_state->gru3_state, ENC_GRU3_OUT_SIZE); |
| 82 | + output_index += ENC_GRU3_OUT_SIZE; |
| 83 | + conv1_cond_init(enc_state->conv3_state, output_index, 2, &enc_state->initialized); |
| 84 | + compute_generic_conv1d_dilation(&model->enc_conv3, &buffer[output_index], enc_state->conv3_state, buffer, output_index, 2, ACTIVATION_TANH, arch); |
| 85 | + output_index += ENC_CONV3_OUT_SIZE; |
| 86 | + |
| 87 | + compute_generic_gru(&model->enc_gru4_input, &model->enc_gru4_recurrent, enc_state->gru4_state, buffer, arch); |
| 88 | + OPUS_COPY(&buffer[output_index], enc_state->gru4_state, ENC_GRU4_OUT_SIZE); |
| 89 | + output_index += ENC_GRU4_OUT_SIZE; |
| 90 | + conv1_cond_init(enc_state->conv4_state, output_index, 2, &enc_state->initialized); |
| 91 | + compute_generic_conv1d_dilation(&model->enc_conv4, &buffer[output_index], enc_state->conv4_state, buffer, output_index, 2, ACTIVATION_TANH, arch); |
| 92 | + output_index += ENC_CONV4_OUT_SIZE; |
| 93 | + |
| 94 | + compute_generic_gru(&model->enc_gru5_input, &model->enc_gru5_recurrent, enc_state->gru5_state, buffer, arch); |
| 95 | + OPUS_COPY(&buffer[output_index], enc_state->gru5_state, ENC_GRU5_OUT_SIZE); |
| 96 | + output_index += ENC_GRU5_OUT_SIZE; |
| 97 | + conv1_cond_init(enc_state->conv5_state, output_index, 2, &enc_state->initialized); |
| 98 | + compute_generic_conv1d_dilation(&model->enc_conv5, &buffer[output_index], enc_state->conv5_state, buffer, output_index, 2, ACTIVATION_TANH, arch); |
| 99 | + output_index += ENC_CONV5_OUT_SIZE; |
| 100 | + |
| 101 | + compute_generic_dense(&model->enc_zdense, padded_latents, buffer, ACTIVATION_LINEAR, arch); |
| 102 | + OPUS_COPY(latents, padded_latents, RADE_LATENT_DIM); |
| 103 | + |
| 104 | + // DR: don't think we need this? |
| 105 | + #ifdef RM_ME |
| 106 | + /* next, calculate initial state */ |
| 107 | + compute_generic_dense(&model->gdense1, state_hidden, buffer, ACTIVATION_TANH, arch); |
| 108 | + compute_generic_dense(&model->gdense2, padded_state, state_hidden, ACTIVATION_LINEAR, arch); |
| 109 | + OPUS_COPY(initial_state, padded_state, DRED_STATE_DIM); |
| 110 | + #endif |
| 111 | +} |
0 commit comments