Skip to content

Commit

Permalink
manual mods post export_weights to accomodate different input_dim for…
Browse files Browse the repository at this point in the history
… model05 & model19_check3, both ctests work, updated opus nnet.h patch
  • Loading branch information
drowe67 committed Nov 13, 2024
1 parent 5d7247f commit 55dd56c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 21 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,12 @@ add_test(NAME bbfm_sc_bpf_loss

# C Port of Core Encoder/decoder

# we test by comparing loss of features_in/features_out, can all happen at rate Rs
# we test by comparing loss of features_in/features_out, can all happen at rate Rs. We load model05.bin weights as
# compiled-in weights are for model19_check3
add_test(NAME c_encoder_model5
COMMAND sh -c "cd ${CMAKE_SOURCE_DIR};
./inference.sh model05/checkpoints/checkpoint_epoch_100.pth wav/brian_g8sez.wav /dev/null; \
cat features_in.f32 | ${CMAKE_CURRENT_BINARY_DIR}/src/test_rade_enc 1 0 > z_c.f32; \
cat features_in.f32 | ${CMAKE_CURRENT_BINARY_DIR}/src/test_rade_enc 1 0 ${CMAKE_SOURCE_DIR}/bin/model05.bin > z_c.f32; \
python3 stateful_encoder.py model05/checkpoints/checkpoint_epoch_100.pth features_in.f32 /dev/null --read_latent z_c.f32 --loss_test 0.2")
set_tests_properties(c_encoder_model5 PROPERTIES PASS_REGULAR_EXPRESSION "PASS")

Expand All @@ -524,7 +525,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 ${CMAKE_CURRENT_BINARY_DIR}/model19_check3.bin > z_c.f32; \
cat features_in.f32 | ${CMAKE_CURRENT_BINARY_DIR}/src/test_rade_enc 3 1 > 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")
Binary file added bin/model05.bin
Binary file not shown.
6 changes: 3 additions & 3 deletions cmake/BuildOpus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if(APPLE AND BUILD_OSX_UNIVERSAL)
ExternalProject_Add(build_opus_x86
DOWNLOAD_EXTRACT_TIMESTAMP NO
BUILD_IN_SOURCE 1
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-export.diff"
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-nnet.h.diff"
CONFIGURE_COMMAND ${CONFIGURE_COMMAND} --host=x86_64-apple-darwin --target=x86_64-apple-darwin CFLAGS=-arch\ x86_64\ -O2\ -mmacosx-version-min=10.11
BUILD_COMMAND $(MAKE)
INSTALL_COMMAND ""
Expand All @@ -24,7 +24,7 @@ ExternalProject_Add(build_opus_x86
ExternalProject_Add(build_opus_arm
DOWNLOAD_EXTRACT_TIMESTAMP NO
BUILD_IN_SOURCE 1
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-export.diff"
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-nnet.h.diff"
CONFIGURE_COMMAND ${CONFIGURE_COMMAND} --host=aarch64-apple-darwin --target=aarch64-apple-darwin CFLAGS=-arch\ arm64\ -O2\ -mmacosx-version-min=10.11
BUILD_COMMAND $(MAKE)
INSTALL_COMMAND ""
Expand Down Expand Up @@ -57,7 +57,7 @@ set_target_properties(opus PROPERTIES
else(APPLE AND BUILD_OSX_UNIVERSAL)
ExternalProject_Add(build_opus
BUILD_IN_SOURCE 1
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-export.diff"
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-nnet.h.diff"
CONFIGURE_COMMAND ${CONFIGURE_COMMAND}
BUILD_COMMAND $(MAKE)
INSTALL_COMMAND ""
Expand Down
2 changes: 1 addition & 1 deletion export_rade_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def c_export(args, model):
# constants
constants_writer.header.write(
f"""
#define RADE_NUM_FEATURES {model.feature_dim}
#define RADE_FRAMES_PER_STEP {model.enc_stride}
#define RADE_LATENT_DIM {args.latent_dim}
Expand Down
4 changes: 4 additions & 0 deletions src/opus-export.diff → src/opus-nnet.h.diff
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
> void RADE_EXPORT compute_generic_conv1d_dilation(const LinearLayer *layer, float *output, float *mem, const float *input, int input_size, int dilation, int activation, int arch);
> void RADE_EXPORT compute_glu(const LinearLayer *layer, float *output, const float *input, int arch);
> void RADE_EXPORT compute_gated_activation(const LinearLayer *layer, float *output, const float *input, int activation, int arch);
96c98
< int parse_weights(WeightArray **list, const void *data, int len);
---
> int RADE_EXPORT parse_weights(WeightArray **list, const void *data, int len);
108c110
< int linear_init(LinearLayer *layer, const WeightArray *arrays,
---
Expand Down
2 changes: 1 addition & 1 deletion src/rade_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "nnet.h"


#define RADE_NUM_FEATURES 21
#define RADE_FRAMES_PER_STEP 4

#define RADE_LATENT_DIM 80

Expand Down
6 changes: 3 additions & 3 deletions src/rade_enc_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -227860,10 +227860,10 @@ const WeightArray radeenc_arrays[] = {
{NULL, 0, 0, NULL}
};
#endif /* USE_WEIGHTS_FILE */
#include <stdio.h>

#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;
int init_radeenc(RADEEnc *model, const WeightArray *arrays, int input_dim) {
if (linear_init(&model->enc_dense1, arrays, "enc_dense1_bias", NULL, NULL,"enc_dense1_weights_float", NULL, NULL, NULL, input_dim, 64)) return 1;
if (linear_init(&model->enc_zdense, arrays, "enc_zdense_bias", NULL, NULL,"enc_zdense_weights_float", NULL, NULL, NULL, 864, 80)) return 1;
if (linear_init(&model->enc_gru1_input, arrays, "enc_gru1_input_bias", "enc_gru1_input_subias", "enc_gru1_input_weights_int8","enc_gru1_input_weights_float", "enc_gru1_input_weights_idx", NULL, "enc_gru1_input_scale", 64, 192)) return 1;
if (linear_init(&model->enc_gru1_recurrent, arrays, "enc_gru1_recurrent_bias", "enc_gru1_recurrent_subias", "enc_gru1_recurrent_weights_int8","enc_gru1_recurrent_weights_float", NULL, NULL, "enc_gru1_recurrent_scale", 64, 192)) return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/rade_enc_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ struct RADEEnc {
LinearLayer enc_conv5;
};

int init_radeenc(RADEEnc *model, const WeightArray *arrays);
int init_radeenc(RADEEnc *model, const WeightArray *arrays, int input_dim);

#endif /* RADE_ENC_DATA_H */
16 changes: 8 additions & 8 deletions src/test_rade_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ int main(int argc, char *argv[])
int nb_total_features = 36;
int num_features = 20;
int num_used_features = 20;
int frames_per_step = 4; // TODO dig this out of network somehow

int frames_per_step = RADE_FRAMES_PER_STEP;
if (auxdata) {
num_features += 1;
}
int input_dim = num_features*frames_per_step;

int fd;
void *data;
Expand All @@ -60,21 +61,20 @@ int main(int argc, char *argv[])
for (int i=0;i<nb_arrays;i++) {
fprintf(stderr, "found %s: size %d\n", list[i].name, list[i].size);
}
if (init_radeenc(&enc_model, list) != 0) {
if (init_radeenc(&enc_model, list, input_dim) != 0) {
fprintf(stderr, "Error initialising encoder model from %s\n", argv[3]);
exit(1);
}
} else if (init_radeenc(&enc_model, radeenc_arrays) != 0) {
} else if (init_radeenc(&enc_model, radeenc_arrays, input_dim) != 0) {
fprintf(stderr, "Error initialising built-in encoder model\n");
exit(1);
}
rade_init_encoder(&enc_state);

int n_features_in = enc_model.enc_dense1.nb_inputs;
assert(enc_model.enc_zdense.nb_outputs == RADE_LATENT_DIM);

float features_read[frames_per_step*nb_total_features];
float features[n_features_in];
float features[input_dim];
float z[RADE_LATENT_DIM];

// From celt/cpu_support.h:
Expand All @@ -90,8 +90,8 @@ int main(int argc, char *argv[])
// This auto-magically selects best arch
// arch = opus_select_arch();

fprintf(stderr, "arch: %d bottleneck: %d auxdata: %d n_features_in: %d n_z_out: %d\n",
arch, bottleneck, auxdata, n_features_in, enc_model.enc_zdense.nb_outputs);
fprintf(stderr, "arch: %d bottleneck: %d auxdata: %d input_dim: %d n_z_out: %d\n",
arch, bottleneck, auxdata, input_dim, enc_model.enc_zdense.nb_outputs);
int nb_feature_vecs = 0;
size_t to_read, nb_read;
to_read = frames_per_step*nb_total_features;
Expand Down
2 changes: 1 addition & 1 deletion weight-exchange/wexchange/c_export/c_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self,
model_struct_name="Model",
nnet_header="nnet.h"):
"""
Writer class for creating souce and header files for weight exports to C
Writer class for creating source and header files for weight exports to C
Parameters:
-----------
Expand Down

0 comments on commit 55dd56c

Please sign in to comment.