From aa6ea2eaa2a5c5b9bce256c525e33fc080af79c1 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 15 Oct 2026 15:03:52 +0200 Subject: [PATCH] fix(model-loader): stop waiting a whole progress tick after the tensors are loaded ModelLoader::load_tensors polled for the end of the workers with a sleep of 200 ms (10 ms for at most four tensors) and printed the progress line at every wake-up. The sleep was also the polling interval, so every load of more than four tensors took at least 200 ms, even when the tensors were already in the page cache or on the device and the workers had finished after a few milliseconds. The cost is invisible when a model is loaded once, but streamed weights are loaded segment by segment at every sampling step. In a 25-step SDXL run with the diffusion weights on disk, about 300 of the 460 loads of the sampling phase took one full tick (0.20-0.23 s) with almost nothing to read and about 100 took two ticks, roughly 4 of the 6.4 s of each step. The same floor is paid by the --offload-to-cpu path. Completion is now polled every millisecond. The progress line keeps its cadence (the first line immediately, then no more often than every 200 ms, 10 ms for at most four tensors), and the loop, the join of the workers and the failure handling are unchanged. A load of 16 small tensors goes from 200 ms to about 1 ms. Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC --- src/model_loader.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/model_loader.cpp b/src/model_loader.cpp index 9f7d7225..4825b69a 100644 --- a/src/model_loader.cpp +++ b/src/model_loader.cpp @@ -1393,20 +1393,25 @@ bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb, } last_n_threads = std::max(1, static_cast(workers.size())); + // Completion is polled every millisecond; only the progress line keeps its own cadence. Sleeping a whole + // progress interval here added up to 200 ms to every load, which dominates when weights are streamed. + const int64_t progress_interval_ms = total_tensors_to_process <= 4 ? 10 : 200; + int64_t next_progress_ms = 0; while (true) { size_t current_idx = tensor_idx.load(); if (current_idx >= tensors_to_process.size() || failed) { break; } - size_t curr_num = total_tensors_processed + current_idx; - float elapsed_seconds = (ggml_time_ms() - t_start) / 1000.0f; - if (log_progress && total_tensors_to_process > 0) { + const int64_t now_ms = ggml_time_ms(); + if (log_progress && total_tensors_to_process > 0 && now_ms >= next_progress_ms) { + size_t curr_num = total_tensors_processed + current_idx; pretty_bytes_progress(static_cast(curr_num), static_cast(total_tensors_to_process), bytes_processed.load(), - elapsed_seconds); + (now_ms - t_start) / 1000.0f); + next_progress_ms = now_ms + progress_interval_ms; } - std::this_thread::sleep_for(std::chrono::milliseconds(total_tensors_to_process <= 4 ? 10 : 200)); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } for (auto& w : workers) {