nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
156 stars 23 forks source link

protect gradient methods in nimOptim C++ from when the user-provided fn or gr modifies the input vector. #1430

Closed perrydv closed 5 months ago

perrydv commented 6 months ago

I think I fixed this once before, but somehow evidently it didn't make it into devel. I'm not sure what happened.

The issue is that a nimOptim call provides an objective function and, optionally, a gradient function. In compiled code, these will take the parameter vector by reference, and they may modify it. If a parameter transformation is in use, they might even shorten the parameter vector. If the calling code assumes the parameter vector was unchanged, a bug can result. Minor changes are made in this PR to protect against that scenario.

Specifically, I believe this was causing a crash in test-ADlaplace, in the test involving a length-changing transformation (from a Dirichlet). There seems to be some kind of other intermittent issue coming up in test-ADlaplace, and I am not sure if they are related.

paciorek commented 5 months ago

This also deals with PR #1432