Skip to content

Add linear_combination to BasicAPI for wide linear layers#205

Open
npow wants to merge 1 commit into
PolyhedraZK:masterfrom
npow:feature/memory-reduction
Open

Add linear_combination to BasicAPI for wide linear layers#205
npow wants to merge 1 commit into
PolyhedraZK:masterfrom
npow:feature/memory-reduction

Conversation

@npow

@npow npow commented May 25, 2026

Copy link
Copy Markdown

Building Σ w_i * x_i with chained api.mul + api.add grows the Pool<Expression<C>> accumulator by one term per weight. Past COMPRESS_THRESHOLD the optimizer starts cascading intermediate variables at ~48 bytes each, so a 3072→1024 dense layer can hit tens of GB of RSS during compile_to_layered.

linear_combination(&[(Variable, F)], constant) emits a single LinComb instruction in O(1) per output neuron and avoids the expression accumulator entirely. Keeps the existing invalid-Variable(0) check via ensure_variable_valid on each input.

On a 784→512→128→10 MLP (1.47M constraints) the peak goes from ~70 GB to ~1.1 GB.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread expander_compiler/bin/expander_mlp_gen.rs Outdated
Comment thread expander_compiler/bin/expander_mlp_gen.rs Outdated
Comment thread expander_compiler/src/frontend/api.rs
…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.
@npow npow force-pushed the feature/memory-reduction branch from 19dd0fe to 1f0370b Compare May 25, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant