Skip to content

MUFU.TANH translates, for f32 and f16 (chain change) - #2058

Open
Know-It-All-Brother wants to merge 3 commits into
seedds:mainfrom
Know-It-All-Brother:ampere-mufu-tanh
Open

Know-It-All-Brother wants to merge 3 commits into
seedds:mainfrom
Know-It-All-Brother:ampere-mufu-tanh

Conversation

@Know-It-All-Brother

@Know-It-All-Brother Know-It-All-Brother commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Part of the Ampere accuracy series (#2040). It depends on #2041 for the test: the commits before this PR's own are #2041's and drop out once it lands, so this PR's own change is the last commit.

What and why

tanh.approx.f32 and tanh.approx.f16x2 compile to MUFU.TANH (MUFU op 9). The translator had no case for it, and its IR had no hyperbolic tangent, so these shaders failed to translate.

This adds a HyperbolicTangent IR instruction (GLSL.std.450 Tanh in SPIR-V) and MUFU op 9:

  • The f32 form is precise::tanh. Like the other MUFU functions, it's Metal's approximation against NVIDIA's, so it joins the test's approximations list. Every special value matches the Orin: ±0, subnormals (tanh(x) = x, no flush), ±inf → ±1 and |x| ≥ 8 → ±1. NaN differs only in its bits.
  • The f16 form follows what the Orin does, read from its results: it returns x for |x| < 2^-8, ±1 from 8 up, and in between tanh rounded toward zero to half. That model matches all but 3 of 1,876 finite halves, and those 3 are 1 ULP off. It costs 7 integer operations and 2 selects per half on top of the tanh.

Reference entry: "MUFU.TANH" in docs/reference/shaders.md.

#1412 also translates MUFU.TANH, as 1 − 2/(e^{2x}+1). Against the Orin the two are close for f32 (498 lanes off, max 323 ULP for #1412; 496, max 116 here), and for f16 this PR is much closer (3 halves against 632), because the GPU rounds tanh toward zero. I've posted the comparison on #1412 and suggested landing TANH through this PR.

How to verify

DMSH_TEST_XLATE=dm/gfx/tools/dmsh_xlate_bin/dmsh_xlate AMPERE_ORIN_ONLY=mufu_tanh,mufu_tanh_f16x2 python3 dm/gfx/tests/test_dmsh_ampere_orin.py
Op main this PR
mufu_tanh (of 1,024) failed to translate 523 differ: 496 within Metal's approximation (median 1 ULP, max 116) and 27 NaN bit patterns
mufu_tanh_f16x2 (halves of 2,048) failed to translate 118 differ: 115 NaN bit patterns, 3 at 1 ULP (632 with a plain round-to-nearest pack)

Both move from KNOWN to APPROX. The f16 thresholds (2^-8 and 8) are inferred from the fixture's samples and aren't proven bit for bit. The GPU's f32 form also returns x for |x| < 2^-8; copying that would take 3 more operations, which I haven't done.

Checked

  • Tests: test_dmsh_ampere_orin.py; every other test_dmsh_* gives the same result as on main (test_dmsh_ampere_mem.py, test_dmsh_meshstage.py and test_dmsh_pairs.py fail on main the same way); test_nsp_chain.py, test_xlate_anycpu.py, test_docs.py. lint_python.py shows no new findings.
  • Translator: the dlls are rebuilt from this tree with .NET 10; tool_lock.py --update-lock and dmsh_chain.py --header were run (chain c87293dfe3d3).
  • Devices and OS: MacBook Pro M5 Max, macOS 27.
  • regress.py: not run. My only local Switch 2 build's shader package comes from an older shader split than main's map, so main's chain can't build it. Ocarina of Time's build already translates every shader, so it can't contain this instruction.
  • Games run: none.

Docs

  • docs/reference/shaders.md has the entry, and python3 dm/tools/docs_symptoms.py was run
  • A chain tool changed: dmsh_chain.py --header was run, and dmsh_chain.h, the translator dlls and the lock are part of the change
  • No game files, keys or data extracted from a game

…re_orin.py runs about 190 operations, each the GPU compiler's own machine code for one PTX operation, through the translator, spirv-cross, the chain's MSL rewrites and Metal on this Mac's GPU, and compares every output word with what a Jetson Orin (sm_87) computed from the same 1,024 input rows. The translator tests checked the MSL a few encodings give, not the values; this one finds the FSETP.NE, SHFL lane, F2FP, rounding-mode and signed-zero differences the tracking issue lists, which KNOWN names until each fix takes its entry out. dm/gfx/tools/ampere_orin/capture.py records the fixture on any Linux machine with an Ampere GPU and NVIDIA's driver, with no toolkit or compiler on either side
…ules with its functions, so a rewrite that uses one (struct in PR seedds#1902's hfma_msl) runs as it does in a build instead of raising NameError
…ion: the Ampere MUFU emitter had no case for op 9 (TANH), so a shader with PTX tanh.approx.f32 or tanh.approx.f16x2 failed with "MUFU op 9". A HyperbolicTangent IR instruction (GLSL.std.450 Tanh, precise::tanh in the chain's MSL) now runs it. The f32 result is Metal's tanh, within 2 ULP of the true value where the GPU's own approximation is up to 116 ULP off it. The .F16 form copies the GPU's shape: x itself below 2^-8 (subnormal halves kept), +-1 from 8 up, and in between the f32 tangent rounded toward zero by clearing its low 13 bits before the pack, with the range tests on the input's bits so fast math keeps a NaN. test_dmsh_ampere_orin.py: mufu_tanh and mufu_tanh_f16x2 didn't translate before; now mufu_tanh differs in 523 of 1,024 lanes (496 by the GPU's approximation, 27 NaN bits) and mufu_tanh_f16x2 in 118 of 2,048 halves (115 NaN bits, 3 by 1 ULP, against 632 with a plain round-to-nearest pack), and both move from KNOWN to APPROX; chain c87293dfe3d3
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders labels Nov 9, 2026
Know-It-All-Brother added a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…nch's translator rebuilt from its own source; chain d9564219dbb0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant