Skip to content

add sparsediff #202

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

Merged
merged 2 commits into from
Jul 21, 2019
Merged

add sparsediff #202

merged 2 commits into from
Jul 21, 2019

Conversation

huanglangwen
Copy link
Contributor

No description provided.

@devmotion
Copy link
Member

Will be interesting to see if this leads to similar compilation time regressions as the ones we observed in OrdinaryDiffEq and DelayDiffEq after the addition of SparseDiffTools.

@huanglangwen
Copy link
Contributor Author

Actually, the sparsediff functionality only depends on SparsediffTool's autodiff jacobian with colors which is rather independent from other parts.

@devmotion
Copy link
Member

In this discourse post I discuss how DelayDiffEq suddenly became completely unusable on Win32 and how the compilation on Win64 and Linux 64 bit increased by roughly 3 minutes, and how this change is clearly related to the PR that added SparseDiffTools to OrdinaryDiffEq. There is some fundamental problem with the integration of SparseDiffTools in OrdinaryDiffEq that I do not understand yet. Maybe it's not even caused by SparseDiffTools but by some dependencies it's pulling in.

colorvec=integrator.f.colorvec
sparsity=integrator.f.jac_prototype
jac=integrator.f.jac
J=jac isa SparseMatrixCSC ? similar(jac) : zeros(size(jac)...)
Copy link
Member

Choose a reason for hiding this comment

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

No need for splatting here, it seems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, what's splatting?

Copy link
Member

Choose a reason for hiding this comment

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

Splatting is the ..., and it leads to bad interactions with the compiler. It should be avoided whenever possible. Here it's not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, find an error. jac should be f.jac_prototype instead of f.jac which is a function. I was thinking about cache.J since cache.J sometimes is different from jac_prototype , but looks like it is not available here. I need to fix OrdDiffEq as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

else
J = ForwardDiff.derivative(f,x)
end
J = jacobian_autodiff(f,x,integrator,Val{DiffEqBase.has_colorvec(integrator.f)})
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if introducing those functions and dispatching is useful at all. Although I think that should not put too much stress on the compiler I'm looking for every tiny bit that could possibly explain a slowdown 😛 I just read through the documentation (https://docs.julialang.org/en/v1/manual/types/index.html#%22Value-types%22-1 and https://docs.julialang.org/en/v1/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)-1) and I'm worried that it's not useful at all to dispatch here. Additionally, according to the documentation one should rather use Val(...) instances for consistency 😮 I had never heard this before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well that's suggested by @ChrisRackauckas to reduce runtime brunch. And yeah, I should use Val(), sorry I didn't read docs carefully.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is an inproper use of Val though. The chunksize needs to be saved as a type, otherwise this will dynamic dispatch every time and be slow unless it infers. Why does this part of the code make a Val?

I don't think this is blocking though and can get cleaned up later.

function DiffEqBase.build_jac_config(alg::StochasticDiffEqAlgorithm,f,uf,du1,uprev,u,tmp,du2)
if !has_jac(f)
if alg_autodiff(alg)
jac_config = ForwardDiff.JacobianConfig(uf,du1,uprev,ForwardDiff.Chunk{determine_chunksize(u,alg)}())
jac_config = jac_cache_autodiff(alg,f,uf,du1,uprev,u,Val{DiffEqBase.has_colorvec(f)})
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@@ -47,6 +47,8 @@ module StochasticDiffEq

import DiffEqBase: iip_get_uf, oop_get_uf, build_jac_config

import SparseDiffTools: forwarddiff_color_jacobian!, ForwardColorJacCache
Copy link
Member

Choose a reason for hiding this comment

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

@devmotion
Copy link
Member

Already the first Travis tests indicate that this PR leads to severe compilation time regressions: a comparison of the times of the first tests in the different test groups for the latest commit on the master branch with the times for this commit reveals a consistent increase of around 2-3 minutes. In particular, if this is supposed to be a small change that is way too much IMO. I assume, on Win32 it will be much worse.

@devmotion
Copy link
Member

Apparently there are some issues with SparseDiffTools even after getting rid of all Int64: JuliaDiff/SparseDiffTools.jl#51 (comment)

@huanglangwen
Copy link
Contributor Author

The real performance issue is including sparsedifftools led to inclusion of many other packages like Zygote. It is possible to get rid of the sparsedifftools dependency once merging colored jacobian into ForwardDiff.

@ChrisRackauckas
Copy link
Member

It is possible to get rid of the sparsedifftools dependency once merging colored jacobian into ForwardDiff.

Yes, and that's the plan, but it'll take some time. So for now I think we are just going to @requires Zygote and go forward with this, since utilizing sparsity is just an absolutely major performance gain so I don't want to hold back on it. If it comes down to it, we can make colored forward differentiation its own package.

@ChrisRackauckas ChrisRackauckas merged commit 814e118 into SciML:master Jul 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants