Destructuring/iterating a `Tangent` does not unthunk fields, unlike `getindex`/`getproperty`

オープン
#712 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
28/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
領域
devtools

調査の方向性

Start in src/tangent_types/structural_tangent.jl, comparing the unthunk calls in getindex and getproperty with the Base.iterate and Base.indexed_iterate forwarding. Reproduce the snippet with collect(t) and first(nt) to see the thunks. Done means the maintainers have chosen between unthunking iteration and documenting indexed access, and the chosen option is implemented or documented with tests. The design choice is still open, so confirm it in the thread before writing code.

索引モデルが issue の本文から書いたものです。

説明

Field access on a Tangent unthunks the field, but iteration does not:

julia> using ChainRulesCore

julia> t = Tangent{Tuple{Float64,Vector{Float64}}}(1.0, @thunk([2.0]));

julia> t[2]          # Vector{Float64}

julia> _, b = t; b   # Thunk

julia> collect(t)    # [1.0, Thunk]

julia> nt = Tangent{NamedTuple{(:a,),Tuple{Vector{Float64}}}}(a=@thunk([1.0]));

julia> nt.a          # Vector{Float64}

julia> (; a) = nt; a # Vector{Float64}

julia> first(nt)     # Thunk

getindex/getproperty call unthunk (src/tangent_types/structural_tangent.jl, since #121), whereas Base.iterate and Base.indexed_iterate forward to the backing without unthunking.

This caused a bug in ChainRules (JuliaDiff/ChainRules.jl#843): the findnz pullbacks did _, _, V̄ = unthunk(Δ). With Zygote, Δ is Tangent{Any}((ZeroTangent(), ZeroTangent(), thunk)), so V̄ was still a thunk and sparse(I, J, V̄, m, n) threw a MethodError.

There are two possible resolutions:

  1. Make iterate/indexed_iterate unthunk, for consistency with getindex/getproperty. This has a downside: positional destructuring would then also compute components the rule discards, e.g. the _ in _, _, V̄ = Δ.
  2. Keep the current behaviour, but document that rules should access the fields they need via getindex/getproperty (or property destructuring (; a) = Δ) and not via positional destructuring or iteration.

Option 2 seems preferable for performance, but the current behaviour is a silent footgun either way. ChainRules.jl#843 now uses indexing (unthunk(Δ)[3]).

主要言語
Julia
スター
267
フォーク
66
平均マージ
1日 1時間
マージ済み PR(30日)
9

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JuliaDiff/ChainRulesCore.jl のほかの issue

JuliaDiff/ChainRulesCore.jl の issue をすべて見る

似ている issue

Julia の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。