Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / ml / src / main / rust / examples / probe_int_dot.rs

probe_int_dot.rs84 lines · 3.4 KiBRaw
1//! Reports what the GPU offers for int8 matrix work: `VK_KHR_shader_integer_dot_product`'s
2//! acceleration flags and every `VK_KHR_cooperative_matrix` shape.
3//!
4//! ```text
5//! cargo run --offline -p modelrunner --example probe_int_dot
6//! ```
7use std::ffi::CStr;
8
9use ash::vk;
10use modelrunner::vulkan::context;
11
12fn main() {
13 let context = match context::shared() {
14 Ok(context) => context,
15 Err(why) => {
16 println!("no usable Vulkan device: {why}");
17 return;
18 }
19 };
20 let (instance, physical) = (&context.instance, context.physical_device);
21 // SAFETY: plain queries on the physical device the context holds alive.
22 let names: Vec<String> = unsafe { instance.enumerate_device_extension_properties(physical) }
23 .unwrap_or_default()
24 .iter()
25 .map(|e| {
26 // SAFETY: the driver NUL-terminates extension names inside the fixed array.
27 unsafe { CStr::from_ptr(e.extension_name.as_ptr()) }.to_string_lossy().into_owned()
28 })
29 .collect();
30 for wanted in [
31 "VK_KHR_shader_integer_dot_product",
32 "VK_KHR_cooperative_matrix",
33 "VK_KHR_8bit_storage",
34 "VK_KHR_shader_float16_int8",
35 ] {
36 println!("{wanted:40} {}", names.iter().any(|n| n == wanted));
37 }
38 println!("runtime enabled int8 dot: {}", context.int_dot);
39
40 let mut dot = vk::PhysicalDeviceShaderIntegerDotProductProperties::default();
41 let mut properties = vk::PhysicalDeviceProperties2::default().push_next(&mut dot);
42 // SAFETY: as above; an unknown chained struct is left zeroed by a driver without it.
43 unsafe { instance.get_physical_device_properties2(physical, &mut properties) };
44 println!(
45 "4x8 packed accelerated: signed {} unsigned {} mixed {}",
46 dot.integer_dot_product4x8_bit_packed_signed_accelerated,
47 dot.integer_dot_product4x8_bit_packed_unsigned_accelerated,
48 dot.integer_dot_product4x8_bit_packed_mixed_signedness_accelerated
49 );
50 println!(
51 "acc-sat 4x8 packed accelerated: signed {} mixed {}",
52 dot.integer_dot_product_accumulating_saturating4x8_bit_packed_signed_accelerated,
53 dot.integer_dot_product_accumulating_saturating4x8_bit_packed_mixed_signedness_accelerated
54 );
55
56 if !names.iter().any(|n| n == "VK_KHR_cooperative_matrix") {
57 return;
58 }
59 let coop = ash::khr::cooperative_matrix::Instance::new(&context.entry, instance);
60 // SAFETY: the extension is present, so the instance-level entry point resolves.
61 match unsafe { coop.get_physical_device_cooperative_matrix_properties(physical) } {
62 Ok(shapes) => {
63 println!("cooperative matrix shapes ({}):", shapes.len());
64 for s in shapes {
65 println!(
66 " M {} N {} K {} A {} B {} C {} R {} sat {} scope {}",
67 s.m_size,
68 s.n_size,
69 s.k_size,
70 s.a_type.as_raw(),
71 s.b_type.as_raw(),
72 s.c_type.as_raw(),
73 s.result_type.as_raw(),
74 s.saturating_accumulation,
75 s.scope.as_raw()
76 );
77 }
78 println!(
79 " (component types: 0 f16, 1 f32, 3 s8, 5 s32, 7 u8, 9 u32; scope 3 subgroup)"
80 );
81 }
82 Err(e) => println!("cooperative matrix query failed: {}", e.as_raw()),
83 }
84}