Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/experiments/direct-sdwa-profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ SDWA validation is per operand, mirroring
`place_subword` destination placement; no modifier or subword selection is
silently ignored.

Still refused in SDWA: `v_sin_f16`/`v_cos_f16` (no direct lowering),
`v_fmac_f32`/`v_fmac_f16` and the rest of `VOP2_SDWA_UNSUPPORTED_OPS`,
`v_sin_f16` and `v_cos_f16` are also admitted with a 16-bit destination: #3300 lowered them, SDWA included
(#3547). Still refused in SDWA: `v_fmac_f32`/`v_fmac_f16` and the rest of `VOP2_SDWA_UNSUPPORTED_OPS`,
64-bit `_f64`/`_b64` forms, and VOP3-only ops (`v_fma_f16`, `v_min3_f16`,
`v_med3_f16`, `v_cvt_pknorm_*`).

Expand All @@ -52,11 +52,12 @@ that moves the binary16 rounding or denormal bits is still refused.
## Checks

The [SDWA profiles fixture](../../tests/native_sdwa_profiles_check.py) gains
a second and third program (12, 28 and 6 slots, each under the 32-slot
hardware layout) with an independent Python/ISA reference model, plus 15
a second and third program (12, 28 and 8 slots, each under the 32-slot
hardware layout) with an independent Python/ISA reference model, plus 14
refusal controls: disallowed VOP2 SDWA ops, float/int modifier violations,
omod/clamp on integer destinations, invalid 16-bit destination selection and
unused mode, a byte source pick, literal 255, and `v_sin_f16`. The new
unused mode, a byte source pick and literal 255. `v_sin_f16` was a refusal control here until #3547; it is
now one of the admitted profiles of the third program, as is `v_cos_f16`. The new
profiles also pin the `v_frexp_*_f32` SDWA source pick (a WORD_1 pick reads
only the high half, so the picked value is a subnormal) and destination
placement (a WORD destination keeps only the result's low half, with the
Expand Down
4 changes: 3 additions & 1 deletion tests/native_cvt_f16_i16_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ def refusal_programs():
programs = []
for op, (vop1_op, vop3_op, _, to_f16) in OPS.items():
programs.append(([vop1(vop1_op, 3, 250), 0xFF00B101, 0xBF810000], 'unsupported DPP arithmetic'))
programs.append(([vop1(vop1_op, 3, 249), 0x06060601, 0xBF810000], 'unimplemented direct SDWA operand profile'))
# SDWA is an admitted profile for these ops (#3470; positive cases in native_sdwa_profiles_check.py
# NEW_PROFILES). dst_select 6 is an f32 placement, which a 16-bit destination refuses (#3547).
programs.append(([vop1(vop1_op, 3, 249), 0x06060601, 0xBF810000], 'unsupported SDWA destination modifier'))
programs.append((vop3_unary(vop3_op, 3, 257, omod=1) + [0xBF810000], f'unsupported {op} clamp or omod'))
programs.append((vop3_unary(vop3_op, 3, 257, op_sel=1) + [0xBF810000], f'unsupported {op} high-half op_sel'))
programs.append((vop3_unary(vop3_op, 3, 257, op_sel=8) + [0xBF810000], f'unsupported {op} high-half op_sel'))
Expand Down
25 changes: 24 additions & 1 deletion tests/native_sdwa_profiles_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path

import native_f16_alu_check as fixture
import native_sin_cos_f16_check as trig
from native_direct_container_check import patched_pixel
from native_dpp_quad_check import request
from native_sdwa_f16_check import SCALAR
Expand Down Expand Up @@ -134,6 +135,11 @@ def profile(name, opcode, unary, a, b=0, src0=DWORD, src1=None, dst=DWORD, unuse
reg1=242, scalar1=True),
profile("v_ldexp_f16", 0x3B, False, 0x00003C00, 0, src0=WORD_0, src1=WORD_1, dst=WORD_0,
reg1=242, scalar1=True),
# v_sin_f16 and v_cos_f16 are admitted with a 16-bit destination (#3300, #3547). sin is odd, so dropping the
# ABS on the picked -0.125 flips the sign of 0.7071; the other half (0.25 turns) gives sin = 1.0, so a wrong
# word pick changes the result too. cos of the picked 0.5 is exactly -1.0.
profile("v_sin_f16", 0x60, True, 0x3400B000, src0=WORD_0 | 0x20, dst=WORD_1, unused=PRESERVE),
profile("v_cos_f16", 0x61, True, 0x3C003800, src0=WORD_0, dst=WORD_0),
)

ALL_PROGRAMS = (PROFILES, NEW_PROFILES, EXTRA_PROFILES)
Expand Down Expand Up @@ -206,6 +212,8 @@ def expected_f16(item):
placement, which expected() applies."""
name = item["name"]
half = pick16(item["a"], item["src0"])
if name in ("v_sin_f16", "v_cos_f16"):
return trig.expected_trig_f16(name[2:5], half)
if name in ("v_floor_f16", "v_ceil_f16", "v_trunc_f16", "v_rndne_f16"):
if half & 0x7FFF == 0 or ((half >> 10) & 0x1F) == 31:
value = half
Expand Down Expand Up @@ -478,7 +486,6 @@ def refusal_cases():
(sdwa_instruction(profile("v_floor_f16", 0x5B, True, 0, src0=DWORD | 0x80, dst=WORD_0), reg0=255),
"literal is unavailable in SDWA"),
(sdwa_instruction(profile("v_fmac_f32", 0x2B, False, 0, 0)), SDWA_REFUSAL),
(sdwa_instruction(profile("v_sin_f16", 0x60, True, 0)), SDWA_REFUSAL),
)


Expand Down Expand Up @@ -553,11 +560,27 @@ def run_dxil(compiler, sdk):


def same_result(got, want, item):
if item["name"] in ("v_sin_f16", "v_cos_f16"):
# The readback tolerance of native_sin_cos_f16_check.py on the placed half; the other half is exact.
shift = 16 if item["dst"] == WORD_1 else 0
if (got ^ want) & ~(0xFFFF << shift) & MASK:
return False
got_h, want_h = (got >> shift) & 0xFFFF, (want >> shift) & 0xFFFF
if got_h == want_h:
return True
return not fixture.M.is_nan(want_h) and want_h not in (0x0000, 0x8000, 0x3C00, 0xBC00) and (
abs(fixture.M.h2f(got_h) - fixture.M.h2f(want_h)) <= 2.0 ** -20 + 2 * trig_ulp(want_h))
if item["name"] in ("v_sin_f32", "v_cos_f32", "v_mul_legacy_f32"):
return math.isclose(fvalue(got), fvalue(want), rel_tol=0.0, abs_tol=2e-6)
return got == want


def trig_ulp(half):
"""One binary16 ulp at the expected value (the subnormal spacing for a subnormal or zero)."""
exponent = half & 0x7C00
return fixture.M.h2f(exponent) * 2.0 ** -10 if exponent else fixture.M.h2f(0x0001)


def run_hardware(replay, sdk):
with tempfile.TemporaryDirectory(prefix="x2w_sdwa_profiles_gpu_") as tmp:
work = Path(tmp)
Expand Down
Loading