@@ -128,23 +128,29 @@ is_linux <- function() {
128128
129129# ' Configure a Keras backend
130130# '
131- # ' @param backend string, can be `"tensorflow"`, `"jax"`, `"numpy"`, or `"torch"`.
132- # ' @param gpu bool, whether to use the GPU.
131+ # ' @param backend string, can be `"tensorflow"`, `"jax"`, `"numpy"`, or
132+ # ' `"torch"`.
133+ # ' @param gpu bool, whether to use the GPU. If `NA` (default), it will attempt
134+ # ' to detect GPU availability on Linux. On M-series Macs, it defaults to
135+ # ' `FALSE` for TensorFlow and `TRUE` for JAX. On Windows, it defaults to
136+ # ' `FALSE`.
133137# '
134138# ' @details
135- # ' These functions allow configuring which backend keras will use.
136- # ' Note that only one backend can be configured at a time.
139+ # '
140+ # ' These functions allow configuring which backend keras will use. Note that
141+ # ' only one backend can be configured at a time.
137142# '
138143# ' The function should be called after `library(keras3)` and before calling
139144# ' other functions within the package (see below for an example).
140145# '
141- # ' There is experimental support for changing the backend after keras has initialized.
142- # ' using `config_set_backend()`.
146+ # ' There is experimental support for changing the backend after keras has
147+ # ' initialized. using `config_set_backend()`.
143148# ' ```r
144149# ' library(keras3)
145150# ' use_backend("tensorflow")
146151# ' ```
147- # ' @returns Called primarily for side effects. Returns the provided `backend`, invisibly.
152+ # ' @returns Called primarily for side effects. Returns the provided `backend`,
153+ # ' invisibly.
148154# ' @export
149155use_backend <- function (backend , gpu = NA ) {
150156
@@ -245,9 +251,11 @@ use_backend <- function(backend, gpu = NA) {
245251 if (gpu ) {
246252 py_require(c(" tensorflow-cpu" , " torch" , " torchvision" , " torchaudio" ))
247253 } else {
248- Sys.setenv(" UV_INDEX" = " https://download.pytorch.org/whl/cpu" )
254+ Sys.setenv(" UV_INDEX" = trimws(paste(sep = " " ,
255+ " https://download.pytorch.org/whl/cpu" ,
256+ Sys.getenv(" UV_INDEX" )
257+ )))
249258 py_require(c(" tensorflow-cpu" , " torch" , " torchvision" , " torchaudio" ))
250- # additional_args = c("--index", "https://download.pytorch.org/whl/cpu"))
251259 }
252260 },
253261
@@ -268,12 +276,14 @@ use_backend <- function(backend, gpu = NA) {
268276
269277 Windows_torch = {
270278 if (is.na(gpu ))
271- gpu <- has_gpu()
279+ gpu <- FALSE
272280
273281 if (gpu ) {
274- Sys.setenv(" UV_INDEX" = " https://download.pytorch.org/whl/cu126" )
282+ Sys.setenv(" UV_INDEX" = trimws(paste(sep = " " ,
283+ " https://download.pytorch.org/whl/cu126" ,
284+ Sys.getenv(" UV_INDEX" )
285+ )))
275286 py_require(c(" torch" , " torchvision" , " torchaudio" ))
276- # additional_args = c("--index", "https://download.pytorch.org/whl/cu126"))
277287 } else {
278288 py_require(c(" torch" , " torchvision" , " torchaudio" ))
279289 }
@@ -289,6 +299,7 @@ use_backend <- function(backend, gpu = NA) {
289299
290300
291301
302+
292303get_os <- function () {
293304 if (is_windows()) " Windows" else if (is_mac_arm64()) " macOS" else " Linux"
294305}
0 commit comments