-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make extract_gradient[_chunk]! GPU compatible #619
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -80,12 +80,12 @@ function extract_gradient!(::Type{T}, result::DiffResult, dual::Dual) where {T} | |||||||||||||
end | ||||||||||||||
|
||||||||||||||
extract_gradient!(::Type{T}, result::AbstractArray, y::Real) where {T} = fill!(result, zero(y)) | ||||||||||||||
extract_gradient!(::Type{T}, result::AbstractArray, dual::Dual) where {T}= copyto!(result, partials(T, dual)) | ||||||||||||||
extract_gradient!(::Type{T}, result::AbstractArray, dual::Dual) where {T} = | ||||||||||||||
extract_gradient_chunk!(T, result, dual, 1, npartials(dual)) | ||||||||||||||
|
||||||||||||||
function extract_gradient_chunk!(::Type{T}, result, dual, index, chunksize) where {T} | ||||||||||||||
offset = index - 1 | ||||||||||||||
for i in 1:chunksize | ||||||||||||||
result[i + offset] = partials(T, dual, i) | ||||||||||||||
map!(view(Base.ReshapedArray(result, (length(result),), ()), index:index+chunksize-1), 1:chunksize) do i | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was looking around a bit and also browsing old julia issues - maybe we just have to use In any case, I think it would be good to add some explanations for why the function is implemented in this way:
Suggested change
|
||||||||||||||
@inbounds partials(T, dual, i) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really safe? The current implementation does not use
Suggested change
|
||||||||||||||
end | ||||||||||||||
return result | ||||||||||||||
end | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still somewhat unhappy about this change, intuitively it feels that it should be easier for base and packages to optimize
copyto!(::AbstractArray, ::Tuple)
than the chunk-based version. But if benchmarks don't show any regression, I guess it seems OK - and we don't have to wait for CUDA to add the implementation.