perf: batchable scan-based Gemma inference engine (restrict-compliant)#9471
Open
koenvanderveen wants to merge 1 commit into
Open
perf: batchable scan-based Gemma inference engine (restrict-compliant)#9471koenvanderveen wants to merge 1 commit into
koenvanderveen wants to merge 1 commit into
Conversation
Adds gemma_inference_restrict_scan.py: a drop-in variant of gemma_inference_restrict_opt.py that runs the layer stack through flax.linen.scan (one compiled layer body) instead of a Python for-loop that jit unrolled into num_layers copies. The unrolled graph kept every layer's weights + fp32 working-copies live at once (~24 bytes/param, ~12x the bf16 weights), so 27b peaked at ~738 GB and OOM'd / could not be batched. Scan keeps only one layer's working set live -> 27b peak ~230 GB regardless of batch, which makes 27b both fit and batch on a CPU box. syft-restrict: the scan primitive (flax.linen.scan) and weight restack live in public wrappers (build_scanned_blocks, stack_layer_params); the private Block is passed by name. Same allow_functions -> policy_id unchanged (d84d1e21530fa500, verified). Greedy output byte-identical to the opt engine on 270m and 1b.
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.
What
Adds
gemma_inference_restrict_scan.py: a variant of the restrict-compliant Gemma-3 inference engine that runs the layer stack throughflax.linen.scan(one compiled layer body) instead of a Pythonfor-loop thatjax.jitunrolled intonum_layerscopies.Why
The unrolled graph kept every layer's weights + fp32 matmul working-copies live at once (~24 bytes/param, ~12× the bf16 weights). On 27B this peaked at ~738 GB — it OOM'd on the largest RAM GCP box and could not be batched at all.
Scan keeps only one layer's working set live → 27B peak ~230 GB regardless of batch size, so 27B both fits and batches on a CPU machine.
Measured on 27B (n2d-highmem-96, CPU)
Decode is memory-bandwidth bound; throughput scales ~linearly with batch until the knee at ~512 (~195× faster per example than unbatched). Beyond 512 it turns compute-bound with a very expensive prefill compile.
syft-restrict
flax.linen.scan) and weight restack live in public wrappers (build_scanned_blocks,stack_layer_params); the privateBlockis passed by name. Sameallow_functions→policy_idunchanged (d84d1e21530fa500, verified).Notes / follow-ups
x = jnp.float32(x)is required becausenn.scan's carry needs a fixed dtype (the unrolled loop silently ran layer 0 in bf16, the rest fp32).jnp.float32is already allow-listed → no policy change.gemma_bench.py(which lives onrasswanth/optimize-inference), place it alongside the bench innotebooks/enclave/gemma/.