Skip to content

Use BLAS for more products of Dual matrices #881

Backlog

Description

@CarloLucibello

#880 computes products of a Dual matrix and a real matrix with BLAS on Julia ≥ 1.13. This tracks the products of #854 that still take the generic kernel. The first version of #880 (872aa27) covered most of them, and its benchmarks show the trade-offs.

Matrix products

  • real * Dual, also real' * Dual: the values and partials of B have to be rearranged first, e.g. into a k×(N+1)n matrix for a single gemm, and the result assembled back into C.
  • Dual * Dual, also Dual * Dual', as in the reproducer of LinearAlgebra.mul! with Matrix{Dual} does not dispatch to BLAS #854: A * value(B) is a Dual * real product, and value(A) * partials(B) a real * Dual one.

Both allocate temporaries, which the generic kernel doesn't, and only pay off from n ≈ 16–32. For n×n matrices they were up to 7× (real * Dual) and 20× (Dual * Dual) slower at n = 2. At large sizes they were 2–24× faster, e.g. 5.0× and 7.9× for the example of #854 (500×300 by 300×400, N = 3). So they would need a size threshold. A copy-free real * Dual is possible with one gemm per column of B, but those are (N+1)×k by k×m products, which BLAS handles poorly.

Matrix–vector products

  • real * Dual vector: copy-free, as the values and partials of y = A * x, as an (N+1)×m matrix, are those of x times transpose(A). Faster than the generic kernel from n ≈ 16–64, but up to 3.5× slower below.
  • Dual * real vector: one gemv on the reinterpreted matrix. It was 1.06–4.3× slower in the review's benchmarks (aarch64, n ≤ 128), but 17× faster for N = 8 with 256×256 Float32 matrices on x86, so it is worth measuring again.

GPU arrays

cuBLAS and GPUArrays define the 7-argument mul! for their arrays of any element type, and Julia picks those methods for CuArrays of Duals, which keep the generic kernel. Using cuBLAS would need e.g. a cuBLAS extension. In the first version of #880 (RTX 5090, 2048×2048 matrices, N = 4), Dual * real took 1.3 instead of 6.9 ms, real * Dual 1.6 instead of 4.0 ms, and Dual * Dual 2.8 instead of 9.0 ms. Small products are bound by launch latency: 15–31 μs instead of 10–11 μs for 256×256 by 256×128.

Julia < 1.13, which has no 7-argument mul!, keeps the generic kernel for all products.

The main use for real * Dual and Dual * Dual is forward-over-reverse through dense layers (FluxML/Zygote.jl#1784), where the inputs are Duals and the weights are real: W * x, W' * Δ, and Δ * x' for the gradient of W. With the first version of #880, the gradient of a Flux MLP loss with a WGAN-GP penalty went from 2.0 to 1.45 ms on CPU.

🧔‍♂️🤖 Assisted by Claude Opus 5.5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions