Skip to content

Reject aliased c in matmulInPlace!, add regression tests - #140

Open
Quafadas wants to merge 2 commits into
mainfrom
matmulInPlace-alias-check
Open

Reject aliased c in matmulInPlace!, add regression tests#140
Quafadas wants to merge 2 commits into
mainfrom
matmulInPlace-alias-check

Conversation

@Quafadas

Copy link
Copy Markdown
Owner

matmulOutputCheck validated c's shape and dense-column-major layout, but never that c's backing array is distinct from m's or b's. BLAS dgemm/sgemm assume the output doesn't overlap the inputs: if c shares an array with m or b, dgemm can read an element of m/b after it's already been overwritten via the aliased c, silently corrupting the result instead of failing loudly - e.g. computing a @@ a in place into a itself. m and b may still safely share an array with each other (dgemm only ever reads those two), so only c-vs-m and c-vs-b are checked.

Also verified the "row-major/strided/undersized c" concern separately raised: already covered by the existing shape and isDenseColMajor checks in matmulOutputCheck (see the "matmulInPlace! throws when c is row-major instead of column-major" and "...wrong shape" tests already in matMulLayoutChecks.test.scala) - no changes needed there.

Added regression tests: c aliasing m throws, c aliasing b throws (via a distinct Matrix wrapping the same backing array, not just the same object reference), and m aliasing b remains allowed and correct.

Simon Parten and others added 2 commits August 11, 2026 09:01
matmulOutputCheck validated c's shape and dense-column-major layout,
but never that c's backing array is distinct from m's or b's. BLAS
dgemm/sgemm assume the output doesn't overlap the inputs: if c shares
an array with m or b, dgemm can read an element of m/b after it's
already been overwritten via the aliased c, silently corrupting the
result instead of failing loudly - e.g. computing `a @@ a` in place
into `a` itself. m and b may still safely share an array with each
other (dgemm only ever reads those two), so only c-vs-m and c-vs-b are
checked.

Also verified the "row-major/strided/undersized c" concern separately
raised: already covered by the existing shape and isDenseColMajor
checks in matmulOutputCheck (see the "matmulInPlace! throws when c is
row-major instead of column-major" and "...wrong shape" tests already
in matMulLayoutChecks.test.scala) - no changes needed there.

Added regression tests: c aliasing m throws, c aliasing b throws (via
a distinct Matrix wrapping the same backing array, not just the same
object reference), and m aliasing b remains allowed and correct.
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