-
Notifications
You must be signed in to change notification settings - Fork 166
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
Remove grad and gradx members from variable exprs #177
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Jan, thanks a lot for the fix. In the end you managed to find a solution that required very little changes. Very good. I have just kindly requested you to add a short comment to two methods below, just to make it clear what are their roles. I've added a memory leak check in the CI.
@@ -254,6 +255,9 @@ struct Expr | |||
/// Destructor (to avoid warning) | |||
virtual ~Expr() {} | |||
|
|||
virtual void bind_value(T* /* grad */) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a short (Doxygen ///) comment here for these bind_*
functions. I like your approach.
I'm glad you approve! By the way, you could cut if(AUTODIFF_SANITIZE_TESTS)
target_compile_options(autodiff-cpptests PRIVATE "-fsanitize=address,undefined")
target_link_options(autodiff-cpptests PRIVATE "-fsanitize=address,undefined")
endif() ... Although that only works for linux and mac and would need safing against windows |
Interesting. I'm OK with either approach. If the CI fails again, could you
please introduce this then (the AUTODIFF_SANITIZE_TESTS logic)?
… |
Setting |
MSVC did add Asan a while ago, so I thought it might be worth trying via the compiler flag testing. Not sure what's been failing the CI pipeline though. |
.github/workflows/windows.yml
Outdated
@@ -49,7 +49,7 @@ jobs: | |||
shell: powershell | |||
run: | | |||
conda activate autodiff | |||
cmake -S . -B .build | |||
cmake -S . -B .build -DAUTODIFF_TEST_SANITIZE=ON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this works in Windows. I'll remove this (the only thing missing to get CI fully passing)
hi, I do functions similar to the following I understand the reasons for the changes, but if i understand it correctly, it could have been limited to the gradx case. imo it's also a nicer design, if you can get the gradient from the variable directly. anyway, what i actually want to say, is, that it would be nice if you also consider such use cases, i.e., don't try to completely hide the implementation, because that makes it harder to adapt for certain use cases (another example is, that some important functionality is now in detail, which also broke my code). |
Hi Adam, many thanks for reporting this. You're right that the memory leak issue was caused only when I would accept changes that revert the |
This reverts parts of commit 44e088c. The var.grad() api is more convenient than requiring to first bind a float and then reading it back. for instance, expr.propagate() and var.grad() can be used in unit tests. see also autodiff#177 (comment)
Implementation of an idea from #160 removing
grad
andgradx
members fromVariableExpr
in favor of value and expression pointers to which updates can be written during expression tree traversal.Fixes the associated regressions in derivative calculation, too. The tests no longer leak memory.
Some details:
bind_value
andbind_expr
to theExpr<T>
interface. For instanceexpr.bind_value(nullptr)
can be replaced with astatic_cast<VariableExpr<T>*>(expr.get())->gradPtr = nullptr;
because this is only ever called on variable expressions. Any preference you might have there is easy to adopt.nullptr
checking inVariableExpr<T>::propagate
and::propagatex
could just be replaced with anassert
for debug build checking if you have runtime performance concerns.