utilities: Utilities to manage Python environment and torch tensors

get_py_MAF_handleR Documentation

Utilities to manage Python environment and torch tensors

Description

Utility initializing a Python environment for running zuko.flows.MAF and retrieving it.

Usage

  get_py_MAF_handle(envir, reset=FALSE, torch_device="cpu", GPU_mem=NULL,
                    verbose = TRUE)

Arguments

envir

An environment (in the R sense) initialized as shown in the Examples.

reset

Boolean: Whether to reinitialize the Python session or not.

torch_device

Character: "cpu"; or a GPU backend, either "cuda" (or "cuda:0", etc.) or "mps" depending on system capabilities.

GPU_mem

For development purposes (effect is complicated). An amount of (dedicated) GPU memory, in bytes.

verbose

Boolean. Whether to print some messages or not.

Value

If successful, get_py_MAF_handle returns the modified input environment. If sourcing the Python code provided by mafR failed (presumably from trying to use an improperly set-up Python environment), the error condition message is returned.

See Also

init_py_env, control_py_env

Examples

## Not run:  # too slow for CRAN checks
# Initialization of python virtual environment:
init_py_env(test_cuda=FALSE)

# Initialization of python session (defines functions etc. 
#   in python session and provide access to them from R):    
#
my_env <- list2env(list(is_set=FALSE),parent = emptyenv())
py_handle <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu")

# => get_py_MAF_handle() has added objects in myenv:
my_env$torch # Imported Python package (result of reticulate::import("torch"))
my_env$device # the torch_device
# and functions for MAF training, such as myenv$MAF_density_estimation.

# The returned 'py_handle' can be used by oher functions, e.g.:
control_py_env(py_handle, seed=0L)
# alters the seed in the python session.

## End(Not run)

mafR documentation built on May 7, 2026, 5:09 p.m.

Related to utilities in mafR...