Skip to content

Commit 636d3cb

Browse files
authored
refactor: reorganize the vocab file structure (#1271)
1 parent adea272 commit 636d3cb

File tree

14 files changed

+67
-55
lines changed

14 files changed

+67
-55
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ file(GLOB SD_LIB_SOURCES
9090
"src/*.h"
9191
"src/*.cpp"
9292
"src/*.hpp"
93+
"src/vocab/*.h"
94+
"src/vocab/*.cpp"
9395
)
9496

9597
find_program(GIT_EXE NAMES git git.exe NO_CMAKE_FIND_ROOT_PATH)

format-code.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
for f in *.cpp *.h *.hpp examples/cli/*.cpp examples/common/*.hpp examples/cli/*.h examples/server/*.cpp; do
1+
for f in src/*.cpp src/*.h src/*.hpp src/vocab/*.h src/vocab/*.cpp examples/cli/*.cpp examples/common/*.hpp examples/cli/*.h examples/server/*.cpp; do
22
[[ "$f" == vocab* ]] && continue
33
echo "formatting '$f'"
44
# if [ "$f" != "stable-diffusion.h" ]; then

src/clip.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "ggml_extend.hpp"
55
#include "model.h"
66
#include "tokenize_util.h"
7+
#include "vocab/vocab.h"
78

89
/*================================================== CLIPTokenizer ===================================================*/
910

@@ -110,7 +111,7 @@ class CLIPTokenizer {
110111
if (merges_utf8_str.size() > 0) {
111112
load_from_merges(merges_utf8_str);
112113
} else {
113-
load_from_merges(ModelLoader::load_merges());
114+
load_from_merges(load_clip_merges());
114115
}
115116
add_special_token("<|startoftext|>");
116117
add_special_token("<|endoftext|>");

src/llm.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "json.hpp"
2020
#include "rope.hpp"
2121
#include "tokenize_util.h"
22+
#include "vocab/vocab.h"
2223

2324
namespace LLM {
2425
constexpr int LLM_GRAPH_SIZE = 10240;
@@ -365,7 +366,7 @@ namespace LLM {
365366
if (merges_utf8_str.size() > 0) {
366367
load_from_merges(merges_utf8_str);
367368
} else {
368-
load_from_merges(ModelLoader::load_qwen2_merges());
369+
load_from_merges(load_qwen2_merges());
369370
}
370371
}
371372
};
@@ -466,7 +467,7 @@ namespace LLM {
466467
if (merges_utf8_str.size() > 0 && vocab_utf8_str.size() > 0) {
467468
load_from_merges(merges_utf8_str, vocab_utf8_str);
468469
} else {
469-
load_from_merges(ModelLoader::load_mistral_merges(), ModelLoader::load_mistral_vocab_json());
470+
load_from_merges(load_mistral_merges(), load_mistral_vocab_json());
470471
}
471472
}
472473
};

src/model.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#include "model.h"
1717
#include "stable-diffusion.h"
1818
#include "util.h"
19-
#include "vocab.hpp"
20-
#include "vocab_mistral.hpp"
21-
#include "vocab_qwen.hpp"
22-
#include "vocab_umt5.hpp"
2319

2420
#include "ggml-alloc.h"
2521
#include "ggml-backend.h"
@@ -1340,36 +1336,6 @@ void ModelLoader::set_wtype_override(ggml_type wtype, std::string tensor_type_ru
13401336
}
13411337
}
13421338

1343-
std::string ModelLoader::load_merges() {
1344-
std::string merges_utf8_str(reinterpret_cast<const char*>(merges_utf8_c_str), sizeof(merges_utf8_c_str));
1345-
return merges_utf8_str;
1346-
}
1347-
1348-
std::string ModelLoader::load_qwen2_merges() {
1349-
std::string merges_utf8_str(reinterpret_cast<const char*>(qwen2_merges_utf8_c_str), sizeof(qwen2_merges_utf8_c_str));
1350-
return merges_utf8_str;
1351-
}
1352-
1353-
std::string ModelLoader::load_mistral_merges() {
1354-
std::string merges_utf8_str(reinterpret_cast<const char*>(mistral_merges_utf8_c_str), sizeof(mistral_merges_utf8_c_str));
1355-
return merges_utf8_str;
1356-
}
1357-
1358-
std::string ModelLoader::load_mistral_vocab_json() {
1359-
std::string json_str(reinterpret_cast<const char*>(mistral_vocab_json_utf8_c_str), sizeof(mistral_vocab_json_utf8_c_str));
1360-
return json_str;
1361-
}
1362-
1363-
std::string ModelLoader::load_t5_tokenizer_json() {
1364-
std::string json_str(reinterpret_cast<const char*>(t5_tokenizer_json_str), sizeof(t5_tokenizer_json_str));
1365-
return json_str;
1366-
}
1367-
1368-
std::string ModelLoader::load_umt5_tokenizer_json() {
1369-
std::string json_str(reinterpret_cast<const char*>(umt5_tokenizer_json_str), sizeof(umt5_tokenizer_json_str));
1370-
return json_str;
1371-
}
1372-
13731339
bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb, int n_threads_p, bool enable_mmap) {
13741340
int64_t process_time_ms = 0;
13751341
std::atomic<int64_t> read_time_ms(0);

src/model.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,6 @@ class ModelLoader {
331331
bool tensor_should_be_converted(const TensorStorage& tensor_storage, ggml_type type);
332332
int64_t get_params_mem_size(ggml_backend_t backend, ggml_type type = GGML_TYPE_COUNT);
333333
~ModelLoader() = default;
334-
335-
static std::string load_merges();
336-
static std::string load_qwen2_merges();
337-
static std::string load_mistral_merges();
338-
static std::string load_mistral_vocab_json();
339-
static std::string load_t5_tokenizer_json();
340-
static std::string load_umt5_tokenizer_json();
341334
};
342335

343336
#endif // __MODEL_H__

src/stable-diffusion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,7 +2679,7 @@ class StableDiffusionGGML {
26792679
};
26802680
sd_tiling_non_square(x, result, vae_scale_factor, tile_size_x, tile_size_y, tile_overlap, on_tiling);
26812681
} else {
2682-
if(!first_stage_model->compute(n_threads, x, true, &result, work_ctx)){
2682+
if (!first_stage_model->compute(n_threads, x, true, &result, work_ctx)) {
26832683
LOG_ERROR("Failed to decode latetnts");
26842684
first_stage_model->free_compute_buffer();
26852685
return nullptr;
@@ -2695,7 +2695,7 @@ class StableDiffusionGGML {
26952695
};
26962696
sd_tiling(x, result, vae_scale_factor, 64, 0.5f, on_tiling);
26972697
} else {
2698-
if(!tae_first_stage->compute(n_threads, x, true, &result)){
2698+
if (!tae_first_stage->compute(n_threads, x, true, &result)) {
26992699
LOG_ERROR("Failed to decode latetnts");
27002700
tae_first_stage->free_compute_buffer();
27012701
return nullptr;

src/t5.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ggml_extend.hpp"
1515
#include "json.hpp"
1616
#include "model.h"
17+
#include "vocab/vocab.h"
1718

1819
// Port from: https://github.com/google/sentencepiece/blob/master/src/unigram_model.h
1920
// and https://github.com/google/sentencepiece/blob/master/src/unigram_model.h.
@@ -341,9 +342,9 @@ class T5UniGramTokenizer {
341342
public:
342343
explicit T5UniGramTokenizer(bool is_umt5 = false) {
343344
if (is_umt5) {
344-
InitializePieces(ModelLoader::load_umt5_tokenizer_json());
345+
InitializePieces(load_umt5_tokenizer_json());
345346
} else {
346-
InitializePieces(ModelLoader::load_t5_tokenizer_json());
347+
InitializePieces(load_t5_tokenizer_json());
347348
}
348349

349350
min_score_ = FLT_MAX;

0 commit comments

Comments
 (0)