diff --git a/crates/metal2vulkan-validation/src/runtime_lane/half_conversions.rs b/crates/metal2vulkan-validation/src/runtime_lane/half_conversions.rs index 8445c24ec..c9bd89b16 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/half_conversions.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/half_conversions.rs @@ -171,7 +171,7 @@ fn result(kernel: &str, o: &[f32; 3]) -> f32 { /// Metal's `half(x)`: to nearest even on the `half` grid, `half` denormals kept, infinity from /// 65520 up (`x` not a NaN). -pub(super) fn half_of(x: f32) -> u16 { +fn half_of(x: f32) -> u16 { let sign = if x.is_sign_negative() { 0x8000 } else { 0 }; let a = f64::from(x.abs()); if a >= 65520.0 { diff --git a/crates/metal2vulkan-validation/src/runtime_lane/heap_extended_reads.rs b/crates/metal2vulkan-validation/src/runtime_lane/heap_extended_reads.rs index 71324049f..b54dfbcd4 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/heap_extended_reads.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/heap_extended_reads.rs @@ -38,14 +38,8 @@ //! `HEAP-SAMPLE-DIFFERS` line. A command whose function samples such a texture otherwise (a mip //! filter, a `half` gather, a runtime sampler with a mip filter) is refused at bind, printed on a //! `HEAP-SAMPLE-REFUSED` line. -//! -//! A `half` read through an argument buffer of every code of such a texture is the device's `float` -//! read of it rounded to the nearest even `half`, and the slot's `half` read, where the stage -//! records no `PrecisionNote::NativeHalfConversion`, which it does not where the profile takes -//! `DriverDeviation::MetalHalfConversion` ([`heap_half_reads_of_every_code_round_their_float`]). use super::degrade_notices::on_device; -use super::half_conversions; use super::host_memory::{not_importable, Mapping, GUEST_PAGE}; use super::Messages; use super::{buffer_with, read_buffer}; @@ -1133,113 +1127,3 @@ fn heap_rgbx_reads_are_the_slot_s() { assert_eq!(tally.differ, 0, "heap reads differ from the slot's"); }); } - -/// A 256 x 256 texture of `format` (texels of `bytes` bytes, one level) whose texel `n` holds -/// `n` in each of its 16-bit units: every code of each of the format's channels, whether a -/// channel is a 16-bit unit (a bfloat code, a 10-bit code in its 16-bit container) or a 10-bit -/// field of a packed 32-bit word (`BGR10_XR`'s, at bits 0, 10 and 20). -fn every_code(device: &Device, format: PixelFormat, bytes: u64) -> Result { - let mut descriptor = TextureDescriptor::texture_2d(format, 256, 256, false); - descriptor.usage = TextureUsage::SHADER_READ; - descriptor.resource_options = ResourceOptions::shared(); - let texture = device.new_texture(&descriptor)?; - let units = bytes / 2; - let texels: Vec = (0..=u16::MAX) - .flat_map(|n| (0..units).flat_map(move |_| n.to_le_bytes())) - .collect(); - texture - .replace_region( - Region::new_2d(0, 0, 256, 256), - 0, - 0, - &texels, - 256 * bytes, - 0, - ) - .unwrap(); - Ok(texture) -} - -/// A `half` read through an argument buffer of every code of each texture a heap holds by its raw -/// texels alone ([`RAW_HELD`], [`every_code`]) is the device's `float` read of the same texel -/// rounded to the nearest even `half` (`half_conversions::half_of`, Metal's `half(float)`), each -/// lane bit for bit (a NaN a NaN), and the slot's `half` read, where the heap read's stage records -/// no `PrecisionNote::NativeHalfConversion`: the device's conversion of the decoded texel's -/// `float` (`narrow::round_texel`). Where the profile takes `DriverDeviation::MetalHalfConversion` -/// (`VkProperty::MetalHalfConversion`; MoltenVK on the Apple M3 Max's macOS 27 identity) the -/// stage records no such note. A lane that differs where the stage records it is counted on a -/// `HEAP-HALF-ROUNDING` line. The padded formats, whose creation the runtime refuses, never reach -/// a read. -#[test] -fn heap_half_reads_of_every_code_round_their_float() { - use metal2vulkan::device::VkProperty; - on_device(|device, _| { - let measured = device - .compile_device() - .profile() - .enabled() - .properties - .contains(&VkProperty::MetalHalfConversion); - let notes = stage_notes(device, true, Texel::Half); - let noted = notes.contains(&PrecisionNote::NativeHalfConversion); - assert!( - !measured || !noted, - "NativeHalfConversion recorded where the profile takes MetalHalfConversion: {notes:?}" - ); - let lanes: Vec<[u32; 4]> = (0..256u32 * 256) - .map(|i| [i % 256, i / 256, 0, 0]) - .collect(); - let float_heap = pipeline(device, true, Texel::Float); - let half_heap = pipeline(device, true, Texel::Half); - let half_slot = pipeline(device, false, Texel::Half); - let mut checked = 0; - for (format, bytes) in RAW_HELD { - let texture = - every_code(device, format, bytes).unwrap_or_else(|e| panic!("{format:?}: {e}")); - let read = |state: &ComputePipelineState, heap: bool| { - dispatch_with(device, (state, heap), (&texture, None), &lanes) - .unwrap_or_else(|e| panic!("{format:?}: {e}")) - }; - let (floats, halves, slot) = ( - read(&float_heap, true), - read(&half_heap, true), - read(&half_slot, false), - ); - let mut differ = 0; - for (i, ((f, h), s)) in floats.iter().zip(&halves).zip(&slot).enumerate() { - for c in 0..4 { - let (f, h) = (f32::from_bits(f[c]), f32::from_bits(h[c])); - let same = match f.is_nan() { - true => h.is_nan(), - false => { - !h.is_nan() - && half_conversions::half_of(h) == half_conversions::half_of(f) - } - }; - let slot_same = - s[c] == f32::to_bits(h) || (h.is_nan() && f32::from_bits(s[c]).is_nan()); - if same && slot_same { - continue; - } - assert!( - noted, - "{format:?} texel {i} channel {c}: heap half {h:?} ({:#010x}), its float \ - {f:?} ({:#010x}), the slot's half {:#010x} (notes {notes:?})", - h.to_bits(), - f.to_bits(), - s[c] - ); - differ += 1; - } - } - checked += 4 * lanes.len(); - println!( - "HEAP-HALF-ROUNDING {} {format:?}: {differ} of {} channels not the float's nearest \ - even half, note {noted}", - device.name(), - 4 * lanes.len() - ); - } - assert!(checked > 0); - }); -} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index ce436ad67..c03df2b64 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 1964; +pub const SEMANTICS_VERSION: u32 = 1965; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/device/driver_facts.rs b/crates/metal2vulkan/src/device/driver_facts.rs index 000ef7f43..219f6ebb3 100644 --- a/crates/metal2vulkan/src/device/driver_facts.rs +++ b/crates/metal2vulkan/src/device/driver_facts.rs @@ -453,14 +453,7 @@ pub(crate) fn among( /// it: Metal's `half` `tanh`, which answers 7 of the 65,536 `half`s otherwise than the nearest /// even `half` of its `float` `tanh`; `exp`, `sqrt` and a reciprocal on `half` matched, but each is /// a function or an operation computed otherwise than by the conversion, so it stays noted -/// (`narrow::rounds_unmeasured`). A texel the module decodes itself rounds by the same conversion -/// of the `float` it holds (`narrow::round_texel`): the gate lane -/// `runtime_lane::heap_extended_reads::heap_half_reads_of_every_code_round_their_float` reads every -/// code of the five forms a heap holds by its raw texels that the runtime makes, as `half` through -/// an argument buffer, against the nearest even `half` of the same read as `float` and the slot's -/// `half` read: on the M3 Max under macOS 27.0.1 (2026-11-05), none of the 262,144 channels of -/// `BGRA10_XR`, `BGR10_XR`, `R16Bfloat`, `RG16Bfloat` and `RGBA16Bfloat` each differs. One row per -/// GPU, as measured. +/// (`narrow::rounds_unmeasured`). One row per GPU, as measured. /// /// `MetalSmallFloatNans`: MoltenVK 1.4.2 hands a storage image write to Metal's own texture /// write. On the M3 Max under macOS 27.0.1 (2026-11-03), byte for byte against plain Metal, a diff --git a/crates/metal2vulkan/src/device/requirements.rs b/crates/metal2vulkan/src/device/requirements.rs index 5ccd25402..a227b880d 100644 --- a/crates/metal2vulkan/src/device/requirements.rs +++ b/crates/metal2vulkan/src/device/requirements.rs @@ -855,8 +855,7 @@ pub enum VkProperty { /// [`DriverDeviation::MetalHalfConversion`](crate::error::DriverDeviation::MetalHalfConversion)). /// No Vulkan query reports it: without the float controls Vulkan leaves the conversion's /// rounding to the device. A module that rounds a `half` through that conversion in those - /// forms alone (a texel's value among them: one the module decodes itself, or a merge of - /// texels) records no + /// forms alone records no /// [`PrecisionNote::NativeHalfConversion`](crate::pipeline::PrecisionNote::NativeHalfConversion) /// and requires it. MetalHalfConversion, diff --git a/crates/metal2vulkan/src/error/mod.rs b/crates/metal2vulkan/src/error/mod.rs index af9c0c50e..ee9223a10 100644 --- a/crates/metal2vulkan/src/error/mod.rs +++ b/crates/metal2vulkan/src/error/mod.rs @@ -857,10 +857,9 @@ pub enum DriverDeviation { /// where the `float` is an image instruction's texel, Metal's own `half` result of that access, /// into which Metal's compiler folds the conversion. Where the profile takes it, a module whose /// every `half` rounding through that conversion (`QuantizeForm::Converted`) is a conversion of - /// a `float` the function holds (an AIR `fptrunc` or `air.convert` from `float`) or of a - /// texel's value (an image instruction's texel, one the module decodes itself, or a merge of - /// them, whose `half` the access's own texel notes state against Metal's) records no - /// `PrecisionNote::NativeHalfConversion` and requires the property. A `half` a function or an operation computes in `float` and rounds + /// a `float` the function holds (an AIR `fptrunc` or `air.convert` from `float`) or of a texel + /// an image instruction hands back records no `PrecisionNote::NativeHalfConversion` and + /// requires the property. A `half` a function or an operation computes in `float` and rounds /// keeps the note on every profile: Metal computes it otherwise (its `half` `tanh` answers 7 /// of the 65,536 `half`s otherwise than the nearest even `half` of its `float` `tanh`, which the /// device's conversion answers). Unmeasured, each records it. diff --git a/crates/metal2vulkan/src/lower/arith/narrow.rs b/crates/metal2vulkan/src/lower/arith/narrow.rs index 955668b9d..23f737bc3 100644 --- a/crates/metal2vulkan/src/lower/arith/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/narrow.rs @@ -182,8 +182,7 @@ fn converted_pair(fb: &mut FunctionBuilder<'_>, wide: Form, x: Value) -> BResult } /// [`round`] of a `float` the function holds to `half` (an AIR `fptrunc` or `air.convert` of a -/// `float`, [`resize`], and a texel's value, [`round_texel`]): the conversion Metal's compiler -/// compiles from the AIR's own, which +/// `float`, [`resize`]): the conversion Metal's compiler compiles from the AIR's own, which /// `DriverDeviation::MetalHalfConversion` measures to be Metal's `half(float)` whatever computed /// the `float` ([`rounds_unmeasured`]). In [`QuantizeForm::Converted`] the pair's narrowing is /// recorded as measured (`ModuleBuilder::note_measured_rounding`); every other form rounds as @@ -333,8 +332,8 @@ pub fn rounds_by_conversion(m: &TypedModule, meter: &mut Meter) -> Result u32 { /// under the float controls ([`QuantizeForm::Controlled`], `ModuleBuilder::native_half_nans`): the /// conversion keeps a NaN a NaN, its bits the device's, as the texel's decode is /// (`PrecisionNote::DeviceNanEncoding`, which the access records). Another form is [`round`]ed. -/// -/// A `half` texel rounds as [`round_measured`] rounds a `float` the function holds: in -/// [`QuantizeForm::Converted`] the pair's narrowing is recorded as measured -/// (`ModuleBuilder::note_measured_rounding`), the device's conversion of the texel's `f32` value, -/// which `DriverDeviation::MetalHalfConversion` measures to be Metal's `half(float)` whatever -/// computed it, so the module records no `PrecisionNote::NativeHalfConversion` for it there. That -/// holds for a texel the module decodes itself (`extended_range::decode`) and for a merge of texels -/// (a heap read's phi of its fetch and its decode routine's texel) as for an image instruction's. -/// Where Metal's `half` texel is not that rounding of the texel's value, the access records its -/// own note: `PrecisionNote::HalfTexelConversion` (by the bound class at a slot; outright for a -/// heap element and a depth texture) or, for a decoded texel, `PrecisionNote::DevicePackConversion` -/// (whose bound states the `half`'s narrowing against Metal's rounding with ties away from zero). pub fn round_texel(fb: &mut FunctionBuilder<'_>, f: Form, x: Value) -> BResult { let native = fb.module().native_half(); match f.held { - Held::Float(FloatFmt::Half) if native || fb.module().native_half_nans() => { - round_measured(fb, f, x) - } + Held::Float(FloatFmt::Half) if native || fb.module().native_half_nans() => round(fb, f, x), Held::Float(FloatFmt::Half) => { - let r = round_measured(fb, f, x)?; + let r = round(fb, f, x)?; let u = u32_form(f); let ut = ty(fb, u)?; let b = fb.convert(ConvOp::Bitcast, x, ut)?; diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 01bfbc6d5..0e1dc7ab4 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1778,17 +1778,12 @@ pub enum PrecisionNote { /// [`DriverDeviation::MetalHalfConversion`](crate::error::DriverDeviation::MetalHalfConversion) /// (MoltenVK on the Apple M3 Max's macOS 27 identity) where every `half` the module rounds /// through the conversion is a conversion of a `float` the function holds (an AIR `fptrunc` or - /// `air.convert` from `float`, whatever computed the `float`) or of a texel's value, measured to - /// answer Metal's `half(float)` there; the module then requires - /// `VkProperty::MetalHalfConversion`. A texel's value is an image instruction's texel, a texel - /// the module decodes itself (an extended-range, padded or bfloat code) or a merge of texels (a - /// heap read's fetched texel and its decode routine's): where Metal's `half` texel is not that - /// rounding of the value, the access records its own note ([`PrecisionNote::HalfTexelConversion`], - /// outright for a heap element; [`PrecisionNote::DevicePackConversion`] for a decoded texel). A - /// `half` operation or function the module computes in `float` and rounds keeps this note - /// there (Metal's `half` `tanh` answers 7 of the 65,536 `half`s otherwise than the conversion - /// of its `float` `tanh`), as does every other rounding - /// (`lower::arith::narrow::rounds_unmeasured`). + /// `air.convert` from `float`, whatever computed the `float`) or of a texel an image instruction + /// hands back, measured to answer Metal's there; the module then requires + /// `VkProperty::MetalHalfConversion`. A `half` operation or function the module computes in + /// `float` and rounds keeps it there too (Metal's `half` `tanh` answers 7 of the 65,536 + /// `half`s otherwise than the conversion of its `float` `tanh`), as do a texel the module + /// decodes itself and every other rounding (`lower::arith::narrow::rounds_unmeasured`). /// /// Every `half` a module reads from its bits (a load, a reinterpretation, a texel's bits) is /// the device's widening of them, on every device: an `OpFConvert` to 32 bits of the bits read diff --git a/crates/metal2vulkan/src/tests/engine_extended_range.rs b/crates/metal2vulkan/src/tests/engine_extended_range.rs index 678d2037d..c278c1d35 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range.rs @@ -1099,67 +1099,3 @@ fn ycbcr_write_slots_store_each_texel_s_bits_and_mark() { } } } - -/// A `half` read of a slot bound in an extended-range form decodes the texel's codes and rounds -/// the decoded `f32` through the device's conversion (`narrow::round_texel`). On `moltenvk-m3`, -/// which takes `DriverDeviation::MetalHalfConversion`, the module records no -/// `NativeHalfConversion` for it and requires `VkProperty::MetalHalfConversion`; it records -/// `DevicePackConversion`, which states the decoded `half` against Metal's. Without the deviation -/// (the same profile with it dropped) it records the note and requires nothing. -#[test] -fn decoded_half_texels_round_by_the_measured_conversion() { - use crate::device::requirements::VkProperty; - use crate::error::DriverDeviation; - use crate::pipeline::artifact::PrecisionNote; - let Some(bc) = assemble("decoded-halves", &kernel(Source::Read, true, 0)) else { - return; - }; - let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../metal2vulkan-validation/profiles/moltenvk-m3.json"); - let text = std::fs::read_to_string(&path).expect("profile"); - for form in FORMS { - let i = inputs(form, Source::Read); - for taken in [true, false] { - let desc = DeviceDescription::from_khronos_json(&text, None).expect("description"); - let p = Device::with_deviation( - desc, - DevicePolicy::default(), - DriverDeviation::MetalHalfConversion, - taken, - ) - .expect("device") - .profile() - .clone(); - let what = format!("{form:?}, the deviation taken: {taken}"); - let a = compile_bitcode( - &bc, - EntryRef::Name("k"), - &FunctionConstantValues::defaults(), - &p, - &i, - &mut meter(), - SPIRV_WORDS, - ) - .unwrap_or_else(|e| panic!("{what}: {e}")); - link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{what}: L3: {e}")); - assert_eq!( - a.precision.contains(&PrecisionNote::NativeHalfConversion), - !taken, - "{what}: {:?}", - a.precision - ); - assert!( - a.precision.contains(&PrecisionNote::DevicePackConversion), - "{what}: {:?}", - a.precision - ); - assert_eq!( - a.requirements - .properties - .contains(&VkProperty::MetalHalfConversion), - taken, - "{what}" - ); - } - } -} diff --git a/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs b/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs index 191ae976f..ffdb8b8e8 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs @@ -373,84 +373,3 @@ fn heap_samples_reach_a_raw_member_through_the_unsigned_twin() { ); } } - -/// A `half` read, nearest sample or gather of a texture an argument buffer holds rounds its texel -/// through the device's conversion of the texel's `f32` value (`narrow::round_texel`): the -/// fetched texel's, merged with the decode routine's texel of a form a heap reads. On -/// `moltenvk-m3`, which takes `DriverDeviation::MetalHalfConversion`, the module records no -/// `NativeHalfConversion` for it and requires `VkProperty::MetalHalfConversion`, and it records -/// `HalfTexelConversion`, which states a heap element's `half` against Metal's; without the -/// deviation (the same profile with it dropped) it records the note and requires nothing. A -/// linear sample's filter sum is computed by the device's arithmetic and rounded by -/// `narrow::round`, which keeps the note on both. -#[test] -fn heap_half_texels_round_by_the_measured_conversion() { - use crate::device::requirements::VkProperty; - use crate::error::DriverDeviation; - use crate::pipeline::PrecisionNote; - let consts = FunctionConstantValues::defaults(); - let entry = EntryRef::Name("k"); - let inputs = LowerInputs::new(); - // (what, the kernel, whether every rounding is a texel's). - let cases = [ - ("read", kernel(Use::Half), true), - ( - "nearest sample", - sampling_kernel(Sampling::HalfSample(words(false, false))), - true, - ), - ( - "gather", - sampling_kernel(Sampling::HalfGather(words(false, false))), - true, - ), - ( - "linear sample", - sampling_kernel(Sampling::HalfSample(words(true, false))), - false, - ), - ]; - let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../metal2vulkan-validation/profiles/moltenvk-m3.json"); - let text = std::fs::read_to_string(&path).expect("profile"); - for (name, source, texels) in &cases { - let Some(bc) = assemble("heap_half", source) else { - return; - }; - for taken in [true, false] { - let desc = DeviceDescription::from_khronos_json(&text, None).expect("description"); - let p = Device::with_deviation( - desc, - DevicePolicy::default(), - DriverDeviation::MetalHalfConversion, - taken, - ) - .expect("device") - .profile() - .clone(); - let what = format!("{name}, the deviation taken: {taken}"); - let a = compile_bitcode(&bc, entry, &consts, &p, &inputs, &mut meter(), SPIRV_WORDS) - .unwrap_or_else(|e| panic!("{what}: {e}")); - link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{what}: L3: {e}")); - let exempt = taken && *texels; - assert_eq!( - a.precision.contains(&PrecisionNote::NativeHalfConversion), - !exempt, - "{what}: {:?}", - a.precision - ); - assert!( - a.precision.contains(&PrecisionNote::HalfTexelConversion), - "{what}: {:?}", - a.precision - ); - assert_eq!( - a.requirements - .properties - .contains(&VkProperty::MetalHalfConversion), - exempt, - "{what}" - ); - } - } -} diff --git a/docs/API.md b/docs/API.md index 260e14e9e..585935e4a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3618,15 +3618,12 @@ bound. zero of its sign. **Exempt:** on a profile that takes `DriverDeviation::MetalHalfConversion`, MoltenVK on Apple's GPUs of the Apple M3 Max's macOS 27 identity, a module whose every `half` rounded through the conversion converts a `float` the function holds (an AIR `fptrunc` or - `air.convert` from `float`) or a texel's value records no note for that rounding and requires - `VkProperty::MetalHalfConversion`: measured word for word against plain Metal, with and without - fast math, over 1,173,760 `float`s loaded, summed, multiplied, divided and fused, an integer's - quotient, and every texel of ten formats read, sampled and gathered as `half`. A texel's value - includes one the module decodes itself (an extended-range or bfloat code) and a heap read's merge - of its fetched texel and its decode routine's; where Metal's `half` texel is not that rounding of - the value the access records `half-texel-conversion` (outright for an argument buffer's texture) - or `device-pack-conversion` (a decoded texel). A `half` operation or function computed in `float` - and rounded keeps the note (Metal's `half` `tanh` answers 7 of the 65,536 `half`s otherwise). + `air.convert` from `float`) or an image instruction's texel records no note for that rounding and + requires `VkProperty::MetalHalfConversion`: measured word for word against plain Metal, with and + without fast math, over 1,173,760 `float`s loaded, summed, multiplied, divided and fused, an + integer's quotient, and every texel of ten formats read, sampled and gathered as `half`. A `half` + operation or function computed in `float` and rounded keeps the note (Metal's `half` `tanh` + answers 7 of the 65,536 `half`s otherwise). - `metal2vulkan-degrade-native-half-quantize` (precision). `PrecisionNote::NativeHalfQuantize`: Native half rounding (a quantize): the module rounds a `half` result it computes in `float` (a `half` operation's, a function's, a conversion from `float`'s, a texel's) by the device's