Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parse_lpcnet_weights.c « dnn - gitlab.xiph.org/xiph/opus.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c2108593323f79fa0784855c7919890b84e41e42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright (c) 2023 Amazon */
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>
#include "nnet.h"
#include "os_support.h"

#define SPARSE_BLOCK_SIZE 32

int parse_record(const unsigned char **data, int *len, WeightArray *array) {
  WeightHead *h = (WeightHead *)*data;
  if (*len < WEIGHT_BLOCK_SIZE) return -1;
  if (h->block_size < h->size) return -1;
  if (h->block_size > *len-WEIGHT_BLOCK_SIZE) return -1;
  if (h->name[sizeof(h->name)-1] != 0) return -1;
  if (h->size < 0) return -1;
  array->name = h->name;
  array->type = h->type;
  array->size = h->size;
  array->data = (*data)+WEIGHT_BLOCK_SIZE;

  *data += h->block_size+WEIGHT_BLOCK_SIZE;
  *len -= h->block_size+WEIGHT_BLOCK_SIZE;
  return array->size;
}

int parse_weights(WeightArray **list, const unsigned char *data, int len)
{
  int nb_arrays=0;
  int capacity=20;
  *list = opus_alloc(capacity*sizeof(WeightArray));
  while (len > 0) {
    int ret;
    WeightArray array = {NULL, 0, 0, 0};
    ret = parse_record(&data, &len, &array);
    if (ret > 0) {
      if (nb_arrays+1 >= capacity) {
        /* Make sure there's room for the ending NULL element too. */
        capacity = capacity*3/2;
        *list = opus_realloc(*list, capacity*sizeof(WeightArray));
      }
      (*list)[nb_arrays++] = array;
    } else {
      opus_free(*list);
      *list = NULL;
      return -1;
    }
  }
  (*list)[nb_arrays].name=NULL;
  return nb_arrays;
}

static const void *find_array_entry(const WeightArray *arrays, const char *name) {
  while (arrays->name && strcmp(arrays->name, name) != 0) arrays++;
  return arrays;
}

static const void *find_array_check(const WeightArray *arrays, const char *name, int size) {
  const WeightArray *a = find_array_entry(arrays, name);
  if (a->name && a->size == size) return a->data;
  else return NULL;
}

static const void *opt_array_check(const WeightArray *arrays, const char *name, int size, int *error) {
  const WeightArray *a = find_array_entry(arrays, name);
  *error = (a->name != NULL && a->size != size);
  if (a->name && a->size == size) return a->data;
  else return NULL;
}

static const void *find_idx_check(const WeightArray *arrays, const char *name, int nb_in, int nb_out, int *total_blocks) {
  int remain;
  const int *idx;
  const WeightArray *a = find_array_entry(arrays, name);
  *total_blocks = 0;
  if (a == NULL) return NULL;
  idx = a->data;
  remain = a->size/sizeof(int);
  while (remain > 0) {
    int nb_blocks;
    int i;
    nb_blocks = *idx++;
    if (remain < nb_blocks+1) return NULL;
    for (i=0;i<nb_blocks;i++) {
      int pos = *idx++;
      if (pos+3 >= nb_in || (pos&0x3)) return NULL;
    }
    nb_out -= 8;
    remain -= nb_blocks+1;
    *total_blocks += nb_blocks;
  }
  if (nb_out != 0) return NULL;
  return a->data;
}

int linear_init(LinearLayer *layer, const WeightArray *arrays,
  const char *bias,
  const char *subias,
  const char *weights,
  const char *float_weights,
  const char *weights_idx,
  const char *diag,
  const char *scale,
  int nb_inputs,
  int nb_outputs)
{
  int err;
  layer->bias = NULL;
  layer->subias = NULL;
  layer->weights = NULL;
  layer->float_weights = NULL;
  layer->weights_idx = NULL;
  layer->diag = NULL;
  layer->scale = NULL;
  if (bias != NULL) {
    if ((layer->bias = find_array_check(arrays, bias, nb_outputs*sizeof(layer->bias[0]))) == NULL) return 1;
  }
  if (subias != NULL) {
    if ((layer->subias = find_array_check(arrays, subias, nb_outputs*sizeof(layer->subias[0]))) == NULL) return 1;
  }
  if (weights_idx != NULL) {
    int total_blocks;
    if ((layer->weights_idx = find_idx_check(arrays, weights_idx, nb_inputs, nb_outputs, &total_blocks)) == NULL) return 1;
    if (weights != NULL) {
      if ((layer->weights = find_array_check(arrays, weights, SPARSE_BLOCK_SIZE*total_blocks*sizeof(layer->weights[0]))) == NULL) return 1;
    }
    if (float_weights != NULL) {
      layer->float_weights = opt_array_check(arrays, float_weights, SPARSE_BLOCK_SIZE*total_blocks*sizeof(layer->float_weights[0]), &err);
      if (err) return 1;
    }
  } else {
    if (weights != NULL) {
      if ((layer->weights = find_array_check(arrays, weights, nb_inputs*nb_outputs*sizeof(layer->weights[0]))) == NULL) return 1;
    }
    if (float_weights != NULL) {
      layer->float_weights = opt_array_check(arrays, float_weights, nb_inputs*nb_outputs*sizeof(layer->float_weights[0]), &err);
      if (err) return 1;
    }
  }
  if (diag != NULL) {
    if ((layer->diag = find_array_check(arrays, diag, nb_outputs*sizeof(layer->diag[0]))) == NULL) return 1;
  }
  if (weights != NULL) {
    if ((layer->scale = find_array_check(arrays, scale, nb_outputs*sizeof(layer->scale[0]))) == NULL) return 1;
  }
  layer->nb_inputs = nb_inputs;
  layer->nb_outputs = nb_outputs;
  return 0;
}


int dense_init(DenseLayer *layer, const WeightArray *arrays,
  const char *bias,
  const char *input_weights,
  int nb_inputs,
  int nb_neurons,
  int activation)
{
  if ((layer->bias = find_array_check(arrays, bias, nb_neurons*sizeof(layer->bias[0]))) == NULL) return 1;
  if ((layer->input_weights = find_array_check(arrays, input_weights, nb_inputs*nb_neurons*sizeof(layer->input_weights[0]))) == NULL) return 1;
  layer->nb_inputs = nb_inputs;
  layer->nb_neurons = nb_neurons;
  layer->activation = activation;
  return 0;
}

int gru_init(GRULayer *layer, const WeightArray *arrays,
  const char *bias,
  const char *subias,
  const char *input_weights,
  const char *input_weights_idx,
  const char *recurrent_weights,
  int nb_inputs,
  int nb_neurons,
  int activation,
  int reset_after)
{
  int total_blocks;
  if ((layer->bias = find_array_check(arrays, bias, 6*nb_neurons*sizeof(layer->bias[0]))) == NULL) return 1;
  if ((layer->subias = find_array_check(arrays, subias, 6*nb_neurons*sizeof(layer->subias[0]))) == NULL) return 1;
  if ((layer->input_weights_idx = find_idx_check(arrays, input_weights_idx, nb_inputs, 3*nb_neurons, &total_blocks)) == NULL) return 1;
  if ((layer->input_weights = find_array_check(arrays, input_weights, SPARSE_BLOCK_SIZE*total_blocks*sizeof(layer->input_weights[0]))) == NULL) return 1;
  if ((layer->recurrent_weights = find_array_check(arrays, recurrent_weights, 3*nb_neurons*nb_neurons*sizeof(layer->recurrent_weights[0]))) == NULL) return 1;
  layer->nb_inputs = nb_inputs;
  layer->nb_neurons = nb_neurons;
  layer->activation = activation;
  layer->reset_after = reset_after;
  return 0;
}

int conv2d_init(Conv2dLayer *layer, const WeightArray *arrays,
  const char *bias,
  const char *float_weights,
  int in_channels,
  int out_channels,
  int ktime,
  int kheight)
{
  int err;
  layer->bias = NULL;
  layer->float_weights = NULL;
  if (bias != NULL) {
    if ((layer->bias = find_array_check(arrays, bias, out_channels*sizeof(layer->bias[0]))) == NULL) return 1;
  }
  if (float_weights != NULL) {
    layer->float_weights = opt_array_check(arrays, float_weights, in_channels*out_channels*ktime*kheight*sizeof(layer->float_weights[0]), &err);
    if (err) return 1;
  }
  layer->in_channels = in_channels;
  layer->out_channels = out_channels;
  layer->ktime = ktime;
  layer->kheight = kheight;
  return 0;
}



#if 0
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>

int main()
{
  int fd;
  unsigned char *data;
  int len;
  int nb_arrays;
  int i;
  WeightArray *list;
  struct stat st;
  const char *filename = "weights_blob.bin";
  stat(filename, &st);
  len = st.st_size;
  fd = open(filename, O_RDONLY);
  data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
  printf("size is %d\n", len);
  nb_arrays = parse_weights(&list, data, len);
  for (i=0;i<nb_arrays;i++) {
    printf("found %s: size %d\n", list[i].name, list[i].size);
  }
  printf("%p\n", list[i].name);
  opus_free(list);
  munmap(data, len);
  close(fd);
  return 0;
}
#endif