Skip to content

llama : add fd-based model loading via llama_model_load_from_fd ( REWORK )#20402

Open
Siddhesh2377 wants to merge 3 commits intoggml-org:masterfrom
Siddhesh2377:fd-loading
Open

llama : add fd-based model loading via llama_model_load_from_fd ( REWORK )#20402
Siddhesh2377 wants to merge 3 commits intoggml-org:masterfrom
Siddhesh2377:fd-loading

Conversation

@Siddhesh2377
Copy link

Adds llama_model_load_from_fd() to load GGUF models from a POSIX file descriptor instead of a file path.

On Android, apps accessing user files through SAF only get a file descriptor, not a path. The alternative is copying the model into app storage or requesting MANAGE_EXTERNAL_STORAGE, which gets rejected by Google Play. This happened with my app (ToolNeuron).

Reworked version of a previous PR that was rejected for code quality.

Not supported on Windows. The fd is dup'd internally so the caller retains ownership.

Tested locally with CI and a real model (vocab_only + mmap).

@Siddhesh2377 Siddhesh2377 changed the title llama : add fd-based model loading via llama_model_load_from_fd llama : add fd-based model loading via llama_model_load_from_fd ( REWORK ) Mar 11, 2026
@github-actions github-actions bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning labels Mar 11, 2026

GGML_API struct gguf_context * gguf_init_empty(void);
GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
GGML_API struct gguf_context * gguf_init_from_fd(int fd, struct gguf_init_params params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your purposes, would it work to expose the current gguf_init_from_file_impl as gguf_init_from_file_ptr and to use that as the basis for the implementation instead? That way we would be able to also use this code on Windows in conjunction with ggml_fopen.

Copy link
Author

@Siddhesh2377 Siddhesh2377 Mar 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, replaced gguf_init_from_fd with gguf_init_from_file_ptr(FILE *) and moved the dup+fdopen logic up to llama-model-loader

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The llama C API should also use a file pointer if at all possible, the conversion from file descriptor to file pointer should be in your user code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, switched the llama C API to FILE pointer as well. The test shows the fd to FILE* conversion on the caller side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants