Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The right way to compile as DLL / Invoke from other languages #182

Open
LSXAxeller opened this issue Jun 11, 2024 · 2 comments
Open

The right way to compile as DLL / Invoke from other languages #182

LSXAxeller opened this issue Jun 11, 2024 · 2 comments

Comments

@LSXAxeller
Copy link

I want to build the library as .dll instead .lib to invoke it from C# using P/Invoke but I can't build it properly.

When I set the library to SHARED instead STATIC in CMakeFileList.txt it produced a 11KB bark.dll which doesn't have any entry points.

when I tried to open bark.sln and set projects properties Configuration Type to Dynamic Library (.dll) and set Common Language Runtime Support to .NET Runtime Support (/clr:netcore) and set Target Framework to .NET 8.0 and compile I get 45KB bark.dll in release.

after I move bark.dll & encodec.lib & ggml.dll to my C# console app folder and do P/Invoke using DllImport and call a method like bark_context_default_params using

[DllImport(NativeLibraryManager.LibraryName, EntryPoint = "bark_context_default_params", CallingConvention = CallingConvention.Cdecl)]
    public static extern ContextParams DefaultParams();

I get error System.EntryPointNotFoundException: Unable to find an entry point named 'bark_context_default_params' in DLL.

@PABannier
Copy link
Owner

Hello @LSXAxeller ! Thanks for reaching out and raising this issue!

I have not exported the functions using DLL export, like this:

#ifdef EXPORTING_BARK
#define BARK_API __declspec(dllexport)
#else
#define BARK_API __declspec(dllimport)
#endif

extern "C" {
    BARK_API ContextParams bark_context_default_params();
}

Could you open a PR to export the functions you'd like to use? You only need to do it for the Bark library.

@LSXAxeller
Copy link
Author

Thanks for your answer @PABannier. I don't code in C++, so I'm not sure if I'm on the right track. I haven't opened a PR because I wanted to check with you first to make sure my edits wouldn't cause any harm. Is that the right approach?

I assume that the file I should edit is the Header.

/*
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2024 Pierre-Antoine Bannier                                        │
│                                                                              │
│ Permission to use, copy, modify, and/or distribute this software for         │
│ any purpose with or without fee is hereby granted, provided that the         │
│ above copyright notice and this permission notice appear in all copies.      │
│                                                                              │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             │
│ PERFORMANCE OF THIS SOFTWARE.                                                │
╚─────────────────────────────────────────────────────────────────────────────*/
#pragma once

#include "encodec.h"
#include "ggml-backend.h"
#include "ggml.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef EXPORTING_BARK
#define BARK_API __declspec(dllexport)
#else
#define BARK_API __declspec(dllimport)
#endif

    enum bark_verbosity_level {
        LOW    = 0,
        MEDIUM = 1,
        HIGH   = 2,
    };

    enum bark_encoding_step {
        SEMANTIC = 0,
        COARSE   = 1,
        FINE     = 2,
    };

    struct bark_context;
    struct bark_model;

    // Holds the vocabulary for the semantic encoder
    struct bark_vocab;

    // Define the GPT architecture for the 3 encoders
    struct gpt_model;

    typedef void (*bark_progress_callback)(struct bark_context * bctx, enum bark_encoding_step step, int progress, void * user_data);

    struct bark_statistics {
        // Time to load model weights
        int64_t t_load_us;
        // Time to generate audio
        int64_t t_eval_us;

        // Time to generate semantic tokens
        int64_t t_semantic_us;
        // Time to generate coarse tokens
        int64_t t_coarse_us;
        // Time to generate fine tokens
        int64_t t_fine_us;

        // Number of semantic tokens sampled
        int32_t n_sample_semantic;
        // Number of coarse tokens sampled
        int32_t n_sample_coarse;
        // Number of fine tokens sampled
        int32_t n_sample_fine;
    };

    struct bark_context_params {
        // Verbosity level
        enum bark_verbosity_level verbosity;

        // Temperature for sampling (text and coarse encoders)
        float temp;
        // Temperature for sampling (fine encoder)
        float fine_temp;

        // Minimum probability for EOS token (text encoder)
        float min_eos_p;
        // Sliding window size for coarse encoder
        int32_t sliding_window_size;
        // Max history for coarse encoder
        int32_t max_coarse_history;

        // Sample rate
        int32_t sample_rate;
        // Target bandwidth
        int32_t target_bandwidth;

        // CLS token ID
        int32_t cls_token_id;
        // SEP token ID
        int32_t sep_token_id;

        // Maximum number of semantic tokens to generate
        int32_t n_steps_text_encoder;

        // Text PAD token ID
        int32_t text_pad_token;
        // Text encoding offset
        int32_t text_encoding_offset;

        // Semantic frequency rate
        float semantic_rate_hz;
        // Semantic PAD token ID
        int32_t semantic_pad_token;
        // Vocabulary size in semantic encoder
        int32_t semantic_vocab_size;
        // Semantic infernce token ID
        int32_t semantic_infer_token;

        // Coarse frequency rate
        float coarse_rate_hz;
        // Coarse infer token ID
        int32_t coarse_infer_token;
        // Coarse semantic pad token ID
        int32_t coarse_semantic_pad_token;

        // Number of codebooks in coarse encoder
        int32_t n_coarse_codebooks;
        // Number of codebooks in fine encoder
        int32_t n_fine_codebooks;
        // Dimension of the codes
        int32_t codebook_size;

        // called on each progress update
        bark_progress_callback progress_callback;
        void * progress_callback_user_data;
    };

    /**
     * @brief Returns the default parameters for a bark context.
     *
     * @return bark_context_params The default parameters for a bark context.
     */
    BARK_API struct bark_context_params bark_context_default_params(void);

    /**
     * Loads a Bark model from the specified file path with the given parameters.
     *
     * @param model_path The directory path of the bark model to load.
     * @param params     The parameters to use for the Bark model.
     * @param seed       The seed to use for random number generation.
     * @return A pointer to the loaded bark model context.
     */
    BARK_API struct bark_context *bark_load_model(
        const char *model_path,
        struct bark_context_params params,
        uint32_t seed);

    /**
     * Generates an audio file from the given text using the specified Bark context.
     *
     * @param bctx The Bark context to use for generating the audio.
     * @param text The text to generate audio from.
     * @param n_threads The number of threads to use for generating the audio.
     * @return An integer indicating the success of the audio generation process.
     */
    BARK_API bool bark_generate_audio(
        struct bark_context *bctx,
        const char *text,
        int n_threads);

    /**
     * Retrieves the audio data generated by the Bark context.
     *
     * @param bctx The Bark context to use for generating the audio.
     * @return A pointer to the audio data generated by the Bark context.
     */
    BARK_API float *bark_get_audio_data(
        struct bark_context *bctx);

    /**
     * Retrieves the audio data generated by the Bark context.
     *
     * @param bctx The Bark context to use for generating the audio.
     * @return The size of the audio data generated by the Bark context.
     */
    BARK_API int bark_get_audio_data_size(
        struct bark_context *bctx);

    /**
     * Retrieves the load time of the last audio generation round.
     *
     * @param bctx The Bark context to use for generating the audio.
     * @return A struct containing the statistics of the last audio generation round.
     */
    BARK_API int64_t bark_get_load_time(
        struct bark_context *bctx);

    /**
     * Retrieves the evaluation time of the last audio generation round.
     *
     * @param bctx The Bark context to use for generating the audio.
     * @return A struct containing the statistics of the last audio generation round.
     */
    BARK_API int64_t bark_get_eval_time(
        struct bark_context *bctx);

    /**
     * Reset the statistics of the last audio generation round.
     *
     * @param bctx The Bark context to use for generating the audio.
     * @return A struct containing the statistics of the last audio generation round.
     */
    BARK_API void bark_reset_statistics(
        struct bark_context *bctx);

    /**
     * Quantizes a bark model and saves the result to a file.
     *
     * @param fname_inp The name of the input file containing the BARK model.
     * @param fname_out The name of the output file to save the quantized model to.
     * @param ftype The type of the model's floating-point values.
     * @return True if the model was successfully quantized and saved, false otherwise.
     */
    BARK_API bool bark_model_quantize(
        const char *fname_inp,
        const char *fname_out,
        enum ggml_ftype ftype);

    /**
     * @brief Frees the memory allocated for a bark context.
     *
     * @param bctx The bark context to free.
     */
    BARK_API void bark_free(
        struct bark_context *bctx);

#ifdef __cplusplus
}
#endif

vinovo added a commit to vinovo/bark.cpp that referenced this issue Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants