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 functions that do not propagate derivatives return prim types #2473
Conversation
Wouldn't this break? var x(1.0);
auto y = x + x;
auto z = ceil(y);
z.grad(); |
The exact code you wrote would not work. But if you need to calculate gradient of a variable you should specify its type as |
Personally idt it's a good idea for us to break code that works in user space currently. In the meta I think this also goes against the idea of how we promote to the least upper bound of our scalar types |
This will definitely break backward compatibility in the math lib by changing the return type. Otherwise, it's safe in that no meaningful derivatives get lost. So re-promoting as @t4c1 suggests is sound. Functions like |
They return 0. |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
This PR has been sitting here for a while. Given that there were some disagreements maybe we should vote on whether to merge it or not? |
Is it an option to discuss this at the monthly math meetings? |
Sure. When is the next meeting? |
The next one is scheduled on the 20th, but we could also just talk about this after the regular Stan meeting this week |
Either is fine by me. Just tell me when do you want me to attend. |
Let's put it on the agenda for this week's Stan meeting on Thursday |
I am not sure the Stan meeting is best to resolve right away, but we can try to settle on a path how to resolve. |
So on today's meeting we can talk about this. |
I just saw that I have a conflict today and can't join the meeting. My view is that it is a massive change given that the type changes between inputs and outputs. I do see the potential performance benefit of this and it does makes sense conceptually. One can also argue that the new C++ world is anyway more "auto" style such that this is not an issue. In short I am willing to go down this route, but I would require at least a major version bump.... and I still have my worries with such a change for backward compatibility worries. |
We discussed the pros and cons of this change on May 20, 2021. See brief notes: This is going to introduce a breaking change to the Math library. I added a new label called |
I don't see what's breaking about this change. Things like the following still work, because var x = 10.3;
var y = ceil(x);
Is there a concrete example you're worried about? I don't know what you meant by "break" here. Sure, if we replaced the above with auto y = ceil(x); then we're changing the type of And how is this different than the changes that led to matrix functions returning expression templates and accepting them as arguments?
Why is making an improved design a downside? This seems similar to where a function designer now needs to think about whether to return a base matrix or a template expression. And it seems to "break" I don't care that much about this issue, but there are 20+ open PRs in math and this one's nearly a year old. So we should either close it or merge it. |
If I recall, the problem we'd have is not with the scalar functions but with containers. For example, if there was code that wrote:
where
I'm closing the PR. |
Thanks, @syclik---just what I was looking for. It totally breaks in C++. P.S. It'd work in Stan, but that's because @WardBrian updated so containers are covariant in Stan. |
Summary
Make functions that do not propagate derivatives (
ceil
,floor
,round
,trunc
) return prim types. This simplifies the code. Working with less autodiff variables could also be faster.Tests
No changes. This is just a refactor.
Side Effects
None.
Release notes
Functions that do not propagate derivatives (
ceil
,floor
,round
,trunc
) now return prim types.Checklist
Math issue #(issue number)
Copyright holder: Tadej Ciglarič
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make test-math-dependencies
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested