Skip to content

Commit 67155ab

Browse files
farbodbjngxsonslaren
authored
feat: Implements retrying logic for downloading models using --model-url flag (#9255)
* feat: Implements retrying logic for downloading models using --model-url flag * Update common/common.cpp Co-authored-by: Xuan Son Nguyen <thichthat@gmail.com> * Update common/common.cpp Co-authored-by: Xuan Son Nguyen <thichthat@gmail.com> * apply comments * implements a retry function to avoid duplication * fix editorconfig * change function name --------- Co-authored-by: farbod <farbod.bjary82@gmail.com> Co-authored-by: Xuan Son Nguyen <thichthat@gmail.com> Co-authored-by: slaren <slarengh@gmail.com> Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
1 parent 5af118e commit 67155ab

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

common/common.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -941,11 +941,37 @@ struct ggml_threadpool_params ggml_threadpool_params_from_cpu_params(const cpu_p
941941

942942
#ifdef LLAMA_USE_CURL
943943

944+
#define CURL_MAX_RETRY 3
945+
#define CURL_RETRY_DELAY_SECONDS 2
946+
947+
944948
static bool starts_with(const std::string & str, const std::string & prefix) {
945949
// While we wait for C++20's std::string::starts_with...
946950
return str.rfind(prefix, 0) == 0;
947951
}
948952

953+
static bool curl_perform_with_retry(const std::string& url, CURL* curl, int max_attempts, int retry_delay_seconds) {
954+
int remaining_attempts = max_attempts;
955+
956+
while (remaining_attempts > 0) {
957+
fprintf(stderr, "%s: Trying to download from %s (attempt %d of %d)...\n", __func__ , url.c_str(), max_attempts - remaining_attempts + 1, max_attempts);
958+
959+
CURLcode res = curl_easy_perform(curl);
960+
if (res == CURLE_OK) {
961+
return true;
962+
}
963+
964+
int exponential_backoff_delay = std::pow(retry_delay_seconds, max_attempts - remaining_attempts) * 1000;
965+
fprintf(stderr, "%s: curl_easy_perform() failed: %s, retrying after %d milliseconds...\n", __func__, curl_easy_strerror(res), exponential_backoff_delay);
966+
967+
remaining_attempts--;
968+
std::this_thread::sleep_for(std::chrono::milliseconds(exponential_backoff_delay));
969+
}
970+
971+
fprintf(stderr, "%s: curl_easy_perform() failed after %d attempts\n", __func__, max_attempts);
972+
return false;
973+
}
974+
949975
static bool llama_download_file(const std::string & url, const std::string & path, const std::string & hf_token) {
950976

951977
// Initialize libcurl
@@ -1049,9 +1075,8 @@ static bool llama_download_file(const std::string & url, const std::string & pat
10491075
curl_easy_setopt(curl.get(), CURLOPT_HEADERFUNCTION, static_cast<CURLOPT_HEADERFUNCTION_PTR>(header_callback));
10501076
curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA, &headers);
10511077

1052-
CURLcode res = curl_easy_perform(curl.get());
1053-
if (res != CURLE_OK) {
1054-
fprintf(stderr, "%s: curl_easy_perform() failed: %s\n", __func__, curl_easy_strerror(res));
1078+
bool was_perform_successful = curl_perform_with_retry(url, curl.get(), CURL_MAX_RETRY, CURL_RETRY_DELAY_SECONDS);
1079+
if (!was_perform_successful) {
10551080
return false;
10561081
}
10571082

@@ -1126,11 +1151,10 @@ static bool llama_download_file(const std::string & url, const std::string & pat
11261151
};
11271152

11281153
// start the download
1129-
fprintf(stderr, "%s: downloading from %s to %s (server_etag:%s, server_last_modified:%s)...\n", __func__,
1130-
llama_download_hide_password_in_url(url).c_str(), path.c_str(), headers.etag.c_str(), headers.last_modified.c_str());
1131-
auto res = curl_easy_perform(curl.get());
1132-
if (res != CURLE_OK) {
1133-
fprintf(stderr, "%s: curl_easy_perform() failed: %s\n", __func__, curl_easy_strerror(res));
1154+
fprintf(stderr, "%s: trying to download model from %s to %s (server_etag:%s, server_last_modified:%s)...\n", __func__,
1155+
llama_download_hide_password_in_url(url).c_str(), path.c_str(), headers.etag.c_str(), headers.last_modified.c_str());
1156+
bool was_perform_successful = curl_perform_with_retry(url, curl.get(), CURL_MAX_RETRY, CURL_RETRY_DELAY_SECONDS);
1157+
if (!was_perform_successful) {
11341158
return false;
11351159
}
11361160

lora-tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit c26d5fb85b4070a9e9c4e65d132c783b98086890

0 commit comments

Comments
 (0)