LinearAlgebra.mul! with Matrix{Dual} does not dispatch to BLAS
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 52/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- julia
- 領域
- performance
調査の方向性
Start by running the provided Julia reproducer and inspecting the 3- and 5-argument LinearAlgebra.mul! paths for Matrix{Dual}. The work is done when GEMM cases involving Dual matrices use BLAS-backed operations where applicable and retain the correctness shown by the final comparison.
索引モデルが issue の本文から書いたものです。
説明
I have noticed that GEMM of matrices involving Dual numbers are not dispatched to BLAS, which significantly impacts performance. This happens with 3- and 5-arguments versions of LinearAlgebra.mul!. Generally, any GEMM of matrices resulting in a Matrix{<:Dual} is impacted, e.g. mul!(C, A, B) will be slow if A isa Matrix{<:Dual} and B isa Matrix{Float64}.
This is illustrated in the reproducer below for the 3-argument method. To speed things up, matrices are split into values and partials, and all the cross terms explicitly calculated. These are dispatched to BLAS because values and partials are Matrix{<:BlasFloat}. On my machine, I get ~30x speedup for this example. The exact number varies with matrix size and number of partials, but the workaround is systematically faster.
sing ForwardDiff
using LinearAlgebra
# Build random Dual-valued matrices.
function random_dual_matrix(m, n, N)
[ForwardDiff.Dual(rand(), ntuple(_ -> rand(), N)) for _ in 1:m, _ in 1:n]
end
# BLAS-capable baseline for Dual matrices: split into value and each partial,
# call ordinary BLAS-backed mul! on the plain arrays, and reassemble the result.
function mul_dual_blas!(C, A, B)
N = ForwardDiff.npartials(eltype(A))
A_val = ForwardDiff.value.(A)
B_val = ForwardDiff.value.(B)
A_parts = ntuple(i -> ForwardDiff.partials.(A, i), N)
B_parts = ntuple(i -> ForwardDiff.partials.(B, i), N)
C_val = ForwardDiff.value.(C)
C_parts = ntuple(i -> ForwardDiff.partials.(C, i), N)
mul!(C_val, A_val, B_val) # BLAS for values
for i in 1:N
# ∂C_i = ∂A_i * B + A * ∂B_i
mul!(C_parts[i], A_parts[i], B_val)
mul!(C_parts[i], A_val, B_parts[i], true, true)
end
# Reconstruct C from value and partials.
for j in 1:size(C, 2), i in 1:size(C, 1)
C[i, j] = ForwardDiff.Dual(
C_val[i, j],
ForwardDiff.Partials(ntuple(l -> C_parts[l][i, j], N))
)
end
C
end
N_partials = 3
m, n, k = 500, 400, 300
A = random_dual_matrix(m, k, N_partials)
B = random_dual_matrix(k, n, N_partials)
C_generic = similar(A, m, n)
C_blas = similar(A, m, n)
# Warm up both paths before timing.
mul!(C_generic, A, B)
mul!(C_generic, A, B)
mul_dual_blas!(C_blas, A, B)
mul_dual_blas!(C_blas, A, B)
# Time generic 3-arg mul!.
println("Timing generic 3-arg mul! with Matrix{Dual}...")
t_generic = minimum(@elapsed mul!(C_generic, A, B) for _ in 1:5)
# Time BLAS-based baseline.
println("Timing BLAS-based baseline...")
t_blas = minimum(@elapsed mul_dual_blas!(C_blas, A, B) for _ in 1:5)
println()
println("Generic mul! : $(round(t_generic; digits=4)) s")
println("BLAS baseline: $(round(t_blas; digits=4)) s")
println("Speedup : $(round(t_generic / t_blas; digits=2))x")
# Verify correctness.
mul!(C_generic, A, B)
mul_dual_blas!(C_blas, A, B)
@assert C_generic ≈ C_blas
println()
println("Results match.")
- 主要言語
- Julia
- スター
- 1k
- フォーク
- 162
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
JuliaDiff/ForwardDiff.jl のほかの issue
-
missing floatmin, floatmax, and issubnormal for Dual対応中かも @devmotion が 13 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
JuliaDiff/ForwardDiff.jl#852 · コメント 6 件 ·
-
Enable NaN-safe mode by default対応中かも @devmotion が今日担当しました。 オープン
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
JuliaDiff/ForwardDiff.jl#857 · リアクション 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 75/100
JuliaDiff/ForwardDiff.jl#849 ·
-
`gradient!` leaves part of the result unwritten when `f` does not depend on its argument対応中かも @devmotion が 42 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 78/100
JuliaDiff/ForwardDiff.jl#847 ·
-
hessian!(::ImmutableDiffResult, f, ::StaticArray) errors when f does not depend on its argument対応中かも @devmotion が 43 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 76/100
JuliaDiff/ForwardDiff.jl#846 · コメント 1 件 ·
JuliaDiff/ForwardDiff.jl の issue をすべて見る
似ている issue
-
tagbot-manual
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
tshort/StaticCompiler.jl#191 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
SciML/SciMLSensitivity.jl#1703 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
JuliaGPU/GPUCompiler.jl#1008 ·
メンテナーはふだん 1 日以内に返信
-
`biggs6` disagrees with CUTEst and is unbounded below対応中かも @timholy が 1 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
Cannot set point coordinates with `Range`s (or arbitrary arrays) using `Makie.Resampler` in `heatmap`対応中かも @ffreyer が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信