1 /* Copyright (c) 2018 Mozilla 2 Copyright (c) 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 #ifndef NNET_H_ 29 #define NNET_H_ 30 31 #include <stddef.h> 32 #include "opus_types.h" 33 34 #define ACTIVATION_LINEAR 0 35 #define ACTIVATION_SIGMOID 1 36 #define ACTIVATION_TANH 2 37 #define ACTIVATION_RELU 3 38 #define ACTIVATION_SOFTMAX 4 39 #define ACTIVATION_SWISH 5 40 41 #define WEIGHT_BLOB_VERSION 0 42 #define WEIGHT_BLOCK_SIZE 64 43 typedef struct { 44 const char *name; 45 int type; 46 int size; 47 const void *data; 48 } WeightArray; 49 50 #define WEIGHT_TYPE_float 0 51 #define WEIGHT_TYPE_int 1 52 #define WEIGHT_TYPE_qweight 2 53 #define WEIGHT_TYPE_int8 3 54 55 typedef struct { 56 char head[4]; 57 int version; 58 int type; 59 int size; 60 int block_size; 61 char name[44]; 62 } WeightHead; 63 64 /* Generic sparse affine transformation. */ 65 typedef struct { 66 const float *bias; 67 const float *subias; 68 const opus_int8 *weights; 69 const float *float_weights; 70 const int *weights_idx; 71 const float *diag; 72 const float *scale; 73 int nb_inputs; 74 int nb_outputs; 75 } LinearLayer; 76 77 /* Generic sparse affine transformation. */ 78 typedef struct { 79 const float *bias; 80 const float *float_weights; 81 int in_channels; 82 int out_channels; 83 int ktime; 84 int kheight; 85 } Conv2dLayer; 86 87 88 void compute_generic_dense(const LinearLayer *layer, float *output, const float *input, int activation, int arch); 89 void compute_generic_gru(const LinearLayer *input_weights, const LinearLayer *recurrent_weights, float *state, const float *in, int arch); 90 void compute_generic_conv1d(const LinearLayer *layer, float *output, float *mem, const float *input, int input_size, int activation, int arch); 91 void compute_generic_conv1d_dilation(const LinearLayer *layer, float *output, float *mem, const float *input, int input_size, int dilation, int activation, int arch); 92 void compute_glu(const LinearLayer *layer, float *output, const float *input, int arch); 93 void compute_gated_activation(const LinearLayer *layer, float *output, const float *input, int activation, int arch); 94 95 96 int parse_weights(WeightArray **list, const void *data, int len); 97 98 99 extern const WeightArray lpcnet_arrays[]; 100 extern const WeightArray plcmodel_arrays[]; 101 extern const WeightArray rdovaeenc_arrays[]; 102 extern const WeightArray rdovaedec_arrays[]; 103 extern const WeightArray fwgan_arrays[]; 104 extern const WeightArray fargan_arrays[]; 105 extern const WeightArray pitchdnn_arrays[]; 106 extern const WeightArray lossgen_arrays[]; 107 108 int linear_init(LinearLayer *layer, const WeightArray *arrays, 109 const char *bias, 110 const char *subias, 111 const char *weights, 112 const char *float_weights, 113 const char *weights_idx, 114 const char *diag, 115 const char *scale, 116 int nb_inputs, 117 int nb_outputs); 118 119 int conv2d_init(Conv2dLayer *layer, const WeightArray *arrays, 120 const char *bias, 121 const char *float_weights, 122 int in_channels, 123 int out_channels, 124 int ktime, 125 int kheight); 126 127 128 void compute_linear_c(const LinearLayer *linear, float *out, const float *in); 129 void compute_activation_c(float *output, const float *input, int N, int activation); 130 void compute_conv2d_c(const Conv2dLayer *conv, float *out, float *mem, const float *in, int height, int hstride, int activation); 131 132 133 #if defined(OPUS_ARM_MAY_HAVE_DOTPROD) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR) 134 #include "arm/dnn_arm.h" 135 #endif 136 137 #if defined(OPUS_X86_MAY_HAVE_SSE2) 138 #include "x86/dnn_x86.h" 139 #endif 140 141 #ifndef OVERRIDE_COMPUTE_LINEAR 142 #define compute_linear(linear, out, in, arch) ((void)(arch),compute_linear_c(linear, out, in)) 143 #endif 144 145 #ifndef OVERRIDE_COMPUTE_ACTIVATION 146 #define compute_activation(output, input, N, activation, arch) ((void)(arch),compute_activation_c(output, input, N, activation)) 147 #endif 148 149 #ifndef OVERRIDE_COMPUTE_CONV2D 150 #define compute_conv2d(conv, out, mem, in, height, hstride, activation, arch) ((void)(arch),compute_conv2d_c(conv, out, mem, in, height, hstride, activation)) 151 #endif 152 153 #if defined(__x86_64__) && !defined(OPUS_X86_MAY_HAVE_SSE4_1) && !defined(OPUS_X86_MAY_HAVE_AVX2) 154 #if defined(_MSC_VER) 155 #pragma message ("Only SSE and SSE2 are available. On newer machines, enable SSSE3/AVX/AVX2 to get better performance") 156 #else 157 #warning "Only SSE and SSE2 are available. On newer machines, enable SSSE3/AVX/AVX2 using -march= to get better performance" 158 #endif 159 #endif 160 161 162 163 #endif /* NNET_H_ */ 164