From 616e8cd0b5babf26436128fcbd88a9fd86479dca Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Fri, 29 Mar 2024 11:59:46 -0700 Subject: [PATCH] Fix memory issues with Tweedie #302 --- scratch/memory-tweedie.R | 68 ++++++++++++++++++++++++++++++++++++++++ src/sdmTMB.cpp | 3 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 scratch/memory-tweedie.R diff --git a/scratch/memory-tweedie.R b/scratch/memory-tweedie.R new file mode 100644 index 000000000..5033484df --- /dev/null +++ b/scratch/memory-tweedie.R @@ -0,0 +1,68 @@ +library(sdmTMB) + +# match glmmTMB: +ctl <- sdmTMBcontrol(newton_loops = 0, multiphase = FALSE) + +N <- 3e4 +set.seed(1) +dat <- data.frame( + y = fishMod::rTweedie(N, 1, 1, 1.5) +) + +bench::mark({m2 <- sdmTMB( + y ~ 1, + data = dat, + spatial = 'off', + family = tweedie("log"), + control = ctl +)}, iterations = 1) + +tidy(m2) + +bench::mark({m1 <- glmmTMB::glmmTMB( + y ~ 1, + data = dat, + family = glmmTMB::tweedie("log") +)}, iterations = 1) + + +N <- 3e4 +set.seed(1) +dat <- data.frame( + y = rnorm(N, 0, 1) +) + +bench::mark({m2 <- sdmTMB( + y ~ 1, + data = dat, + spatial = 'off', + control = sdmTMBcontrol(newton_loops = 1, multiphase = FALSE) +)}, iterations = 1) + +bench::mark({m1 <- glmmTMB::glmmTMB( + y ~ 1, + data = dat +)}, iterations = 1) + + +############## + +N <- 3e4 +set.seed(1) +dat <- data.frame( + y = rgamma(N, 0.2, 0.2/3) +) + +bench::mark({m2 <- sdmTMB( + y ~ 1, + data = dat, + spatial = 'off', + family = Gamma(link = "log"), + control = sdmTMBcontrol(newton_loops = 1, multiphase = FALSE) +)}, iterations = 1) + +bench::mark({m1 <- glmmTMB::glmmTMB( + y ~ 1, + data = dat, + family = Gamma(link = "log"), +)}, iterations = 1) diff --git a/src/sdmTMB.cpp b/src/sdmTMB.cpp index d3d27f396..268a65d87 100644 --- a/src/sdmTMB.cpp +++ b/src/sdmTMB.cpp @@ -793,7 +793,6 @@ Type objective_function::operator()() } case tweedie_family: { tweedie_p = invlogit(thetaf) + Type(1.0); - ADREPORT(tweedie_p); // FIXME! move this out of loop!!!!!!!! if (!sdmTMB::isNA(priors(12))) { error("Priors not enabled for Tweedie p currently"); @@ -1380,6 +1379,8 @@ Type objective_function::operator()() REPORT(phi); } + if (family(0) == tweedie_family) ADREPORT(tweedie_p); // #302 + REPORT(epsilon_st_A_vec); // spatio-temporal effects; vector REPORT(b_rw_t); // time-varying effects REPORT(omega_s_A); // spatial effects; n_s length vector