Skip to content

Commit 3bddc6a

Browse files
Make byteswapping opt-in
Enable it for llama.cpp
1 parent 3ed07ff commit 3bddc6a

File tree

12 files changed

+48
-31
lines changed

12 files changed

+48
-31
lines changed

common/common.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,8 +1418,9 @@ struct llama_model * common_load_model_from_url(
14181418
int n_split = 0;
14191419
{
14201420
struct gguf_init_params gguf_params = {
1421-
/*.no_alloc = */ true,
1422-
/*.ctx = */ NULL,
1421+
/*.no_alloc = */ true,
1422+
/*.ctx = */ NULL,
1423+
/*.allow_byteswapping = */ true,
14231424
};
14241425
auto * ctx_gguf = gguf_init_from_file(local_path.c_str(), gguf_params);
14251426
if (!ctx_gguf) {
@@ -2063,8 +2064,9 @@ static common_control_vector_data common_control_vector_load_one(const common_co
20632064

20642065
ggml_context * ctx = nullptr;
20652066
struct gguf_init_params meta_gguf_params = {
2066-
/* .no_alloc = */ false,
2067-
/* .ctx = */ &ctx,
2067+
/* .no_alloc = */ false,
2068+
/* .ctx = */ &ctx,
2069+
/* .allow_byteswapping = */ true,
20682070
};
20692071
struct gguf_context * ctx_gguf = gguf_init_from_file(load_info.fname.c_str(), meta_gguf_params);
20702072
if (!ctx_gguf) {

examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,9 @@ static void load_vocab(const char * filename, const Config * config, struct my_l
533533
struct ggml_context * ctx_data = NULL;
534534

535535
struct gguf_init_params params = {
536-
/*.no_alloc = */ false,
537-
/*.ctx = */ &ctx_data,
536+
/*.no_alloc = */ false,
537+
/*.ctx = */ &ctx_data,
538+
/*.allow_byteswapping = */ true,
538539
};
539540

540541
struct gguf_context * ctx = gguf_init_from_file(filename, params);

examples/export-lora/export-lora.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ static std::string ggml_ne_string(const ggml_tensor * t) {
4848

4949
static struct gguf_context * load_gguf(std::string & fname, struct ggml_context ** ctx_ggml) {
5050
struct gguf_init_params params = {
51-
/*.no_alloc = */ true,
52-
/*.ctx = */ ctx_ggml,
51+
/*.no_alloc = */ true,
52+
/*.ctx = */ ctx_ggml,
53+
/*.allow_byteswapping = */ true,
5354
};
5455
struct gguf_context * ctx_gguf = gguf_init_from_file(fname.c_str(), params);
5556
if (!ctx_gguf) {

examples/gguf-hash/gguf-hash.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
288288
struct ggml_context * ctx_data = NULL;
289289

290290
struct gguf_init_params params = {
291-
/*.no_alloc = */ false,
292-
/*.ctx = */ &ctx_data,
291+
/*.no_alloc = */ false,
292+
/*.ctx = */ &ctx_data,
293+
/*.allow_byteswapping = */ true,
293294
};
294295

295296
// xxh64 init

examples/gguf-split/gguf-split.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,9 @@ static void gguf_split(const split_params & split_params) {
372372
struct ggml_context * ctx_meta = NULL;
373373

374374
struct gguf_init_params params = {
375-
/*.no_alloc = */ true,
376-
/*.ctx = */ &ctx_meta,
375+
/*.no_alloc = */ true,
376+
/*.ctx = */ &ctx_meta,
377+
/*.allow_byteswapping = */ true,
377378
};
378379

379380
std::ifstream f_input(split_params.input.c_str(), std::ios::binary);
@@ -437,8 +438,9 @@ static void gguf_merge(const split_params & split_params) {
437438
struct ggml_context * ctx_meta = NULL;
438439

439440
struct gguf_init_params params = {
440-
/*.no_alloc = */ true,
441-
/*.ctx = */ &ctx_meta,
441+
/*.no_alloc = */ true,
442+
/*.ctx = */ &ctx_meta,
443+
/*.allow_byteswapping = */ true,
442444
};
443445

444446
if (i_split > 0) {

examples/gguf/gguf.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ static bool gguf_ex_write(const std::string & fname) {
8585
// just read tensor info
8686
static bool gguf_ex_read_0(const std::string & fname) {
8787
struct gguf_init_params params = {
88-
/*.no_alloc = */ false,
89-
/*.ctx = */ NULL,
88+
/*.no_alloc = */ false,
89+
/*.ctx = */ NULL,
90+
/*.allow_byteswapping = */ true,
9091
};
9192

9293
struct gguf_context * ctx = gguf_init_from_file(fname.c_str(), params);
@@ -151,8 +152,9 @@ static bool gguf_ex_read_1(const std::string & fname, bool check_data) {
151152
struct ggml_context * ctx_data = NULL;
152153

153154
struct gguf_init_params params = {
154-
/*.no_alloc = */ false,
155-
/*.ctx = */ &ctx_data,
155+
/*.no_alloc = */ false,
156+
/*.ctx = */ &ctx_data,
157+
/*.allow_byteswapping = */ true,
156158
};
157159

158160
struct gguf_context * ctx = gguf_init_from_file(fname.c_str(), params);

examples/llava/clip.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,9 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
11221122
struct ggml_context * meta = NULL;
11231123

11241124
struct gguf_init_params params = {
1125-
/*.no_alloc = */ true,
1126-
/*.ctx = */ &meta,
1125+
/*.no_alloc = */ true,
1126+
/*.ctx = */ &meta,
1127+
/*.allow_byteswapping = */ true,
11271128
};
11281129

11291130
struct gguf_context * ctx = gguf_init_from_file(fname, params);

ggml/include/gguf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ extern "C" {
7474

7575
// if not NULL, create a ggml_context and allocate the tensor data in it
7676
struct ggml_context ** ctx;
77+
78+
bool allow_byteswapping;
7779
};
7880

7981
GGML_API struct gguf_context * gguf_init_empty(void);

ggml/src/gguf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
353353
int64_t n_tensors = 0;
354354

355355
if (ok && gr.read(ctx->version)) {
356-
if (((ctx->version & 0x0000FFFF) == 0) && ((ctx->version & 0xFFFF0000) != 0)) {
356+
if ((params.allow_byteswapping) && ((ctx->version & 0x0000FFFF) == 0) && ((ctx->version & 0xFFFF0000) != 0)) {
357357
// most likely different endianness, do byteswapping
358358
gr.do_byteswap = true;
359359
ctx->needs_byteswap = true;

src/llama-adapter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ static void llama_adapter_lora_init_impl(struct llama_model & model, const char
151151

152152
ggml_context * ctx_init;
153153
struct gguf_init_params meta_gguf_params = {
154-
/* .no_alloc = */ true,
155-
/* .ctx = */ &ctx_init,
154+
/* .no_alloc = */ true,
155+
/* .ctx = */ &ctx_init,
156+
/* .allow_byteswapping = */ true,
156157
};
157158

158159
gguf_context_ptr ctx_gguf { gguf_init_from_file(path_lora, meta_gguf_params) };

0 commit comments

Comments
 (0)