`vect` rrule pullbacks index a thunked cotangent once per argument (quadratic), same pattern as #860

已关闭 适合新手
#862 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
julia
领域
performance

调研方向

三个 vect_pullback 方法位于 src/rulesets/Base/array.jl。在每个方法顶部添加 ȳ = unthunk(ȳ),镜像针对 reduce 的 #861 修复。用 issue 的基准测试确认:thunked back(...) 的时间应下降到接近 unthunked 测量值。

由索引模型根据 Issue 内容生成。

描述

Follow-up to #860 / #861. The rrules for Base.vect (src/rulesets/Base/array.jl, three methods) read the input cotangent once per argument (projects[n](ȳ[n]), ȳ[n], NTuple{N}(ȳ)). When ȳ is a thunk, each access re-evaluates the thunk, so the pullback is quadratic in the number of arguments. This is the same pattern as the reduce(vcat/hcat) pullbacks fixed in #861.

using ChainRules, ChainRulesCore
for N in (256, 1024, 4096)
  X = ntuple(i -> iseven(i) ? 1.0 : 1.0f0, N)      # hits the Union{Number,AbstractArray{<:Number}} method
  y, back = rrule(Base.vect, X...)
  dY = @thunk(ones(length(y)))
  back(dY); back(unthunk(dY))
  t1 = minimum(@elapsed(back(dY)) for _ in 1:20); t2 = minimum(@elapsed(back(unthunk(dY))) for _ in 1:20)
  println("N=$N  thunk: ", round(t1*1e6, digits=1), " µs   unthunked: ", round(t2*1e6, digits=1), " µs")
end
N thunk after unthunk
256 28.1 µs 6.0 µs
1024 217 µs 24.2 µs
4096 2389 µs 95.7 µs

At N = 2048 all three methods (mixed numbers, Any, and uniform Float64) were measured at roughly 600 to 670 µs with a thunk against roughly 40 µs after unthunk. ChainRules 1.74.8, ChainRulesCore 1.26.8, Julia 1.13.2.

Proposal: ȳ = unthunk(ȳ) at the top of each vect_pullback. Downstream in Zygote the cotangent arriving from sum is a thunk, as in FluxML/Zygote.jl#1686.

🧔‍♂️🤖 Assisted by AI

主要语言
Julia
星标
479
派生
100
平均合并
1 天 8 小时
30 天内合并 PR
10

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JuliaDiff/ChainRules.jl 的其他 Issue

查看 JuliaDiff/ChainRules.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。