Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / examples / probe_int_dot.rs
probe_int_dot.rs70 lines · 3.2 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 | //! ``` |
| 7 | use std::ffi::CStr; |
| 8 | |
| 9 | use ash::vk; |
| 10 | use modelrunner::vulkan::context; |
| 11 | |
| 12 | fn 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!("4x8 packed accelerated: signed {} unsigned {} mixed {}", |
| 45 | dot.integer_dot_product4x8_bit_packed_signed_accelerated, |
| 46 | dot.integer_dot_product4x8_bit_packed_unsigned_accelerated, |
| 47 | dot.integer_dot_product4x8_bit_packed_mixed_signedness_accelerated); |
| 48 | println!("acc-sat 4x8 packed accelerated: signed {} mixed {}", |
| 49 | dot.integer_dot_product_accumulating_saturating4x8_bit_packed_signed_accelerated, |
| 50 | dot.integer_dot_product_accumulating_saturating4x8_bit_packed_mixed_signedness_accelerated); |
| 51 | |
| 52 | if !names.iter().any(|n| n == "VK_KHR_cooperative_matrix") { |
| 53 | return; |
| 54 | } |
| 55 | let coop = ash::khr::cooperative_matrix::Instance::new(&context.entry, instance); |
| 56 | // SAFETY: the extension is present, so the instance-level entry point resolves. |
| 57 | match unsafe { coop.get_physical_device_cooperative_matrix_properties(physical) } { |
| 58 | Ok(shapes) => { |
| 59 | println!("cooperative matrix shapes ({}):", shapes.len()); |
| 60 | for s in shapes { |
| 61 | println!(" M {} N {} K {} A {} B {} C {} R {} sat {} scope {}", |
| 62 | s.m_size, s.n_size, s.k_size, s.a_type.as_raw(), s.b_type.as_raw(), |
| 63 | s.c_type.as_raw(), s.result_type.as_raw(), s.saturating_accumulation, |
| 64 | s.scope.as_raw()); |
| 65 | } |
| 66 | println!(" (component types: 0 f16, 1 f32, 3 s8, 5 s32, 7 u8, 9 u32; scope 3 subgroup)"); |
| 67 | } |
| 68 | Err(e) => println!("cooperative matrix query failed: {}", e.as_raw()), |
| 69 | } |
| 70 | } |