add matvec builtin for matrix-vector product#517
Merged
Conversation
matvec xm ys returns the flat vector r[i] = sum_j xm[i][j] * ys[j]. One call instead of the wrap-as-column + matmul + flatten ceremony every linear-regression-style persona was paying: flat (matmul xm (map (y:n>L n;[y]) ys)) -> matvec xm ys Three lines, ~10 tokens per use-site, gone. Same precision tier as matmul (direct row-by-row dot product over f64), errors as ILO-R009 on dim mismatch, empty matrix, or ragged rows. Tree-bridge eligible: composes existing matrix/vector helpers so VM and Cranelift inherit through the bridge without new opcodes. The implementation is wrapped #[inline(never)] from the start, matching the recurring band-aid in #494 / #506 / lstsq for call_function's oversized dispatch frame on cargo-nextest stack budgets. Doc sync: SPEC.md linalg row + prose, ai.txt (auto-regenerated by build.rs from SPEC.md), skills/ilo/SKILL.md 4-char+ list, new Linear algebra section in skills/ilo/ilo-builtins-math.md, CHANGELOG Unreleased Added entry. 9 cross-engine regression tests in tests/regression_matvec.rs cover identity, 2x2 / 2x3 / 1x1 happy paths, negatives + zeros, parity with the flat+matmul ceremony, and the ILO-R009 dim-mismatch / empty / ragged error paths. examples/matvec.ilo gives agents a worked example with -- run / -- out assertions across every engine. Closes pending.md #5an.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Two ILO-T013 cases mirroring the matmul / dot pattern in coverage_verify.rs. Lifts the codecov patch coverage past the threshold by exercising the per-arg verifier rejections directly (the runtime regression tests in tests/regression_matvec.rs hit the happy path through the verifier, not the error branches).
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.
Summary
Adds
matvec xm ys > L n, a native matrix-vector product. Skips the wrap-as-column-matrix + flatten ceremony every linear-regression-style persona was paying:Three lines, ~10 tokens per use-site, gone. The linear-regression persona that surfaced this would have its
flatten (matmul (transpose xm) (map (y:n>L n;[y]) ys))recipe collapse tomatvec (transpose xm) ys.Closes pending.md #5an (P1+S).
Repro before / after
Before (manual recipe):
After:
Both produce
[50, 122]; the regression suite asserts they stay equal.What's in the diff
Single logical commit:
Builtin::Matvecvariant + name/from-name/ALL(appended to preserve every existing on-wire tag) + builtin-name round-trip test arrays.("matvec", &["L (L n)", "L n"], "L n")table row + a per-argILO-T013arm.#[inline(never)] fn matvec_runnext tomatrix_from_value/vec_from_value. Errors returnILO-R009on empty matrix, zero-cols, ragged rows, and dim mismatch. Thecall_functionarm is a single tail-call into the helper, matching the lstsq feature: add lstsq builtin for ordinary least squares #515 / feature: crypto primitives - sha256, hmac-sha256, base64, hex, ct-eq #494 / feat(builtins): URL + base64url encoding cluster (urlenc/urldec/b64u/b64u-dec) #506 pattern that keeps the dispatch frame offcargo-nextest's tighter stack budget.(Builtin::Matvec, 2) => trueinis_tree_bridge_eligible, so VM and Cranelift JIT/AOT inherit semantics with no new opcodes.ai.txt(auto-regenerated bybuild.rs),skills/ilo/SKILL.md4-char+ list, newLinear algebrasection inskills/ilo/ilo-builtins-math.md, CHANGELOG Unreleased Added entry.tests/regression_matvec.rs(identity, 2x2, 2x3, 1x1, negatives+zeros, parity vsflat matmulceremony, dim mismatch, empty, ragged). VM + Cranelift JIT; error tests are VM-only (matches the existingregression_linalg_basic.rsconvention since the JIT path bubbles tree-bridge errors as nil).examples/matvec.iloworked example with-- run:/-- out:markers across every engine viatests/examples_engines.rs.Test plan
cargo test --release --features cranelift --test regression_matvec— 9/9 passcargo test --release --features cranelift --test examples_engines— pass (matvec example runs through every engine)cargo test --release --features craneliftfull suite — pass (after the standard libilo.a symlink workaround for the AOT tests, pending #5av in flight as Claude/playwright ilo exploration r opnx #63)cargo fmt --checkcleancargo clippy --release --features cranelift --all-targets -- -D warningscleanmatvec [1,2,3] [4,5,6](first arg notL (L n)) withILO-T013matvec [[1,2],[3,4]] [[5,6]](second arg notL n) withILO-T013Follow-ups
call_function, the#[inline(never)]band-aid onmatvec_runcan come off alongside every other pre-emptive helper.