Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add model download metrics

laurenspriemb2aee1833902
diff --git a/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/llm/InferenceRsProvider.kt b/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/llm/InferenceRsProvider.kt
index f313b7868e..c57a260998 100644
--- a/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/llm/InferenceRsProvider.kt
+++ b/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/llm/InferenceRsProvider.kt
@@ -10,6 +10,7 @@ import io.ente.ensu.domain.llm.GenerationSummary
import io.ente.ensu.domain.llm.LlmMessage
import io.ente.ensu.domain.llm.LlmModelTarget
import io.ente.ensu.domain.llm.LlmProvider
+import io.ente.ensu.domain.util.formatBytes
import io.ente.labs.inference_rs.ContextHandle
import io.ente.labs.inference_rs.ContextParams
import io.ente.labs.inference_rs.GenerateChatRequest
@@ -464,6 +465,7 @@ class InferenceRsProvider(
targets,
object : LlmModelDownloadCallback {
override fun onProgress(progress: LlmModelDownloadProgress) {
+ logDownloadMetrics(progress)
onProgress(progress.toDomainProgress())
}
@@ -475,6 +477,37 @@ class InferenceRsProvider(
}
}
+ private fun logDownloadMetrics(progress: LlmModelDownloadProgress) {
+ if (progress.fileComplete) {
+ Log.i(
+ "InferenceRsProvider",
+ "Model download file complete label=${progress.label} " +
+ "bytes=${progress.fileDownloadedBytes} " +
+ "elapsedMs=${progress.fileElapsedMs} " +
+ "rate=${formatRate(progress.fileBytesPerSecond)} " +
+ "retries=${progress.fileRetryCount}"
+ )
+ }
+ if (progress.complete) {
+ Log.i(
+ "InferenceRsProvider",
+ "Model download complete bytes=${progress.downloadedBytes} " +
+ "elapsedMs=${progress.elapsedMs} " +
+ "rate=${formatRate(progress.bytesPerSecond)} " +
+ "retries=${progress.retryCount}"
+ )
+ }
+ }
+
+ private fun formatRate(bytesPerSecond: Double): String {
+ val bytes = if (java.lang.Double.isFinite(bytesPerSecond) && bytesPerSecond > 0.0) {
+ bytesPerSecond.toLong()
+ } else {
+ 0L
+ }
+ return "${formatBytes(bytes)}/s"
+ }
+
private fun LlmModelDownloadProgress.toDomainProgress(): DownloadProgress {
val downloaded = downloadedBytes.coerceAtLeast(0L)
val total = totalBytes?.takeIf { it > 0 }
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Services/InferenceRsProvider.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Services/InferenceRsProvider.swift
index 1159916636..0ad6b538fc 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Services/InferenceRsProvider.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Services/InferenceRsProvider.swift
@@ -99,6 +99,7 @@ final class InferenceRsProvider {
private let modelLoadGate = AsyncSerialGate()
private let rustDownloadCancelLock = NSLock()
private var rustDownloadCancelled = false
+ private let logger = EnsuLogging.shared.logger("InferenceRsProvider")
init(modelDir: URL) {
self.modelDir = modelDir
@@ -556,6 +557,7 @@ final class InferenceRsProvider {
guard let self else { return }
let callback = ModelDownloadCallbackSink(
onProgress: { progress in
+ self.logDownloadMetrics(progress)
onProgress(progress.toInferenceProgress())
},
isCancelled: { [weak self] in
@@ -578,6 +580,28 @@ final class InferenceRsProvider {
rustDownloadCancelLock.unlock()
return cancelled
}
+
+ private func logDownloadMetrics(_ progress: LlmModelDownloadProgress) {
+ if progress.fileComplete {
+ logger.info(
+ "Model download file complete",
+ details: "label=\(progress.label) bytes=\(progress.fileDownloadedBytes) elapsedMs=\(progress.fileElapsedMs) rate=\(formatRate(progress.fileBytesPerSecond)) retries=\(progress.fileRetryCount)"
+ )
+ }
+ if progress.complete {
+ logger.info(
+ "Model download complete",
+ details: "bytes=\(progress.downloadedBytes) elapsedMs=\(progress.elapsedMs) rate=\(formatRate(progress.bytesPerSecond)) retries=\(progress.retryCount)"
+ )
+ }
+ }
+
+ private func formatRate(_ bytesPerSecond: Double) -> String {
+ guard bytesPerSecond.isFinite, bytesPerSecond > 0 else {
+ return "0 B/s"
+ }
+ return "\(Int64(bytesPerSecond).formattedFileSize)/s"
+ }
}
private final class CallbackSink: GenerateEventCallback, @unchecked Sendable {
diff --git a/rust/apps/ensu/src-tauri/src/commands.rs b/rust/apps/ensu/src-tauri/src/commands.rs
index d9e76680af..31a69e2eb1 100644
--- a/rust/apps/ensu/src-tauri/src/commands.rs
+++ b/rust/apps/ensu/src-tauri/src/commands.rs
@@ -1635,6 +1635,7 @@ pub async fn llm_download_model_files(
llm::download_llm_model_files(
targets,
move |progress| {
+ log_download_metrics(&progress);
let payload = tauri_download_progress(progress);
let _ = progress_window.emit("llm-download-progress", payload);
},
@@ -1697,6 +1698,35 @@ fn download_progress_status(progress: &llm::LlmModelDownloadProgress) -> String
}
}
+fn log_download_metrics(progress: &llm::LlmModelDownloadProgress) {
+ if progress.file_complete {
+ logging::log(
+ "LLMDownload",
+ format!(
+ "file_complete label={} bytes={} elapsed_ms={} rate={} retries={}",
+ progress.label,
+ progress.file_downloaded_bytes,
+ progress.file_elapsed_ms,
+ format_rate(progress.file_bytes_per_second),
+ progress.file_retry_count
+ ),
+ );
+ }
+
+ if progress.complete {
+ logging::log(
+ "LLMDownload",
+ format!(
+ "complete bytes={} elapsed_ms={} rate={} retries={}",
+ progress.downloaded_bytes,
+ progress.elapsed_ms,
+ format_rate(progress.bytes_per_second),
+ progress.retry_count
+ ),
+ );
+ }
+}
+
fn format_bytes(bytes: u64) -> String {
const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
let mut value = bytes as f64;
@@ -1712,6 +1742,13 @@ fn format_bytes(bytes: u64) -> String {
}
}
+fn format_rate(bytes_per_second: f64) -> String {
+ if !bytes_per_second.is_finite() || bytes_per_second <= 0.0 {
+ return "0 B/s".to_string();
+ }
+ format!("{}/s", format_bytes(bytes_per_second.round() as u64))
+}
+
#[tauri::command]
pub async fn llm_init_backend() -> Result<(), ApiError> {
logging::log("LLM", "init backend requested");
diff --git a/rust/ensu/inference/src/model_download.rs b/rust/ensu/inference/src/model_download.rs
index f2e16ba8fc..dcca578a09 100644
--- a/rust/ensu/inference/src/model_download.rs
+++ b/rust/ensu/inference/src/model_download.rs
@@ -31,6 +31,43 @@ pub struct LlmModelDownloadProgress {
pub file_downloaded_bytes: u64,
pub file_total_bytes: Option<u64>,
pub percentage: f64,
+ pub elapsed_ms: u64,
+ pub bytes_per_second: f64,
+ pub file_elapsed_ms: u64,
+ pub file_bytes_per_second: f64,
+ pub retry_count: u32,
+ pub file_retry_count: u32,
+ pub file_complete: bool,
+ pub complete: bool,
+}
+
+#[derive(Debug, Clone, Copy, Default)]
+struct DownloadProgressMetrics {
+ elapsed_ms: u64,
+ bytes_per_second: f64,
+ file_elapsed_ms: u64,
+ file_bytes_per_second: f64,
+ retry_count: u32,
+ file_retry_count: u32,
+ file_complete: bool,
+ complete: bool,
+}
+
+#[derive(Debug, Clone, Copy)]
+struct FileDownloadProgress {
+ downloaded_bytes: u64,
+ total_bytes: Option<u64>,
+ network_downloaded_bytes: u64,
+ elapsed: Duration,
+ retry_count: u32,
+}
+
+#[derive(Debug, Clone, Copy)]
+struct FileDownloadReport {
+ final_size: u64,
+ network_downloaded_bytes: u64,
+ elapsed: Duration,
+ retry_count: u32,
}
pub fn download_llm_model_files(
@@ -63,6 +100,7 @@ async fn download_llm_model_files_async(
.connect_timeout(CONNECT_TIMEOUT)
.build()
.map_err(|err| err.to_string())?;
+ let download_started_at = Instant::now();
let mut file_totals = Vec::with_capacity(targets.len());
for target in &targets {
@@ -89,6 +127,8 @@ async fn download_llm_model_files_async(
total.map_or(existing, |value| existing.min(value))
})
.sum::<u64>();
+ let mut network_downloaded_so_far = 0u64;
+ let mut completed_retry_count = 0u32;
emit_combined_progress(
"Preparing downloads",
@@ -96,6 +136,7 @@ async fn download_llm_model_files_async(
total_bytes,
0,
None,
+ DownloadProgressMetrics::default(),
&mut on_progress,
);
@@ -113,18 +154,19 @@ async fn download_llm_model_files_async(
let bytes_before_file = downloaded_so_far.saturating_sub(existing_for_file);
let expected_file_total = file_totals.get(index).copied().flatten();
- let final_file_size = download_llm_model_file(
+ let file_report = download_llm_model_file(
&client,
target,
&destination,
expected_file_total,
- |file_downloaded, file_total| {
- let overall_downloaded = bytes_before_file.saturating_add(file_downloaded);
+ |file_progress| {
+ let overall_downloaded =
+ bytes_before_file.saturating_add(file_progress.downloaded_bytes);
let overall_total = total_bytes.or_else(|| {
let mut known_total = 0u64;
for (known_index, total) in file_totals.iter().enumerate() {
known_total += if known_index == index {
- file_total.unwrap_or(0)
+ file_progress.total_bytes.unwrap_or(0)
} else {
total.unwrap_or(0)
};
@@ -135,8 +177,19 @@ async fn download_llm_model_files_async(
&target.label,
overall_downloaded,
overall_total,
- file_downloaded,
- file_total,
+ file_progress.downloaded_bytes,
+ file_progress.total_bytes,
+ progress_metrics(
+ download_started_at.elapsed(),
+ network_downloaded_so_far
+ .saturating_add(file_progress.network_downloaded_bytes),
+ file_progress.elapsed,
+ file_progress.network_downloaded_bytes,
+ completed_retry_count.saturating_add(file_progress.retry_count),
+ file_progress.retry_count,
+ false,
+ false,
+ ),
&mut on_progress,
);
},
@@ -144,7 +197,29 @@ async fn download_llm_model_files_async(
)
.await?;
- downloaded_so_far = bytes_before_file.saturating_add(final_file_size);
+ downloaded_so_far = bytes_before_file.saturating_add(file_report.final_size);
+ network_downloaded_so_far =
+ network_downloaded_so_far.saturating_add(file_report.network_downloaded_bytes);
+ completed_retry_count = completed_retry_count.saturating_add(file_report.retry_count);
+
+ emit_combined_progress(
+ &target.label,
+ downloaded_so_far,
+ total_bytes.or(Some(downloaded_so_far)),
+ file_report.final_size,
+ expected_file_total.or(Some(file_report.final_size)),
+ progress_metrics(
+ download_started_at.elapsed(),
+ network_downloaded_so_far,
+ file_report.elapsed,
+ file_report.network_downloaded_bytes,
+ completed_retry_count,
+ file_report.retry_count,
+ true,
+ false,
+ ),
+ &mut on_progress,
+ );
}
emit_combined_progress(
@@ -153,6 +228,16 @@ async fn download_llm_model_files_async(
total_bytes.or(Some(downloaded_so_far)),
0,
None,
+ progress_metrics(
+ download_started_at.elapsed(),
+ network_downloaded_so_far,
+ Duration::ZERO,
+ 0,
+ completed_retry_count,
+ 0,
+ false,
+ true,
+ ),
&mut on_progress,
);
@@ -164,15 +249,18 @@ async fn download_llm_model_file(
target: &LlmModelDownloadTarget,
destination: &Path,
expected_file_total: Option<u64>,
- mut on_progress: impl FnMut(u64, Option<u64>),
+ mut on_progress: impl FnMut(FileDownloadProgress),
is_cancelled: &impl Fn() -> bool,
-) -> Result<u64, String> {
+) -> Result<FileDownloadReport, String> {
let parent = destination
.parent()
.ok_or_else(|| format!("Invalid destination path: {}", destination.display()))?;
fs::create_dir_all(parent).map_err(|err| err.to_string())?;
let tmp_path = tmp_path_for(destination);
+ let file_started_at = Instant::now();
+ let mut network_downloaded_bytes = 0u64;
+ let mut retry_count = 0u32;
for attempt in 1..=MAX_ATTEMPTS {
if is_cancelled() {
@@ -186,6 +274,7 @@ async fn download_llm_model_file(
if attempt == MAX_ATTEMPTS {
return Err(format!("Failed to download {}: {}", target.label, err));
}
+ retry_count = retry_count.saturating_add(1);
continue;
}
};
@@ -199,6 +288,7 @@ async fn download_llm_model_file(
if attempt == MAX_ATTEMPTS {
return Err(format!("Failed to download {}: {}", target.label, err));
}
+ retry_count = retry_count.saturating_add(1);
continue;
}
};
@@ -212,6 +302,7 @@ async fn download_llm_model_file(
response.status()
));
}
+ retry_count = retry_count.saturating_add(1);
continue;
}
@@ -219,6 +310,7 @@ async fn download_llm_model_file(
if let Some(total) = file_total {
if total <= resume_from {
let _ = fs::remove_file(&tmp_path);
+ retry_count = retry_count.saturating_add(1);
continue;
}
}
@@ -241,7 +333,13 @@ async fn download_llm_model_file(
let mut last_progress = Instant::now();
let mut retry_attempt = false;
- on_progress(downloaded, file_total);
+ on_progress(FileDownloadProgress {
+ downloaded_bytes: downloaded,
+ total_bytes: file_total,
+ network_downloaded_bytes,
+ elapsed: file_started_at.elapsed(),
+ retry_count,
+ });
loop {
if is_cancelled() {
@@ -256,6 +354,7 @@ async fn download_llm_model_file(
if attempt == MAX_ATTEMPTS {
return Err(format!("Failed to download {}: {}", target.label, err));
}
+ retry_count = retry_count.saturating_add(1);
retry_attempt = true;
break;
}
@@ -268,6 +367,7 @@ async fn download_llm_model_file(
READ_STALL_TIMEOUT.as_secs()
));
}
+ retry_count = retry_count.saturating_add(1);
retry_attempt = true;
break;
}
@@ -287,9 +387,16 @@ async fn download_llm_model_file(
file.write_all(&chunk).map_err(|err| err.to_string())?;
downloaded = downloaded.saturating_add(chunk.len() as u64);
+ network_downloaded_bytes = network_downloaded_bytes.saturating_add(chunk.len() as u64);
if last_progress.elapsed() >= PROGRESS_INTERVAL {
- on_progress(downloaded, file_total);
+ on_progress(FileDownloadProgress {
+ downloaded_bytes: downloaded,
+ total_bytes: file_total,
+ network_downloaded_bytes,
+ elapsed: file_started_at.elapsed(),
+ retry_count,
+ });
last_progress = Instant::now();
}
}
@@ -302,7 +409,13 @@ async fn download_llm_model_file(
file.flush().map_err(|err| err.to_string())?;
drop(file);
- on_progress(downloaded, file_total);
+ on_progress(FileDownloadProgress {
+ downloaded_bytes: downloaded,
+ total_bytes: file_total,
+ network_downloaded_bytes,
+ elapsed: file_started_at.elapsed(),
+ retry_count,
+ });
if let Some(total) = file_total {
if downloaded < total {
@@ -311,6 +424,7 @@ async fn download_llm_model_file(
"Download incomplete: expected {total} bytes, got {downloaded}"
));
}
+ retry_count = retry_count.saturating_add(1);
continue;
}
}
@@ -338,7 +452,12 @@ async fn download_llm_model_file(
));
}
- return Ok(final_size);
+ return Ok(FileDownloadReport {
+ final_size,
+ network_downloaded_bytes,
+ elapsed: file_started_at.elapsed(),
+ retry_count,
+ });
}
Err("Failed to download model".to_string())
@@ -414,6 +533,7 @@ fn emit_combined_progress(
total_bytes: Option<u64>,
file_downloaded_bytes: u64,
file_total_bytes: Option<u64>,
+ metrics: DownloadProgressMetrics,
on_progress: &mut impl FnMut(LlmModelDownloadProgress),
) {
let percentage = total_bytes
@@ -428,9 +548,52 @@ fn emit_combined_progress(
file_downloaded_bytes,
file_total_bytes,
percentage,
+ elapsed_ms: metrics.elapsed_ms,
+ bytes_per_second: metrics.bytes_per_second,
+ file_elapsed_ms: metrics.file_elapsed_ms,
+ file_bytes_per_second: metrics.file_bytes_per_second,
+ retry_count: metrics.retry_count,
+ file_retry_count: metrics.file_retry_count,
+ file_complete: metrics.file_complete,
+ complete: metrics.complete,
});
}
+fn progress_metrics(
+ elapsed: Duration,
+ downloaded_bytes: u64,
+ file_elapsed: Duration,
+ file_downloaded_bytes: u64,
+ retry_count: u32,
+ file_retry_count: u32,
+ file_complete: bool,
+ complete: bool,
+) -> DownloadProgressMetrics {
+ DownloadProgressMetrics {
+ elapsed_ms: duration_ms(elapsed),
+ bytes_per_second: bytes_per_second(downloaded_bytes, elapsed),
+ file_elapsed_ms: duration_ms(file_elapsed),
+ file_bytes_per_second: bytes_per_second(file_downloaded_bytes, file_elapsed),
+ retry_count,
+ file_retry_count,
+ file_complete,
+ complete,
+ }
+}
+
+fn duration_ms(duration: Duration) -> u64 {
+ u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
+}
+
+fn bytes_per_second(bytes: u64, elapsed: Duration) -> f64 {
+ let seconds = elapsed.as_secs_f64();
+ if seconds > 0.0 {
+ bytes as f64 / seconds
+ } else {
+ 0.0
+ }
+}
+
fn existing_download_bytes(destination: &Path) -> u64 {
if destination.exists() {
return if is_valid_gguf_download(destination) {
diff --git a/rust/uniffi/ensu/inference/src/api.rs b/rust/uniffi/ensu/inference/src/api.rs
index 78fb7b9b8c..98c9dc1636 100644
--- a/rust/uniffi/ensu/inference/src/api.rs
+++ b/rust/uniffi/ensu/inference/src/api.rs
@@ -124,6 +124,14 @@ pub struct LlmModelDownloadProgress {
pub file_downloaded_bytes: i64,
pub file_total_bytes: Option<i64>,
pub percentage: f64,
+ pub elapsed_ms: i64,
+ pub bytes_per_second: f64,
+ pub file_elapsed_ms: i64,
+ pub file_bytes_per_second: f64,
+ pub retry_count: i32,
+ pub file_retry_count: i32,
+ pub file_complete: bool,
+ pub complete: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Enum)]
@@ -311,6 +319,14 @@ impl From<core::LlmModelDownloadProgress> for LlmModelDownloadProgress {
file_downloaded_bytes: u64_to_i64(value.file_downloaded_bytes),
file_total_bytes: value.file_total_bytes.map(u64_to_i64),
percentage: value.percentage,
+ elapsed_ms: u64_to_i64(value.elapsed_ms),
+ bytes_per_second: value.bytes_per_second,
+ file_elapsed_ms: u64_to_i64(value.file_elapsed_ms),
+ file_bytes_per_second: value.file_bytes_per_second,
+ retry_count: u32_to_i32(value.retry_count),
+ file_retry_count: u32_to_i32(value.file_retry_count),
+ file_complete: value.file_complete,
+ complete: value.complete,
}
}
}
@@ -339,6 +355,10 @@ fn u64_to_i64(value: u64) -> i64 {
i64::try_from(value).unwrap_or(i64::MAX)
}
+fn u32_to_i32(value: u32) -> i32 {
+ i32::try_from(value).unwrap_or(i32::MAX)
+}
+
struct CallbackSink {
callback: Box<dyn GenerateEventCallback>,
}