tanh: give aarch64 cores without FEAT_FP16 an f32-roundtrip f16 kernel - #2584
Open
czoli1976 wants to merge 1 commit into
Open
tanh: give aarch64 cores without FEAT_FP16 an f32-roundtrip f16 kernel#2584czoli1976 wants to merge 1 commit into
czoli1976 wants to merge 1 commit into
Conversation
sigmoid_f16 and silu_f16 fall back to NEON kernels that widen into an f32 scratch when FEAT_FP16 is missing, but tanh_f16 had no such kernel and dropped to the generic scalar one instead. Add the matching roundtrip kernel and register it on that branch. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
sigmoid_f16andsilu_f16both fall back to NEON kernels that widen into an f32 scratch when FEAT_FP16 is missing (arm64simd_sigmoid_f16_4n,arm64simd_silu_f16_4n).tanh_f16had no such kernel, so that branch left it ongeneric::HTanh8— a scalar loop. This adds the matching roundtrip kernel and registers it there.Twelve lines of wiring: the kernel is one
ew_impl_f16_via_f32!over the existingarm64simd_tanh_f32_4n, exactly as the sigmoid and silu ones are.Numbers
Both arms in one process. Cores with FEAT_FP16 are untouched — they keep
arm64fp16_tanh_f16_8n, which at 7.9 Gelem/s is far ahead of either of these.Accuracy moves toward the reference rather than away: the roundtrip evaluates the f32 Padé and rounds once at the end, where
HTanh8evaluates a lower-degree Padé in f16 throughout. Same change of character the sigmoid fallback already made.How I got here
I set out to put a 2^16 lookup table behind
sigmoid_f16andtanh_f16, as #2568/#2579/#2583 do for gelu, erf and silu. Measuring first killed it: the table tops out at ~3.9 Gelem/s for any f16 unary — it is a scalar gather, bound by load issue rather than by the cost of the function — while the native FEAT_FP16 kernels run at 7.33 (sigmoid) and 7.90 (tanh) Gelem/s. A table would have been a 2x regression on every core that has FEAT_FP16.What the same measurements did show is that the non-FEAT_FP16 branch was leaving 2x on the table for tanh, which is what this PR is.
Tests
tract-linalg 4421, tract-core 270, test-f16 2378 — green.
cargo fmt --allandcargo clippyclean.🍍