-
Notifications
You must be signed in to change notification settings - Fork 8
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
Ensure reproducible if using model cache #470
base: main
Are you sure you want to change the base?
Conversation
@gowerc doesn't .Random.seed always exist? At least I get Restarting R session...
> exists(".Random.seed")
[1] TRUE
> ls()
character(0)
> .Random.seed <<- .Random.seed
> ls()
character(0)
> exists(".Random.seed")
[1] TRUE |
I think it doesn't necessarily exist. eg if I start a new R session from the terminal
|
# Note that .Random.seed is only set if the seed has been set or if a random number | ||
# has been generated. | ||
if (exists(".Random.seed")) { | ||
current_seed_state <- .Random.seed |
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.
@gowerc
To be a bit more robust, eg in case the function has an error, you can do something like
current_seed_state <- .Random.seed | |
current_seed_state <- .Random.seed | |
on.exit(suspendInterrupts( | |
assign(".Random.seed", value = current_seed_state, envir = globalenv(), inherits = FALSE) | |
)) |
In another package we have
# Make sure to leave '.Random.seed' as-is on exit
old_seed <- globalenv()$.Random.seed
on.exit(suspendInterrupts(set_random_seed(old_seed)))
with the function
# Restore the RNG back to a previous state using the global .Random.seed
set_random_seed <- function(old_seed) {
if (is.null(old_seed)) {
rm(".Random.seed", envir = globalenv(), inherits = FALSE)
} else {
assign(".Random.seed", value = old_seed, envir = globalenv(), inherits = FALSE)
}
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.
yer good idea will update !
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.
@luwidmer / @gravesti - Sorry if a stupid question but what exactly is suspendInterrupts supposed to be doing here ? My guess based on the sparse docs would be that it block SIG* interrupts however SIGTERM still seems to kill the program regardless:
I then wondered if it meant R based signals but it has no affect on that either:
> tryCatch(
+ {
+ suspendInterrupts({
+ warning("hi")
+ print("hello")
+ })
+ },
+ warning = \(e) print("Caught warning")
+ )
[1] "Caught warning"
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.
Looks like it just doesn't work properly on MacOS / Linux:
HenrikBengtsson/Wishlist-for-R#82
rstan::stan_model( | ||
model <- rstan::stan_model( | ||
file = model_file, | ||
auto_write = TRUE, | ||
model_name = "rbmi_mmrm" | ||
) | ||
|
||
if (exists("current_seed_state")) { | ||
.Random.seed <- current_seed_state | ||
} | ||
return(model) |
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.
These changes wouldn't be needed if you use on.exit()
In fairness I had no idea it wasn't created on new R sessions until all the CICD tests failed unexpectedly 😓 |
Closes #469
FYI @luwidmer