Skip to content

feat(mohu-ops): implement matrix multiplication - #299

Open
gayatripadalia wants to merge 4 commits into
mohu-org:mainfrom
gayatripadalia:feat/matmul
Open

feat(mohu-ops): implement matrix multiplication#299
gayatripadalia wants to merge 4 commits into
mohu-org:mainfrom
gayatripadalia:feat/matmul

Conversation

@gayatripadalia

@gayatripadalia gayatripadalia commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

What

Implements matrix multiplication for mohu-ops by adding a new matmul operation that supports the following cases:

  • 1D × 1D (dot product)
  • 1D × 2D (row vector × matrix)
  • 2D × 1D (matrix × vector)
  • 2D × 2D (matrix × matrix)

The implementation performs shape validation, dispatches over supported numeric dtypes, promotes the output dtype appropriately, and returns errors for incompatible shapes or unsupported ranks.

Additionally, this PR adds unit tests covering the supported multiplication variants, shape mismatch handling, and integer-to-f64 promotion.

Why

Matrix multiplication is a fundamental linear algebra primitive and is required for many higher-level numerical operations. This PR addresses the implementation requested in issue #152.

Closes #152.

How

  • Added crates/mohu-ops/src/matmul.rs containing the matrix multiplication implementation.

  • Exported the new module from mohu-ops.

  • Used runtime numeric dtype dispatch to invoke the appropriate typed kernel while rejecting unsupported dtypes.

  • Implemented validation for supported input ranks (1D and 2D) and compatible inner dimensions.

  • Added unit tests covering:

    • Matrix × Matrix multiplication
    • Vector × Vector (dot product)
    • Row Vector × Matrix multiplication
    • Matrix × Vector multiplication
    • Shape mismatch handling
    • Integer input promotion to f64

Checklist

  • cargo test --workspace passes
  • cargo clippy --workspace -- -D warnings passes
  • cargo fmt --all applied
  • CHANGELOG.md updated (not required; internal API addition)
  • Benchmarks added or updated (not applicable)

##SS

Screenshot (1335) Screenshot (1334)

Summary by CodeRabbit

  • New Features
    • Added public matrix multiplication support via matmul, including matrix×matrix, matrix×vector, vector×matrix, and dot-product behavior with correct output shape handling.
    • Promotes operand types automatically and returns floating-point results for integer inputs.
  • Bug Fixes
    • Added stronger validation with clear errors for incompatible dimensions and unsupported input shape combinations.
  • Tests
    • Added new matmul test coverage for results, output shapes, type promotion, and error cases.

@gayatripadalia
gayatripadalia requested a review from Bbn08 as a code owner July 13, 2026 16:19
@github-actions

Copy link
Copy Markdown

PR Check Summary

Item Value
Branch feat/matmul
Changed crates crates/mohu-ops
Files changed 5

CI will run: build, test, clippy, fmt, cargo-deny, DCO, semver.
Reviewer assigned from CODEOWNERS.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@gayatripadalia, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 49 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 17ca8974-d07d-4a86-bb30-008e114d1548

📥 Commits

Reviewing files that changed from the base of the PR and between e013cad and 0fcb76c.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • crates/mohu-ops/Cargo.toml
  • crates/mohu-ops/src/lib.rs
  • crates/mohu-ops/src/matmul.rs
  • crates/mohu-ops/tests/matmul_tests.rs
📝 Walkthrough

Walkthrough

Adds and exports mohu_ops::matmul, supporting matrix/vector multiplication, dot products, shape validation, dtype promotion, and typed naive computation. Integration tests cover supported shapes, mismatch errors, computed values, and integer promotion.

Changes

Matrix multiplication

Layer / File(s) Summary
Crate dependencies and export wiring
crates/mohu-ops/Cargo.toml, crates/mohu-ops/src/lib.rs
Adds the dependencies required by matmul and publicly exports the matmul module.
Matmul validation and computation
crates/mohu-ops/src/matmul.rs
Normalizes supported operand ranks, validates shared dimensions, promotes integer inputs to F64, casts operands, allocates row-major output, and runs a typed naive kernel.
Matmul integration coverage
crates/mohu-ops/tests/matmul_tests.rs
Tests matrix multiplication, vector cases, dot products, shape mismatches, output values, and integer dtype promotion.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant matmul
  participant check_k
  participant matmul_typed
  participant Buffer
  Caller->>matmul: Provide lhs and rhs buffers
  matmul->>check_k: Validate shared dimension
  matmul->>Buffer: Cast operands and allocate output
  matmul->>matmul_typed: Dispatch typed kernel
  matmul_typed-->>matmul: Write computed values
  matmul-->>Caller: Return output Buffer
Loading

Suggested reviewers: bbn08

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding matrix multiplication to mohu-ops.
Linked Issues check ✅ Passed The PR implements the required matmul ranks, shape validation, integer-to-f64 promotion, and adds integration tests matching issue #152.
Out of Scope Changes check ✅ Passed All changes are directly tied to matmul implementation, exports, dependencies, and tests; no unrelated edits are evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gayatripadalia
gayatripadalia force-pushed the feat/matmul branch 2 times, most recently from 0f91ebb to ae5aef7 Compare July 13, 2026 16:41

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
crates/mohu-ops/src/matmul.rs (1)

10-16: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Public API missing doc comment.

matmul is exported via pub mod matmul; in lib.rs and is the crate's headline public entry point (per issue #152), but has no doc comment. Consider adding one describing supported shape combinations, promotion rules, and error conditions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/mohu-ops/src/matmul.rs` around lines 10 - 16, Add a Rust doc comment
to the public matmul function describing supported input shape combinations, how
lower-rank operands are promoted, and the shape or validation errors it returns.
Keep the documentation attached directly to matmul and aligned with its
implemented behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/mohu-ops/tests/matmul_tests.rs`:
- Line 22: Add mohu-testing = { workspace = true } to the [dev-dependencies] in
crates/mohu-ops/Cargo.toml, then update all four float assertions in
crates/mohu-ops/tests/matmul_tests.rs (lines 22, 51, 69, and 87) to use
mohu_testing::approx::assert_allclose instead of assert_eq!, preserving each
expected value slice.

---

Nitpick comments:
In `@crates/mohu-ops/src/matmul.rs`:
- Around line 10-16: Add a Rust doc comment to the public matmul function
describing supported input shape combinations, how lower-rank operands are
promoted, and the shape or validation errors it returns. Keep the documentation
attached directly to matmul and aligned with its implemented behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2e9dd5d3-53ad-47ab-a319-c38f4c45793a

📥 Commits

Reviewing files that changed from the base of the PR and between 3c07775 and 4afd987.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • crates/mohu-ops/Cargo.toml
  • crates/mohu-ops/src/lib.rs
  • crates/mohu-ops/src/matmul.rs
  • crates/mohu-ops/tests/matmul_tests.rs

Comment thread crates/mohu-ops/tests/matmul_tests.rs Outdated

let out = c.as_slice::<f32>().unwrap();

assert_eq!(out, &[19.0, 22.0, 43.0, 50.0,]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Wire up mohu-testing and use assert_allclose instead of assert_eq! on floats. The four value-checking matmul tests compare f32 slices with assert_eq!, and no mohu-testing dev-dependency is present to support the required helper — same root cause across both files.

  • crates/mohu-ops/tests/matmul_tests.rs#L22-L22: replace assert_eq!(out, &[19.0, 22.0, 43.0, 50.0,]) with mohu_testing::approx::assert_allclose.
  • crates/mohu-ops/Cargo.toml#L15-L24: add mohu-testing = { workspace = true } as a [dev-dependencies] entry scoped to this crate.
  • crates/mohu-ops/tests/matmul_tests.rs#L51-L51: replace assert_eq!(out, &[32.0]) with assert_allclose.
  • crates/mohu-ops/tests/matmul_tests.rs#L69-L69: replace assert_eq!(out, &[13.0, 16.0]) with assert_allclose.
  • crates/mohu-ops/tests/matmul_tests.rs#L87-L87: replace assert_eq!(out, &[17.0, 39.0]) with assert_allclose.
🧪 Proposed fix
-    assert_eq!(out, &[19.0, 22.0, 43.0, 50.0,]);
+    mohu_testing::approx::assert_allclose(out, &[19.0, 22.0, 43.0, 50.0]);
📍 Affects 2 files
  • crates/mohu-ops/tests/matmul_tests.rs#L22-L22 (this comment)
  • crates/mohu-ops/Cargo.toml#L15-L24
  • crates/mohu-ops/tests/matmul_tests.rs#L51-L51
  • crates/mohu-ops/tests/matmul_tests.rs#L69-L69
  • crates/mohu-ops/tests/matmul_tests.rs#L87-L87
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/mohu-ops/tests/matmul_tests.rs` at line 22, Add mohu-testing = {
workspace = true } to the [dev-dependencies] in crates/mohu-ops/Cargo.toml, then
update all four float assertions in crates/mohu-ops/tests/matmul_tests.rs (lines
22, 51, 69, and 87) to use mohu_testing::approx::assert_allclose instead of
assert_eq!, preserving each expected value slice.

Source: Coding guidelines

@gayatripadalia
gayatripadalia force-pushed the feat/matmul branch 2 times, most recently from d4587f0 to 618d02a Compare July 13, 2026 17:00
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 92.53731% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/mohu-ops/src/matmul.rs 92.53% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions
github-actions Bot requested a review from mugiwaraluffy56 July 13, 2026 17:33
Signed-off-by: Gayatri Padalia <gayatripadalia@gmail.com>
Signed-off-by: gayatripadalia <gayatripadalia@gmail.com>
Signed-off-by: gayatripadalia <gayatripadalia@gmail.com>
Signed-off-by: gayatripadalia <gayatripadalia@gmail.com>
Signed-off-by: gayatripadalia <gayatripadalia@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add matmul (matrix multiplication) to mohu-ops

2 participants