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

sync: use encodec's latest version as a submodule #124

Merged
merged 69 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
2302881
rm encodec
PABannier Oct 26, 2023
450a606
add git submodules
PABannier Oct 26, 2023
4ece5fa
removed bark util
PABannier Oct 26, 2023
9c9f7e8
updated CMakeLists
PABannier Oct 26, 2023
e2b350a
rm build scripts
PABannier Oct 26, 2023
d491cc7
mv dr_wav in examples
PABannier Oct 26, 2023
38c2e49
common cpp
PABannier Oct 26, 2023
6b32b3b
moved def constants
PABannier Oct 26, 2023
ab9b528
text encoder loaded with the latest ggml API
PABannier Oct 27, 2023
6a9b50a
pulled ggml upstream
PABannier Oct 27, 2023
efbdd56
temporarily removed subdirectory encodec.cpp
PABannier Oct 27, 2023
753d5cf
clean forward pass text encoder
PABannier Oct 27, 2023
e4e712f
compiling
PABannier Oct 27, 2023
d8fc378
fix issue definition
PABannier Oct 27, 2023
b258c08
clean
PABannier Oct 28, 2023
6642e75
remove codec parsing functions
PABannier Oct 28, 2023
33d186e
kinda works
PABannier Oct 28, 2023
83a21ec
bias is stored in hparams
PABannier Oct 28, 2023
6cad888
working text encoder
PABannier Oct 28, 2023
242e7c5
cln tests
PABannier Oct 28, 2023
c1d0edd
coarse working?
PABannier Oct 28, 2023
94cd5e2
override bias
PABannier Oct 29, 2023
acf9dfa
working fine encoder
PABannier Oct 29, 2023
c1def75
rename quantize.cpp into main.cpp
PABannier Oct 29, 2023
cfaa59c
included quantize as a target
PABannier Oct 29, 2023
05ef89d
exposed quantization function
PABannier Oct 29, 2023
6172381
minor
PABannier Oct 29, 2023
6d0db93
update CIs
PABannier Oct 29, 2023
a978908
updated CIs
PABannier Oct 29, 2023
8ae7dc5
passing tokenizer test
PABannier Oct 29, 2023
7ad8cd5
Merge branch 'main' of https://github.com/PABannier/bark.cpp into enc…
PABannier Oct 29, 2023
7c2ae84
fast text encoder
PABannier Dec 11, 2023
d3971c2
Merge branch 'main' of https://github.com/PABannier/bark.cpp into enc…
PABannier Dec 11, 2023
5874a87
`bark.cpp` -> `bark`
PABannier Dec 11, 2023
5312577
server abides by latest API
PABannier Dec 12, 2023
e7b7d75
rm fast-text-encoder example
PABannier Dec 12, 2023
2aaf7b2
pass `-O3` release flag
PABannier Dec 12, 2023
79ed551
rm fast_text_encoder from CMakeLists
PABannier Dec 12, 2023
4f72d56
restructured
PABannier Dec 12, 2023
f13498a
CMakeLists arranged
PABannier Dec 13, 2023
f517570
update CIs
PABannier Dec 13, 2023
b8bdd76
add encodec.cpp in the loop
PABannier Dec 14, 2023
5319d26
add verbosity level
PABannier Dec 15, 2023
11c3f9a
Fix CIs (#128)
AlexHayton Dec 30, 2023
da3cc56
Merge branch 'encodec_as_submodule' of https://github.com/PABannier/b…
PABannier Jan 2, 2024
07a322c
fix coarse encoder internal pass
PABannier Jan 3, 2024
3002698
`VerbosityLevel` -> `bark_verbosity_level`
PABannier Jan 5, 2024
747345c
updated examples
PABannier Jan 5, 2024
a3e3e92
populated time per token
PABannier Jan 5, 2024
19e1683
remove whitespace
PABannier Jan 5, 2024
fa6975c
BarkProgressBar implemented
PABannier Jan 6, 2024
b9e2109
verbosity level controlled for cleaner output
PABannier Jan 6, 2024
4401975
removed params as macros and moved them into default constructor
PABannier Jan 6, 2024
38846ec
updated README
PABannier Jan 6, 2024
59d5352
removed useless `n_predict` in params
PABannier Jan 6, 2024
07e92de
removed old tests
PABannier Jan 6, 2024
ec677fb
fix wrong return type, quantization works again
PABannier Jan 6, 2024
035ef16
Added Metal and CUDA backend
PABannier Jan 6, 2024
6e4ac9a
updated docs
PABannier Jan 6, 2024
ac327a9
cosmit
PABannier Jan 6, 2024
d347134
rm submodule
PABannier Jan 7, 2024
d7e9661
added encodec submodule
PABannier Jan 7, 2024
1fbe29d
remove mem_per_token
PABannier Jan 7, 2024
b3d9179
more verbose errors
PABannier Jan 7, 2024
94fea82
clean
PABannier Jan 7, 2024
bec8547
reset allocr to reduce memory footprint
PABannier Jan 7, 2024
df7c22a
add tests
PABannier Jan 7, 2024
6fbc184
expose forward passes
PABannier Jan 7, 2024
87a102b
enhanced README.md
PABannier Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
passing tokenizer test
  • Loading branch information
PABannier committed Oct 29, 2023
commit 8ae7dc5a79a3c1e31ccda3ab557f9844c5bc74ee
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ if (BARK_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

# if (BARK_BUILD_TESTS)
# include(CTest)
# add_subdirectory(tests)
# endif ()
if (BARK_BUILD_TESTS)
include(CTest)
add_subdirectory(tests)
endif ()

target_link_libraries(${BARK_LIB} PUBLIC ggml)
# target_link_libraries(${BARK_LIB} PUBLIC encodec)
Expand Down
6 changes: 3 additions & 3 deletions bark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static bark_token gpt_sample(
return res;
}

static bool bark_vocab_load(
bool bark_vocab_load(
const std::string & fname,
bark_vocab * vocab,
int32_t expected_size) {
Expand Down Expand Up @@ -308,7 +308,7 @@ static std::string strip_accents(const std::string & in_str) {
return out_str;
}

static void bert_tokenize(
void bert_tokenize(
const bark_vocab * vocab,
const char * text,
int32_t * tokens,
Expand Down Expand Up @@ -1808,7 +1808,7 @@ static bool bark_eval_fine_encoder(struct bark_context * bctx, int n_threads) {
return true;
}

static bool bark_forward_text_encoder(struct bark_context * bctx, int n_threads) {
bool bark_forward_text_encoder(struct bark_context * bctx, int n_threads) {
const int64_t t_main_start_us = ggml_time_us();

auto & model = bctx->model.text_model;
Expand Down
41 changes: 41 additions & 0 deletions bark.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,44 @@ BARK_API bool bark_model_quantize(
*/
BARK_API void bark_free(
struct bark_context * bctx);

/**
* Loads a vocabulary from a file.
*
* @param fname The name of the file to load the vocabulary from.
* @param vocab A pointer to the bark_vocab struct to store the loaded vocabulary in.
* @param expected_size The expected size of the vocabulary.
* @return true if the vocabulary was loaded successfully, false otherwise.
*/
bool bark_vocab_load(
const std::string & fname,
bark_vocab * vocab,
int32_t expected_size);

/**
* Tokenizes the input text using the provided vocabulary.
*
* @param vocab Pointer to the vocabulary to use for tokenization.
* @param text The input text to tokenize.
* @param tokens Pointer to an array where the resulting tokens will be stored.
* @param n_tokens Pointer to an integer where the number of resulting tokens will be stored.
* @param n_max_tokens The maximum number of tokens that can be stored in the tokens array.
*/
void bert_tokenize(
const bark_vocab * vocab,
const char * text,
int32_t * tokens,
int32_t * n_tokens,
int32_t n_max_tokens);


/**
* Encodes the input text using the forward algorithm.
*
* @param bctx A pointer to the bark context struct.
* @param n_threads The number of threads to use for encoding.
* @return Returns true if the encoding was successful, false otherwise.
*/
bool bark_forward_text_encoder(
struct bark_context * bctx,
int n_threads);
37 changes: 24 additions & 13 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
function(bark_add_test source)
get_filename_component(TEST_TARGET ${source} NAME_WE)
add_executable(${TEST_TARGET} ${source})
install(TARGETS ${TEST_TARGET} RUNTIME)
target_link_libraries(${TEST_TARGET} PRIVATE bark ${CMAKE_THREAD_LIBS_INIT})
target_compile_features(${TEST_TARGET} PRIVATE cxx_std_11)
add_test(NAME ${TEST_TARGET} COMMAND $<TARGET_FILE:${TEST_TARGET}> ${ARGN})
endfunction()

bark_add_test(test-tokenizer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../ggml_weights/ggml_vocab.bin)
# bark_add_test(test-forward-semantic.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../ggml_weights/ggml_weights_text.bin)
bark_add_test(test-forward-coarse.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../ggml_weights/ggml_weights_text.bin)
# bark_add_test(test-forward-fine.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../ggml_weights/ggml_weights_fine.bin)
add_library(test_utils STATIC common.cpp)
target_include_directories(test_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(test_utils PRIVATE cxx_std_11)

#
# test-tokenizer

set(TEST_TARGET test-tokenizer)
add_executable(${TEST_TARGET} ${TEST_TARGET}.cpp)
target_link_libraries(${TEST_TARGET} PRIVATE bark)

#
# test-gpt-eval

# set(TEST_TARGET test-gpt-eval)
# add_executable(${TEST_TARGET} ${TEST_TARGET}.cpp)
# target_link_libraries(${TEST_TARGET} PRIVATE bark)

#
# test-forward-semantic

set(TEST_TARGET test-forward-semantic)
add_executable(${TEST_TARGET} ${TEST_TARGET}.cpp)
target_link_libraries(${TEST_TARGET} PRIVATE bark test_utils)
11 changes: 7 additions & 4 deletions tests/common.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <cmath>
#include <fstream>
#include <vector>
#include <tuple>
#include <vector>

#include "bark-util.h"
#include "common.h"

#define BARK_API_INTERNAL

int64_t bytes_left(std::ifstream & f) {
// utils to check all bytes are read from stream
int64_t curr_pos = f.tellg();
Expand All @@ -16,6 +14,11 @@ int64_t bytes_left(std::ifstream & f) {
return bytes_left_to_read;
}

template<typename T>
static void read_safe(std::ifstream& fin, T& dest) {
fin.read((char*)& dest, sizeof(T));
}

template <typename T, typename U>
inline bool all_close(
std::vector<T> s1,
Expand Down
3 changes: 2 additions & 1 deletion tests/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "bark.h"

#include <tuple>
#include <vector>
Expand All @@ -9,6 +8,8 @@
typedef std::vector<float> logit_sequence;
typedef std::vector<std::vector<float>> logit_matrix;

typedef std::vector<std::vector<int32_t>> bark_codes;

/* Comparison utils */
template <typename T, typename U>
inline bool all_equal(std::vector<T> s1, std::vector<U> s2, int * n_violations);
Expand Down
46 changes: 23 additions & 23 deletions tests/test-forward-semantic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,54 @@
#include "bark.h"
#include "common.h"

static const std::vector<std::string> test_data = {
"./data/semantic/test_pass_semantic_1.bin", // prompt: Ceci est un texte en français pour tester le bon fonctionnement de bark.
"./data/semantic/test_pass_semantic_2.bin", // prompt: Sometimes the heart sees what is invisible to the eye
"./data/semantic/test_pass_semantic_3.bin", // prompt: El Arte de Vencer se Aprende en las Derrotas
const std::vector<std::string> test_data = {
"../tests/data/semantic/test_pass_semantic_1.bin", // prompt: Ceci est un texte en français pour tester le bon fonctionnement de bark.
"../tests/data/semantic/test_pass_semantic_2.bin", // prompt: Sometimes the heart sees what is invisible to the eye
"../tests/data/semantic/test_pass_semantic_3.bin", // prompt: El Arte de Vencer se Aprende en las Derrotas
};

static const int n_threads = 4;
static const float min_eos_p = 0.2;
static const float temp = 0.0f; // deterministic sampling
const int n_threads = 4;
const float min_eos_p = 0.2;
const float temp = 0.0f; // deterministic sampling

int main() {
const std::string fname = "../ggml_weights/ggml_weights_text.bin";
const std::string dirname = "../ggml_weights/";

std::mt19937 rng(0);
bark_sequence input, gt_tokens;

bark_model model;
std::mt19937 rng(0);

if (gpt_model_load(fname, model.text_model) > 0) {
fprintf(stderr, "%s: invalid model file '%s'\n", __func__, fname.c_str());
return 1;
// initialize bark context
struct bark_context * bctx = bark_load_model(dirname);
if (!bctx) {
fprintf(stderr, "%s: Could not load model\n", __func__);
exit(1);
}

bark_context * ctx = bark_new_context_with_model(&model);
ctx->rng = rng;

bark_sequence input;
bark_sequence gt_tokens;
bctx->rng = rng;

for (int i = 0; i < (int) test_data.size(); i++) {
input.clear();
gt_tokens.clear();

std::string path = test_data[i];
load_test_data(path, input, gt_tokens);
ctx->tokens = input;
bctx->tokens = input;

bark_forward_text_encoder(ctx, temp, min_eos_p, n_threads);
if (!bark_forward_text_encoder(bctx, n_threads)) {
fprintf(stderr, "%s: failed to forward text encoder\n", __func__);
exit(1);
}

printf("\n");
printf("%s: %s\n", __func__, path.c_str());
if (!run_test(gt_tokens, ctx->semantic_tokens)) {
if (!run_test(gt_tokens, bctx->semantic_tokens)) {
printf("%s: test %d failed.\n", __func__, i+1);
} else {
printf("%s: test %d passed.\n", __func__, i+1);
}
}

bark_free(ctx);
bark_free(bctx);

return 0;
}
27 changes: 12 additions & 15 deletions tests/test-gpt-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "common.h"


static const std::vector<std::tuple<std::string, bool>> test_args = {
const std::vector<std::tuple<std::string, bool>> test_args = {
{ "./data/gpt_eval/test_gpt_eval_1_no_merge.bin", false }, // prompt: Hello, my name is Suno. And, uh - and I like pizza. [laughs] But I also have other interests such as playing tic tac toe.
{ "./data/gpt_eval/test_gpt_eval_2_no_merge.bin", false }, // prompt: Buenos días Miguel. Tu colega piensa que tu alemán es extremadamente malo. But I suppose your english isn't terrible.
{ "./data/gpt_eval/test_gpt_eval_3_no_merge.bin", false }, // prompt: ♪ In the jungle, the mighty jungle, the lion barks tonight ♪
Expand All @@ -15,30 +15,27 @@ static const std::vector<std::tuple<std::string, bool>> test_args = {
{ "./data/gpt_eval/test_gpt_eval_3_merge.bin", true }, // prompt: Ceci est un texte en français pour tester le bon fonctionnement de bark.
};

static const int n_threads = 4;
const int n_threads = 4;

int main() {
const std::string fname = "../ggml_weights/ggml_weights_text.bin";
const std::string fname = "../ggml_weights/";

gpt_model model;
if (gpt_model_load(fname, model) > 0) {
fprintf(stderr, "%s: invalid model file '%s'\n", __func__, fname.c_str());
return 1;
// initialize bark context
struct bark_context * bctx = bark_load_model(fname);
if (!bctx) {
fprintf(stderr, "%s: Could not load model\n", __func__);
exit(1);
}

bark_sequence tokens;
logit_sequence gt_logits, logits;

auto & model = bctx->model.text_model;
auto & hparams = model.hparams;

int n_vocab = hparams.n_out_vocab;
logits.resize(n_vocab);

// dry run to estimate mem_per_token
{
int n_past = 0;
bark_token decoy[4] = { 0, 1, 2, 3 };
gpt_eval(model, decoy, 4, nullptr, &n_past, false, n_threads);
}
logits.resize(n_vocab);

for (int i = 0; i < (int) test_args.size(); i++) {
tokens.clear();
Expand All @@ -50,7 +47,7 @@ int main() {
load_test_data(path, tokens, gt_logits);

int n_past = 0;
gpt_eval(model, tokens.data(), tokens.size(), logits.data(), &n_past, merge_ctx, n_threads);
// gpt_eval(model, tokens.data(), tokens.size(), logits.data(), &n_past, merge_ctx, n_threads);

printf("\n");
printf("%s: %s\n", __func__, path.c_str());
Expand Down
19 changes: 8 additions & 11 deletions tests/test-tokenizer.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/* Usage:

```bash
./bin/test-tokenizer ../ggml_weights/ggml_vocab.bin
```
*/
#include <cstdio>
#include <string>
#include <map>
#include <vector>

#define BARK_API_INTERNAL
#include "bark.h"

struct bark_vocab {
using id = int32_t;
using token = std::string;

std::map<token, id> token_to_id;
std::map<id, token> id_to_token;
};

static const std::map<std::string, bark_sequence> & k_tests()
{
static std::map<std::string, bark_sequence> _k_tests = {
Expand All @@ -39,9 +36,9 @@ int main(int argc, char **argv) {
bark_vocab vocab;
int max_ctx_size = 256;

if (bark_vocab_load(fname.c_str(), &vocab, 119547) > 0) {
if (!bark_vocab_load(fname.c_str(), &vocab, 119547)) {
fprintf(stderr, "%s: invalid vocab file '%s'\n", __func__, fname.c_str());
return 1;
exit(1);
}

for (const auto & test_kv : k_tests()) {
Expand Down