← Back to Pull requests

Grouped gemm for `mul!` of CUDA tensors #611

Open
unknown opened this pull request with 268 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 05, 2026 14:53 UTC
Author

⚠️ vibe-coded, not meant for merging, mostly to get an idea of how much this matters vs the current approach.

---

Replaces the per-block loop in mul! for CUDA tensors by a single grouped gemm call (cublasGemmGroupedBatchedEx, cuBLAS ≥ 12.5) for all small blocks, and individual gemm calls for the few large ones.

  • Grouped gemm is real-only, so complex element types use the 1m embedding: α op(A) is expanded into real 2m × 2k blocks [ar -ai; ai ar] by a small kernel, while B and C are simply reinterpreted as real. Complex β is applied upfront.

  • Blocks with m n k above TensorKitCUDAExt.GROUPED_GEMM_THRESHOLD[] (1024^3 real, 256^3 complex) use individual cublasGemmEx calls, since tensor-core gemm beats the grouped (SIMT) kernel there. These calls upload α and β once, instead of once per block as in CUDA.jl's mul!.

  • Complex products with an adjoint B, and cuBLAS < 12.5, use only the individual gemm calls.

  • The block structure and all host-side arguments are cached per pair of spaces in a GroupedGemmPlan.

  • The grouped call always uses plain 32F/64F compute types: TF32 crashes on H100 with a misaligned-address error for unaligned blocks.
  • Benchmarks

    C = A B, with the old mul! as baseline.

  • RTX A6000: 24–190× faster for tensors with many small or medium blocks (U(1), SU(2), U(1)×U(1), Float32/ComplexF32).

  • H100: 10–140× faster for many small blocks. With few, very large blocks the timings are within a few % of the old implementation (2–3% slower on the largest blocks, up to ~11% for ComplexF32 under TF32 math mode, where the direct cublasGemmEx path is slower than CUDA.jl's gemmEx! for reasons I haven't pinned down).

  • Under TF32 math mode, a lower real threshold (~256³) would be better; the current default is tuned for the default math mode.
  • Tests

    The existing "Multiplication and inverse: test via CPU" testset covers the grouped path (the test spaces have small blocks). A new testset covers the direct path and real/complex β for every combination of adjoints.

    🤖 Generated with Claude Code

    Diff: 268 lines (+201 / -67) across 3 files Reviewer: kshyatt Review duration: 13626m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.39) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 268 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.335
    Real PR Conversation Timeline
    1 comments
    "Review active on #611"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.4% (MEDIUM) · Higher = larger blast radius
    39.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.0% · Safe to merge
    21.0%