Skip to content
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

PolyesterForwardDiff automatic chunksize selection? #546

Closed
avik-pal opened this issue Oct 7, 2024 · 4 comments · Fixed by #547
Closed

PolyesterForwardDiff automatic chunksize selection? #546

avik-pal opened this issue Oct 7, 2024 · 4 comments · Fixed by #547
Labels
backend Related to one or more autodiff backends bug Something isn't working

Comments

@avik-pal
Copy link

avik-pal commented Oct 7, 2024

Currently PolyesterForwardDiff forwards a chunksize of nothing. I think it would make sense to reuse the automatic chuunksize selection of ForwardDiff here as well

@gdalle
Copy link
Owner

gdalle commented Oct 7, 2024

I think this already happens here?

function single_threaded(backend::AutoPolyesterForwardDiff{C,T}) where {C,T}
return AutoForwardDiff{C,T}(backend.tag)
end
DI.check_available(::AutoPolyesterForwardDiff) = true
function DI.pick_batchsize(backend::AutoPolyesterForwardDiff, dimension::Integer)
return DI.pick_batchsize(single_threaded(backend), dimension)
end

@avik-pal
Copy link
Author

avik-pal commented Oct 7, 2024

Interesting. I got the error:

[OOP] u0: SVector{2, Float64}: Error During Test at REPL[87]:7
  Got exception outside of a @test
  MethodError: no method matching rem(::Int64, ::Type{Union{Nothing, Int64}})
  
  Closest candidates are:
    rem(::Any, ::Any, ::RoundingMode{:FromZero})
     @ Base div.jl:103
    rem(::Any, ::Any, ::RoundingMode{:Nearest})
     @ Base div.jl:100
    rem(::Any, ::Any, ::RoundingMode{:Up})
     @ Base div.jl:99
    ...
  
  Stacktrace:
    [1] cld_fast(a::Int64, b::Nothing)
      @ PolyesterForwardDiff /mnt/.julia/packages/PolyesterForwardDiff/oqvIt/src/PolyesterForwardDiff.jl:10
    [2] threaded_jacobian!(f::DifferentiationInterface.FixTail{NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(quadratic_f), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, Tuple{Float64}}, Δx::Matrix{Float64}, x::SVector{2, Float64}, ::ForwardDiff.Chunk{nothing}, check::Val{false})
      @ PolyesterForwardDiff /mnt/.julia/packages/PolyesterForwardDiff/oqvIt/src/PolyesterForwardDiff.jl:113
    [3] threaded_jacobian!(f::DifferentiationInterface.FixTail{NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(quadratic_f), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, Tuple{Float64}}, Δx::Matrix{Float64}, x::SVector{2, Float64}, ::ForwardDiff.Chunk{nothing})
      @ PolyesterForwardDiff /mnt/.julia/packages/PolyesterForwardDiff/oqvIt/src/PolyesterForwardDiff.jl:112
    [4] jacobian!(f::Function, jac::Matrix{Float64}, ::DifferentiationInterface.NoJacobianPrep, ::AutoPolyesterForwardDiff{nothing, Nothing}, x::SVector{2, Float64}, contexts::DifferentiationInterface.Constant{Float64})
      @ DifferentiationInterfacePolyesterForwardDiffExt /mnt/.julia/packages/DifferentiationInterface/gPcSq/ext/DifferentiationInterfacePolyesterForwardDiffExt/onearg.jl:205
    [5] jacobian(f::Function, prep::DifferentiationInterface.NoJacobianPrep, backend::AutoPolyesterForwardDiff{nothing, Nothing}, x::SVector{2, Float64}, contexts::DifferentiationInterface.Constant{Float64})
      @ DifferentiationInterfacePolyesterForwardDiffExt /mnt/.julia/packages/DifferentiationInterface/gPcSq/ext/DifferentiationInterfacePolyesterForwardDiffExt/onearg.jl:229
    [6] jacobian(f::NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(quadratic_f), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, backend::AutoPolyesterForwardDiff{nothing, Nothing}, x::SVector{2, Float64}, contexts::DifferentiationInterface.Constant{Float64})
      @ DifferentiationInterface /mnt/.julia/packages/DifferentiationInterface/gPcSq/src/fallbacks/no_prep.jl:49
    [7] (::SimpleNonlinearSolve.Utils.var"#4#6"{ImmutableNonlinearProblem{SVector{2, Float64}, false, Float64, NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(quadratic_f), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, AutoPolyesterForwardDiff{nothing, Nothing}})(u::SVector{2, Float64}, p::Float64)
      @ SimpleNonlinearSolve.Utils /mnt/software/nonlinearsolve/NonlinearSolve.jl/lib/SimpleNonlinearSolve/src/utils.jl:228
    [8] value_and_jacobian(f::Function, prep::DifferentiationInterface.NoJacobianPrep, backend::AutoPolyesterForwardDiff{nothing, Nothing}, x::SVector{2, Float64}, contexts::DifferentiationInterface.Constant{Float64})
      @ DifferentiationInterfacePolyesterForwardDiffExt /mnt/.julia/packages/DifferentiationInterface/gPcSq/ext/DifferentiationInterfacePolyesterForwardDiffExt/onearg.jl:215
    [9] value_and_jacobian(f::SimpleNonlinearSolve.Utils.var"#4#6"{ImmutableNonlinearProblem{SVector{2, Float64}, false, Float64, NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(quadratic_f), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, AutoPolyesterForwardDiff{nothing, Nothing}}, backend::AutoPolyesterForwardDiff{nothing, Nothing}, x::SVector{2, Float64}, contexts::DifferentiationInterface.Constant{Float64})
      @ DifferentiationInterface /mnt/.julia/packages/DifferentiationInterface/gPcSq/src/fallbacks/no_prep.jl:61

I can give you a better MWE once SciML/NonlinearSolve.jl#458 this massive PR is merged

@gdalle
Copy link
Owner

gdalle commented Oct 7, 2024

My bad, I see what you mean. My custom batch size selection is forwarded, but in methods like this one

function DI.gradient!(
f,
grad,
::GradientPrep,
::AutoPolyesterForwardDiff{K},
x::AbstractVector,
contexts::Vararg{Context,C},
) where {K,C}
fc = with_contexts(f, contexts...)
threaded_gradient!(fc, grad, x, Chunk{K}())
return grad
end

I should distinguish on whether the chunksize in the backend is nothing

@gdalle
Copy link
Owner

gdalle commented Oct 7, 2024

It's an easy fix, I'll do it tomorrow

@gdalle gdalle added bug Something isn't working backend Related to one or more autodiff backends labels Oct 7, 2024
@gdalle gdalle linked a pull request Oct 7, 2024 that will close this issue
7 tasks
@gdalle gdalle mentioned this issue Oct 8, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to one or more autodiff backends bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants