Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

convert to immutable for CUDA tests #151

Closed
wants to merge 12 commits into from
Closed

Conversation

m-bossart
Copy link
Contributor

Converts NonlinearProblem to ImmutableNonlinearProblem for CUDA compatibility (SciML/NonlinearSolve.jl#439)

@m-bossart
Copy link
Contributor Author

m-bossart commented Jun 7, 2024

Tests are failing due to this methoderror. I'm not sure why given the constructor in the new ImmutableNonlinearProblem struct :

MethodError: no method matching SimpleNonlinearSolve.ImmutableNonlinearProblem(::SciMLBase.NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(Main.var"##CUDA Kernel Launch Test#1070".f), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, ::StaticArraysCore.SVector{2, Float32}, ::SciMLBase.NullParameters, ::SciMLBase.StandardNonlinearProblem)

Comment on lines 85 to 86
function SciMLBase.solve(prob::Union{ImmutableNonlinearProblem}, alg::AbstractSimpleNonlinearSolveAlgorithm,
args...; sensealg = nothing, u0 = nothing, p = nothing, kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conversion should go here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the conversion to inside the solve in the last commit. Now I'm getting a method error when precompiling due to the startup workload. It is complaining about this line when solving a problem that has been converted and its not clear to me how to fix it:

fx = prob.f(x, prob.p)

@ChrisRackauckas ChrisRackauckas requested a review from avik-pal June 11, 2024 10:50
@avik-pal
Copy link
Member

I will review it in detail later, but tagging @Vaibhavdixit02 because it will very likely break GPUPSO. Maybe we tag a breaking release just for safety

@Vaibhavdixit02
Copy link
Member

Since that package wasn't released, you should be fine on that end.

@ChrisRackauckas
Copy link
Member

GPUPSO should be fine if this is done right? We just need to make sure the kernels use everything immutable.

Comment on lines 56 to 65
staticarray_itize(x) = x
staticarray_itize(x::Vector) = SVector{length(x)}(x)
staticarray_itize(x::SizedVector) = SVector{length(x)}(x)
staticarray_itize(x::Matrix) = SMatrix{size(x)...}(x)
staticarray_itize(x::SizedMatrix) = SMatrix{size(x)...}(x)

function Base.convert(::Type{ImmutableNonlinearProblem}, prob::T) where {T <: NonlinearProblem}
ImmutableNonlinearProblem{isinplace(prob)}(prob.f,
staticarray_itize(prob.u0),
staticarray_itize(prob.p),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't going to be type stable, we shouldn't copy this part. Don't automatically make things static array, just immutable the problem.

@@ -22,7 +22,7 @@ end

__get_linesearch(::SimpleBroyden{LS}) where {LS} = Val(LS)

function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleBroyden, args...;
function SciMLBase.__solve(prob::Union{NonlinearProblem, ImmutableNonlinearProblem}, alg::SimpleBroyden, args...;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these unions should be required. If done correctly, the top level should change to an ImmutableNonlinearProblem, and then they should all be that.

@ChrisRackauckas
Copy link
Member

Left a few comments. If those two things are done this should be quick to finish.

@m-bossart
Copy link
Contributor Author

I made changes according to your comments. The simple adjoint test is failing with this error:
Cannot determine ordering of Dual tags Nothing and ForwardDiff.Tag{Base.Fix2{SciMLBase.NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(Main.var"##Simple Adjoint Test#264".ff), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing}, Vector{Float64}}, Float64}

Any thoughts on the fix for that?

@m-bossart
Copy link
Contributor Author

These tests are also failing: https://github.com/SciML/SimpleNonlinearSolve.jl/blob/40958d2c2fdff0986d113ea1f5ed1ffa96316e89/test/gpu/cuda_tests.jl#L40C1-L40C107

Doesn't the kernel_function need to change given that the conversion to an Immutable struct happens within the solve call? I'm not sure how that test should be restructured.

@ChrisRackauckas
Copy link
Member

They kernel function needs to be the immutable form. And this would need an FAQ entry.

Any thoughts on the fix for that?

Add the immutable stuff to the AD dispatches?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants