Skip to content

feat: introduce matrix layer with end-to-end harness#40

Merged
Oppen merged 2 commits into
mainfrom
feat/matrix_ops
Jun 2, 2026
Merged

feat: introduce matrix layer with end-to-end harness#40
Oppen merged 2 commits into
mainfrom
feat/matrix_ops

Conversation

@Oppen

@Oppen Oppen commented May 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds a separate matrix e-graph layer (MatrixExpr / MatrixOp / MatrixPipeline) with constructors var_matrix, const_matrix, matmul, transpose, plus one rewrite rule transpose∘transpose → x so the saturation path is non-trivially exercised.
  • Adds MatrixLower (unrolls a matrix expression into a dense grid of ArithExpr cells, allocating scalar input slots row-major per var_matrix occurrence with a shared base for repeated indices) and a trzk --matrix CLI flag that drives the matrix pipeline through to scalar codegen — no separate Rust matrix runtime. The pipeline lowers all output cells and emits a single function returning [u32; N] row-major.
  • Adds three matrix integration ops covering distinct pipeline slices, each with frozen unseeded vectors and fuzz: matrix_matmul, matrix_matmul_const, matrix_transpose_matmul (the last verifies the rewrite rule fires during saturation). Each op verifies the complete output matrix (all 4 cells for 2×2), not a single chosen cell.
  • Standardises run.sh and CI on <family>_<suffix> op naming (arith_add0, matrix_matmul, …); CI gains arith_mul_chain (was missing) and all three matrix ops.

Test plan

  • lake build clean
  • All 7 scalar integration ops pass frozen-vector + fuzz harness
  • All 3 matrix integration ops pass frozen-vector + fuzz harness, all output cells checked
  • Verified the transpose∘transpose → x rule actually fires by inspecting the post-saturation matrix expression artifact (transposes gone)
  • CI green on PR

Oppen added 2 commits May 19, 2026 16:14
Adds a separate matrix e-graph (MatrixExpr / MatrixOp / MatrixPipeline)
with constructors var_matrix, const_matrix, matmul, transpose and one
rewrite rule (transpose∘transpose → x). Lowering unrolls a matrix
expression into the ArithExpr for a chosen output cell, allocating
scalar input slots row-major per var_matrix occurrence (shared base
across repeated indices), which lets the existing scalar pipeline emit
and link without a separate matrix runtime.

trzk gains --matrix; matrix specs declare `def spec : MatrixExpr` and
`def out : Nat × Nat`. Three integration ops exercise the pipeline
end-to-end against frozen unseeded vectors and fuzz: matrix_matmul,
matrix_matmul_const, matrix_transpose_matmul (the last verifies the
rewrite rule fires during saturation). run.sh and CI standardise on
<family>_<suffix> op naming (arith_add0, matrix_matmul, …).

Other matrix ops named in the umbrella plan (hadamard, pointwise_scalar,
reshape, permute) are deferred to the sub-changes whose harness will
exercise each; nothing speculative lands here.
Matrix specs previously lowered a single chosen output cell, leaving the
other m*n-1 cells untested. This adds `emitMatrixFunction` to Emit.lean
which emits `-> [u32; N]` returning all cells row-major, updates
Compile.lean to lower and saturate every cell, and wires the harness,
verifier, generator, and test vectors through the change.

The three matrix integration ops now verify the complete 2×2 output (4
values) on every vector instead of one cell.
@manuelpuebla

Copy link
Copy Markdown
Collaborator

All checks pass, good step by step approach.

@manuelpuebla manuelpuebla left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good step by step tactics for maximal clarity of development. Couldn't agree more.

@Oppen Oppen merged commit 572fb5c into main Jun 2, 2026
11 checks passed
@Oppen Oppen deleted the feat/matrix_ops branch June 2, 2026 18:37
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.

2 participants