Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix addmm with empty matrices and beta != 1.0#2715

Merged
angeloskath merged 3 commits intoml-explore:mainfrom
harsh-sutariya:fix/addmm-empty-matrices
Nov 3, 2025
Merged

Fix addmm with empty matrices and beta != 1.0#2715
angeloskath merged 3 commits intoml-explore:mainfrom
harsh-sutariya:fix/addmm-empty-matrices

Conversation

@harsh-sutariya
Copy link
Contributor

Fixes #2698 [BUG] mx.addmm with empty a and b produces incorrect result

Proposed changes

  • Fix CPU backend to apply beta scaling for empty matrices (K=0)
  • Fix Metal backend to apply beta scaling for empty matrices (K=0)
  • Add tests for empty matrix addmm with various beta values

The bug occurred when addmm was called with empty matrices (one dimension is 0) and beta != 1.0. The function would return C instead of beta * C because the early return for empty matrices didn't apply beta scaling.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

I left a minor comment for the gpu implementation and a rather larger one for the cpu. Feel free to ask any questions if something doesn't make sense.

@harsh-sutariya harsh-sutariya force-pushed the fix/addmm-empty-matrices branch from 9d5577a to ec39583 Compare October 30, 2025 22:40
@harsh-sutariya
Copy link
Contributor Author

@angeloskath I’ve addressed your feedback,
Please take another look when convenient. Thanks!

Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

Looks good I will merge after the tests pass.

- Fix CPU backend to apply beta scaling for empty matrices (K=0)
- Fix Metal backend to apply beta scaling for empty matrices (K=0)
- Add comprehensive tests for empty matrix addmm with various beta values
- Tests cover different shapes and batched empty matrices

The bug occurred when addmm was called with empty matrices (one dimension is 0)
and beta != 1.0. The function would return C instead of beta * C because
the early return for empty matrices didn't apply beta scaling.

Fixes ml-explore#2698
@harsh-sutariya harsh-sutariya force-pushed the fix/addmm-empty-matrices branch from ec39583 to e4140bd Compare November 3, 2025 20:17
@harsh-sutariya
Copy link
Contributor Author

harsh-sutariya commented Nov 3, 2025

Temporary array beta_scalar was destroyed before the asynchronous computation used it, causing garbage memory reads on Linux CI.

Added encoder.add_temporary(std::move(beta_scalar)); to keep the array alive until computation completes.

The fix is now passing locally and ready for CI rerun. Could you please approve the workflow when you get a chance?
@angeloskath

@angeloskath angeloskath merged commit 50fa315 into ml-explore:main Nov 3, 2025
7 checks passed
@harsh-sutariya harsh-sutariya deleted the fix/addmm-empty-matrices branch November 3, 2025 22:21
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.

[BUG] mx.addmm with empty a and b produces incorrect result

2 participants