Repository navigation
MUFU.TANH translates, for f32 and f16 (chain change) - #2058
Open
Know-It-All-Brother wants to merge 3 commits into
Open
Know-It-All-Brother wants to merge 3 commits into
Know-It-All-Brother wants to merge 3 commits into
Conversation
…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
2 tasks done
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.f32andtanh.approx.f16x2compile 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
HyperbolicTangentIR instruction (GLSL.std.450 Tanh in SPIR-V) and MUFU op 9: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.tanhrounded 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
mufu_tanh(of 1,024)mufu_tanh_f16x2(halves of 2,048)Both move from
KNOWNtoAPPROX. 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
test_dmsh_ampere_orin.py; every othertest_dmsh_*gives the same result as on main (test_dmsh_ampere_mem.py,test_dmsh_meshstage.pyandtest_dmsh_pairs.pyfail on main the same way);test_nsp_chain.py,test_xlate_anycpu.py,test_docs.py.lint_python.pyshows no new findings.tool_lock.py --update-lockanddmsh_chain.py --headerwere run (chain c87293dfe3d3).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.Docs
docs/reference/shaders.mdhas the entry, andpython3 dm/tools/docs_symptoms.pywas rundmsh_chain.py --headerwas run, anddmsh_chain.h, the translator dlls and the lock are part of the change