Skip to content

Commit

Permalink
MNT Stricter tests (PABannier#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
PABannier committed Aug 18, 2023
1 parent e60a459 commit c86bc39
Show file tree
Hide file tree
Showing 48 changed files with 336 additions and 425 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ quantize
!ggml_weights/ggml_vocab.bin
!ggml_weights/vocab.txt

tests/test-text-encoder
tests/test-coarse-encoder
tests/test-fine-encoder
tests/test-gpt-eval
tests/test-fine-gpt-eval

tests/test-forward-coarse
tests/test-forward-semantic
Expand Down
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"preLaunchTask": "build test fine encoder",
},
{
"name": "Debug qkv",
"name": "Debug gpt-eval",
"type": "lldb",
"request": "launch",
"program": "./tests/test-qkv",
"program": "./tests/test-gpt-eval",
"args": [],
"cwd": "${fileDirname}",
"preLaunchTask": "build test qkv",
"preLaunchTask": "build test gpt eval",
}
]
}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"algorithm": "cpp"
"algorithm": "cpp",
"bit": "cpp",
"cinttypes": "cpp",
"codecvt": "cpp"
}
}
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"tasks": [
{
"type": "shell",
"label": "build test qkv",
"label": "build test gpt eval",
"options": {
"env": {
"BARK_DEBUG": "1",
}
},
"command": "make tests/test-qkv",
"command": "make tests/test-gpt-eval",
"problemMatcher": [],
},
{
Expand Down
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BUILD_TARGETS = main quantize

# Binaries only useful for tests
TEST_TARGETS = tests/test-tokenizer tests/test-text-encoder tests/test-coarse-encoder tests/test-fine-encoder
TEST_TARGETS = tests/test-tokenizer tests/test-gpt-eval tests/test-fine-gpt-eval
TEST_TARGETS += tests/test-forward-semantic tests/test-forward-coarse tests/test-forward-fine
TEST_TARGETS += tests/test-forward-encodec

Expand Down Expand Up @@ -324,13 +324,10 @@ common.o: tests/common.cpp
tests/test-tokenizer: tests/test-tokenizer.cpp ggml.o bark.o encodec.o $(OBJS)
$(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)

tests/test-text-encoder: tests/test-text-encoder.cpp ggml.o bark.o encodec.o common.o $(OBJS)
tests/test-gpt-eval: tests/test-gpt-eval.cpp ggml.o bark.o encodec.o common.o $(OBJS)
$(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)

tests/test-coarse-encoder: tests/test-coarse-encoder.cpp ggml.o bark.o encodec.o common.o $(OBJS)
$(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)

tests/test-fine-encoder: tests/test-fine-encoder.cpp ggml.o bark.o encodec.o common.o $(OBJS)
tests/test-fine-gpt-eval: tests/test-fine-gpt-eval.cpp ggml.o bark.o encodec.o common.o $(OBJS)
$(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)

tests/test-forward-semantic: tests/test-forward-semantic.cpp ggml.o bark.o encodec.o common.o $(OBJS)
Expand Down
4 changes: 3 additions & 1 deletion bark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Author: Pierre-Antoine Bannier <[email protected]>
#include <regex>
#include <string>

#define BARK_DEBUG 0

bool allequal(struct ggml_tensor * a, struct ggml_tensor * b, std::string test_name) {
assert(a->ne[0] == b->ne[0]);
assert(a->ne[1] == b->ne[1]);
Expand Down Expand Up @@ -1177,7 +1179,7 @@ bool gpt_eval(
cur = ggml_add(ctx0,
ggml_repeat(ctx0, model.layers[il].c_mlp_fc_b, cur),
cur);

// GELU activation
// [3072, N]
cur = ggml_gelu(ctx0, cur);
Expand Down
122 changes: 83 additions & 39 deletions tests/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
#include "bark-util.h"
#include "common.h"

template <typename T, typename U>
inline bool all_equal(std::vector<T> s1, std::vector<U> s2, int * n_violations) {
if (s1.size() != s2.size()) { return false; }
for (int i = 0; i < (int) s1.size(); i++) {
if (s1[i] != s2[i])
*n_violations += 1;
}
return *n_violations == 0;
int64_t bytes_left(std::ifstream & f) {
// utils to check all bytes are read from stream
int64_t curr_pos = f.tellg();
f.seekg(0, std::ios::end);
int64_t file_size = f.tellg();
int64_t bytes_left_to_read = file_size - curr_pos;
return bytes_left_to_read;
}

template bool all_equal(std::vector<int> s1, std::vector<int> s2, int * n_violations);
template bool all_equal(std::vector<float> s1, std::vector<float> s2, int * n_violations);

template <typename T, typename U>
inline bool all_close(
std::vector<T> s1, std::vector<U> s2, float * max_violation, int * n_violations) {
std::vector<T> s1,
std::vector<U> s2,
float * max_violation,
int * n_violations) {
if (s1.size() != s2.size()) { return false; }
for (int i = 0; i < (int) s1.size(); i++) {
float violation = fabs(s1[i] - s2[i]);
Expand All @@ -31,12 +30,23 @@ inline bool all_close(
return *n_violations == 0;
}

template bool all_close(std::vector<int> s1, std::vector<int> s2, float * max_violation, int * n_violations);
template bool all_close(std::vector<float> s1, std::vector<float> s2, float * max_violation, int * n_violations);
template bool all_close(
std::vector<int> s1,
std::vector<int> s2,
float * max_violation,
int * n_violations);

inline bool all_close_nested(
std::vector<std::vector<float>> s1, std::vector<std::vector<float>> s2,
float * max_violation, int * n_violations) {
template bool all_close(
std::vector<float> s1,
std::vector<float> s2,
float * max_violation,
int * n_violations);

inline bool all_close(
std::vector<std::vector<float>> s1,
std::vector<std::vector<float>> s2,
float * max_violation,
int * n_violations) {
if (s1.size() != s2.size()) { return false; }
for (int i = 0; i < (int) s1.size(); i++) {
if (s1[i].size() != s2[i].size()) { return false; }
Expand All @@ -51,9 +61,23 @@ inline bool all_close_nested(
}

template <typename T, typename U>
inline bool all_equal_nested(
std::vector<std::vector<T>> s1, std::vector<std::vector<U>> s2,
int * n_violations) {
inline bool all_equal(std::vector<T> s1, std::vector<U> s2, int * n_violations) {
if (s1.size() != s2.size()) { return false; }
for (int i = 0; i < (int) s1.size(); i++) {
if (s1[i] != s2[i])
*n_violations += 1;
}
return *n_violations == 0;
}

template bool all_equal(std::vector<int> s1, std::vector<int> s2, int * n_violations);
template bool all_equal(std::vector<float> s1, std::vector<float> s2, int * n_violations);

template <typename T, typename U>
inline bool all_equal(
std::vector<std::vector<T>> s1,
std::vector<std::vector<U>> s2,
int * n_violations) {
if (s1.size() != s2.size()) { return false; }
for (int i = 0; i < (int) s1.size(); i++) {
if (s1[i].size() != s2[i].size()) { return false; }
Expand All @@ -65,25 +89,27 @@ inline bool all_equal_nested(
return *n_violations == 0;
}

template bool all_equal_nested(std::vector<std::vector<int>>, std::vector<std::vector<int>>, int * n_violations);
template bool all_equal(
std::vector<std::vector<int>>,
std::vector<std::vector<int>>,
int * n_violations);

bool run_test_on_sequence(std::vector<float> truth, std::vector<float> result) {
bool run_test(std::vector<float> truth, std::vector<float> result) {
float max_violation = 0.0f;
int n_violations = 0;
if (!all_close(result, truth, &max_violation, &n_violations)) {
if (n_violations == 0) {
fprintf(stderr, "%s : wrong shape (%zu != %zu).\n", __func__, truth.size(), result.size());
} else {
fprintf(stderr, "\n");
fprintf(stderr, " abs_tol=%.4f, abs max viol=%.4f, viol=%.1f%%", ABS_TOL, max_violation, (float)n_violations/truth.size()*100);
fprintf(stderr, "%s: abs_tol=%.4f, abs max viol=%.4f, viol=%.1f%%", __func__, ABS_TOL, max_violation, (float)n_violations/truth.size()*100);
fprintf(stderr, "\n");
}
return false;
}
return true;
}

bool run_test_on_sequence(std::vector<int> truth, std::vector<int> result) {
bool run_test(std::vector<int> truth, std::vector<int> result) {
int n_violations = 0;
if (!all_equal(result, truth, &n_violations)) {
if (n_violations == 0) {
Expand All @@ -98,10 +124,10 @@ bool run_test_on_sequence(std::vector<int> truth, std::vector<int> result) {
return true;
}

bool run_test_on_codes(logit_matrix truth, logit_matrix result) {
bool run_test(logit_matrix truth, logit_matrix result) {
float max_violation = 0.0f;
int n_violations = 0;
if (!all_close_nested(result, truth, &max_violation, &n_violations)) {
if (!all_close(result, truth, &max_violation, &n_violations)) {
fprintf(stderr, "\n");
fprintf(stderr, "%s : failed test\n", __func__);
if (n_violations == 0) {
Expand All @@ -115,9 +141,9 @@ bool run_test_on_codes(logit_matrix truth, logit_matrix result) {
return true;
}

bool run_test_on_codes(bark_codes truth, bark_codes result) {
bool run_test(bark_codes truth, bark_codes result) {
int n_violations = 0;
if (!all_equal_nested(result, truth, &n_violations)) {
if (!all_equal(result, truth, &n_violations)) {
fprintf(stderr, "\n");
fprintf(stderr, "%s : failed test\n", __func__);
if (n_violations == 0) {
Expand Down Expand Up @@ -171,14 +197,26 @@ void load_test_data(std::string fname, std::vector<T>& input, std::vector<U>& ou
fin.read(reinterpret_cast<char *>(output.data()), nelements*sizeof(U));
}

assert(fin.eof());
if (bytes_left(fin) > 0) {
throw std::runtime_error("EOF not reached");
}
}

template void load_test_data(std::string fname, std::vector<int>& input, std::vector<float>& output);
template void load_test_data(std::string fname, std::vector<int32_t>& input, std::vector<int32_t>& output);
template void load_test_data(
std::string fname,
std::vector<int32_t> & input,
std::vector<float> & output);

template void load_test_data(
std::string fname,
std::vector<int32_t> & input,
std::vector<int32_t> & output);

template <typename T, typename U>
void load_test_data(std::string fname, std::vector<T>& input, std::vector<std::vector<U>>& output) {
void load_test_data(
std::string fname,
std::vector<T> & input,
std::vector<std::vector<U>> & output) {
auto fin = std::ifstream(fname, std::ios::binary);
if (!fin) {
fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname.c_str());
Expand Down Expand Up @@ -216,7 +254,9 @@ void load_test_data(std::string fname, std::vector<T>& input, std::vector<std::v
}
}

assert(fin.eof());
if (bytes_left(fin) > 0) {
throw std::runtime_error("EOF not reached");
}
}

template void load_test_data(std::string fname, std::vector<int32_t>& input, std::vector<std::vector<int32_t>>& output);
Expand Down Expand Up @@ -259,11 +299,13 @@ void load_test_data(std::string fname, std::vector<std::vector<int32_t>>& input,
fin.read(reinterpret_cast<char *>(output.data()), nelements*sizeof(float));
}

assert(fin.eof());
if (bytes_left(fin) > 0) {
throw std::runtime_error("EOF not reached");
}
}

template <typename T>
void load_nested_test_data(
void load_test_data(
std::string fname,
std::vector<std::vector<int32_t>> & input,
std::vector<std::vector<T>> & output) {
Expand Down Expand Up @@ -303,15 +345,17 @@ void load_nested_test_data(
}
}

// assert(fin.eof());
if (bytes_left(fin) > 0) {
throw std::runtime_error("EOF not reached");
}
}

template void load_nested_test_data(
template void load_test_data(
std::string fname,
std::vector<std::vector<int32_t>> & input,
std::vector<std::vector<float>> & output);

template void load_nested_test_data(
template void load_test_data(
std::string fname,
std::vector<std::vector<int32_t>> & input,
std::vector<std::vector<int32_t>> & output);
29 changes: 24 additions & 5 deletions tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@
typedef std::vector<float> logit_sequence;
typedef std::vector<std::vector<float>> logit_matrix;

bool run_test_on_sequence(std::vector<int> truth, std::vector<int> result);
bool run_test_on_sequence(std::vector<float> truth, std::vector<float> result);
/* Comparison utils */
template <typename T, typename U>
inline bool all_equal(std::vector<T> s1, std::vector<U> s2, int * n_violations);

template <typename T, typename U>
inline bool all_equal(
std::vector<std::vector<T>> s1,
std::vector<std::vector<U>> s2,
int * n_violations);

template <typename T, typename U>
inline bool all_close(
std::vector<T> s1,
std::vector<U> s2,
float * max_violation,
int * n_violations);

/* Test utils */
bool run_test(std::vector<int> truth, std::vector<int> result);
bool run_test(std::vector<float> truth, std::vector<float> result);

bool run_test_on_codes(logit_matrix truth, logit_matrix result);
bool run_test_on_codes(bark_codes truth, bark_codes result);
bool run_test(logit_matrix truth, logit_matrix result);
bool run_test(bark_codes truth, bark_codes result);

/* Load utils */
template <typename T, typename U>
void load_test_data(std::string fname, std::vector<T>& input, std::vector<U>& output);

Expand All @@ -24,7 +43,7 @@ void load_test_data(std::string fname, std::vector<T>& input, std::vector<std::v
void load_test_data(std::string fname, std::vector<std::vector<int32_t>>& input, std::vector<float>& output);

template <typename T>
void load_nested_test_data(
void load_test_data(
std::string fname,
std::vector<std::vector<int32_t>> & input,
std::vector<std::vector<T>> & output);
Expand Down
Binary file removed tests/data/coarse/test_coarse_1.bin
Binary file not shown.
Binary file removed tests/data/coarse/test_coarse_2.bin
Binary file not shown.
Binary file modified tests/data/coarse/test_pass_coarse_1.bin
Binary file not shown.
Binary file modified tests/data/coarse/test_pass_coarse_2.bin
Binary file not shown.
Binary file added tests/data/coarse/test_pass_coarse_3.bin
Binary file not shown.
Binary file removed tests/data/debug_test.bin
Binary file not shown.
Binary file removed tests/data/fine/test_fine_1.bin
Binary file not shown.
Binary file removed tests/data/fine/test_fine_2.bin
Binary file not shown.
Binary file removed tests/data/fine/test_fine_3.bin
Binary file not shown.
Binary file modified tests/data/fine/test_pass_fine_1.bin
Binary file not shown.
Binary file modified tests/data/fine/test_pass_fine_2.bin
Binary file not shown.
Binary file added tests/data/fine/test_pass_fine_3.bin
Binary file not shown.
File renamed without changes.
Binary file added tests/data/fine_gpt_eval/test_fine_gpt_eval_2.bin
Binary file not shown.
Binary file added tests/data/fine_gpt_eval/test_fine_gpt_eval_3.bin
Binary file not shown.
Binary file added tests/data/fine_gpt_eval/test_fine_gpt_eval_4.bin
Binary file not shown.
Binary file added tests/data/fine_gpt_eval/test_fine_gpt_eval_5.bin
Binary file not shown.
Binary file added tests/data/fine_gpt_eval/test_fine_gpt_eval_6.bin
Binary file not shown.
Binary file added tests/data/gpt_eval/test_gpt_eval_1_merge.bin
Binary file not shown.
Binary file added tests/data/gpt_eval/test_gpt_eval_1_no_merge.bin
Binary file not shown.
Binary file added tests/data/gpt_eval/test_gpt_eval_2_merge.bin
Binary file not shown.
Binary file added tests/data/gpt_eval/test_gpt_eval_2_no_merge.bin
Binary file not shown.
Binary file added tests/data/gpt_eval/test_gpt_eval_3_merge.bin
Binary file not shown.
Binary file added tests/data/gpt_eval/test_gpt_eval_3_no_merge.bin
Binary file not shown.
Binary file modified tests/data/semantic/test_pass_semantic_1.bin
Binary file not shown.
Binary file modified tests/data/semantic/test_pass_semantic_2.bin
Binary file not shown.
Binary file added tests/data/semantic/test_pass_semantic_3.bin
Binary file not shown.
Binary file removed tests/data/semantic/test_semantic_merge_1.bin
Binary file not shown.
Binary file removed tests/data/semantic/test_semantic_merge_2.bin
Binary file not shown.
Binary file removed tests/data/semantic/test_semantic_no_merge_1.bin
Binary file not shown.
Binary file removed tests/data/semantic/test_semantic_no_merge_2.bin
Binary file not shown.
Loading

0 comments on commit c86bc39

Please sign in to comment.