-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
backends.R
1011 lines (932 loc) · 33 KB
/
backends.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# parse Stan model code
# @param model Stan model code
# @return validated Stan model code
parse_model <- function(model, backend, ...) {
backend <- as_one_character(backend)
.parse_model <- get(paste0(".parse_model_", backend), mode = "function")
.parse_model(model, ...)
}
# parse Stan model code with rstan
# @param model Stan model code
# @return validated Stan model code
.parse_model_rstan <- function(model, silent = 1, ...) {
out <- eval_silent(
rstan::stanc(model_code = model, ...),
type = "message", try = TRUE, silent = silent
)
out$model_code
}
# parse Stan model code with cmdstanr
# @param model Stan model code
# @return validated Stan model code
.parse_model_cmdstanr <- function(model, silent = 1, ...) {
require_package("cmdstanr")
temp_file <- cmdstanr::write_stan_file(model)
# if (cmdstanr::cmdstan_version() >= "2.29.0") {
# .canonicalize_stan_model(temp_file, overwrite_file = TRUE)
# }
out <- eval_silent(
cmdstanr::cmdstan_model(temp_file, compile = FALSE, ...),
type = "message", try = TRUE, silent = silent
)
out$check_syntax(quiet = TRUE)
collapse(out$code(), "\n")
}
# parse model with a mock backend for testing
.parse_model_mock <- function(model, silent = TRUE, parse_error = NULL,
parse_check = "rstan", ...) {
if (!is.null(parse_error)) {
stop2(parse_error)
} else if (parse_check == "rstan") {
out <- .parse_model_rstan(model, silent = silent, ...)
} else if (parse_check == "cmdstanr") {
out <- .parse_model_cmdstanr(model, silent = silent, ...)
} else if (is.null(parse_check)) {
out <- "mock_code"
} else {
stop2("Unknown 'parse_check' value.")
}
out
}
# compile Stan model
# @param model Stan model code
# @return validated Stan model code
compile_model <- function(model, backend, ...) {
backend <- as_one_character(backend)
.compile_model <- get(paste0(".compile_model_", backend), mode = "function")
.compile_model(model, ...)
}
# compile Stan model with rstan
# @param model Stan model code
# @return model compiled with rstan
.compile_model_rstan <- function(model, threads, opencl, silent = 1, ...) {
args <- list(...)
args$model_code <- model
if (silent < 2) {
message("Compiling Stan program...")
}
if (use_threading(threads)) {
if (utils::packageVersion("rstan") >= "2.26") {
threads_per_chain_def <- rstan::rstan_options("threads_per_chain")
on.exit(rstan::rstan_options(threads_per_chain = threads_per_chain_def))
rstan::rstan_options(threads_per_chain = threads$threads)
} else {
stop2("Threading is not supported by backend 'rstan' version ",
utils::packageVersion("rstan"), ".")
}
}
if (use_opencl(opencl)) {
stop2("OpenCL is not supported by backend 'rstan' version ",
utils::packageVersion("rstan"), ".")
}
eval_silent(
do_call(rstan::stan_model, args),
type = "message", try = TRUE, silent = silent >= 2
)
}
# compile Stan model with cmdstanr
# @param model Stan model code
# @return model compiled with cmdstanr
.compile_model_cmdstanr <- function(model, threads, opencl, silent = 1, ...) {
require_package("cmdstanr")
args <- list(...)
args$stan_file <- cmdstanr::write_stan_file(model)
# if (cmdstanr::cmdstan_version() >= "2.29.0") {
# .canonicalize_stan_model(args$stan_file, overwrite_file = TRUE)
# }
if (use_threading(threads)) {
args$cpp_options$stan_threads <- TRUE
}
if (use_opencl(opencl)) {
args$cpp_options$stan_opencl <- TRUE
}
eval_silent(
do_call(cmdstanr::cmdstan_model, args),
type = "message", try = TRUE, silent = silent >= 2
)
}
# compile model with a mock backend for testing
.compile_model_mock <- function(model, threads, opencl, compile_check = "rstan",
compile_error = NULL, silent = 1, ...) {
if (!is.null(compile_error)) {
stop2(compile_error)
} else if (compile_check == "rstan") {
out <- .parse_model_rstan(model, silent = silent, ...)
} else if (compile_check == "cmdstanr") {
out <- .parse_model_cmdstanr(model, silent = silent, ...)
} else if (is.null(compile_check)) {
out <- list()
} else {
stop2("Unknown 'compile_check' value.")
}
out
}
# fit Stan model
# @param model Stan model code
# @return validated Stan model code
fit_model <- function(model, backend, ...) {
backend <- as_one_character(backend)
.fit_model <- get(paste0(".fit_model_", backend), mode = "function")
.fit_model(model, ...)
}
# fit Stan model with rstan
# @param model a compiled Stan model
# @param sdata named list to be passed to Stan as data
# @return a fitted Stan model
.fit_model_rstan <- function(model, sdata, algorithm, iter, warmup, thin,
chains, cores, threads, opencl, init, exclude,
seed, control, silent, future, ...) {
# some input checks and housekeeping
if (use_threading(threads)) {
if (utils::packageVersion("rstan") >= "2.26") {
threads_per_chain_def <- rstan::rstan_options("threads_per_chain")
on.exit(rstan::rstan_options(threads_per_chain = threads_per_chain_def))
rstan::rstan_options(threads_per_chain = threads$threads)
} else {
stop2("Threading is not supported by backend 'rstan' version ",
utils::packageVersion("rstan"), ".")
}
}
if (use_opencl(opencl)) {
stop2("OpenCL is not supported by backend 'rstan' version ",
utils::packageVersion("rstan"), ".")
}
if (is.null(init)) {
init <- "random"
} else if (is.character(init) && !init %in% c("random", "0")) {
init <- get(init, mode = "function", envir = parent.frame())
}
args <- nlist(
object = model, data = sdata, iter, seed,
init = init, pars = exclude, include = FALSE
)
dots <- list(...)
args[names(dots)] <- dots
# do the actual sampling
if (silent < 2) {
message("Start sampling")
}
if (algorithm %in% c("sampling", "fixed_param")) {
c(args) <- nlist(warmup, thin, control, show_messages = !silent)
if (algorithm == "fixed_param") {
args$algorithm <- "Fixed_param"
}
if (future) {
if (cores > 1L) {
warning2("Argument 'cores' is ignored when using 'future'.")
}
args$chains <- 1L
futures <- fits <- vector("list", chains)
for (i in seq_len(chains)) {
args$chain_id <- i
if (is.list(init)) {
args$init <- init[i]
}
futures[[i]] <- future::future(
brms::do_call(rstan::sampling, args),
packages = "rstan",
seed = TRUE
)
}
for (i in seq_len(chains)) {
fits[[i]] <- future::value(futures[[i]])
}
out <- rstan::sflist2stanfit(fits)
rm(futures, fits)
} else {
c(args) <- nlist(chains, cores)
out <- do_call(rstan::sampling, args)
}
} else if (algorithm %in% c("fullrank", "meanfield")) {
# vb does not support parallel execution
c(args) <- nlist(algorithm)
out <- do_call(rstan::vb, args)
} else {
stop2("Algorithm '", algorithm, "' is not supported.")
}
# TODO: add support for pathfinder and laplace
out <- repair_stanfit(out)
out
}
# fit Stan model with cmdstanr
# @param model a compiled Stan model
# @param sdata named list to be passed to Stan as data
# @return a fitted Stan model
.fit_model_cmdstanr <- function(model, sdata, algorithm, iter, warmup, thin,
chains, cores, threads, opencl, init, exclude,
seed, control, silent, future, ...) {
require_package("cmdstanr")
# some input checks and housekeeping
class(sdata) <- "list"
if (isNA(seed)) {
seed <- NULL
}
if (is_equal(init, "random")) {
init <- NULL
} else if (is_equal(init, "0")) {
init <- 0
}
if (future) {
stop2("Argument 'future' is not supported by backend 'cmdstanr'.")
}
args <- nlist(data = sdata, seed, init)
if (use_opencl(opencl)) {
args$opencl_ids <- opencl$ids
}
dots <- list(...)
args[names(dots)] <- dots
args[names(control)] <- control
chains <- as_one_numeric(chains)
empty_model <- chains <= 0
if (empty_model) {
# fit the model with minimal amount of draws
# TODO: replace with a better solution
chains <- 1
iter <- 2
warmup <- 1
thin <- 1
cores <- 1
}
# do the actual sampling
if (silent < 2) {
message("Start sampling")
}
if (algorithm %in% c("sampling", "fixed_param")) {
c(args) <- nlist(
iter_sampling = iter - warmup,
iter_warmup = warmup,
chains, thin,
parallel_chains = cores,
show_messages = silent < 2,
show_exceptions = silent == 0,
fixed_param = algorithm == "fixed_param"
)
if (use_threading(threads)) {
args$threads_per_chain <- threads$threads
}
out <- do_call(model$sample, args)
} else if (algorithm %in% c("fullrank", "meanfield")) {
c(args) <- nlist(iter, algorithm)
if (use_threading(threads)) {
args$threads <- threads$threads
}
out <- do_call(model$variational, args)
} else if (algorithm %in% c("pathfinder")) {
if (use_threading(threads)) {
args$num_threads <- threads$threads
}
out <- do_call(model$pathfinder, args)
} else if (algorithm %in% c("laplace")) {
if (use_threading(threads)) {
args$threads <- threads$threads
}
out <- do_call(model$laplace, args)
} else {
stop2("Algorithm '", algorithm, "' is not supported.")
}
out <- read_csv_as_stanfit(
out$output_files(), variables = out$metadata()$stan_variables,
model = model, exclude = exclude, algorithm = algorithm
)
if (empty_model) {
# allow correct updating of an 'empty' model
out@sim <- list()
}
out
}
# fit model with a mock backend for testing
.fit_model_mock <- function(model, sdata, algorithm, iter, warmup, thin,
chains, cores, threads, opencl, init, exclude,
seed, control, silent, future, mock_fit, ...) {
if (is.function(mock_fit)) {
out <- mock_fit()
} else {
out <- mock_fit
}
out
}
# extract the compiled stan model
# @param x brmsfit object
compiled_model <- function(x) {
stopifnot(is.brmsfit(x))
backend <- x$backend %||% "rstan"
if (backend == "rstan") {
out <- rstan::get_stanmodel(x$fit)
} else if (backend == "cmdstanr") {
out <- attributes(x$fit)$CmdStanModel
} else if (backend == "mock") {
stop2("'compiled_model' is not supported in the mock backend.")
}
out
}
# Does the model need recompilation before being able to sample again?
needs_recompilation <- function(x) {
stopifnot(is.brmsfit(x))
backend <- x$backend %||% "rstan"
if (backend == "rstan") {
# TODO: figure out when rstan requires recompilation
out <- FALSE
} else if (backend == "cmdstanr") {
exe_file <- attributes(x$fit)$CmdStanModel$exe_file()
out <- !is.character(exe_file) || !file.exists(exe_file)
} else if (backend == "mock") {
out <- FALSE
}
out
}
#' Recompile Stan models in \code{brmsfit} objects
#'
#' Recompile the Stan model inside a \code{brmsfit} object, if necessary.
#' This does not change the model, it simply recreates the executable
#' so that sampling is possible again.
#'
#' @param x An object of class \code{brmsfit}.
#' @param recompile Logical, indicating whether the Stan model should be
#' recompiled. If \code{NULL} (the default), \code{recompile_model} tries
#' to figure out internally, if recompilation is necessary. Setting it to
#' \code{FALSE} will cause \code{recompile_model} to always return the
#' \code{brmsfit} object unchanged.
#'
#' @return A (possibly updated) \code{brmsfit} object.
#'
#' @export
recompile_model <- function(x, recompile = NULL) {
stopifnot(is.brmsfit(x))
if (is.null(recompile)) {
recompile <- needs_recompilation(x)
}
recompile <- as_one_logical(recompile)
if (!recompile) {
return(x)
}
message("Recompiling the Stan model")
backend <- x$backend %||% "rstan"
new_model <- compile_model(
stancode(x), backend = backend, threads = x$threads,
opencl = x$opencl, silent = 2
)
if (backend == "rstan") {
x$fit@stanmodel <- new_model
} else if (backend == "cmdstanr") {
attributes(x)$CmdStanModel <- new_model
} else if (backend == "mock") {
stop2("'recompile_model' is not supported in the mock backend.")
}
x
}
# extract the elapsed time during model fitting
# @param x brmsfit object
elapsed_time <- function(x) {
stopifnot(is.brmsfit(x))
backend <- x$backend %||% "rstan"
if (backend == "rstan") {
out <- rstan::get_elapsed_time(x$fit)
out <- data.frame(
chain_id = seq_len(nrow(out)),
warmup = out[, "warmup"],
sampling = out[, "sample"]
)
out$total <- out$warmup + out$sampling
rownames(out) <- NULL
} else if (backend == "cmdstanr") {
out <- attributes(x$fit)$metadata$time$chains
} else if (backend == "mock") {
stop2("'elapsed_time' not supported in the mock backend.")
}
out
}
# supported Stan backends
backend_choices <- function() {
c("rstan", "cmdstanr", "mock")
}
# supported Stan algorithms
algorithm_choices <- function() {
c("sampling", "meanfield", "fullrank", "pathfinder", "laplace", "fixed_param")
}
# check if the model was fit the the required backend
require_backend <- function(backend, x) {
stopifnot(is.brmsfit(x))
backend <- match.arg(backend, backend_choices())
if (isTRUE(x$backend != backend)) {
stop2("Backend '", backend, "' is required for this method.")
}
invisible(TRUE)
}
#' Threading in Stan
#'
#' Use threads for within-chain parallelization in \pkg{Stan} via the \pkg{brms}
#' interface. Within-chain parallelization is experimental! We recommend its use
#' only if you are experienced with Stan's \code{reduce_sum} function and have a
#' slow running model that cannot be sped up by any other means.
#'
#' @param threads Number of threads to use in within-chain parallelization.
#' @param grainsize Number of observations evaluated together in one chunk on
#' one of the CPUs used for threading. If \code{NULL} (the default),
#' \code{grainsize} is currently chosen as \code{max(100, N / (2 *
#' threads))}, where \code{N} is the number of observations in the data. This
#' default is experimental and may change in the future without prior notice.
#' @param static Logical. Apply the static (non-adaptive) version of
#' \code{reduce_sum}? Defaults to \code{FALSE}. Setting it to \code{TRUE}
#' is required to achieve exact reproducibility of the model results
#' (if the random seed is set as well).
#'
#' @return A \code{brmsthreads} object which can be passed to the
#' \code{threads} argument of \code{brm} and related functions.
#'
#' @details The adaptive scheduling procedure used by \code{reduce_sum} will
#' prevent the results to be exactly reproducible even if you set the random
#' seed. If you need exact reproducibility, you have to set argument
#' \code{static = TRUE} which may reduce efficiency a bit.
#'
#' To ensure that chunks (whose size is defined by \code{grainsize}) require
#' roughly the same amount of computing time, we recommend storing
#' observations in random order in the data. At least, please avoid sorting
#' observations after the response values. This is because the latter often
#' cause variations in the computing time of the pointwise log-likelihood,
#' which makes up a big part of the parallelized code.
#'
#' @examples
#' \dontrun{
#' # this model just serves as an illustration
#' # threading may not actually speed things up here
#' fit <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = negbinomial(),
#' chains = 1, threads = threading(2, grainsize = 100),
#' backend = "cmdstanr")
#' summary(fit)
#' }
#'
#' @export
threading <- function(threads = NULL, grainsize = NULL, static = FALSE) {
out <- list(threads = NULL, grainsize = NULL)
class(out) <- "brmsthreads"
if (!is.null(threads)) {
threads <- as_one_numeric(threads)
if (!is_wholenumber(threads) || threads < 1) {
stop2("Number of threads needs to be positive.")
}
out$threads <- threads
}
if (!is.null(grainsize)) {
grainsize <- as_one_numeric(grainsize)
if (!is_wholenumber(grainsize) || grainsize < 1) {
stop2("The grainsize needs to be positive.")
}
out$grainsize <- grainsize
}
out$static <- as_one_logical(static)
out
}
is.brmsthreads <- function(x) {
inherits(x, "brmsthreads")
}
# validate 'thread' argument
validate_threads <- function(threads) {
if (is.null(threads)) {
threads <- threading()
} else if (is.numeric(threads)) {
threads <- as_one_numeric(threads)
threads <- threading(threads)
} else if (!is.brmsthreads(threads)) {
stop2("Argument 'threads' needs to be numeric or ",
"specified via the 'threading' function.")
}
threads
}
# is threading activated?
use_threading <- function(threads) {
isTRUE(validate_threads(threads)$threads > 0)
}
#' GPU support in Stan via OpenCL
#'
#' Use OpenCL for GPU support in \pkg{Stan} via the \pkg{brms} interface. Only
#' some \pkg{Stan} functions can be run on a GPU at this point and so
#' a lot of \pkg{brms} models won't benefit from OpenCL for now.
#'
#' @param ids (integer vector of length 2) The platform and device IDs of the
#' OpenCL device to use for fitting. If you don't know the IDs of your OpenCL
#' device, \code{c(0,0)} is most likely what you need.
#'
#' @return A \code{brmsopencl} object which can be passed to the
#' \code{opencl} argument of \code{brm} and related functions.
#'
#' @details For more details on OpenCL in \pkg{Stan}, check out
#' \url{https://mc-stan.org/docs/2_26/cmdstan-guide/parallelization.html#opencl}
#' as well as \url{https://mc-stan.org/docs/2_26/stan-users-guide/opencl.html}.
#'
#' @examples
#' \dontrun{
#' # this model just serves as an illustration
#' # OpenCL may not actually speed things up here
#' fit <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson(),
#' chains = 2, cores = 2, opencl = opencl(c(0, 0)),
#' backend = "cmdstanr")
#' summary(fit)
#' }
#'
#' @export
opencl <- function(ids = NULL) {
out <- list(ids = NULL)
class(out) <- "brmsopencl"
if (!is.null(ids)) {
ids <- as.integer(ids)
if (!length(ids) == 2L) {
stop2("OpenCl 'ids' needs to be an integer vector of length 2.")
}
out$ids <- ids
}
out
}
is.brmsopencl <- function(x) {
inherits(x, "brmsopencl")
}
# validate the 'opencl' argument
validate_opencl <- function(opencl) {
if (is.null(opencl)) {
opencl <- opencl()
} else if (is.numeric(opencl)) {
opencl <- opencl(opencl)
} else if (!is.brmsopencl(opencl)) {
stop2("Argument 'opencl' needs to an integer vector or ",
"specified via the 'opencl' function.")
}
opencl
}
# is OpenCL activated?
use_opencl <- function(opencl) {
!is.null(validate_opencl(opencl)$ids)
}
# validate the 'silent' argument
validate_silent <- function(silent) {
silent <- as_one_integer(silent)
if (silent < 0 || silent > 2) {
stop2("'silent' must be between 0 and 2.")
}
silent
}
# ensure that variable dimensions at the end are correctly written
# convert names like b.1.1 to b[1,1]
repair_variable_names <- function(x) {
x <- sub("\\.", "[", x)
x <- gsub("\\.", ",", x)
x[grep("\\[", x)] <- paste0(x[grep("\\[", x)], "]")
x
}
# repair parameter names of stanfit objects
repair_stanfit <- function(x) {
stopifnot(is.stanfit(x))
if (!length(x@sim$fnames_oi)) {
# nothing to rename
return(x)
}
# the posterior package cannot deal with non-unique parameter names
# this case happens rarely but might happen when sample_prior = "yes"
x@sim$fnames_oi <- make.unique(as.character(x@sim$fnames_oi), "__")
for (i in seq_along(x@sim$samples)) {
# stanfit may have renamed dimension suffixes (#1218)
if (length(x@sim$samples[[i]]) == length(x@sim$fnames_oi)) {
names(x@sim$samples[[i]]) <- x@sim$fnames_oi
}
}
x
}
# possible options for argument 'file_refit'
file_refit_options <- function() {
c("never", "always", "on_change")
}
# canonicalize Stan model file in accordance with the current Stan version
# this function may no longer be needed due to rstan 2.26+ now being on CRAN
# for more details see https://github.com/paul-buerkner/brms/issues/1544
# .canonicalize_stan_model <- function(stan_file, overwrite_file = TRUE) {
# cmdstan_mod <- cmdstanr::cmdstan_model(stan_file, compile = FALSE)
# out <- utils::capture.output(
# cmdstan_mod$format(
# canonicalize = list("deprecations", "braces", "parentheses"),
# overwrite_file = overwrite_file, backup = FALSE
# )
# )
# paste0(out, collapse = "\n")
# }
#' Read CmdStan CSV files as a brms-formatted stanfit object
#'
#' \code{read_csv_as_stanfit} is used internally to read CmdStan CSV files into a
#' \code{stanfit} object that is consistent with the structure of the fit slot of a
#' brmsfit object.
#'
#' @param files Character vector of CSV files names where draws are stored.
#' @param variables Character vector of variables to extract from the CSV files.
#' @param sampler_diagnostics Character vector of sampler diagnostics to extract.
#' @param model A compiled cmdstanr model object (optional). Provide this argument
#' if you want to allow updating the model without recompilation.
#' @param exclude Character vector of variables to exclude from the stanfit. Only
#' used when \code{variables} is also specified.
#' @param algorithm The algorithm with which the model was fitted.
#' See \code{\link{brm}} for details.
#'
#' @return A stanfit object consistent with the structure of the \code{fit}
#' slot of a brmsfit object.
#'
#' @examples
#' \dontrun{
#' # fit a model manually via cmdstanr
#' scode <- stancode(count ~ Trt, data = epilepsy)
#' sdata <- standata(count ~ Trt, data = epilepsy)
#' mod <- cmdstanr::cmdstan_model(cmdstanr::write_stan_file(scode))
#' stanfit <- mod$sample(data = sdata)
#'
#' # feed the Stan model back into brms
#' fit <- brm(count ~ Trt, data = epilepsy, empty = TRUE, backend = 'cmdstanr')
#' fit$fit <- read_csv_as_stanfit(stanfit$output_files(), model = mod)
#' fit <- rename_pars(fit)
#' summary(fit)
#' }
#'
#' @export
read_csv_as_stanfit <- function(files, variables = NULL, sampler_diagnostics = NULL,
model = NULL, exclude = "", algorithm = "sampling") {
require_package("cmdstanr")
if (!is.null(variables)) {
# ensure that only relevant variables are read from CSV
variables <- repair_variable_names(variables)
variables <- unique(sub("\\[.+", "", variables))
variables <- setdiff(variables, exclude)
# temp fix for cmdstanr not recognizing the variable names it produces #1473
if (algorithm %in% c("meanfield", "fullrank")) {
variables <- ifelse(variables == "lp_approx__", "log_g__", variables)
} else if (algorithm %in% "pathfinder") {
variables <- setdiff(variables, "lp_approx__")
} else if (algorithm %in% "laplace") {
variables <- setdiff(variables, c("lp__", "lp_approx__"))
}
}
csfit <- cmdstanr::read_cmdstan_csv(
files = files, variables = variables,
sampler_diagnostics = sampler_diagnostics,
format = NULL
)
# @model_name
model_name = gsub(".csv", "", basename(files[[1]]))
# @model_pars
model_pars <- csfit$metadata$stan_variables
if (!is.null(variables)) {
model_pars <- intersect(model_pars, variables)
}
# special variables will be added back later on
special_vars <- c("lp__", "lp_approx__")
model_pars <- setdiff(model_pars, special_vars)
# @par_dims
par_dims <- vector("list", length(model_pars))
names(par_dims) <- model_pars
par_dims <- lapply(par_dims, function(x) integer(0))
pdims_num <- ulapply(model_pars, function(x)
sum(grepl(paste0("^", x, "\\[.*\\]$"), csfit$metadata$model_params))
)
par_dims[pdims_num != 0] <-
csfit$metadata$stan_variable_sizes[model_pars][pdims_num != 0]
# @mode
mode <- 0L
# @sim
rstan_diagn_order <- c("accept_stat__", "treedepth__", "stepsize__",
"divergent__", "n_leapfrog__", "energy__")
if (!is.null(sampler_diagnostics)) {
rstan_diagn_order <- rstan_diagn_order[rstan_diagn_order %in% sampler_diagnostics]
}
res_vars <- c(".chain", ".iteration", ".draw")
if ("post_warmup_draws" %in% names(csfit)) {
# for MCMC samplers
n_chains <- max(
nchains(csfit$warmup_draws),
nchains(csfit$post_warmup_draws)
)
n_iter_warmup <- niterations(csfit$warmup_draws)
n_iter_sample <- niterations(csfit$post_warmup_draws)
if (n_iter_warmup > 0) {
csfit$warmup_draws <- as_draws_df(csfit$warmup_draws)
csfit$warmup_sampler_diagnostics <-
as_draws_df(csfit$warmup_sampler_diagnostics)
}
if (n_iter_sample > 0) {
csfit$post_warmup_draws <- as_draws_df(csfit$post_warmup_draws)
csfit$post_warmup_sampler_diagnostics <-
as_draws_df(csfit$post_warmup_sampler_diagnostics)
}
# called 'samples' for consistency with rstan
samples <- rbind(csfit$warmup_draws, csfit$post_warmup_draws)
# manage memory
csfit$warmup_draws <- NULL
csfit$post_warmup_draws <- NULL
# prepare sampler diagnostics
diagnostics <- rbind(
csfit$warmup_sampler_diagnostics,
csfit$post_warmup_sampler_diagnostics
)
# manage memory
csfit$warmup_sampler_diagnostics <- NULL
csfit$post_warmup_sampler_diagnostics <- NULL
# convert to regular data.frame
diagnostics <- as.data.frame(diagnostics)
diag_chain_ids <- diagnostics$.chain
diagnostics[res_vars] <- NULL
} else if ("draws" %in% names(csfit)) {
# for variational inference "samplers"
n_chains <- 1
n_iter_warmup <- 0
n_iter_sample <- niterations(csfit$draws)
if (n_iter_sample > 0) {
csfit$draws <- as_draws_df(csfit$draws)
}
# called 'samples' for consistency with rstan
samples <- csfit$draws
# manage memory
csfit$draws <- NULL
# VI has no sampler diagnostics
diag_chain_ids <- rep(1L, nrow(samples))
diagnostics <- as.data.frame(matrix(nrow = nrow(samples), ncol = 0))
}
# convert to regular data.frame
samples <- as.data.frame(samples)
chain_ids <- samples$.chain
samples[res_vars] <- NULL
# only add special variables to dims if there are present in samples
# this ensures that dims_oi, pars_oi, and fnames_oi match with samples
for (p in special_vars) {
if (p %in% colnames(samples)) {
samples <- move2end(samples, p)
par_dims[[p]] <- integer(0)
}
}
model_pars <- names(par_dims)
fnames_oi <- colnames(samples)
# split samples into chains
samples <- split(samples, chain_ids)
names(samples) <- NULL
# split diagnostics into chains
diagnostics <- split(diagnostics, diag_chain_ids)
names(diagnostics) <- NULL
# @sim$sample: largely 113-130 from rstan::read_stan_csv
values <- list()
values$algorithm <- csfit$metadata$algorithm
values$engine <- csfit$metadata$engine
values$metric <- csfit$metadata$metric
sampler_t <- NULL
if (!is.null(values$algorithm)) {
if (values$algorithm == "rwm" || values$algorithm == "Metropolis") {
sampler_t <- "Metropolis"
} else if (values$algorithm == "hmc") {
if (values$engine == "static") {
sampler_t <- "HMC"
} else {
if (values$metric == "unit_e") {
sampler_t <- "NUTS(unit_e)"
} else if (values$metric == "diag_e") {
sampler_t <- "NUTS(diag_e)"
} else if (values$metric == "dense_e") {
sampler_t <- "NUTS(dense_e)"
}
}
}
}
adapt_info <- vector("list", 4)
idx_samples <- (n_iter_warmup + 1):(n_iter_warmup + n_iter_sample)
for (i in seq_along(samples)) {
m <- colMeans(samples[[i]][idx_samples, , drop = FALSE])
rownames(samples[[i]]) <- seq_rows(samples[[i]])
attr(samples[[i]], "sampler_params") <- diagnostics[[i]][rstan_diagn_order]
rownames(attr(samples[[i]], "sampler_params")) <- seq_rows(diagnostics[[i]])
# reformat back to text
if (length(csfit$inv_metric)) {
if (is_equal(sampler_t, "NUTS(dense_e)")) {
mmatrix_txt <- "\n# Elements of inverse mass matrix:\n# "
mmat <- paste0(apply(csfit$inv_metric[[i]], 1, paste0, collapse = ", "),
collapse = "\n# ")
} else {
mmatrix_txt <- "\n# Diagonal elements of inverse mass matrix:\n# "
mmat <- paste0(csfit$inv_metric[[i]], collapse = ", ")
}
adapt_info[[i]] <- paste0("# Step size = ",
csfit$step_size[[i]],
mmatrix_txt,
mmat, "\n# ")
attr(samples[[i]], "adaptation_info") <- adapt_info[[i]]
} else {
attr(samples[[i]], "adaptation_info") <- character(0)
}
attr(samples[[i]], "args") <- list(sampler_t = sampler_t, chain_id = i)
if (NROW(csfit$metadata$time)) {
time_i <- as.double(csfit$metadata$time[i, c("warmup", "sampling")])
names(time_i) <- c("warmup", "sample")
attr(samples[[i]], "elapsed_time") <- time_i
}
attr(samples[[i]], "mean_pars") <- m[-length(m)]
attr(samples[[i]], "mean_lp__") <- m["lp__"]
}
perm_lst <- lapply(seq_len(n_chains), function(id) sample.int(n_iter_sample))
# @sim
sim <- list(
samples = samples,
iter = csfit$metadata$iter_sampling + csfit$metadata$iter_warmup,
thin = csfit$metadata$thin,
warmup = csfit$metadata$iter_warmup,
chains = n_chains,
n_save = rep(n_iter_sample + n_iter_warmup, n_chains),
warmup2 = rep(n_iter_warmup, n_chains),
permutation = perm_lst,
pars_oi = model_pars,
dims_oi = par_dims,
fnames_oi = fnames_oi,
n_flatnames = length(fnames_oi)
)
# @stan_args
sargs <- list(
stan_version_major = as.character(csfit$metadata$stan_version_major),
stan_version_minor = as.character(csfit$metadata$stan_version_minor),
stan_version_patch = as.character(csfit$metadata$stan_version_patch),
model = csfit$metadata$model_name,
start_datetime = gsub(" ", "", csfit$metadata$start_datetime),
method = csfit$metadata$method,
iter = csfit$metadata$iter_sampling + csfit$metadata$iter_warmup,
warmup = csfit$metadata$iter_warmup,
save_warmup = csfit$metadata$save_warmup,
thin = csfit$metadata$thin,
engaged = as.character(csfit$metadata$adapt_engaged),
gamma = csfit$metadata$gamma,
delta = csfit$metadata$adapt_delta,
kappa = csfit$metadata$kappa,
t0 = csfit$metadata$t0,
init_buffer = as.character(csfit$metadata$init_buffer),
term_buffer = as.character(csfit$metadata$term_buffer),
window = as.character(csfit$metadata$window),
algorithm = csfit$metadata$algorithm,
engine = csfit$metadata$engine,
max_depth = csfit$metadata$max_treedepth,
metric = csfit$metadata$metric,
metric_file = character(0), # not stored in metadata
stepsize = NA, # add in loop
stepsize_jitter = csfit$metadata$stepsize_jitter,
num_chains = as.character(csfit$metadata$num_chains),
chain_id = NA, # add in loop
file = character(0), # not stored in metadata
init = NA, # add in loop
seed = as.character(csfit$metadata$seed),
file = NA, # add in loop
diagnostic_file = character(0), # not stored in metadata
refresh = as.character(csfit$metadata$refresh),
sig_figs = as.character(csfit$metadata$sig_figs),
profile_file = csfit$metadata$profile_file,
num_threads = as.character(csfit$metadata$threads_per_chain),
stanc_version = gsub(" ", "", csfit$metadata$stanc_version),
stancflags = character(0), # not stored in metadata
adaptation_info = NA, # add in loop
has_time = is.numeric(csfit$metadata$time$total),
time_info = NA, # add in loop
sampler_t = sampler_t
)
sargs_rep <- replicate(n_chains, sargs, simplify = FALSE)
for (i in seq_along(sargs_rep)) {
sargs_rep[[i]]$chain_id <- i
sargs_rep[[i]]$stepsize <- csfit$metadata$step_size[i]
sargs_rep[[i]]$init <- as.character(csfit$metadata$init[i])
# two 'file' elements: select the second
file_idx <- which(names(sargs_rep[[i]]) == "file")
sargs_rep[[i]][[file_idx[2]]] <- files[[i]]
sargs_rep[[i]]$adaptation_info <- adapt_info[[i]]
if (NROW(csfit$metadata$time)) {
sargs_rep[[i]]$time_info <- paste0(
c("# Elapsed Time: ", "# ", "# ", "# "),
c(csfit$metadata$time[i, c("warmup", "sampling", "total")], ""),
c(" seconds (Warm-up)", " seconds (Sampling)", " seconds (Total)", "")
)
}
}
# @stanmodel
null_dso <- new(
"cxxdso", sig = list(character(0)), dso_saved = FALSE,
dso_filename = character(0), modulename = character(0),
system = R.version$system, cxxflags = character(0),
.CXXDSOMISC = new.env(parent = emptyenv())
)
null_sm <- new(
"stanmodel", model_name = model_name, model_code = character(0),
model_cpp = list(), dso = null_dso
)
# @date
sdate <- do.call(max, lapply(files, function(csv) file.info(csv)$mtime))
sdate <- format(sdate, "%a %b %d %X %Y")
out <- new(
"stanfit",
model_name = model_name,
model_pars = model_pars,
par_dims = par_dims,
mode = mode,
sim = sim,