diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cfe3ea..9a7d623 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") endif("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") # Set default flags (from opus-ng build) -set(CMAKE_C_FLAGS "-O2 -fstack-protector-strong -W -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -DHAVE_CONFIG_H") +set(CMAKE_C_FLAGS "-g -O2 -fstack-protector-strong -W -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -DHAVE_CONFIG_H") if(NOT CMAKE_CROSSCOMPILING) # Python tells us the CFLAGS we need for Embedding Python in a C lib. @@ -524,7 +524,7 @@ add_test(NAME c_encoder_model19_check3 COMMAND sh -c "cd ${CMAKE_SOURCE_DIR}; ./inference.sh model05/checkpoints/checkpoint_epoch_100.pth wav/brian_g8sez.wav /dev/null --write_latent z.f32; \ cat features_in.f32 | python3 radae_txe.py | python3 radae_rxe.py > features_out.f32; \ - cat features_in.f32 | ${CMAKE_CURRENT_BINARY_DIR}/src/test_rade_enc 3 1 > z_c.f32; \ + cat features_in.f32 | ${CMAKE_CURRENT_BINARY_DIR}/src/test_rade_enc 3 1 ${CMAKE_CURRENT_BINARY_DIR}/model19_check3.bin > z_c.f32; \ cat z_c.f32 | python3 radae_txe.py --bypass_enc | python3 radae_rxe.py > features_rx_out.f32; \ python3 loss.py features_in.f32 features_out.f32 --features_hat2 features_rx_out.f32 --compare") set_tests_properties(c_encoder_model19_check3 PROPERTIES PASS_REGULAR_EXPRESSION "PASS") diff --git a/bin/model19_check3.bin b/bin/model19_check3.bin new file mode 100644 index 0000000..7eccda5 Binary files /dev/null and b/bin/model19_check3.bin differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d1bf376..dd70348 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,3 +31,6 @@ set_target_properties(radecore PROPERTIES add_executable(test_rade_enc test_rade_enc.c) target_link_libraries(test_rade_enc radecore opus m) + +add_executable(write_rade_weights write_rade_weights.c) +target_link_libraries(write_rade_weights radecore opus m) diff --git a/src/rade_enc_data.c b/src/rade_enc_data.c index c05a255..c7aa034 100644 --- a/src/rade_enc_data.c +++ b/src/rade_enc_data.c @@ -227860,7 +227860,7 @@ const WeightArray radeenc_arrays[] = { {NULL, 0, 0, NULL} }; #endif /* USE_WEIGHTS_FILE */ - +#include #ifndef DUMP_BINARY_WEIGHTS int init_radeenc(RADEEnc *model, const WeightArray *arrays) { if (linear_init(&model->enc_dense1, arrays, "enc_dense1_bias", NULL, NULL,"enc_dense1_weights_float", NULL, NULL, NULL, 84, 64)) return 1; diff --git a/src/test_rade_enc.c b/src/test_rade_enc.c index 876c47f..7e79232 100644 --- a/src/test_rade_enc.c +++ b/src/test_rade_enc.c @@ -3,6 +3,10 @@ #include #include #include +#include +#include +#include +#include #ifdef HAVE_CONFIG_H #include "config.h" @@ -18,8 +22,8 @@ int main(int argc, char *argv[]) RADEEnc enc_model; RADEEncState enc_state; - if (argc < 2) { - fprintf(stderr, "usage: %s bottleneck[1-3] auxdata[0-1]\n", argv[0]); + if (argc < 3) { + fprintf(stderr, "usage: %s bottleneck[1-3] auxdata[0-1] [weights_blob.bin]\n", argv[0]); exit(1); } @@ -34,8 +38,34 @@ int main(int argc, char *argv[]) num_features += 1; } - if (init_radeenc(&enc_model, radeenc_arrays) != 0) { - fprintf(stderr, "Error initialising encoder model\n"); + int fd; + void *data; + int len; + int nb_arrays; + struct stat st; + WeightArray *list; + + if (argc == 4) { + const char *filename = argv[3]; + fprintf(stderr, "loading %s ....\n", filename); + int ret = stat(filename, &st); + assert(ret != -1); + len = st.st_size; + fprintf(stderr, "size is %d\n", len); + fd = open(filename, O_RDONLY); + assert(fd != -1); + // note this needs to stay mapped at run time + data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); + nb_arrays = parse_weights(&list, data, len); + for (int i=0;i +#include +#include +#include +#include "nnet.h" +#include "os_support.h" +#include "arch.h" + +/* This is a bit of a hack because we need to build nnet_data.c and plc_data.c without USE_WEIGHTS_FILE, + but USE_WEIGHTS_FILE is defined in config.h. */ +#undef HAVE_CONFIG_H +#ifdef USE_WEIGHTS_FILE +#undef USE_WEIGHTS_FILE +#endif +#include "rade_enc_data.c" +#include "rade_dec_data.c" + +void write_weights(const WeightArray *list, FILE *fout) +{ + int i=0; + unsigned char zeros[WEIGHT_BLOCK_SIZE] = {0}; + while (list[i].name != NULL) { + WeightHead h; + if (strlen(list[i].name) >= sizeof(h.name) - 1) { + printf("[write_weights] warning: name %s too long\n", list[i].name); + } + memcpy(h.head, "DNNw", 4); + h.version = WEIGHT_BLOB_VERSION; + h.type = list[i].type; + h.size = list[i].size; + h.block_size = (h.size+WEIGHT_BLOCK_SIZE-1)/WEIGHT_BLOCK_SIZE*WEIGHT_BLOCK_SIZE; + OPUS_CLEAR(h.name, sizeof(h.name)); + strncpy(h.name, list[i].name, sizeof(h.name)); + h.name[sizeof(h.name)-1] = 0; + celt_assert(sizeof(h) == WEIGHT_BLOCK_SIZE); + fwrite(&h, 1, WEIGHT_BLOCK_SIZE, fout); + fwrite(list[i].data, 1, h.size, fout); + fwrite(zeros, 1, h.block_size-h.size, fout); + i++; + } +} + +int main(int argc,char *argv[]) +{ + if (argc != 2) { + fprintf(stderr, "usage: %s weights_blob.bin\n", argv[0]); + exit(1); + } + FILE *fout = fopen(argv[1], "w"); + assert(fout != NULL); + write_weights(radeenc_arrays, fout); + write_weights(radedec_arrays, fout); + fclose(fout); + return 0; +}