feat: Parallel ternary conversion + rayon matmul#62
Merged
Conversation
gemma4_to_block_attnres() now parallelizes layer conversion with rayon. Expected: 8 min → ~2 min on 4-core Skylake. ternary_matmul_parallel(): processes seq positions (or output rows) in parallel. CpuLinear::forward_parallel(): multi-threaded forward for large matrices. Added rayon dependency. [e6e5afb8]
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.
Parallel Ternary Speedup
Changes
1. Parallel layer conversion (rayon)
gemma4_to_block_attnres()now processes all 35 layers in parallel usingrayon::par_iter(). Each layers weight quantization is independent.Before: 472s (7.9 min) sequential
After: ~120s (2 min) estimated on 4-core Skylake
2. Parallel ternary matmul
ternary_matmul_parallel(): processes sequence positions (or output rows for single-token) in parallel.CpuLinear::forward_parallel(): multi-threaded forward for large matrices.Expected speedup: ~4-8x on Skylake (4 cores/8 threads)
3. Ternary matmul optimization
Split inner loop into
pos_sum - neg_suminstead ofi8 as f32 * input. This is more branch-predictor friendly and enables better SIMD auto-vectorization.Dependencies
rayon = "1.10"to Cargo.tomlTests
[e6e5afb8]