From 64e2abe95bb1dbf06b4461284b8002a17006dc83 Mon Sep 17 00:00:00 2001 From: jhs-panda Date: Sun, 21 Apr 2024 18:08:31 -0400 Subject: [PATCH 1/9] does not show progress bar if downloaded in shards --- common/common.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index b6143e41c02cb..ffa0af952e282 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1866,7 +1866,7 @@ void llama_batch_add( #ifdef LLAMA_USE_CURL -static bool llama_download_file(CURL * curl, const char * url, const char * path) { +static bool llama_download_file(CURL * curl, const char * url, const char * path, boolean_t isShard) { bool force_download = false; // Set the URL, allow to follow http redirection @@ -2000,7 +2000,18 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); // display download progress - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); + //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + std::cout<<"HEREEEEEEEEEEEEEE\n"; + std::cout< std::string { @@ -2075,6 +2086,7 @@ struct llama_model * llama_load_model_from_url( const char * model_url, const char * path_model, const struct llama_model_params & params) { + std::cout<<"ISSSSSSUUUUUUEEEEEEE\n"; // Basic validation of the model_url if (!model_url || strlen(model_url) == 0) { fprintf(stderr, "%s: invalid model_url\n", __func__); @@ -2089,7 +2101,7 @@ struct llama_model * llama_load_model_from_url( return NULL; } - if (!llama_download_file(curl, model_url, path_model)) { + if (!llama_download_file(curl, model_url, path_model, false)) { return NULL; } @@ -2117,6 +2129,8 @@ struct llama_model * llama_load_model_from_url( curl_easy_cleanup(curl); + std::cout<<"HELLLLLLLOOOOOO\n"; + std::cout<<"SPLITS "< 1) { char split_prefix[PATH_MAX] = {0}; char split_url_prefix[LLAMA_CURL_MAX_URL_LENGTH] = {0}; @@ -2148,7 +2162,7 @@ struct llama_model * llama_load_model_from_url( llama_split_path(split_url, sizeof(split_url), split_url_prefix, download_idx, n_split); auto * curl = curl_easy_init(); - bool res = llama_download_file(curl, split_url, split_path); + bool res = llama_download_file(curl, split_url, split_path, true); curl_easy_cleanup(curl); return res; From 465a1a2fa547c3b496565cf9fefd82554e2f9702 Mon Sep 17 00:00:00 2001 From: jhs-panda Date: Sun, 21 Apr 2024 18:56:24 -0400 Subject: [PATCH 2/9] removed print statements and cleaned up code --- common/common.cpp | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index ffa0af952e282..c108a7d1f3eed 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1999,17 +1999,10 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, static_cast(write_callback)); curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); - // display download progress - //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); - std::cout<<"HEREEEEEEEEEEEEEE\n"; - std::cout< 1) { char split_prefix[PATH_MAX] = {0}; char split_url_prefix[LLAMA_CURL_MAX_URL_LENGTH] = {0}; From 0de4b6d0fb9357f28084ec6122640d97ea959c45 Mon Sep 17 00:00:00 2001 From: jhs-panda Date: Sun, 21 Apr 2024 19:04:26 -0400 Subject: [PATCH 3/9] corrected style --- common/common.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index c108a7d1f3eed..94e0b582b54af 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -2000,9 +2000,9 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); // display download progress if not sharded - if(isShard){ + if (isShard) { curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); - }else{ + } else { curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); } From af5af31aa0e4c9974ab5499f2bb6191e3b9ddb78 Mon Sep 17 00:00:00 2001 From: Tevin Wang Date: Mon, 22 Apr 2024 13:29:56 -0400 Subject: [PATCH 4/9] Update boolean isShard to snake case --- common/common.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 94e0b582b54af..32963c6c01e7c 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1866,7 +1866,7 @@ void llama_batch_add( #ifdef LLAMA_USE_CURL -static bool llama_download_file(CURL * curl, const char * url, const char * path, boolean_t isShard) { +static bool llama_download_file(CURL * curl, const char * url, const char * path, boolean_t is_shard) { bool force_download = false; // Set the URL, allow to follow http redirection @@ -2000,7 +2000,7 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); // display download progress if not sharded - if (isShard) { + if (is_shard) { curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); } else { curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); From 309a918ed7f291501be33dd786a5a2339416572f Mon Sep 17 00:00:00 2001 From: Tevin Wang Date: Mon, 22 Apr 2024 13:49:17 -0400 Subject: [PATCH 5/9] Change boolean_t to bool type --- common/common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index 32963c6c01e7c..59095a126bb97 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1866,7 +1866,7 @@ void llama_batch_add( #ifdef LLAMA_USE_CURL -static bool llama_download_file(CURL * curl, const char * url, const char * path, boolean_t is_shard) { +static bool llama_download_file(CURL * curl, const char * url, const char * path, bool is_shard) { bool force_download = false; // Set the URL, allow to follow http redirection From 763460ba71b19878924e6c232d4d680c71a3451c Mon Sep 17 00:00:00 2001 From: TevinWang Date: Sun, 28 Apr 2024 17:54:39 -0400 Subject: [PATCH 6/9] Use a global progress callback instead of completing removing progress --- common/common.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 111 insertions(+), 7 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 59095a126bb97..d8ecbf5dc9294 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -68,6 +68,8 @@ #endif #define LLAMA_CURL_MAX_URL_LENGTH 2084 // Maximum URL Length in Chrome: 2083 #define LLAMA_CURL_MAX_HEADER_LENGTH 256 +#define LLAMA_PROGRESS_UPDATE_INTERVAL 1 +#define LLAMA_PROGRESS_PERCENTAGE_WIDTH 10 #endif // LLAMA_USE_CURL using json = nlohmann::ordered_json; @@ -1866,6 +1868,72 @@ void llama_batch_add( #ifdef LLAMA_USE_CURL +struct shard_file_progress { + std::string filename; + double total_bytes; + double received_bytes; +}; + +std::map progress_table; +std::mutex progress_mutex; +std::stringstream download_done_buffer; + +static int shard_progress_callback(void* clientp, double dltotal, double dlnow, double ultotal, double ulnow) { + // upload not needed for downloading + (void) ultotal; + (void) ulnow; + char* url = static_cast(clientp); + + std::lock_guard lock(progress_mutex); + + shard_file_progress& progress = progress_table[url]; + progress.total_bytes = static_cast(dltotal); + progress.received_bytes = static_cast(dlnow); + + std::string url_string = static_cast(url); + progress.filename = url_string.substr(url_string.find_last_of('/') + 1); + + return 0; +} + +static void print_shard_progress_table(bool first_progress) { + if (first_progress) { + fprintf(stderr, "=========================\n"); + } else { + // use updating output + { + std::lock_guard lock(progress_mutex); + for (unsigned int i = 0; i < progress_table.size(); i++) { + fprintf(stderr, "\033[1A\033[K\033[1A\033[K"); + } + fprintf(stderr, "\r"); + } + } + + struct winsize ws; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); + int progress_bar_width = ws.ws_col - LLAMA_PROGRESS_PERCENTAGE_WIDTH; + + // Print the progress information for each downloading file + { + std::lock_guard lock(progress_mutex); + for (const auto& entry : progress_table) { + shard_file_progress progress = entry.second; + int progress_width = static_cast((progress.received_bytes / progress.total_bytes) * progress_bar_width); + + fprintf(stderr, "%s\n", progress.filename.c_str()); + fprintf(stderr, "["); + for (int i = 0; i < progress_width; ++i) { + fprintf(stderr, "="); + } + for (int i = progress_width; i < progress_bar_width; ++i) { + fprintf(stderr, " "); + } + fprintf(stderr, "] %d%%\n", static_cast((progress.received_bytes / progress.total_bytes) * 100)); + } + } +} + static bool llama_download_file(CURL * curl, const char * url, const char * path, bool is_shard) { bool force_download = false; @@ -1999,11 +2067,13 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, static_cast(write_callback)); curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); - // display download progress if not sharded + // display download progress + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); + + // custom progress callback on sharded download if (is_shard) { - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); - } else { - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, shard_progress_callback); + curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, url); } // helper function to hide password in URL @@ -2050,7 +2120,11 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path if (etag_file) { fputs(headers.etag, etag_file); fclose(etag_file); - fprintf(stderr, "%s: file etag saved %s: %s\n", __func__, etag_path, headers.etag); + if (is_shard) { + download_done_buffer << __func__ << ": file etag saved " << etag_path << ": " << headers.etag << "\n"; + } else { + fprintf(stderr, "%s: file etag saved %s: %s\n", __func__, etag_path, headers.etag); + } } } @@ -2060,8 +2134,11 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path if (last_modified_file) { fputs(headers.last_modified, last_modified_file); fclose(last_modified_file); - fprintf(stderr, "%s: file last modified saved %s: %s\n", __func__, last_modified_path, - headers.last_modified); + if (is_shard) { + download_done_buffer << __func__ << ": unable to rename file: " << path_temporary << " to " << path << "\n"; + } else { + fprintf(stderr, "%s: unable to rename file: %s to %s\n", __func__, path_temporary, path); + } } } @@ -2158,6 +2235,33 @@ struct llama_model * llama_load_model_from_url( return res; }, idx)); } + + bool first_progress = true; + while (true) { + // Print the progress table periodically + std::this_thread::sleep_for(std::chrono::seconds(LLAMA_PROGRESS_UPDATE_INTERVAL)); + // Print the progress table header + print_shard_progress_table(first_progress); + first_progress = false; + + // Check if all downloads are complete + bool all_complete = true; + { + std::lock_guard lock(progress_mutex); + for (const auto& entry : progress_table) { + const shard_file_progress& progress = entry.second; + if (progress.received_bytes < progress.total_bytes) { + all_complete = false; + break; + } + } + } + + if (all_complete) { + fprintf(stderr, "%s", download_done_buffer.str().c_str()); + break; + } + } // Wait for all downloads to complete for (auto & f : futures_download) { From 6d5c7f09037631ff09854e337e3b3a609b1fb938 Mon Sep 17 00:00:00 2001 From: TevinWang Date: Sun, 28 Apr 2024 18:49:09 -0400 Subject: [PATCH 7/9] Add windows support for console width --- common/common.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index d8ecbf5dc9294..1d8db119ed37b 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1896,6 +1896,21 @@ static int shard_progress_callback(void* clientp, double dltotal, double dlnow, return 0; } +// function to get the console width +static int get_console_width() { +#ifdef _WIN32 + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + return csbi.dwSize.X; +#elif defined(__linux__) || defined(__APPLE__) + struct winsize ws; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); + return ws.ws_col; +#else + return 80; // Default value +#endif +} + static void print_shard_progress_table(bool first_progress) { if (first_progress) { fprintf(stderr, "=========================\n"); @@ -1910,9 +1925,8 @@ static void print_shard_progress_table(bool first_progress) { } } - struct winsize ws; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); - int progress_bar_width = ws.ws_col - LLAMA_PROGRESS_PERCENTAGE_WIDTH; + + int progress_bar_width = get_console_width() - LLAMA_PROGRESS_PERCENTAGE_WIDTH; // Print the progress information for each downloading file { From c010ce52740ca65bee86b29a772afd28a8f9d3aa Mon Sep 17 00:00:00 2001 From: TevinWang Date: Sun, 28 Apr 2024 18:52:01 -0400 Subject: [PATCH 8/9] Set console width constant --- common/common.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index 1d8db119ed37b..b8d1b99cb466b 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -70,6 +70,7 @@ #define LLAMA_CURL_MAX_HEADER_LENGTH 256 #define LLAMA_PROGRESS_UPDATE_INTERVAL 1 #define LLAMA_PROGRESS_PERCENTAGE_WIDTH 10 +#define LLAMA_DEFAULT_CONSOLE_WIDTH 80 #endif // LLAMA_USE_CURL using json = nlohmann::ordered_json; @@ -1907,7 +1908,7 @@ static int get_console_width() { ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); return ws.ws_col; #else - return 80; // Default value + return LLAMA_DEFAULT_CONSOLE_WIDTH; // Default value #endif } From 9f6f44d75f4be5393e7c36129d109167e96c4dc6 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 10 May 2024 02:02:33 +1000 Subject: [PATCH 9/9] Update common.cpp - trim all trailing whitespace --- common/common.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index b8d1b99cb466b..8deeff2a896e9 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1884,7 +1884,7 @@ static int shard_progress_callback(void* clientp, double dltotal, double dlnow, (void) ultotal; (void) ulnow; char* url = static_cast(clientp); - + std::lock_guard lock(progress_mutex); shard_file_progress& progress = progress_table[url]; @@ -1893,7 +1893,7 @@ static int shard_progress_callback(void* clientp, double dltotal, double dlnow, std::string url_string = static_cast(url); progress.filename = url_string.substr(url_string.find_last_of('/') + 1); - + return 0; } @@ -2084,7 +2084,7 @@ static bool llama_download_file(CURL * curl, const char * url, const char * path // display download progress curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); - + // custom progress callback on sharded download if (is_shard) { curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, shard_progress_callback); @@ -2250,7 +2250,7 @@ struct llama_model * llama_load_model_from_url( return res; }, idx)); } - + bool first_progress = true; while (true) { // Print the progress table periodically