`gradient!` leaves part of the result unwritten when `f` does not depend on its argument
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- julia
调研方向
从 src/gradient.jl 中 extract_gradient! 附近开始,检查在进行 array 和 DiffResults 梯度提取时如何分派嵌套的 Dual 值。重现所提供的嵌套微分示例,然后验证梯度会向每个条目写入零,并验证 DiffResult 形式可以在没有转换错误的情况下完成。为这些情况添加或运行针对性的回归测试。
由索引模型根据 Issue 内容生成。
描述
When f does not depend on its argument but carries a perturbation from an enclosing differentiation, ForwardDiff.gradient! writes only part of the result and leaves the rest exactly as it found it. The gradient is zero in this situation, so every entry should be written.
using ForwardDiff, DiffResults
x = [1.0, 2.0, 3.0]
ForwardDiff.derivative(1.0) do a
out = fill(a * 111.0, 3) # a buffer with recognisable junk
ForwardDiff.gradient!(out, z -> a * 2.0, x)
@show ForwardDiff.value.(out) # [0.0, 111.0, 111.0], should be [0.0, 0.0, 0.0]
return zero(a)
end
The allocating form has the same defect against the buffer it just allocated, so it returns uninitialized memory. That is often zero, which hides the bug, but not always:
julia> ForwardDiff.derivative(1.0) do a
@show ForwardDiff.gradient(z -> a * 2.0, x)
return zero(a)
end
gradient = Dual{…}[Dual(0.0,0.0), Dual(2.3611913415e-314,2.361521164e-314), Dual(5.0e-324,2.260386021e-314)]
The DiffResult form errors instead:
ForwardDiff.derivative(1.0) do a
r = DiffResults.GradientResult(fill(a * 0.0, 3))
ForwardDiff.gradient!(r, z -> a * 2.0, x)
return zero(a)
end
# ERROR: MethodError: Cannot `convert` an object of type Dual{…} to an object of type Float64
The StaticArray path is correct and returns [0.0, 0.0, 0.0], because its @generated extract_gradient is built from length(x) and uses the indexed partials(T, y, i).
Cause
extract_gradient!(::Type{T}, result::AbstractArray, y::Real) where {T} = fill!(result, zero(y))
function extract_gradient!(::Type{T}, result::AbstractArray, dual::Dual) where {T}
idxs = structural_eachindex(result)
for (i, idx) in zip(1:npartials(dual), idxs)
result[idx] = partials(T, dual, i)
end
return result
end
A Dual{S,V,N} with S ≺ T carries no T-perturbation, so its gradient is zero everywhere and the ::Real method above is what it needs — but it is a Dual, so it takes the second method, where npartials reports N for the wrong layer. Whenever that N is smaller than length(x) the tail of the result is never written.
L59-L63 shares the dispatch and then passes partials(T, dual) to DiffResults.gradient!; for this case that is zero(dual), a scalar Dual rather than a Partials, which is the same shape problem as in #846.
Suggested fix
Dispatch on Dual{T} rather than on Dual, so a value carrying no T-perturbation falls through to the ::Real method, which already fills the whole result with zeros.
Version: ForwardDiff master (v1.4.5), Julia 1.12.7, StaticArrays 1.9.19.
- 主要语言
- Julia
- 星标
- 1k
- 派生
- 162
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 于 1 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 25/100
JuliaDiff/ForwardDiff.jl#857 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 52/100
JuliaDiff/ForwardDiff.jl#854 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 75/100
JuliaDiff/ForwardDiff.jl#849 ·
-
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 天内回复