From 26c04d4b31f265589e81605eca7accae7e7d22bc Mon Sep 17 00:00:00 2001 From: Siddhesh2377 Date: Sat, 14 Mar 2026 21:40:01 +0530 Subject: [PATCH] llama : use FILE pointer consistently, address review feedback --- ggml/src/ggml-impl.h | 1 - ggml/src/gguf.cpp | 15 ++++++--------- include/llama.h | 4 +++- src/llama-mmap.cpp | 31 ++++++++++++------------------- src/llama-mmap.h | 2 +- src/llama-model-loader.cpp | 2 +- tests/test-gguf.cpp | 4 ++-- 7 files changed, 25 insertions(+), 34 deletions(-) diff --git a/ggml/src/ggml-impl.h b/ggml/src/ggml-impl.h index e3714b38a6..ba0730ead2 100644 --- a/ggml/src/ggml-impl.h +++ b/ggml/src/ggml-impl.h @@ -718,6 +718,5 @@ inline bool ggml_check_edges(const struct ggml_cgraph * cgraph, // expose GGUF internals for test code GGML_API size_t gguf_type_size(enum gguf_type type); -GGML_API struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_params params); GGML_API void gguf_write_to_buf(const struct gguf_context * ctx, std::vector & buf, bool only_meta); #endif // __cplusplus diff --git a/ggml/src/gguf.cpp b/ggml/src/gguf.cpp index bf28dabb06..49afeacae3 100644 --- a/ggml/src/gguf.cpp +++ b/ggml/src/gguf.cpp @@ -394,7 +394,11 @@ bool gguf_read_emplace_helper(const struct gguf_reader & gr, std::vector(fname, mode, use_direct_io)) {} -llama_file::llama_file(int fd) : pimpl(std::make_unique(fd)) {} +llama_file::llama_file(FILE * file) : pimpl(std::make_unique(file)) {} llama_file::~llama_file() = default; diff --git a/src/llama-mmap.h b/src/llama-mmap.h index 2d1eac91a3..32fab23119 100644 --- a/src/llama-mmap.h +++ b/src/llama-mmap.h @@ -15,7 +15,7 @@ using llama_mlocks = std::vector>; struct llama_file { llama_file(const char * fname, const char * mode, bool use_direct_io = false); - llama_file(int fd); + llama_file(FILE * file); ~llama_file(); size_t tell() const; diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp index 6af0ee1fe5..8046df0194 100644 --- a/src/llama-model-loader.cpp +++ b/src/llama-model-loader.cpp @@ -674,7 +674,7 @@ llama_model_loader::llama_model_loader( get_key(llm_kv(LLM_KV_GENERAL_ARCHITECTURE), arch_name, false); llm_kv = LLM_KV(llm_arch_from_string(arch_name)); - files.emplace_back(new llama_file(fileno(file))); + files.emplace_back(new llama_file(file)); contexts.emplace_back(ctx); // Save tensors data offset info of the main file. diff --git a/tests/test-gguf.cpp b/tests/test-gguf.cpp index 8ebd16ba82..78ca95dcbd 100644 --- a/tests/test-gguf.cpp +++ b/tests/test-gguf.cpp @@ -742,7 +742,7 @@ static std::pair test_handcrafted_file(const unsigned int seed) { /*ctx =*/ hft >= offset_has_data ? &ctx : nullptr, }; - struct gguf_context * gguf_ctx = gguf_init_from_file_impl(file, gguf_params); + struct gguf_context * gguf_ctx = gguf_init_from_file_ptr(file, gguf_params); if (expect_context_not_null(hft)) { printf("%s: - context_not_null: ", __func__); @@ -1137,7 +1137,7 @@ static std::pair test_roundtrip(ggml_backend_dev_t dev, const unsigned /*no_alloc =*/ false, /*ctx =*/ only_meta ? nullptr : &ctx_1, }; - struct gguf_context * gguf_ctx_1 = gguf_init_from_file_impl(file, gguf_params); + struct gguf_context * gguf_ctx_1 = gguf_init_from_file_ptr(file, gguf_params); printf("%s: same_version: ", __func__); if (gguf_get_version(gguf_ctx_0) == gguf_get_version(gguf_ctx_1)) {