Skip to content

AI media harness for planning, generating, and editing locally

TypeScript
5.4 KB
Raw Blame
use super::{events::NativeEvent, ffi::{Api, RequestHandle}, platform::ComputePlatform, runtime::QueueItem, types::ProgressEvent};
use sha2::{Digest, Sha256};
use std::{io::Read, path::{Path, PathBuf}, sync::atomic::{AtomicBool, Ordering}, time::Instant};

fn calibration_path(directory: &Path, vae: &Path, cancel: &AtomicBool, mut checking: impl FnMut()) -> Result<PathBuf, String> {
    use super::lcs_fingerprint::{check_cancel, Fingerprints};
    static FINGERPRINTS: std::sync::OnceLock<std::sync::Mutex<Fingerprints>> = std::sync::OnceLock::new();
    let digest = FINGERPRINTS.get_or_init(Default::default).lock().map_err(|_| "VAE fingerprint cache lock failed.")?
        .get(directory, vae, cancel, |source| {
            checking();
            let mut hash = Sha256::new();
            let mut buffer = vec![0; 1024 * 1024];
            loop {
                check_cancel(cancel)?;
                let count = source.read(&mut buffer).map_err(|e| e.to_string())?;
                if count == 0 { break; }
                hash.update(&buffer[..count]);
            }
            Ok(format!("{:x}", hash.finalize()))
        })?;
    Ok(directory.join(format!("h3-v1-64-256-{digest}.safetensors")))
}

pub(super) fn prepare(api: &Api, request: &RequestHandle, item: &QueueItem, platform: ComputePlatform) -> Result<bool, String> {
    let Some(mut settings) = item.request.generation_effects.as_ref().map(|e| e.native_settings()).transpose()?.flatten() else { return Ok(false); };
    api.require_lcs()?;
    if settings.get("contrast").is_some() && item.request.single_shot.as_ref().is_some_and(|single| single.ending_path.is_some()) {
        api.require_output_tone()?;
        settings["tone_after_denoise"] = serde_json::json!(true);
    }
    // Retain the library's small fingerprint cache across isolated jobs, including
    // Vulkan hosts. The queue still releases reused model/GPU buffers when idle.
    static RUNTIME: std::sync::Mutex<Option<Api>> = std::sync::Mutex::new(None);
    *RUNTIME.lock().map_err(|_| "LCS runtime cache lock failed.")? = Some(api.clone());
    let started = Instant::now();
    let report = |completed, total| {
        (item.events)(NativeEvent::Progress(ProgressEvent {
            job_id: item.request.job_id.clone(), stage: "calibrating", step: completed, total_steps: total, planned_steps: 0,
            elapsed_seconds: started.elapsed().as_secs_f64(), frames: item.request.frames,
            canvas_width: item.request.canvas_width, canvas_height: item.request.canvas_height,
            reference_count: item.request.reference_count(), timing_profile: String::new(),
        }));
        !item.cancel.load(Ordering::Acquire)
    };
    super::lcs_fingerprint::check_cancel(&item.cancel)?;
    if let Some(calibration) = item.request.generation_effects.as_ref().and_then(|effects| effects.drift.as_ref())
        .filter(|drift| drift.enabled != Some(false) && drift.strength > 0.0 && drift.max > 0.0).and_then(|drift| drift.calibration.as_ref()) {
        // Native set_lcs snapshots and validates every tensor. Generation also
        // verifies the archive's VAE fingerprint against the actual loaded VAE.
        let path = tempfile::Builder::new().suffix(".safetensors").tempfile().map_err(|e| e.to_string())?.into_temp_path();
        std::fs::write(&path, calibration.bytes()?).map_err(|e| e.to_string())?;
        api.set_lcs(request, &path, &settings.to_string()).map_err(|e| format!("Cannot apply LCS Drift calibration: {e}"))?;
        return Ok(true);
    }
    let vae = item.configuration.models.iter().find(|(id, _, _)| *id == 3).and_then(|(_, _, path)| path.as_deref()).ok_or("Generation effects require a video VAE.")?;
    let log_path = PathBuf::from(crate::diagnostics::log_info()?.path);
    let directory = log_path.parent().ok_or("Cannot locate the generation effects cache.")?.join("lcs");
    std::fs::create_dir_all(&directory).map_err(|e| format!("Cannot create the generation effects cache: {e}"))?;
    let path = calibration_path(&directory, vae, &item.cancel, || { report(0, 0); })?;
    let settings = settings.to_string();
    // set_lcs snapshots the archive, so a valid cache needs no GPU calibration.
    if path.is_file() && api.set_lcs(request, &path, &settings).is_ok() { return Ok(true); }
    let calibration = RequestHandle::new(api)?;
    api.set_model(&calibration, 3, vae)?;
    api.set_inference_backend(&calibration, platform.backend())?;
    api.calibrate_lcs(&calibration, &path, &mut { report })?;
    if item.cancel.load(Ordering::Acquire) { return Err("Generation effects preparation cancelled.".into()); }
    api.set_lcs(request, &path, &settings)?;
    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn calibration_cache_tracks_vae_contents_and_cancellation() {
        let directory = tempfile::tempdir().unwrap();
        let vae = directory.path().join("vae");
        let cancel = AtomicBool::new(false);
        std::fs::write(&vae, b"first").unwrap();
        let first = calibration_path(directory.path(), &vae, &cancel, || {}).unwrap();
        assert_eq!(first, calibration_path(directory.path(), &vae, &cancel, || panic!("cached VAE must not report another calibration check")).unwrap());
        std::fs::write(&vae, b"other").unwrap();
        assert_ne!(first, calibration_path(directory.path(), &vae, &cancel, || {}).unwrap());
        cancel.store(true, Ordering::Release);
        assert!(calibration_path(directory.path(), &vae, &cancel, || {}).unwrap_err().contains("cancelled"));
    }
}