From f1def3dffef04072b540cd19718649259d38368f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 5 Nov 2026 03:09:24 -0800 Subject: [PATCH] Count a texel's half rounding as the measured conversion on MetalHalfConversion profiles Re-applies 384734851d (reverted in the stack by 5dbf6f3be3) on top of the previous commit, which merges a heap half read's class arms as the texel's half bits. What: narrow::round_texel rounds through round_measured, so in QuantizeForm::Converted the conversion pair's narrowing is recorded as measured. On a profile that takes DriverDeviation::MetalHalfConversion (MoltenVK on the Apple M3 Max's macOS 27 identity) a texel's half rounding records no PrecisionNote::NativeHalfConversion, and the module requires VkProperty::MetalHalfConversion instead: a texel the module decodes itself (extended_range::decode), a merge of texels (a heap read's phi of its fetched texel and its decode routine's) and an image instruction's. The note stays for roundings by narrow::round: half operations and functions computed in float, filter sums, packs and the rest. Other profiles never read the measured set, so they are unchanged by construction, and round_measured emits the same pair, so the SPIR-V is unchanged. SEMANTICS_VERSION 1967: recorded notes change on moltenvk-m3. Why: reims-vgpu's strict Mac tests die on native-half-conversion at a kernel that reads an argument buffer's texture as half (their ycbcr_probe.metal read_held, over BGRA8Unorm). Every heap half read carries the decode routine of the seven extended-range/padded/bfloat forms, and its merge, so it recorded the note whatever the texture. MetalHalfConversion measured the device's conversion as Metal's half(float) for loaded and computed floats; where Metal's half texel is not the nearest even half of the texel's value, the access's own note states it: HalfTexelConversion (outright for a heap element, by the bound class at a slot) or DevicePackConversion (a decoded texel). The stack 28 gate's cross-class lane failure that reverted it came from the heap read's merge of widened halves, not from a rounding: an integer texel's bits read as half go through no rounding, and the previous commit merges them as integers, so they keep their bits without a note. Kept from 384734851d: the core tests heap_half_texels_round_by_the_measured_conversion and decoded_half_texels_round_by_the_measured_conversion, the M3 lane heap_extended_reads::heap_half_reads_of_every_code_round_their_float (half_conversions::half_of made pub(super) for it), and the docs: the PrecisionNote, DriverDeviation and VkProperty rustdoc, API.md's native-half-conversion exemption, the driver-facts record (which now also says the argument buffer's cross-class half read rounds nothing and names its lane), narrow's docs. runtime-sources.sha256 moves for the doc edits in driver_facts.rs and requirements.rs, which change no behaviour of a run. Verified: pending in this commit; the next jobs run the core and M3 lane checks on it. (cherry picked from commit 1757bd7866aa87c4e38ab83f0b4c4d83e68c75bb) --- .../src/runtime_lane/half_conversions.rs | 2 +- .../src/runtime_lane/heap_extended_reads.rs | 116 ++++++++++++++++++ crates/metal2vulkan/src/cache/mod.rs | 2 +- .../metal2vulkan/src/device/driver_facts.rs | 11 +- .../metal2vulkan/src/device/requirements.rs | 3 +- crates/metal2vulkan/src/error/mod.rs | 8 +- crates/metal2vulkan/src/lower/arith/narrow.rs | 25 +++- crates/metal2vulkan/src/pipeline/artifact.rs | 17 ++- .../src/tests/engine_extended_range.rs | 64 ++++++++++ .../src/tests/engine_extended_range/heap.rs | 81 ++++++++++++ docs/API.md | 15 ++- 11 files changed, 320 insertions(+), 24 deletions(-) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/half_conversions.rs b/crates/metal2vulkan-validation/src/runtime_lane/half_conversions.rs index c9bd89b16..8445c24ec 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). -fn half_of(x: f32) -> u16 { +pub(super) 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 b54dfbcd4..71324049f 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/heap_extended_reads.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/heap_extended_reads.rs @@ -38,8 +38,14 @@ //! `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}; @@ -1127,3 +1133,113 @@ 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 abb44f4ff..0ba53eead 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 = 1966; +pub const SEMANTICS_VERSION: u32 = 1967; /// 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 219f6ebb3..de4fa10ac 100644 --- a/crates/metal2vulkan/src/device/driver_facts.rs +++ b/crates/metal2vulkan/src/device/driver_facts.rs @@ -453,7 +453,16 @@ 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`). One row per GPU, as measured. +/// (`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. An argument buffer's `half` read of a member of another numeric class rounds +/// nothing: its arms merge the texel's `half` bits as integers (`lower::texture::emit`'s +/// `read_crossed`), widened once after the merge as a slot's read widens them, which the gate lane +/// `runtime_lane::cross_class_reads::argument_buffer_cross_class_reads_are_the_native_words` reads +/// against the slot's words. 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 a227b880d..5ccd25402 100644 --- a/crates/metal2vulkan/src/device/requirements.rs +++ b/crates/metal2vulkan/src/device/requirements.rs @@ -855,7 +855,8 @@ 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 records no + /// forms alone (a texel's value among them: one the module decodes itself, or a merge of + /// texels) 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 ee9223a10..76ae151a9 100644 --- a/crates/metal2vulkan/src/error/mod.rs +++ b/crates/metal2vulkan/src/error/mod.rs @@ -857,9 +857,11 @@ 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 - /// 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 + /// 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 /// 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 23f737bc3..955668b9d 100644 --- a/crates/metal2vulkan/src/lower/arith/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/narrow.rs @@ -182,7 +182,8 @@ 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`]): the conversion Metal's compiler compiles from the AIR's own, which +/// `float`, [`resize`], and a texel's value, [`round_texel`]): 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 @@ -332,8 +333,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(fb, f, x), + Held::Float(FloatFmt::Half) if native || fb.module().native_half_nans() => { + round_measured(fb, f, x) + } Held::Float(FloatFmt::Half) => { - let r = round(fb, f, x)?; + let r = round_measured(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 0e1dc7ab4..01bfbc6d5 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1778,12 +1778,17 @@ 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 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`). + /// `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`). /// /// 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 c278c1d35..678d2037d 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range.rs @@ -1099,3 +1099,67 @@ 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 ffdb8b8e8..191ae976f 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range/heap.rs @@ -373,3 +373,84 @@ 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 585935e4a..260e14e9e 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3618,12 +3618,15 @@ 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 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). + `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). - `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