Skip to content

Commit

Permalink
wip C port of core encoder, test_rade_enc.c builds (not tested), atte…
Browse files Browse the repository at this point in the history
…mpt at fixing Issue #7, patch to export libopus selected nnet.h funcs
  • Loading branch information
drowe67 committed Nov 9, 2024
1 parent 5482f85 commit 3b060b7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 -fvisibility=hidden -fstack-protector-strong -W -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes")
set(CMAKE_C_FLAGS "-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.
Expand Down
5 changes: 4 additions & 1 deletion cmake/BuildOpus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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"
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 @@ -23,6 +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"
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 @@ -56,6 +58,7 @@ else(APPLE AND BUILD_OSX_UNIVERSAL)
ExternalProject_Add(build_opus
DOWNLOAD_EXTRACT_TIMESTAMP NO
BUILD_IN_SOURCE 1
PATCH_COMMAND sh -c "patch dnn/nnet.h < ${CMAKE_SOURCE_DIR}/src/opus-export.diff"
CONFIGURE_COMMAND ${CONFIGURE_COMMAND}
BUILD_COMMAND $(MAKE)
INSTALL_COMMAND ""
Expand All @@ -72,5 +75,5 @@ set_target_properties(opus PROPERTIES
IMPORTED_IMPLIB "${BINARY_DIR}/.libs/libopus${CMAKE_STATIC_LIBRARY_SUFFIX}"
)

include_directories(${SOURCE_DIR}/dnn ${SOURCE_DIR}/celt ${SOURCE_DIR}/include)
include_directories(${SOURCE_DIR}/dnn ${SOURCE_DIR}/celt ${SOURCE_DIR}/include ${SOURCE_DIR})
endif(APPLE AND BUILD_OSX_UNIVERSAL)
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ set_target_properties(radecore PROPERTIES
SOVERSION "0.1"
PUBLIC_HEADER "rade_api.h"
)

add_executable(test_rade_enc test_rade_enc.c)
target_link_libraries(test_rade_enc radecore opus m)
25 changes: 25 additions & 0 deletions src/opus-export.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
30a31,32
> #define RADE_EXPORT __attribute__((visibility("default")))
>
88,93c90,95
< void compute_generic_dense(const LinearLayer *layer, float *output, const float *input, int activation, int arch);
< void compute_generic_gru(const LinearLayer *input_weights, const LinearLayer *recurrent_weights, float *state, const float *in, int arch);
< void compute_generic_conv1d(const LinearLayer *layer, float *output, float *mem, const float *input, int input_size, int activation, int arch);
< void compute_generic_conv1d_dilation(const LinearLayer *layer, float *output, float *mem, const float *input, int input_size, int dilation, int activation, int arch);
< void compute_glu(const LinearLayer *layer, float *output, const float *input, int arch);
< void compute_gated_activation(const LinearLayer *layer, float *output, const float *input, int activation, int arch);
---
> void RADE_EXPORT compute_generic_dense(const LinearLayer *layer, float *output, const float *input, int activation, int arch);
> void RADE_EXPORT compute_generic_gru(const LinearLayer *input_weights, const LinearLayer *recurrent_weights, float *state, const float *in, int arch);
> void RADE_EXPORT compute_generic_conv1d(const LinearLayer *layer, float *output, float *mem, const float *input, int input_size, int activation, int arch);
> 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);
108c110
< int linear_init(LinearLayer *layer, const WeightArray *arrays,
---
> int RADE_EXPORT linear_init(LinearLayer *layer, const WeightArray *arrays,
119c121
< int conv2d_init(Conv2dLayer *layer, const WeightArray *arrays,
---
> int RADE_EXPORT conv2d_init(Conv2dLayer *layer, const WeightArray *arrays,
5 changes: 3 additions & 2 deletions src/rade_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
- 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.
documentatio
n 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
Expand All @@ -32,7 +33,7 @@

#include "opus_types.h"

typedef struct RADEDec RADEDec;
typedef struct RADEDec RADEDec;
typedef struct RADEEnc RADEEnc;
typedef struct RADEDecStruct RADEDecState;
typedef struct RADEEncStruct RADEEncState;
Expand Down
1 change: 1 addition & 0 deletions src/rade_enc_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,6 @@ struct RADEEnc {
};

int init_radeenc(RADEEnc *model, const WeightArray *arrays);
extern const WeightArray radeenc_arrays[];

#endif /* RADE_ENC_DATA_H */
39 changes: 39 additions & 0 deletions src/test_rade_enc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "rade_core.h"
#include "rade_enc.h"
#include "rade_enc_data.h"

int opus_select_arch(void);

int main(void)
{
RADEEnc enc_model;
RADEEncState enc_state;

if (init_radeenc(&enc_model, rdovaeenc_arrays) != 0) {
fprintf(stderr, "Error initialising 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);
fprintf(stderr, "n_features_in: %d n_z_out: %d", n_features_in, enc_model.enc_zdense.nb_outputs);
float features[n_features_in];
float z[RADE_LATENT_DIM];
int arch = opus_select_arch();

while((size_t)n_features_in == fread(features, sizeof(float), n_features_in, stdin)) {
rade_core_encoder(&enc_state, &enc_model, z, features, arch);
fwrite(z, sizeof(float), RADE_LATENT_DIM, stdout);
fflush(stdout);
}

return 0;
}

0 comments on commit 3b060b7

Please sign in to comment.