Skip to main content

sample

Function sample 

Source
pub fn sample<D: Denoiser>(
    denoiser: &mut D,
    scheduler: Scheduler,
    latents: Matrix,
    config: &SamplingConfig,
    on_step: impl FnMut(usize, &Matrix) -> bool,
) -> Result<Matrix, NetworkError>
Expand description

Runs the whole loop and returns the latents it ends on.

latents is the starting noise, which noise produces at the shape the model wants. The result is still a latent: turning it into pixels is the decoder’s job, and that is model-specific.

// A model that always points straight at a solid grey image.
struct Grey;
impl Denoiser for Grey {
    fn denoise(&mut self, latents: &Matrix, sigma: f32) -> Result<Matrix, NetworkError> {
        let velocity = latents.data.iter().map(|value| (value - 0.5) / sigma.max(1e-3));
        Ok(Matrix::from_vec(latents.rows, latents.cols, velocity.collect()))
    }
}

let start = Matrix::from_vec(1, 4, vec![2.0, -1.0, 0.0, 3.0]);
let config = SamplingConfig { steps: 50, ..SamplingConfig::default() };
let image = sample(&mut Grey, Scheduler::flow_match(1.0), start, &config, |_, _| true)?;
assert!(image.data.iter().all(|value| (value - 0.5).abs() < 0.05));