Rules that use scalar indexing are not GPU-compatible
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- julia
- 領域
- backend, performance
調査の方向性
src/rulesets/LinearAlgebra/norm.jl の _normInf_back の実装付近から始め、スカラーインデックスを無効にして CUDA MWE を実行します。rulesets 内で同様のスカラーインデックスを検索し、提案されている @allowscalar と size-1 view のアプローチを比較します。影響を受けるルールが、スカラーインデックスのエラーを発生させずに GPU arrays で動作すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Some rules use scalar indexing, breaking GPU compatibility, e.g., https://github.com/JuliaDiff/ChainRules.jl/blob/3b3791f10bc88c41f004fbb9eb229745d1764593/src/rulesets/LinearAlgebra/norm.jl#L187
One solution would be to use @allowscalar from GPUArrays, but one concern about adding that dependency is the loading time (h/t @mcabbott).
Another is to be clever and replace all scalar indexing by size-1 views, such as:
@inbounds @views ∂x[yind:yind] .= sign.(x[yind:yind]) .* Δu
For people unfamiliar with GPU programming and @allowscalar: The reason scalar setindex/getindex is disallowed for GPU arrays is that sequential processing of GPU arrays from loopy CPU code defeats the purpose of using the GPU and leads to terrible performance. However, scalar setindex/getindex is of course legitimate in cases like the above, where you're not in a loop and only intend to set or retrieve that single element in the array. @allowscalar from GPUArrays is how you indicate that an exception should be made in any given instance, e.g.,
@inbounds @allowscalar ∂x[yind] = sign(x[yind]) * Δu
See https://cuda.juliagpu.org/stable/usage/workflow/#UsageWorkflowScalar for more about this mechanism
MWE:
using ChainRules
using CUDA
using LinearAlgebra
CUDA.allowscalar(false)
x = CUDA.ones(3)
nx, pb = ChainRules.rrule(norm, x, Inf)
ChainRules.unthunk(pb(1f0)[2])
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] assertscalar(op::String)
@ GPUArrays ~/.julia/packages/GPUArrays/Zecv7/src/host/indexing.jl:53
[3] getindex
@ ~/.julia/packages/GPUArrays/Zecv7/src/host/indexing.jl:86 [inlined]
[4] findprev(testf::ChainRules.var"#1770#1771"{Float32}, A::CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, start::Int64)
@ Base ./array.jl:2151
[5] findlast
@ ./array.jl:2199 [inlined]
[6] _normInf_back(x::CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, y::Float32, Δy::Float32)
@ ChainRules ~/.julia/packages/ChainRules/SrdPq/src/rulesets/LinearAlgebra/norm.jl:185
[7] (::ChainRules.var"#1742#1746"{Float32, CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Float64, Float32})()
@ ChainRules ~/.julia/packages/ChainRules/SrdPq/src/rulesets/LinearAlgebra/norm.jl:49
[8] unthunk
@ ~/.julia/packages/ChainRulesCore/RbX5a/src/tangent_types/thunks.jl:195 [inlined]
[9] unthunk(x::ChainRulesCore.InplaceableThunk{ChainRulesCore.Thunk{ChainRules.var"#1742#1746"{Float32, CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Float64, Float32}}, ChainRules.var"#1741#1745"{Float32, CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Float64, Float32}})
@ ChainRulesCore ~/.julia/packages/ChainRulesCore/RbX5a/src/tangent_types/thunks.jl:222
[10] top-level scope
@ REPL[329]:1
[11] top-level scope
- 主要言語
- Julia
- スター
- 475
- フォーク
- 98
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
JuliaDiff/ChainRules.jl のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
JuliaDiff/ChainRules.jl#844 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 55/100
JuliaDiff/ChainRules.jl#818 ·
-
refactoring
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
JuliaDiff/ChainRules.jl#815 ·
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
JuliaDiff/ChainRules.jl#810 ·
-
stack overflow issueオープン
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
JuliaDiff/ChainRules.jl#809 ·
JuliaDiff/ChainRules.jl の issue をすべて見る
似ている issue
-
難易度 2/5 1時間未満 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
CliMA/ClimaAtmos.jl#4891 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
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 日以内に返信