|
| 1 | +/* Copyright (c) 2023 Amazon */ |
| 2 | +/* |
| 3 | + Redistribution and use in source and binary forms, with or without |
| 4 | + modification, are permitted provided that the following conditions |
| 5 | + are met: |
| 6 | +
|
| 7 | + - Redistributions of source code must retain the above copyright |
| 8 | + notice, this list of conditions and the following disclaimer. |
| 9 | +
|
| 10 | + - Redistributions in binary form must reproduce the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer in the |
| 12 | + documentation and/or other materials provided with the distribution. |
| 13 | +
|
| 14 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 | + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 18 | + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 22 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 23 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | +*/ |
| 26 | + |
| 27 | +#ifdef HAVE_CONFIG_H |
| 28 | +#include "config.h" |
| 29 | +#endif |
| 30 | + |
| 31 | +#include <stdio.h> |
| 32 | +#include <string.h> |
| 33 | +#include <stddef.h> |
| 34 | +#include "nnet.h" |
| 35 | +#include "arch.h" |
| 36 | +#include "nnet.h" |
| 37 | + |
| 38 | +/* This is a bit of a hack because we need to build nnet_data.c and plc_data.c without USE_WEIGHTS_FILE, |
| 39 | + but USE_WEIGHTS_FILE is defined in config.h. */ |
| 40 | +#undef HAVE_CONFIG_H |
| 41 | +#ifdef USE_WEIGHTS_FILE |
| 42 | +#undef USE_WEIGHTS_FILE |
| 43 | +#endif |
| 44 | +#include "rnnoise_data.c" |
| 45 | + |
| 46 | +void write_weights(const WeightArray *list, FILE *fout) |
| 47 | +{ |
| 48 | + int i=0; |
| 49 | + unsigned char zeros[WEIGHT_BLOCK_SIZE] = {0}; |
| 50 | + while (list[i].name != NULL) { |
| 51 | + WeightHead h; |
| 52 | + if (strlen(list[i].name) >= sizeof(h.name) - 1) { |
| 53 | + printf("[write_weights] warning: name %s too long\n", list[i].name); |
| 54 | + } |
| 55 | + memcpy(h.head, "DNNw", 4); |
| 56 | + h.version = WEIGHT_BLOB_VERSION; |
| 57 | + h.type = list[i].type; |
| 58 | + h.size = list[i].size; |
| 59 | + h.block_size = (h.size+WEIGHT_BLOCK_SIZE-1)/WEIGHT_BLOCK_SIZE*WEIGHT_BLOCK_SIZE; |
| 60 | + RNN_CLEAR(h.name, sizeof(h.name)); |
| 61 | + strncpy(h.name, list[i].name, sizeof(h.name)); |
| 62 | + h.name[sizeof(h.name)-1] = 0; |
| 63 | + celt_assert(sizeof(h) == WEIGHT_BLOCK_SIZE); |
| 64 | + fwrite(&h, 1, WEIGHT_BLOCK_SIZE, fout); |
| 65 | + fwrite(list[i].data, 1, h.size, fout); |
| 66 | + fwrite(zeros, 1, h.block_size-h.size, fout); |
| 67 | + i++; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +int main(void) |
| 72 | +{ |
| 73 | + FILE *fout = fopen("weights_blob.bin", "w"); |
| 74 | + write_weights(rnnoise_arrays, fout); |
| 75 | + fclose(fout); |
| 76 | + return 0; |
| 77 | +} |
0 commit comments