From d62f808b2503671e9131d5c20302e33312823854 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Wed, 11 Nov 2026 17:56:27 -0500 Subject: [PATCH 1/3] Fix TensorFlow requirements with NumPy 2 on Windows --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/install.R | 13 ++++--------- man/install_tensorflow.Rd | 9 ++++----- tests/testthat/test-requirements.R | 18 ++++++++++++++++++ 5 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 tests/testthat/test-requirements.R diff --git a/DESCRIPTION b/DESCRIPTION index 5c6f8e9..f4d9d09 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,7 +49,7 @@ Imports: lifecycle Roxygen: list(markdown = TRUE) Suggests: - testthat (>= 2.1.0), + testthat (>= 3.1.7), keras3, pillar, withr, diff --git a/NEWS.md b/NEWS.md index e87a74f..f5334d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # tensorflow (development version) +- `py_require_tensorflow()` no longer adds an obsolete NumPy constraint on + Windows, allowing TensorFlow to resolve with Python 3.13. - Updated docs to recommend `py_require_tensorflow()` for TensorFlow setup (#627). - Fixed `tensorboard()` startup with recent TensorBoard versions (#626). diff --git a/R/install.R b/R/install.R index cd41016..7a554db 100644 --- a/R/install.R +++ b/R/install.R @@ -40,11 +40,10 @@ #' if `tensorflow-metal` is installed. There are known issues with random number #' generators like `tf$random$stateless_uniform()`, likely others as well. #' -#' - On Windows: `"tensorflow"` and `"numpy<2"` are declared. Note that -#' TensorFlow GPU usage on Windows is no longer supported (Since TensorFlow -#' 2.10). To use a GPU on windows, use TensorFlow via WSL. `"numpy<2"` is -#' declared because at the time of this publishing, the pre-built binaries of -#' `tensorflow` for Windows are not compatible with `numpy>2`. +#' - On Windows: `"tensorflow"` is declared. TensorFlow's package metadata +#' determines the compatible NumPy versions. TensorFlow 2.10 was the last +#' release with GPU support on native Windows. To use a GPU with newer +#' versions, use TensorFlow via WSL. #' #' `install_tensorflow()` remains available for users who want to explicitly #' create a persistent virtual environment. It creates a new virtual environment @@ -411,10 +410,6 @@ get_py_requirements <- function(use_gpu = NA) { packages <- c("tensorflow", "tensorflow-metal") } - } else if (is_windows()) { - - packages <- c(packages, "numpy<2") - } list(packages = packages, python_version = python_version) diff --git a/man/install_tensorflow.Rd b/man/install_tensorflow.Rd index cac9be3..232bbae 100644 --- a/man/install_tensorflow.Rd +++ b/man/install_tensorflow.Rd @@ -128,11 +128,10 @@ of using the GPU. To enable TensorFlow usage of the GPU, call initialized Python. Note that not all features of TensorFlow work correctly if \code{tensorflow-metal} is installed. There are known issues with random number generators like \code{tf$random$stateless_uniform()}, likely others as well. -\item On Windows: \code{"tensorflow"} and \code{"numpy<2"} are declared. Note that -TensorFlow GPU usage on Windows is no longer supported (Since TensorFlow -2.10). To use a GPU on windows, use TensorFlow via WSL. \code{"numpy<2"} is -declared because at the time of this publishing, the pre-built binaries of -\code{tensorflow} for Windows are not compatible with \code{numpy>2}. +\item On Windows: \code{"tensorflow"} is declared. TensorFlow's package metadata +determines the compatible NumPy versions. TensorFlow 2.10 was the last +release with GPU support on native Windows. To use a GPU with newer +versions, use TensorFlow via WSL. } \code{install_tensorflow()} remains available for users who want to explicitly diff --git a/tests/testthat/test-requirements.R b/tests/testthat/test-requirements.R new file mode 100644 index 0000000..27c480a --- /dev/null +++ b/tests/testthat/test-requirements.R @@ -0,0 +1,18 @@ +test_that("TensorFlow requirements allow NumPy 2 on Windows", { + requirements <- callr::r(function() { + library(tensorflow) + testthat::local_mocked_bindings( + is_linux = function() FALSE, + is_mac_arm64 = function() FALSE, + is_windows = function() TRUE, + .package = "tensorflow" + ) + + reticulate::py_require("numpy>=2", python_version = "3.13", action = "set") + tensorflow::py_require_tensorflow() + reticulate::py_require() + }) + + expect_setequal(requirements$packages, c("numpy>=2", "tensorflow")) + expect_identical(requirements$python_version, "3.13") +}) From db6aba229527196f1914715d4ffd4f836af225b5 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Wed, 11 Nov 2026 17:56:27 -0500 Subject: [PATCH 2/3] Refresh GitHub Actions workflows --- .github/workflows/R-CMD-check.yaml | 2 +- .github/workflows/awaiting-response-close.yml | 2 +- .github/workflows/awaiting-response-remove-label.yml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 8b67d01..ee43bbf 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -65,7 +65,7 @@ jobs: steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v7 - uses: r-lib/actions/setup-pandoc@v2 diff --git a/.github/workflows/awaiting-response-close.yml b/.github/workflows/awaiting-response-close.yml index 36cd92e..5496c4c 100644 --- a/.github/workflows/awaiting-response-close.yml +++ b/.github/workflows/awaiting-response-close.yml @@ -14,7 +14,7 @@ jobs: pull-requests: write steps: - - uses: actions/stale@v4 + - uses: actions/stale@v11 with: repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/awaiting-response-remove-label.yml b/.github/workflows/awaiting-response-remove-label.yml index a10e70b..ccf714c 100644 --- a/.github/workflows/awaiting-response-remove-label.yml +++ b/.github/workflows/awaiting-response-remove-label.yml @@ -11,8 +11,8 @@ jobs: - name: Remove 'awaiting response' label and maybe reopen if: contains(github.event.issue.labels.*.name, 'awaiting response') run: | - gh issue edit $ISSUE --remove-label 'awaiting response' - gh issue reopen $ISSUE + gh issue edit "$ISSUE" --remove-label 'awaiting response' + gh issue reopen "$ISSUE" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ISSUE: ${{ github.event.issue.html_url }} From 285b7749ef567dff459b6895b51b2bf8fb08991c Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Wed, 11 Nov 2026 18:09:15 -0500 Subject: [PATCH 3/3] Preserve large numeric values in integer tensor conversion --- NEWS.md | 2 ++ R/generics.R | 4 +++- tests/testthat/test-as_tensor.R | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index f5334d5..6232054 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # tensorflow (development version) +- `as_tensor()` preserves large numeric values when converting to `int64`, + `uint64`, or `uint32`, and avoids an integer overflow error on Windows. - `py_require_tensorflow()` no longer adds an obsolete NumPy constraint on Windows, allowing TensorFlow to resolve with Python 3.13. - Updated docs to recommend `py_require_tensorflow()` for TensorFlow setup (#627). diff --git a/R/generics.R b/R/generics.R index 14ba666..7df815f 100644 --- a/R/generics.R +++ b/R/generics.R @@ -824,7 +824,9 @@ as_tensor.double <- function(x, dtype = NULL, ..., name = NULL) { # tf.dtypes.saturate_cast() is better, but still can be surprising. # Users must opt in to saturate cast (or overflow cast) # by casting in a separate call after converting to tensor. - if (dtype$max <= .Machine$integer.max) + # Compare width and signedness without converting dtype$max to an R + # integer, which overflows for int64, uint64, and uint32. + if (dtype$size < 4L || (dtype$size == 4L && !dtype$is_unsigned)) storage.mode(x) <- "integer" if (anyNA(x)) diff --git a/tests/testthat/test-as_tensor.R b/tests/testthat/test-as_tensor.R index 9fe6893..726bc13 100644 --- a/tests/testthat/test-as_tensor.R +++ b/tests/testthat/test-as_tensor.R @@ -97,6 +97,22 @@ test_that("as_tensor works", { }) +test_that("as_tensor preserves values outside R's integer range", { + skip_if_no_tensorflow() + + for (dtype in c("int64", "uint64", "uint32")) { + value <- if (dtype == "uint32") 2^31 else 2^40 + for (x in list(value, c(value, value + 1))) { + tensor <- as_tensor(x, dtype) + expect_identical(tensor$dtype$name, dtype) + expect_equal(as.numeric(tensor), x) + } + } + + expect_equal(as.numeric(as_tensor(-2^40, "int64")), -2^40) +}) + + test_that("conversion of tf.string dtype tensors", { xx <- list(array("foo"),