Skip to content

Fix matmul() bug where conj() operands could return incorrect results - #1250

Open
tbensonatl wants to merge 1 commit into
mainfrom
tbenson/fix-matmul-unsupported-cublas-op-c
Open

tbensonatl wants to merge 1 commit into
mainfrom
tbenson/fix-matmul-unsupported-cublas-op-c

Conversation

@tbensonatl

Copy link
Copy Markdown
Collaborator

matmul() with a conj() operand over a row-major tensor fails for complex types. For example, matmul(conj(A), transpose_matrix(A)). This is because cuBLASLt rejects CUBLAS_OP_C on an operand whose layout differs from that of C/D, returning CUBLAS_STATUS_NOT_SUPPORTED. On debug builds, this error throws an exception, but release builds elide that check.

WithMatmulOperand lowers conj(A) as (A^T)^H, handing cuBLASLt transpose_matrix(A) with CUBLAS_OP_C. Transposing a row-major view yields a column-major operand, while matmul always describes C as row-major, so the combination is unsupported for row-major inputs. Existing coverage exercised conj(transpose_matrix(a)), where the extra transpose lands back on row-major.

This change only uses the CUBLAS_OP_C path when the operand handed to cuBLASLt is row-major. Otherwise, it falls back to evaluation into a temporary for the input operator. We also make cuBLAS status checks unconditional on build type so that cuBLAS errors will throw in release builds as well.

Finally, we free the cublasLt handle, which was previously never destroyed, in the operator destructor.

Add new test coverage and enable a previously disabled conj() test.

matmul() with a conj() operand over a row-major tensor fails for complex types. For example,
matmul(conj(A), transpose_matrix(A)). This is because cuBLASLt rejects CUBLAS_OP_C on an
operand whose layout differs from that of C/D, returning CUBLAS_STATUS_NOT_SUPPORTED. On
debug builds, this error throws an exception, but release builds elide that check.

WithMatmulOperand lowers conj(A) as (A^T)^H, handing cuBLASLt transpose_matrix(A) with CUBLAS_OP_C.
Transposing a row-major view yields a column-major operand, while matmul always describes C as
row-major, so the combination is unsupported for row-major inputs. Existing coverage exercised
conj(transpose_matrix(a)), where the extra transpose lands back on row-major.

This change only uses the CUBLAS_OP_C path when the operand handed to cuBLASLt is row-major.
Otherwise, it falls back to evaluation into a temporary for the input operator. We also make
cuBLAS status checks unconditional on build type so that cuBLAS errors will throw in release
builds as well.

Finally, we free the cublasLt handle, which was previously never destroyed, in the operator
destructor.

Add new test coverage and enable a previously disabled conj() test.

Signed-off-by: Thomas Benson <tbenson@nvidia.com>
@tbensonatl tbensonatl self-assigned this Sep 11, 2026
@copy-pr-bot

copy-pr-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@greptile-apps

greptile-apps Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the corrected fallback and cleanup paths consistent with existing stream and cache ownership semantics.

Summary

  • Centralizes cuBLAS operand memory-order classification.
  • Evaluates incompatible conjugate operands into row-major temporaries and invokes cuBLASLt with CUBLAS_OP_N.
  • Makes cuBLAS failures visible in release builds.
  • Adds exception-safe ownership and destruction of cuBLASLt resources.
  • Adds coverage for conjugated operands, transposed views, and transposed outputs.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[matmul operand] --> B{Conjugate or Hermitian metadata path?}
    B -- No --> C[Evaluate unsupported expression if needed]
    B -- Yes --> D[Obtain cuBLAS-compatible underlying tensor]
    D --> E{CUBLAS_OP_C and column-major?}
    E -- No --> F[Use metadata transpose/conjugate operation]
    E -- Yes --> C
    C --> G[Use row-major temporary with CUBLAS_OP_N]
    F --> H[Configure or reuse cuBLASLt plan]
    G --> H
    H --> I[Launch matmul with unconditional status checking]
Loading

Reviews (1) · Last reviewed commit: "Fix matmul() bug where conj() operands c..."

@tbensonatl

Copy link
Copy Markdown
Collaborator Author

/build

@tbensonatl

Copy link
Copy Markdown
Collaborator Author

/build

2 similar comments
@tbensonatl

Copy link
Copy Markdown
Collaborator Author

/build

@cliffburdick

Copy link
Copy Markdown
Collaborator

/build

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