`gradient!` leaves part of the result unwritten when `f` does not depend on its argument
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- julia
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Bắt đầu tại src/gradient.jl, quanh extract_gradient!, và kiểm tra cách các giá trị Dual lồng nhau được dispatch để trích xuất gradient cho array và DiffResults. Tái hiện các ví dụ vi phân lồng nhau đã cung cấp, sau đó xác minh rằng các gradient ghi số 0 vào mọi phần tử và dạng DiffResult hoàn tất mà không có lỗi chuyển đổi. Thêm hoặc chạy các kiểm thử hồi quy tập trung cho những trường hợp này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Julia
- Star
- 1k
- Fork
- 162
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của JuliaDiff/ForwardDiff.jl
-
missing floatmin, floatmax, and issubnormal for DualCó thể đã có người làm @devmotion đã nhận 13 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
JuliaDiff/ForwardDiff.jl#852 · 6 bình luận ·
-
Enable NaN-safe mode by defaultCó thể đã có người làm @devmotion đã nhận 1 ngày trước. Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
JuliaDiff/ForwardDiff.jl#857 · 1 reaction ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 52/100
JuliaDiff/ForwardDiff.jl#854 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 75/100
JuliaDiff/ForwardDiff.jl#849 ·
-
hessian!(::ImmutableDiffResult, f, ::StaticArray) errors when f does not depend on its argumentCó thể đã có người làm @devmotion đã nhận 43 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 76/100
JuliaDiff/ForwardDiff.jl#846 · 1 bình luận ·
Tất cả issue của JuliaDiff/ForwardDiff.jl
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
CliMA/ClimaAtmos.jl#4891 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
tagbot-manual
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
tshort/StaticCompiler.jl#191 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
SciML/SciMLSensitivity.jl#1703 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
JuliaGPU/GPUCompiler.jl#1008 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`biggs6` disagrees with CUTEst and is unbounded belowCó thể đã có người làm @timholy đã nhận 1 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100