Add linear_combination to BasicAPI for wide linear layers#205
Open
npow wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces two new binaries: expander-mlp-gen for generating GKR circuits and witnesses for quantized linear MLPs, and transformer_bench for benchmarking GPU transformer proofs. It also adds a new linear_combination API to BasicAPI and Builder to optimize wide linear layers by emitting a single LinComb instruction, significantly reducing memory usage and instruction count. The review feedback highlights a potential signed overflow panic in i64_to_cf when handling i64::MIN, suggests avoiding unnecessary vector clones in set_all_layers by destructuring, and recommends optimizing the fallback linear_combination implementation by skipping zero-coefficient terms.
…linear layers Adds linear_combination(terms, constant) to BasicAPI<C> with a default fallback using repeated mul+add and an optimized Builder<C> override that emits a single LinComb instruction per output neuron. Repeated mul+add causes the optimizer to expand expressions quadratically with layer width, leading to very high peak memory for circuits with dense linear layers (n_in > 64). The LinComb instruction keeps instruction count O(1) per output neuron and avoids the blowup. Also adds transformer_bench and expander-mlp-gen binaries demonstrating use of linear_combination for multi-layer MLP and transformer circuits. Includes equivalence tests verifying linear_combination matches mul+add semantics and that invalid Variable(0) panics correctly. Also fixes pre-existing clippy lints in api.rs: unused import, unused variable, needless &ref patterns, useless as_ref, redundant format args.
19dd0fe to
1f0370b
Compare
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.
Building
Σ w_i * x_iwith chainedapi.mul+api.addgrows thePool<Expression<C>>accumulator by one term per weight. PastCOMPRESS_THRESHOLDthe optimizer starts cascading intermediate variables at ~48 bytes each, so a 3072→1024 dense layer can hit tens of GB of RSS duringcompile_to_layered.linear_combination(&[(Variable, F)], constant)emits a singleLinCombinstruction in O(1) per output neuron and avoids the expression accumulator entirely. Keeps the existing invalid-Variable(0)check viaensure_variable_validon each input.On a 784→512→128→10 MLP (1.47M constraints) the peak goes from ~70 GB to ~1.1 GB.