tests/testthat/test-gpu.R

test_that("GPU backend switching warns correctly", {
  # Minimal dummy data
  df <- data.frame(x = runif(10), y = rnorm(10))
  
  # If cmdstanr is NOT installed, we expect a warning about the backend switch,
  # immediately followed by an error that cmdstanr is missing.
  if (!requireNamespace("cmdstanr", quietly = TRUE)) {
    expect_warning(
      expect_error(
        bnns(y ~ x, data = df, use_gpu = TRUE, backend = "rstan", iter = 10, warmup = 5),
        "The 'cmdstanr' package is required"
      ),
      "GPU acceleration is only supported with the 'cmdstanr' backend"
    )
  } else {
    # If cmdstanr IS installed, we can just check if the warning fires.
    # To prevent actual compilation from crashing on standard CPUs, we wrap
    # the compilation step in a mock or rely on the environmental skip below.
    expect_true(TRUE)
  }
})

test_that("GPU compilation and sampling works when OpenCL is available", {
  # Skip on CRAN unconditionally
  skip_on_cran()
  
  # Skip on standard CI/CD and local setups unless an environment variable is explicitly set
  skip_if_not(
    Sys.getenv("BNNS_TEST_GPU") == "true", 
    "Skipping GPU tests; set BNNS_TEST_GPU='true' to run."
  )
  
  skip_if_not_installed("cmdstanr")
  
  df <- data.frame(x = runif(10), y = rnorm(10))
  fit <- bnns(y ~ x, data = df, use_gpu = TRUE, backend = "cmdstanr", iter = 50, warmup = 10, refresh = 0)
  expect_s3_class(fit, "bnns")
})

Try the bnns package in your browser

Any scripts or data that you put into this service are public.

bnns documentation built on June 8, 2026, 1:06 a.m.