Skip to content

Commit 0ffad97

Browse files
authored
Merge pull request #623 from rstudio/v2.20-release
Updates for v2.20 release
2 parents 94bccf6 + e51c68b commit 0ffad97

File tree

10 files changed

+103
-69
lines changed

10 files changed

+103
-69
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ jobs:
4242

4343
## old keras/tf versions
4444
# - {os: 'ubuntu-latest', tf: '2.18', r: 'release'}
45-
- {os: 'ubuntu-latest', tf: '2.17', r: 'release'}
46-
- {os: 'ubuntu-latest', tf: '2.16', r: 'release'}
47-
- {os: 'ubuntu-latest', tf: '2.15', r: 'release'}
48-
- {os: 'ubuntu-latest', tf: '2.14', r: 'release'}
49-
- {os: 'ubuntu-latest', tf: '2.13', r: 'release'}
50-
- {os: 'ubuntu-latest', tf: '2.12', r: 'release'}
45+
# - {os: 'ubuntu-latest', tf: '2.17', r: 'release'}
46+
# - {os: 'ubuntu-latest', tf: '2.16', r: 'release'}
47+
# - {os: 'ubuntu-latest', tf: '2.15', r: 'release'}
48+
# - {os: 'ubuntu-latest', tf: '2.14', r: 'release'}
49+
# - {os: 'ubuntu-latest', tf: '2.13', r: 'release'}
50+
# - {os: 'ubuntu-latest', tf: '2.12', r: 'release'}
5151

5252
# these are allowed to fail
5353
# - {os: 'ubuntu-latest', tf: '2.14.0rc1', r: 'release'}
@@ -59,6 +59,7 @@ jobs:
5959
env:
6060
R_KEEP_PKG_SOURCE: yes
6161
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
62+
PY_REQUIRE_TENSORFLOW: true
6263
# R_REMOTES_NO_ERRORS_FROM_WARNINGS: 'true'
6364
# R_COMPILE_AND_INSTALL_PACKAGES: 'never'
6465

@@ -91,20 +92,20 @@ jobs:
9192

9293
- uses: r-lib/actions/setup-r-dependencies@v2
9394
with:
94-
extra-packages: any::rcmdcheck local::. rstudio/reticulate
95-
cache-version: 4
95+
extra-packages: any::rcmdcheck local::.
96+
# rstudio/reticulate
97+
cache-version: 5
9698
upgrade: 'TRUE'
9799

98-
- name: Install TensorFlow
99-
run: |
100-
print(sessionInfo())
101-
print(Sys.info())
102-
version <- '${{ matrix.tf }}'
103-
if (version != "default")
104-
tensorflow::install_tensorflow(version = '${{ matrix.tf }}')
105-
tensorflow::tf_config()
100+
# - name: Install TensorFlow
101+
# run: |
102+
# print(sessionInfo())
103+
# print(Sys.info())
104+
# version <- '${{ matrix.tf }}'
105+
# if (version != "default")
106+
# tensorflow::install_tensorflow(version = '${{ matrix.tf }}')
107+
# tensorflow::tf_config()
106108

107109
- uses: r-lib/actions/check-r-package@v2
108110
with:
109111
upload-snapshots: true
110-

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: tensorflow
22
Type: Package
33
Title: R Interface to 'TensorFlow'
4-
Version: 2.16.0.9000
4+
Version: 2.20.0.9000
55
Authors@R: c(
66
person("JJ", "Allaire", role = c("aut", "cph")),
77
person("Tomasz", "Kalinowski", role = c("ctb", "cph", "cre"),

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export(iterate)
142142
export(np_array)
143143
export(parse_arguments)
144144
export(parse_flags)
145+
export(py_require)
146+
export(py_require_tensorflow)
145147
export(run_dir)
146148
export(set_random_seed)
147149
export(shape)
@@ -162,6 +164,7 @@ export(use_python)
162164
export(use_session_with_seed)
163165
export(use_virtualenv)
164166
export(view_savedmodel)
167+
if (getRversion() >= "4.3.0") S3method("%*%",tensorflow.tensor)
165168
import(reticulate)
166169
importFrom(grDevices,as.raster)
167170
importFrom(lifecycle,deprecated)
@@ -171,6 +174,7 @@ importFrom(reticulate,dict)
171174
importFrom(reticulate,import)
172175
importFrom(reticulate,iterate)
173176
importFrom(reticulate,np_array)
177+
importFrom(reticulate,py_require)
174178
importFrom(reticulate,tuple)
175179
importFrom(reticulate,use_condaenv)
176180
importFrom(reticulate,use_python)

NEWS.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# tensorflow (development version)
22

3-
- Updates for reticulate 1.41. The tensorflow R package now calls
4-
`reticuate::py_require()` when it is loaded. Calling `install_tensorflow()`
3+
# tensorflow 2.20.0
4+
5+
- Updates for reticulate 1.41 and `reticuate::py_require()`.
6+
New function `py_require_tensorflow()` which can be called at the start of an R session
7+
to enable reticulate to resolve tensorflow. Calling `install_tensorflow()`
58
in most circumstances is no longer necessary.
6-
- `install_tensorflow()` installs TensorFlow v2.18 by default.
9+
- `install_tensorflow()` installs TensorFlow v2.20 by default.
710
- Fixed an issue where GPUs would not be found when running on Windows
811
WSL Linux (reported in rstudio/keras3#1456, fixed in #599)
912
- Fixes for NumPy 2.0 (#601)

R/generics.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,7 @@ py_to_r.keras.src.utils.tracking.TrackedList <- function(x) import("builtins")$l
884884
#' @export
885885
py_to_r.keras.src.utils.tracking.TrackedSet <- function(x) import("builtins")$list(x)
886886

887-
888-
#' @rdname Ops-python-methods
889-
#' @rawNamespace if (getRversion() >= "4.3.0") S3method("%*%",python.builtin.object)
887+
#' @rawNamespace if (getRversion() >= "4.3.0") S3method("%*%",tensorflow.tensor)
890888
`%*%.tensorflow.tensor` <- function(x, y) {
891889
if (is.atomic(x) && is_tensor(y)) {
892890
if (length(x) > 1L)

R/install.R

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22
#'
33
#' @description
44
#'
5-
#' Beginning with reticulate version 1.41, in most circumstances, calling the
6-
#' `install_tensorflow()` function is no longer necessary, because reticulate
7-
#' automatically registers python requirements with `reticulate::py_require()`
8-
#' when tensorflow is loaded.
5+
#' This function installs TensorFlow into a persistant virtual environment.
6+
#' Beginning with reticulate version 1.41, in most circumstances, creating a
7+
#' persistent virtual environment by calling the `install_tensorflow()` function
8+
#' is no longer necessary, because reticulate automatically will resolve a
9+
#' python environment that satisfies all python requirements declared with
10+
#' `reticulate::py_require()`.
911
#'
10-
#' The Python packages registered with `py_require()` by the tensorflow R
11-
#' package:
12+
#' New code is recommended to call `py_require_tensorflow()` at the start of an
13+
#' R session to declare tensorflow requirements via `py_requore()`. In a future
14+
#' package update this will by default be done in tensorflow's `.onLoad` hook.
15+
#'
16+
#' The `py_require_tensorflow()` function that can dynamically modify the python
17+
#' requirements to enable usage of a GPU if one is available and usable by the R
18+
#' session.
19+
#'
20+
#' The Python packages registered with `py_require()` by
21+
#' `py_require_tensorflow()`:
1222
#'
1323
#' - On Linux: if a GPU is detected: `"tensorflow[and-cuda]"`, otherwise,
1424
#' `"tensorflow-cpu"`.
@@ -31,17 +41,6 @@
3141
#' virtual environment with more complete set packages that includes additional
3242
#' optional dependencies, use [`keras3::install_keras()`].
3343
#'
34-
#' @details You may be prompted to download and install miniconda if reticulate
35-
#' did not find a non-system installation of python. Miniconda is the
36-
#' recommended installation method for most users, as it ensures that the R
37-
#' python installation is isolated from other python installations. All python
38-
#' packages will by default be installed into a self-contained conda or venv
39-
#' environment named "r-reticulate". Note that "conda" is the only supported
40-
#' method on M1 Mac.
41-
#'
42-
#' If you initially declined the miniconda installation prompt, you can later
43-
#' manually install miniconda by running [`reticulate::install_miniconda()`].
44-
#'
4544
#' @section Custom Installation: `install_tensorflow()` or
4645
#' `keras3::install_keras()` isn't required to use tensorflow with the
4746
#' package. If you manually configure a python environment with the required
@@ -131,6 +130,12 @@
131130
#' @param new_env If `TRUE`, any existing Python virtual environment and/or
132131
#' conda environment specified by `envname` is deleted first.
133132
#'
133+
#' @param use_gpu Only consulted if on Linux. It has no effect on macOS or
134+
#' Windows. If `NA`, the R package will attempt to detect a GPU. If a GPU is
135+
#' detected, then this is taken as `TRUE`, `FALSE` otherwise. If `TRUE`, then
136+
#' `tensorflow[and-cuda]` is declared, otherwise, `tensorflow-cpu` is declared
137+
#' via `py_require()`.
138+
#'
134139
#' @param ... other arguments passed to [`reticulate::conda_install()`] or
135140
#' [`reticulate::virtualenv_install()`], depending on the `method` used.
136141
#'
@@ -372,13 +377,15 @@ has_gpu <- function() {
372377
}
373378

374379

375-
get_py_requirements <- function() {
380+
get_py_requirements <- function(use_gpu = NA) {
376381
python_version <- NULL
377382
packages <- "tensorflow"
378383

379384
if(is_linux()) {
385+
if(is.na(use_gpu))
386+
use_gpu <- has_gpu()
380387

381-
if(has_gpu()) {
388+
if(use_gpu) {
382389
packages <- "tensorflow[and-cuda]"
383390
} else {
384391
packages <- "tensorflow-cpu"
@@ -402,7 +409,7 @@ get_py_requirements <- function() {
402409
list(packages = packages, python_version = python_version)
403410
}
404411

405-
default_version <- numeric_version("2.18")
412+
default_version <- numeric_version("2.20")
406413

407414
parse_tensorflow_version <- function(version) {
408415
# returns unquoted string directly passable to pip, e.g 'tensorflow==2.5.*'

R/package.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ tf_v2 <- function() {
3737
.globals <- new.env(parent = emptyenv())
3838
.globals$tensorboard <- NULL
3939

40+
#' @export
41+
#' @rdname install_tensorflow
42+
py_require_tensorflow <- function(use_gpu = NA) {
43+
# register requirements with py_require()
44+
reqs <- get_py_requirements(use_gpu)
45+
reticulate::py_require(reqs$packages, reqs$python_version)
46+
}
4047

4148
.onLoad <- function(libname, pkgname) {
4249

@@ -61,9 +68,9 @@ tf_v2 <- function() {
6168
if (!is.null(cpp_log_opt))
6269
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = max(min(cpp_log_opt, 3), 0))
6370

64-
# register requirements with py_require()
65-
reqs <- get_py_requirements()
66-
reticulate::py_require(reqs$packages, reqs$python_version)
71+
if (tolower(Sys.getenv("PY_REQUIRE_TENSORFLOW")) %in% c("1", "true", "yes")) {
72+
py_require_tensorflow()
73+
}
6774

6875
# delay load tensorflow
6976
tryCatch({
@@ -288,5 +295,8 @@ tf_config_error_message <- function() {
288295

289296
message <- paste0(message,
290297
"\nYou can install TensorFlow using the install_tensorflow() function.\n")
298+
base::message(
299+
'Hint: To use tensorflow with `py_require()`, call `py_require("tensorflow")` at the start of the R session'
300+
)
291301
message
292302
}

R/reexports.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ reticulate::use_virtualenv
3030
#' @export
3131
reticulate::use_condaenv
3232

33+
#' @export
34+
reticulate::py_require
35+
3336
#' @importFrom tfruns flags
3437
#' @export
3538
tfruns::flags

man/install_tensorflow.Rd

Lines changed: 28 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reexports.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)