-
Notifications
You must be signed in to change notification settings - Fork 99
Add support for async file read #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| #pragma once | ||
|
|
||
| #ifdef __EMSCRIPTEN__ | ||
| #include <emscripten/emscripten.h> | ||
| #endif | ||
|
|
||
| #include <algorithm> | ||
| #include <map> | ||
| #include <vector> | ||
| #include <cstring> | ||
|
|
||
| static std::map<FILE *, std::string> s_file_path_map; | ||
|
|
||
| namespace wllama_fs | ||
| { | ||
| bool ready = false; | ||
| bool use_async = false; | ||
|
|
||
| static const size_t CACHE_SIZE = 1024 * 1024; // 1 MB read-ahead | ||
|
|
||
| std::vector<uint8_t> cache_data; | ||
| size_t cache_start = 0; | ||
| FILE *cache_file = nullptr; | ||
|
|
||
| void make_sure_ready() | ||
| { | ||
| if (ready) | ||
| return; | ||
| use_async = getenv("USE_ASYNC_FILE") != nullptr; | ||
| ready = true; | ||
| } | ||
|
|
||
| size_t try_cache(FILE *f, char *ptr, size_t req_bytes, size_t fpos) | ||
| { | ||
| if (f != cache_file || cache_data.empty()) | ||
| return 0; | ||
| if (fpos >= cache_start && fpos + req_bytes <= cache_start + cache_data.size()) | ||
| { | ||
| memcpy(ptr, cache_data.data() + (fpos - cache_start), req_bytes); | ||
| return req_bytes; | ||
| } | ||
| return 0; | ||
| } | ||
|
ngxson marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Thin stub — real implementation lives in llama-cpp.js to avoid | ||
| // C++ formatter mangling the JS syntax inside EM_ASYNC_JS macros. | ||
|
|
||
| EM_ASYNC_JS(size_t, js_file_read, (const char *path_ptr, size_t offset, size_t req_size, void *out_ptr), { | ||
| return await _wllama_js_file_read(UTF8ToString(Number(path_ptr)), Number(offset), Number(req_size), Number(out_ptr)); | ||
| }); | ||
|
|
||
| extern "C" | ||
| { | ||
| FILE *__real_fopen(const char *path, const char *mode); | ||
| int __real_fclose(FILE *f); | ||
| size_t __real_fread(void *ptr, size_t size, size_t nmemb, FILE *f); | ||
| int __real_fseek(FILE *f, long offset, int whence); | ||
| long __real_ftell(FILE *f); | ||
|
|
||
| FILE *__wrap_fopen(const char *path, const char *mode) | ||
| { | ||
| wllama_fs::make_sure_ready(); | ||
| FILE *f = __real_fopen(path, mode); | ||
| if (f) | ||
| { | ||
| s_file_path_map[f] = path; | ||
| } | ||
| return f; | ||
| } | ||
|
|
||
| int __wrap_fclose(FILE *f) | ||
| { | ||
| if (wllama_fs::cache_file == f) | ||
| { | ||
| wllama_fs::cache_file = nullptr; | ||
| wllama_fs::cache_data.clear(); | ||
| } | ||
| s_file_path_map.erase(f); | ||
| return __real_fclose(f); | ||
| } | ||
|
|
||
| int __wrap_fseek(FILE *f, long offset, int whence) | ||
| { | ||
| return __real_fseek(f, offset, whence); | ||
| } | ||
|
|
||
| long __wrap_ftell(FILE *f) | ||
| { | ||
| return __real_ftell(f); | ||
| } | ||
|
|
||
| size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *f) | ||
| { | ||
| wllama_fs::make_sure_ready(); | ||
| if (!wllama_fs::use_async) | ||
| return __real_fread(ptr, size, nmemb, f); | ||
|
|
||
| auto nit = s_file_path_map.find(f); | ||
| if (nit == s_file_path_map.end()) | ||
| return __real_fread(ptr, size, nmemb, f); | ||
|
|
||
| size_t req_bytes = size * nmemb; | ||
| if (req_bytes == 0) | ||
| return 0; | ||
|
|
||
| size_t fpos = (size_t)__real_ftell(f); | ||
|
|
||
| // Large reads (>= 1 MB): write directly into ptr, skip cache entirely. | ||
| if (req_bytes >= wllama_fs::CACHE_SIZE) | ||
| { | ||
| size_t actual = (size_t)js_file_read( | ||
| nit->second.c_str(), fpos, req_bytes, ptr); | ||
| if (actual == 0) | ||
| return 0; | ||
| size_t copy_bytes = std::min(req_bytes, actual); | ||
| __real_fseek(f, fpos + copy_bytes, SEEK_SET); | ||
| return copy_bytes / size; | ||
| } | ||
|
|
||
| // Small reads: try cache first. | ||
| size_t cached = wllama_fs::try_cache(f, (char *)ptr, req_bytes, fpos); | ||
| if (cached == req_bytes) | ||
| { | ||
| __real_fseek(f, fpos + req_bytes, SEEK_SET); | ||
| return nmemb; | ||
| } | ||
|
|
||
| // Cache miss: fetch a full CACHE_SIZE block from main thread. | ||
| wllama_fs::cache_data.resize(wllama_fs::CACHE_SIZE); | ||
| size_t actual = (size_t)js_file_read( | ||
| nit->second.c_str(), fpos, wllama_fs::CACHE_SIZE, | ||
| wllama_fs::cache_data.data()); | ||
|
|
||
| wllama_fs::cache_data.resize(actual); | ||
| wllama_fs::cache_file = f; | ||
| wllama_fs::cache_start = fpos; | ||
|
|
||
| if (actual == 0) | ||
| return 0; | ||
|
|
||
| size_t copy_bytes = std::min(req_bytes, actual); | ||
| memcpy(ptr, wllama_fs::cache_data.data(), copy_bytes); | ||
| __real_fseek(f, fpos + copy_bytes, SEEK_SET); | ||
|
|
||
| return copy_bytes / size; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule llama.cpp
updated
from 2dfeca to 64b38b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.