biodiverse / ubms

Fit models to data from unmarked animals using Stan. Uses a similar interface to the R package 'unmarked', while providing the advantages of Bayesian inference and allowing estimation of random effects.
https://biodiverse.github.io/ubms/
GNU General Public License v3.0
35 stars 8 forks source link

How did you fix the Clang-UBSAN in 1.0.2 release? #50

Closed formidify closed 3 years ago

formidify commented 3 years ago

Hi Ken,

Sorry for posting this here, but I am just wondering how you fixed the Clang-UBSAN error in your 1.0.2 package release. I encountered a similar error in my R package (which depends on RStan) and I just couldn't find a way to fix it.

Thank you so much! James

kenkellner commented 3 years ago

The hardest part for me was getting R-devel to compile properly with the UBSAN flags so that I could replicate CRAN's error messages. I'm not sure if you've gotten to that point; if not this guide was helpful. I also had problems with this modified R running out of memory, solved by this answer.

Even when I got everything set up I couldn't replicate the UBSAN errors 100% of the time, I'd often have to run the tests/examples dozens of times to get them to trigger.

With all that said, we might be in different situations. While ubms uses Stan, the UBSAN errors I encountered actually came from my own C++ code. I was using unsigned integer matrices with Armadillo (type umat), and my inputs could sometimes have NAs which was the issue. I changed to matrices of doubles (type mat) and that solved things.

I looked at the UBSAN errors your package generated and it seems like they are coming from the Stan C++ code which means you would have to talk to them to get things fixed. Before doing that, once you are able to replicate the error locally, I would try playing around with the input data for the model that caused the error (does it have NaNs?) and tinker with it until it doesn't error anymore. That might help narrow things down, or you might just modify the example to one that doesn't cause the error.

In general I have observed a lot of weird errors/behavior of Stan models when they run on CRAN servers. Very frustrating to deal with since I can't reliably replicate their environments.

Hope that's helpful,

Ken

formidify commented 3 years ago

Thanks Ken for the help! Really appreciate your quick response. I tried various approaches to replicate the error, but didn't work. I ended up running my code with the unofficial CMDStanR package (https://mc-stan.org/cmdstanr/articles/cmdstanr.html) and I managed to replicate the NaN error.

For future references, the cause for the NaNs for me is that sometimes Stan samples very high values, and when coupled with the exp() function, the result ends up being Inf and then NaNs. To solve this, one should set some constraint the parameters.