examples: cache hf model when --model not provided

This commit is contained in:
Amir 2024-05-17 22:23:10 +00:00
parent 4679fde41b
commit 6d72ed3fff
2 changed files with 33 additions and 2 deletions

View file

@ -1354,8 +1354,7 @@ void gpt_params_handle_model_default(gpt_params & params) {
} }
params.hf_file = params.model; params.hf_file = params.model;
} else if (params.model.empty()) { } else if (params.model.empty()) {
const char* cache_dir = getenv("LLAMA_CACHE") ? getenv("LLAMA_CACHE") : DEFAULT_LLAMA_CACHE; params.model = get_cache_directory() + string_split(params.hf_file, '/').back();
params.model = cache_dir + string_split(params.hf_file, '/').back();
} }
} else if (!params.model_url.empty()) { } else if (!params.model_url.empty()) {
if (params.model.empty()) { if (params.model.empty()) {
@ -2528,6 +2527,37 @@ bool create_directory_with_parents(const std::string & path) {
#endif // _WIN32 #endif // _WIN32
} }
std::string get_cache_directory() {
std::string cache_directory = "";
if (getenv("LLAMA_CACHE")) {
cache_directory = std::getenv("LLAMA_CACHE");
if (cache_directory.back() != DIRECTORY_SEPARATOR) {
cache_directory += DIRECTORY_SEPARATOR;
}
} else {
#ifdef __linux__
if (std::getenv("XDG_CACHE_HOME")) {
cache_directory = std::getenv("XDG_CACHE_HOME");
} else {
cache_directory = std::getenv("HOME") + std::string("/.cache/");
}
#elif defined(__APPLE__)
cache_directory = std::getenv("HOME") + std::string("/Library/Caches/");
#elif defined(_WIN32)
cache_directory = std::getenv("APPDATA");
#endif // __linux__
cache_directory += "llama.cpp";
cache_directory += DIRECTORY_SEPARATOR;
}
const bool success = create_directory_with_parents(cache_directory);
if (!success) {
throw std::runtime_error("failed to create cache directory: " + cache_directory);
}
return cache_directory;
}
void dump_vector_float_yaml(FILE * stream, const char * prop_name, const std::vector<float> & data) { void dump_vector_float_yaml(FILE * stream, const char * prop_name, const std::vector<float> & data) {
if (data.empty()) { if (data.empty()) {
fprintf(stream, "%s:\n", prop_name); fprintf(stream, "%s:\n", prop_name);

View file

@ -287,6 +287,7 @@ bool llama_should_add_bos_token(const llama_model * model);
// //
bool create_directory_with_parents(const std::string & path); bool create_directory_with_parents(const std::string & path);
std::string get_cache_directory();
void dump_vector_float_yaml(FILE * stream, const char * prop_name, const std::vector<float> & data); void dump_vector_float_yaml(FILE * stream, const char * prop_name, const std::vector<float> & data);
void dump_vector_int_yaml(FILE * stream, const char * prop_name, const std::vector<int> & data); void dump_vector_int_yaml(FILE * stream, const char * prop_name, const std::vector<int> & data);
void dump_string_yaml_multiline(FILE * stream, const char * prop_name, const char * data); void dump_string_yaml_multiline(FILE * stream, const char * prop_name, const char * data);