-
Notifications
You must be signed in to change notification settings - Fork 22.3k
common: refactor model handling #24980
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
Changes from 7 commits
ac9d8be
2188cc7
5fcd847
0bc03bb
206b378
89de8da
4079ce6
df822d2
6244ba7
c75ee8a
89e37a5
c705582
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| #include "common.h" | ||
| #include "download.h" | ||
| #include "hf-cache.h" | ||
|
|
||
| #include <set> | ||
| #include <map> | ||
|
|
@@ -130,19 +131,29 @@ bool common_params_to_map(int argc, char ** argv, llama_example ex, std::map<com | |
| // see: https://github.com/ggml-org/llama.cpp/issues/18163 | ||
| void common_params_add_preset_options(std::vector<common_arg> & args); | ||
|
|
||
| struct common_params_handle_models_params { | ||
| struct common_models_handler { | ||
| common_params & params; | ||
| common_download_callback * callback = nullptr; | ||
| bool preset_only = false; // if true, only check & download remote preset (for router mode) | ||
| }; | ||
| hf_cache::hf_plan plan; | ||
| common_download_opts opts; | ||
|
|
||
| common_models_handler(common_params & params) : params(params) {} | ||
|
|
||
| // fetch the metadata if needed (but do not download the model) | ||
| void fetch_meta(llama_example curr_ex); | ||
|
|
||
| // populate model paths (main model, mmproj, etc) from -hf if necessary | ||
| // return true if the model is ready to use | ||
| // throw an exception if there is an error that prevents the model from being used (e.g. network error, model not found, etc) | ||
| // if params.skip_download is true, no downloads will be attempted. return false if the model is invalid or missing (e.g. ETag check failed) | ||
| bool common_params_handle_models( | ||
| common_params & params, | ||
| llama_example curr_ex, | ||
| const common_params_handle_models_params & handle_params); | ||
| // return true if the input -hf is a preset-only repo (i.e. contains a preset.ini file) | ||
| bool is_preset_repo() const; | ||
|
|
||
| // download the model if needed, then apply it to the common_params | ||
| void apply(); | ||
|
|
||
| private: | ||
| std::string get_default_local_path(const std::string & url); | ||
|
|
||
| // build download tasks for a plain (non-hf) url model, honoring a user-supplied -m path | ||
| std::vector<common_download_task> build_url_tasks(const common_params_model & model); | ||
| }; | ||
|
Comment on lines
+134
to
+137
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This struct seems a bit unnecessary - likely can remain a function. The private methods can be static functions in the cpp file.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sense, I refactored it again: struct common_models_handler {
common_download_hf_plan plan;
common_download_opts opts;
};
common_models_handler common_models_handler_init(const common_params & params, llama_example curr_ex);
bool common_models_handler_is_preset_repo(const common_models_handler & handler);
void common_models_handler_apply(common_models_handler & handler, common_params & params, common_download_callback * callback = nullptr);Usage:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since all the usages are like: {
auto handler = common_models_handler_init(...);
if (need_apply) {
common_models_handler_apply(handler);
}
}It does not seem necessary to introduce the void common_params_update(
common_params & params,
llama_example ex,
bool apply,
common_download_callback * callback);
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your idea is similar to the existing models_handler = common_models_handler_init(params, LLAMA_EXAMPLE_SERVER);
if (common_models_handler_is_preset_repo(models_handler)) {
// apply the preset and start the server in router mode
common_models_handler_apply(models_handler, params);
}
// do other endpoint setup
ctx_http.get ("/models/sse", progress_download_report);
// ... then later on
if (not_preset) {
// non-roter + non-preset: download model, with progress callback via `progress_download_report`
common_models_handler_apply(models_handler, params, callback);
}We could also get rid of
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, note that if we only call |
||
|
|
||
| // initialize argument parser context - used by test-arg-parser and preset | ||
| common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed