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

Handling Dynamic parameters in CppADCodeGen #18

Open
jcarpent opened this issue Aug 26, 2018 · 23 comments
Open

Handling Dynamic parameters in CppADCodeGen #18

jcarpent opened this issue Aug 26, 2018 · 23 comments

Comments

@jcarpent
Copy link
Contributor

jcarpent commented Aug 26, 2018

Current master branch of CppAD allows the use of dynamic parameters.
In other words, it is possible to change at running time some parameters values that are not consider as Independent variables.
See https://github.com/coin-or/CppAD/blob/46be2e2dbcaba47d8a38e8d9110efc48c7245754/cppad/core/new_dynamic.hpp#L103 for further details.

I want to know how to implement this feature in CppADCOdeGen.
@joaoleal can you explain me where I need to edit this new feature?

@jcarpent
Copy link
Contributor Author

@joaoleal And also if you have some guess on how to proceed?

@jcarpent jcarpent changed the title Dynamic paremeter Handling Dynamic paremeters in CppADCodeGen Aug 26, 2018
@jcarpent jcarpent changed the title Handling Dynamic paremeters in CppADCodeGen Handling Dynamic parameters in CppADCodeGen Aug 26, 2018
@joaoleal
Copy link
Owner

I have not update CodeGen to deal with the changes in CppAD explicitly.
However, it is already possible to do this in CppADCodeGen since you can specify which derivatives you are interested in.
A model can be taped considering all variables/parameters and then the source generation can produce derivatives for custom sparsity patterns (e.g., excluding derivatives for a subset of variables, a.k.a, parameters).
This allows you to redefine values for parameters after the taping/generation process for which no derivatives are computed.
I've used this multiple times.
For instance, see https://github.com/joaoleal/CppADCodeGen/blob/master/test/cppad/cg/model/dynamiclib/dynamic.cpp.

I would say that there is no need to make changes to CppADCodegen for the new feature of CppAD.
It should just work, now you don't have to define custom sparsity patterns.
I have not tested it though.

@joaoleal
Copy link
Owner

I need to take a better look at the implementation in cppad.
It might not be working the way I initially thought.
Is there a complete example using parameters?

@jcarpent
Copy link
Contributor Author

@bradbell
Copy link
Collaborator

The basic idea is that all of the sparsity patters and derivative computations are relative to the variables and do not include the dynamic parameters.

You can find extensive use of dynamic parameters in the test file
https://github.com/coin-or/CppAD/blob/master/test_more/general/new_dynamic.cpp

I am experimenting, and have seen some improvement, with the effect on speed and memory in the cppad_mixed project:
https://github.com/bradbell/cppad_mixed

It is hard for me to know what the effect would be for the CppADCodeGen project. I would suggest just implementing a small subset of operations and tests to see if it helps.

@joaoleal
Copy link
Owner

I started adapting CppADCodeGen to support CppAD parameters but it is quite an invasive change.
It will take some time to get it right.
I am currently adding an additional argument for parameters in most model methods (CppADCodeGen models are stateless).
However, this is breaking some functionality which depends on all data coming from the independent variables.
I will push a new branch once I have something that compiles...

@jcarpent
Copy link
Contributor Author

Sounds really great.
Thanks for these code developments.

@joaoleal
Copy link
Owner

joaoleal commented Sep 3, 2018

Started a new branch: feature/parameters
It is still work in progress and I might still make changes to the API but there is a test already working: dynamic_with_params.cpp

@jcarpent
Copy link
Contributor Author

jcarpent commented Sep 4, 2018

Ok thanks for reporting this adding.
I will have a look to the code to see the API modifications.

@jcarpent
Copy link
Contributor Author

@joaoleal Do you have some news for this feature?

@joaoleal
Copy link
Owner

joaoleal commented Oct 24, 2018 via email

@jcarpent
Copy link
Contributor Author

It sounds great. Thanks for this quick feedback.
I'll wait for the next feature from CppAD.

@bradbell
Copy link
Collaborator

Thanks for the feedback, that this is important and holding you back. I will try to get to it soon.

@bradbell
Copy link
Collaborator

I just realized that this issue is still outstanding. Does the atomic_three interface
https://coin-or.github.io/CppAD/doc/atomic_three.htm
satisfy the requirements for this issue ?

@joaoleal
Copy link
Owner

It suits my current needs but there is still a test failing related to custom Hessian sparsity patterns. Hopefully I will have some time during the Christmas holidays to solve it.

@mohamdev
Copy link

mohamdev commented May 3, 2020

Hi, Is there a way to use dynamic parameters with CppADCodeGen, or is this issue still topical ?

@joaoleal
Copy link
Owner

joaoleal commented May 4, 2020

I haven't completed the work on the branch yet.
At the moment the way to use dynamic parameters in CppADCodeGen is to consider them as regular variables and to define custom sparsity patterns without them.
The end result is the same but you do not configure dynamic parameters directly in CppAD.

@mohamdev
Copy link

mohamdev commented May 5, 2020

Great, thank you very much for your feedback :)

@bradbell
Copy link
Collaborator

bradbell commented May 6, 2020

Using the techniques in
#34 (comment)

You can compute derivatives any way you like in CppAD (using dynamic parameters and such) and then, put those computations in the zero order forward mode for a CppAD function and then generate the source code for that function.

@mohamdev
Copy link

mohamdev commented May 7, 2020

Thank you for the example, it's really helpful to me

@bradbell
Copy link
Collaborator

bradbell commented Mar 15, 2021

One thing that may not be obvious is that in order to use base2ad with atomic functions, the atomic functions need to define the AD<Base> case; see AD<Base> in atomic forward
https://coin-or.github.io/CppAD/doc/atomic_three_forward.htm#AD&lt;Base>
and in atomic reverse
https://coin-or.github.io/CppAD/doc/atomic_three_reverse.htm#AD&lt;Base>

These implementations many in turn call atomic functions to do their operations; e.g., the derivatives of matrix multiply may in tern use matrix multiply; see
https://coin-or.github.io/CppAD/doc/atomic_two_eigen_mat_mul.hpp.htm
I have not yet converted this example to use the atomic_three interface nor the AD<Base> case.

@chrisFlexiv
Copy link

By any chance, do you have an update for this feature? Thanks!

@bradbell
Copy link
Collaborator

bradbell commented Jan 5, 2022

I am working on an example that implements element-wise vector operations as atomic functions. This shows how to implement the AD derivative calculations using atomic functions; see
https://coin-or.github.io/CppAD/doc/atomic_vector.htm

I plan to extend this example to include all the possible atomic function calculations and all the standard math functions.
Once I have this implemented where the base class is double, I plan to convert it to the case where the base class is a template parameter.

@joaoleal I am not sure how one would use such atomic functions in CppADCodeGen (once the base type is a template parameter) ?

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

No branches or pull requests

5 participants