Skip to main content

cpu_scope

Function cpu_scope 

Source
pub fn cpu_scope<R>(f: impl FnOnce() -> R) -> R
Expand description

Run f with the GPU gates off on this thread (pure-CPU arm).

Examples found in repository?
examples/qwen_image_denoiser_metal_fixture.rs (lines 361-369)
354fn run(
355    transformer: &QwenImageTransformer,
356    fixture: &Fixture,
357    cpu: bool,
358) -> Result<(Vec<f32>, Duration), String> {
359    let start = Instant::now();
360    let output = if cpu {
361        gpu::cpu_scope(|| {
362            transformer.forward(
363                &fixture.image,
364                &fixture.text,
365                &fixture.shapes,
366                fixture.text_len,
367                fixture.timestep,
368            )
369        })?
370    } else {
371        transformer.forward(
372            &fixture.image,
373            &fixture.text,
374            &fixture.shapes,
375            fixture.text_len,
376            fixture.timestep,
377        )?
378    };
379    Ok((output, start.elapsed()))
380}
More examples
Hide additional examples
examples/gemmbench.rs (lines 34-36)
12fn main() {
13    unsafe { std::env::set_var("CMF_GPU", "1") };
14    let cases = [
15        (384usize, 4096usize, 128usize),
16        (384, 128, 4096),
17        (384, 16384, 128),
18        (384, 128, 16384),
19    ];
20    for (n, k, m) in cases {
21        let x = vec![0.01f32; n * k];
22        let w = vec![0.02f32; m * k];
23        let mut y = vec![0f32; n * m];
24        // warm
25        cortiq_engine::fcd_ops::gemm_nt(&x, &w, &mut y, n, k, m, None);
26        let t = std::time::Instant::now();
27        for _ in 0..10 {
28            cortiq_engine::fcd_ops::gemm_nt(&x, &w, &mut y, n, k, m, None);
29        }
30        let d = t.elapsed().as_secs_f64() / 10.0;
31        let g = 2.0 * n as f64 * k as f64 * m as f64 / d / 1e9;
32        let t2 = std::time::Instant::now();
33        for _ in 0..10 {
34            cortiq_engine::gpu::cpu_scope(|| {
35                cortiq_engine::fcd_ops::gemm_nt(&x, &w, &mut y, n, k, m, None)
36            });
37        }
38        let d2 = t2.elapsed().as_secs_f64() / 10.0;
39        let g2 = 2.0 * n as f64 * k as f64 * m as f64 / d2 / 1e9;
40        println!(
41            "{n}x{k}x{m}:  default {:.2} ms ({g:.0} GF/s)   cpu_scope {:.2} ms ({g2:.0} GF/s)",
42            d * 1e3,
43            d2 * 1e3
44        );
45    }
46}