From 471afba1f10471f8e9305b138a0951c4a7245a8d Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Fri, 14 Jun 2019 09:50:26 -0300 Subject: [PATCH 1/8] delegate installation to reticulate --- R/install.R | 641 ++++++++++------------------------------------------ 1 file changed, 118 insertions(+), 523 deletions(-) diff --git a/R/install.R b/R/install.R index 19ebf7bc..5008ed73 100644 --- a/R/install.R +++ b/R/install.R @@ -1,5 +1,3 @@ - - #' Install TensorFlow and its dependencies #' #' @inheritParams reticulate::conda_list @@ -32,222 +30,92 @@ #' @importFrom jsonlite fromJSON #' #' @export -install_tensorflow <- function(method = c("auto", "virtualenv", "conda", "system"), +install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), conda = "auto", version = "default", envname = "r-tensorflow", extra_packages = NULL, restart_session = TRUE) { - # verify os - if (!is_windows() && !is_osx() && !is_linux()) { - stop("Unable to install TensorFlow on this platform. ", - "Binary installation is available for Windows, OS X, and Linux") - } - # verify 64-bit if (.Machine$sizeof.pointer != 8) { stop("Unable to install TensorFlow on this platform.", "Binary installation is only available for 64-bit platforms.") } - # resolve and validate method method <- match.arg(method) - if (identical(method, "system") && !is_windows()) { - stop("Installing TensorFlow into the system library is only supported on Windows", - call. = FALSE) - } - if (identical(method, "virtualenv") && is_windows()) { - stop("Installing TensorFlow into a virtualenv is not supported on Windows", - call. = FALSE) - } # unroll version ver <- parse_tensorflow_version(version) + version <- ver$version gpu <- ver$gpu - packages <- ver$packages - - # extra packages - extra_packages <- unique(c(extra_packages, c("keras", "tensorflow-hub"))) - if (version == "nightly") { - extra_packages <- unique(c(extra_packages, "tfp-nightly")) - extra_packages <- setdiff(extra_packages, "tensorflow-probability") - } - if (version == "default" || substr(version, 1, 4) %in% c("1.12", "1.13")) { - extra_packages <- unique(c(extra_packages, "tensorflow-probability")) - extra_packages <- setdiff(extra_packages, ("tfp-nightly")) - } - - # flags indicating what methods are available - method_available <- function(name) method %in% c("auto", name) - virtualenv_available <- method_available("virtualenv") - conda_available <- method_available("conda") - system_available <- is_windows() && method_available("site") - - # resolve and look for conda - conda_bin <- tryCatch(conda_binary(conda), error = function(e) NULL) - have_conda <- conda_available && !is.null(conda_bin) - - # mac and linux - if (is_unix()) { + package <- ver$package - # check for explicit conda method - if (identical(method, "conda")) { + # Packages in this list should always be installed. - # validate that we have conda - if (!have_conda) - stop("Conda installation failed (no conda binary found)\n", call. = FALSE) + default_packages <- c("tensorflow-hub") - # do install - install_tensorflow_conda(conda, version, gpu, envname, packages, extra_packages) - - } else { - - # find system python binary - pyver <- "" - python <- python_unix_binary("python") - if (is.null(python)) { - # try for python3 if we are on linux - if (is_linux()) { - python <- python_unix_binary("python3") - if (is.null(python)) - stop("Unable to locate Python on this system.", call. = FALSE) - pyver <- "3" - } - } - - # find other required tools - pip <- python_unix_binary(paste0("pip", pyver)) - have_pip <- !is.null(pip) - virtualenv <- python_unix_binary("virtualenv") - have_virtualenv <- virtualenv_available && !is.null(virtualenv) - - # if we don't have pip and virtualenv then try for conda if it's allowed - if ((!have_pip || !have_virtualenv) && have_conda) { - - install_tensorflow_conda(conda, version, gpu, envname, packages, extra_packages) - - - # otherwise this is either an "auto" installation w/o working conda - # or it's an explicit "virtualenv" installation - } else { - - # validate that we have the required tools for the method - install_commands <- NULL - if (is_osx()) { - if (!have_pip) - install_commands <- c(install_commands, "$ sudo /usr/bin/easy_install pip") - if (!have_virtualenv) { - if (is.null(pip)) - pip <- "/usr/local/bin/pip" - install_commands <- c(install_commands, sprintf("$ sudo %s install --upgrade virtualenv", pip)) - } - if (!is.null(install_commands)) - install_commands <- paste(install_commands, collapse = "\n") - } else if (is_ubuntu()) { - if (!have_pip) { - install_commands <- c(install_commands, paste0("$ sudo apt-get install python", pyver ,"-pip")) - pip <- paste0("/usr/bin/pip", pyver) - } - if (!have_virtualenv) { - if (identical(pyver, "3")) - install_commands <- c(install_commands, paste("$ sudo", pip, "install virtualenv")) - else - install_commands <- c(install_commands, "$ sudo apt-get install python-virtualenv") - } - if (!is.null(install_commands)) - install_commands <- paste(install_commands, collapse = "\n") - } else { - if (!have_pip) - install_commands <- c(install_commands, "pip") - if (!have_virtualenv) - install_commands <- c(install_commands, "virtualenv") - if (!is.null(install_commands)) { - install_commands <- paste("Please install the following Python packages before proceeding:", - paste(install_commands, collapse = ", ")) - } - } - if (!is.null(install_commands)) { - - # if these are terminal commands then add special preface - if (grepl("^\\$ ", install_commands)) { - install_commands <- paste0( - "Execute the following at a terminal to install the prerequisites:\n\n", - install_commands - ) - } - - stop("Prerequisites for installing TensorFlow not available.\n\n", - install_commands, "\n\n", call. = FALSE) - } - - # do the install - install_tensorflow_virtualenv(python, virtualenv, version, gpu, envname, packages, extra_packages) - - } + # install tensorflow-probability + if (!is.na(version) && substr(version, 1, 4) %in% c("1.12", "1.13", "1.14")) { + default_packages <- c(default_packages, "tensorflow-probability") + # extra_packages cannot contain the nightly version of tfp. + if ("tfp-nightly" %in% extra_packages) { + warning("tfp-nightly won't be installed since it requires TensorFlow nightly. ", + "tensorflow-probability will be installed instead.") + extra_packages <- setdiff(extra_packages, "tfp-nightly") } - # windows installation - } else { - - # determine whether we have system python - python_versions <- py_versions_windows() - python_versions <- python_versions[python_versions$type == "PythonCore",] - python_versions <- python_versions[python_versions$version %in% c("3.5","3.6"),] - python_versions <- python_versions[python_versions$arch == "x64",] - have_system <- nrow(python_versions) > 0 - if (have_system) - python_system_version <- python_versions[1,] - - # resolve auto - if (identical(method, "auto")) { - - if (!have_system && !have_conda) { - stop("Installing TensorFlow requires a 64-bit version of Python 3.5 or 3.6\n\n", - "Please install 64-bit Python 3.5 or 3.6 to continue, supported versions include:\n\n", - " - Anaconda Python (Recommended): https://www.anaconda.com/download/#windows\n", - " - Python Software Foundation : https://www.python.org/downloads/\n\n", - call. = FALSE) - } else if (have_conda) { - method <- "conda" - } else if (have_system) { - method <- "system" - } + # install tfp-nightly + } else if (is.na(version) && + (substr(version, 1, 4) %in% c("2.0.") || version == "nightly")) { + default_packages <- c(default_packages, "tfp-nightly") + # extra_packages cannot contain tensorflow-probability version + if ("tensorflow-probability" %in% extra_packages) { + warning("tensorflow-probability won't be installed since it requires TensorFlow nightly. ", + "tfp-nightly will be installed instead.") + extra_packages <- setdiff(extra_packages, "tfp-nightly") } + } - if (identical(method, "conda")) { - - # validate that we have conda - if (!have_conda) { - stop("Conda installation failed (no conda binary found)\n\n", - "Install Anaconda 3.x for Windows (https://www.anaconda.com/download/#windows)\n", - "before installing TensorFlow.", - call. = FALSE) - } - - # do the install - install_tensorflow_conda(conda, version, gpu, envname, packages, extra_packages) + extra_packages <- unique(c(default_packages, extra_packages)) + + # Main OS verification. + if (is_osx() || is_linux()) { + + if (method == "conda") { + install_conda( + package = package, + extra_packages = extra_packages, + envname = envname, + conda = conda + ) + } else if (method == "virtualenv" || method == "auto") { + install_virtualenv( + package = package, + extra_packages = extra_packages + ) + } - } else if (identical(method, "system")) { + } else if (is_windows()) { - # if we don't have it then error - if (!have_system) { - stop("Installing TensorFlow requires a 64-bit version of Python 3.5 or 3.6\n\n", - "Please install 64-bit Python 3.5 or 3.6 this location to continue:\n\n", - " - https://www.python.org/downloads/\n\n", - call. = FALSE) - } + if (method == "virtualenv") { + stop("Installing TensorFlow into a virtualenv is not supported on Windows", + call. = FALSE) + } else if (method == "conda" || method == "auto") { - # do system installation - python <- python_system_version$executable_path - pip <- file.path(python_system_version$install_path, "Scripts", "pip.exe") - install_tensorflow_windows_system(python, pip, version, gpu, packages, extra_packages) + install_conda( + package = package, + extra_packages = extra_packages, + envname = envname, + conda = conda + ) - } else { - stop("Invalid/unexpected installation method '", method, "'", - call. = FALSE) } + + } else { + stop("Unable to install TensorFlow on this platform. ", + "Binary installation is available for Windows, OS X, and Linux") } cat("\nInstallation complete.\n\n") @@ -258,396 +126,123 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda", "system invisible(NULL) } -install_tensorflow_conda <- function(conda, version, gpu, envname, packages, extra_packages = NULL) { +install_conda <- function(package, extra_packages, envname, conda) { - # remove existing conda environment if we need to (necessary to work around TF upgrade bugs) - conda_envs <- conda_list(conda = conda) - conda_env <- subset(conda_envs, conda_envs$name == envname) - if (nrow(conda_env) == 1) - conda_remove(envname, conda = conda) - # create conda environment - cat("Creating", envname, "conda environment for TensorFlow installation...\n") - if (grepl("1.1[0-2]", substr(version, 1, 4))) { - python_packages <- "python=3.6" - } else { - python_packages <- "python=3.7" - } - python <- conda_create(envname, packages = python_packages, conda = conda) - - - # Short circuit to install everything with conda when no custom packages - # (typically tf-nightly or a daily build URL) and no gpu-enabled build - # is requested (conda doesn't currently have GPU enabled builds. - # - # This avoids any use of pip, which addresses the following issue: - # https://github.com/rstudio/keras/issues/147 - # - # This issue is in turn created by two other issues: - # - # 1) TensorBoard appears to rely on an older version of html5lib which is - # force installed, and which as a result breaks pip: - # https://github.com/tensorflow/tensorboard/issues/588 - # - # 2) Anaconda 5.0.0 is unable to recover from this because the installation - # of the old version of html5lib actually propagates to the root - # environment, which permantely breaks pip for *all* conda environments: - # https://github.com/conda/conda/issues/6079 - # - # Hopefully these two issues will be addressed and we can return to using - # pip in all scenarios (as that is the officially supported version) - # - if (is_windows() && is.null(packages) && gpu == FALSE) { - conda_forge_install( - envname, - tf_pkgs(version, gpu, packages, extra_packages = extra_packages), - conda = conda - ) - return(invisible(NULL)) - } + # find if environment exists + envname_exists <- envname %in% reticulate::conda_list(conda = conda)$name - # determine tf version - if (version == "latest") { - cat("Determining latest installable release of TensorFlow...") - releases <- fromJSON("https://api.github.com/repos/tensorflow/tensorflow/releases") - latest <- subset(releases, grepl("^v\\d+\\.\\d+\\.\\d+$", releases$tag_name))$tag_name[[1]] - version <- sub("v", "", latest) - # workaround the fact that v1.X.1 releases often have no tarball - version_split <- strsplit(version, ".", fixed = TRUE)[[1]] - if (length(version_split) > 2 && version_split[[length(version_split)]] != "0") - version_split <- c(version_split[-length(version_split)], "0") - version <- paste(version_split, collapse = ".") - cat("done\n") - } - - # determine python version - py_version <- python_version(python) - py_version_str <- if (is_osx()) { - if (py_version >= "3.0") - "py3-none" - else - "py2-none" - } else { - if (py_version >= "3.0") { - ver <- gsub(".", "", as.character(py_version), fixed = TRUE) - sprintf("cp%s-cp%sm", ver, ver) - } else { - "cp27-none" + # remove environment + if (envname_exists) { + cat("Removing ", envname, " conda environment... \n") + reticulate::conda_remove(envname = envname, conda = conda) } - } - - # determine arch - arch <- ifelse(is_windows(), "win_amd64", ifelse(is_osx(), "any", "linux_x86_64")) - - # determine packages url if necessary - if (is.null(packages)) { - # version must have 3 digits - if (grepl("^\\d+\\.\\d+$", version)) - version <- paste0(version, ".0") - platform <- ifelse(is_windows(), "windows", ifelse(is_osx(), "mac", "linux")) - packages <- sprintf( - "https://storage.googleapis.com/tensorflow/%s/%s/tensorflow%s-%s-%s-%s.whl", - platform, - ifelse(gpu, "gpu", "cpu"), - ifelse(gpu, "_gpu", ""), - version, - py_version_str, - arch - ) - } - - # install base tensorflow using pip - cat("Installing TensorFlow...\n") - conda_install(envname, packages, pip = TRUE, conda = conda) - - # install additional packages - conda_install(envname, tf_extra_pkgs(), conda = conda) - - # install extra packages (use pip to ensure we don't get legacy versions, set - # pip_ignore_installed to FALSE to ensure that pip source installs on - # windows don't attempt to override conda binary packages (e.g. SciPy which - # will likely fail to install via pip due to compilation dependencies) - if (!is.null(extra_packages)) { - conda_install( - envname, - extra_packages, - pip = TRUE, - pip_ignore_installed = FALSE, - conda = conda) - } -} - - -conda_forge_install <- function(envname, packages, conda = "auto") { - # resolve conda binary - conda <- conda_binary(conda) - # use native conda package manager with conda forge enabled - result <- system2(conda, shQuote(c("install", "-c", "conda-forge", "--yes", "--name", envname, packages))) + cat("Creating ", envname, " conda environment... \n") + reticulate::conda_create(envname = envname, conda = conda) - # check for errors - if (result != 0L) { - stop("Error ", result, " occurred installing packages into conda environment ", - envname, call. = FALSE) - } + cat("Installing python modules...\n") + reticulate::conda_install( + envname = envname, + packages = c(package, extra_packages), + conda = conda, + pip = TRUE # always use pip since it's the recommend way. + ) - invisible(NULL) } +install_virtualenv <- function(package, extra_packages, envname) { + # find if environment exists + envname_exists <- envname %in% reticulate::virtualenv_list() -install_tensorflow_virtualenv <- function(python, virtualenv, version, gpu, envname, packages, extra_packages = NULL) { - - # determine python version to use - is_python3 <- python_version(python) >= "3.0" - pip_version <- ifelse(is_python3, "pip3", "pip") + # remove environment + if (envname_exists) + reticulate::virtualenv_remove(envname = envname, confirm = FALSE) - # create virtualenv - virtualenv_root <- Sys.getenv("WORKON_HOME", unset = "~/.virtualenvs") - if (!file.exists(virtualenv_root)) - dir.create(virtualenv_root, recursive = TRUE) + reticulate::virtualenv_create(envname = envname) - # helper to construct paths to virtualenv binaries - virtualenv_bin <- function(bin) path.expand(file.path(virtualenv_path, "bin", bin)) - - # destroy and re-create virtualenv (necessary to work around tf upgrade bugs) - virtualenv_path <- file.path(virtualenv_root, envname) - if (dir_exists(virtualenv_path)) - unlink(virtualenv_path, recursive = TRUE) - cat("Creating virtualenv for TensorFlow at ", virtualenv_path, "\n") - result <- system2(virtualenv, shQuote(c( - "--system-site-packages", - "--python", python, - path.expand(virtualenv_path))) - ) - if (result != 0L) - stop("Error ", result, " occurred creating virtualenv at ", virtualenv_path, - call. = FALSE) - - - # see what version of pip is installed (assume 0.1 on error) - installed_pip_version <- function() { - tryCatch({ - # check existing version - cmd <- sprintf("%ssource %s && %s --version%s", - ifelse(is_osx(), "", "/bin/bash -c \""), - shQuote(path.expand(virtualenv_bin("activate"))), - shQuote(path.expand(virtualenv_bin(pip_version))), - ifelse(is_osx(), "", "\"")) - result <- system(cmd, intern = TRUE, ignore.stderr = TRUE) - - # parse result - matches <- regexec("^[^ ]+\\s+(\\d+)\\.(\\d+).*$", result) - matches <- regmatches(result, matches)[[1]] - - # return as R numeric version - numeric_version(paste(matches[[2]], matches[[3]], sep = ".")) - - }, error = function(e) { - warning("Error occurred checking pip version: ", e$message) - numeric_version("0.1") - }) - } - - # function to call pip within virtual env - pip_install <- function(pkgs, message) { - cmd <- sprintf("%ssource %s && %s install --ignore-installed --upgrade %s%s", - ifelse(is_osx(), "", "/bin/bash -c \""), - shQuote(path.expand(virtualenv_bin("activate"))), - shQuote(path.expand(virtualenv_bin(pip_version))), - paste(shQuote(pkgs), collapse = " "), - ifelse(is_osx(), "", "\"")) - cat(message, "...\n") - result <- system(cmd) - if (result != 0L) - stop("Error ", result, " occurred installing TensorFlow", call. = FALSE) - } - - # upgrade pip and related utilities if its older than 8.1 - if (installed_pip_version() < "8.1") { - pip_install("pip", "Upgrading pip") - pip_install("wheel", "Upgrading wheel") - pip_install("setuptools", "Upgrading setuptools") - } - - # install tensorflow and related dependencies - pkgs <- tf_pkgs(version, gpu, packages, scipy = TRUE, extra_packages = extra_packages) - pip_install(pkgs, "Installing TensorFlow") -} - - -install_tensorflow_windows_system <- function(python, pip, version, gpu, packages, extra_packages = NULL) { - - # ensure pip is up to date - cat("Preparing for installation (updating pip if necessary)\n") - result <- system2(python, c("-m", "pip", "install", "--upgrade", "pip")) - if (result != 0L) - stop("Error ", result, " occurred updating pip", call. = FALSE) - - # install tensorflow and dependencies (don't install scipy b/c it requires - # native code compilation) - cat("Installing TensorFlow...\n") - pkgs <- tf_pkgs(version, gpu, packages, scipy = FALSE, extra_packages = extra_packages) - result <- system2(pip, c("install", "--upgrade --ignore-installed", - paste(shQuote(pkgs), collapse = " "))) - if (result != 0L) - stop("Error ", result, " occurred installing tensorflow package", call. = FALSE) + reticulate::virtualenv_install( + envname = envname, + packages = c(package, extra_packages) + ) - cat("\nInstallation of TensorFlow complete.\n\n") } - parse_tensorflow_version <- function(version) { - # defaults + default_version <- "1.13.1" + ver <- list( - version = "latest", + version = default_version, gpu = FALSE, - packages = NULL + package = NULL ) - # full url provided - if (identical(version, "default")) { + if (version == "default") { - ver$version <- "1.13.1" + ver$package <- paste0("tensorflow==", ver$version) - # gpu version - } else if (identical(version, "gpu")) { - - ver$version <- "1.13.1" + # default gpu version + } else if (version == "gpu") { ver$gpu <- TRUE + ver$package <- paste0("tensorflow-gpu==", ver$version) - # gpu qualifier provided + # gpu qualifier provided } else if (grepl("-gpu$", version)) { split <- strsplit(version, "-")[[1]] ver$version <- split[[1]] - ver$gpu <-TRUE + ver$gpu <- TRUE - # full path to installer binary + # full path to whl. } else if (grepl("^.*\\.whl$", version)) { + ver$gpu <- NA + ver$version <- NA + if (grepl("^http", version)) - ver$packages <- version + ver$package <- version else - ver$packages <- normalizePath(version) + ver$package <- normalizePath(version) - # another version + # another version } else { ver$version <- version } - # if it's the nightly version then set packages to nightly[-gpu] - # as of 02/15/2019 tf-nightly includes tb-nightly, tf-estimator-nightly - version <- sub("^tf-nightly$", "nightly", version) - if (identical(version, "nightly")) { - ver$packages <- c(sprintf("tf-nightly%s", ifelse(ver$gpu, "-gpu", ""))) - } + # find the right package for nightly and other versions + if (is.null(ver$package)) { - # return - ver -} - -python_unix_binary <- function(bin) { - locations <- file.path(c("/usr/bin", "/usr/local/bin", path.expand("~/.local/bin")), bin) - locations <- locations[file.exists(locations)] - if (length(locations) > 0) - locations[[1]] - else - NULL -} - -python_version <- function(python) { - - # check for the version - result <- system2(python, "--version", stdout = TRUE, stderr = TRUE) - - # check for error - error_status <- attr(result, "status") - if (!is.null(error_status)) - stop("Error ", error_status, " occurred while checking for python version", call. = FALSE) - - # parse out the major and minor version numbers - matches <- regexec("^[^ ]+\\s+(\\d+)\\.(\\d+).*$", result) - matches <- regmatches(result, matches)[[1]] - if (length(matches) != 3) - stop("Unable to parse Python version '", result[[1]], "'", call. = FALSE) - - # return as R numeric version - numeric_version(paste(matches[[2]], matches[[3]], sep = ".")) -} - - -# form list of tf pkgs -tf_pkgs <- function(version, gpu, packages, scipy = TRUE, extra_packages = NULL) { - if (is.null(packages)) - packages <- sprintf("tensorflow%s%s", - ifelse(gpu, "-gpu", ""), - ifelse(version == "latest", "", paste0("==", version))) - c(packages, tf_extra_pkgs(scipy = scipy, extra_packages = extra_packages)) -} - -# additional dependencies to install (required for keras) -tf_extra_pkgs <- function(scipy = TRUE, extra_packages = NULL) { - pkgs <- c("h5py", "pyyaml", "requests", "Pillow") - pkgs <- c(pkgs, extra_packages) - if (scipy) - c(pkgs, "scipy") - else - pkgs -} + if (ver$version == "nightly") { + if (ver$gpu) { + ver$package <- "tf-nightly-gpu" + } else { + ver$package <- "tf-nightly" + } -virtualenv_install <- function(envname, packages) { - - # TODO: refactor to share code between this and install_tensorflow_virtualenv - # (we added this code late in the v1.0 cycle so didn't want to do the - # refactor then) + } else { - # determine path to virtualenv - virtualenv_root <- Sys.getenv("WORKON_HOME", unset = "~/.virtualenvs") - virtualenv_path <- file.path(virtualenv_root, envname) + if (ver$gpu) { + ver$package <- paste0("tensorflow-gpu==", ver$version) + } else { + ver$package <- paste0("tensorflow==", ver$version) + } - # helper to construct paths to virtualenv binaries - virtualenv_bin <- function(bin) path.expand(file.path(virtualenv_path, "bin", bin)) + } - # determine pip version to use - python <- virtualenv_bin("python") - is_python3 <- python_version(python) >= "3.0" - pip_version <- ifelse(is_python3, "pip3", "pip") + } - # build and execute install command - cmd <- sprintf("%ssource %s && %s install --ignore-installed --upgrade %s%s", - ifelse(is_osx(), "", "/bin/bash -c \""), - shQuote(path.expand(virtualenv_bin("activate"))), - shQuote(path.expand(virtualenv_bin(pip_version))), - paste(shQuote(packages), collapse = " "), - ifelse(is_osx(), "", "\"")) - result <- system(cmd) - if (result != 0L) - stop("Error ", result, " occurred installing packages", call. = FALSE) + ver } -windows_system_install <- function(python, packages) { - - # TODO: refactor to share code with install_tensorflow_windows_system - - # determine pip location from python binary location - pip <- file.path(dirname(python), "Scripts", "pip.exe") - - # execute the installation - result <- system2(pip, c("install", "--upgrade --ignore-installed", - paste(shQuote(packages), collapse = " "))) - if (result != 0L) - stop("Error ", result, " occurred installing tensorflow package", call. = FALSE) -} - #' Install additional Python packages alongside TensorFlow #' #' This function is deprecated. Use the `extra_packages` argument to From 46981f6589c2fbafeee66ef3e78a1c36eb9f9ae2 Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Fri, 14 Jun 2019 09:54:14 -0300 Subject: [PATCH 2/8] pass envname to install virtualenv --- R/install.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index 5008ed73..ed9e0f38 100644 --- a/R/install.R +++ b/R/install.R @@ -93,7 +93,8 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), } else if (method == "virtualenv" || method == "auto") { install_virtualenv( package = package, - extra_packages = extra_packages + extra_packages = extra_packages, + envname = envname ) } From 1f3da4058f0b52fa94ead521fa5a45f899729da0 Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Tue, 18 Jun 2019 13:27:57 +0200 Subject: [PATCH 3/8] use || instead --- R/install.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index ed9e0f38..d1ca9d2a 100644 --- a/R/install.R +++ b/R/install.R @@ -67,7 +67,7 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), } # install tfp-nightly - } else if (is.na(version) && + } else if (is.na(version) || (substr(version, 1, 4) %in% c("2.0.") || version == "nightly")) { default_packages <- c(default_packages, "tfp-nightly") # extra_packages cannot contain tensorflow-probability version @@ -262,3 +262,4 @@ install_tensorflow_extras <- function(packages, conda = "auto") { } + From 61024fd1c189da6e8fc20cb5080f5dc1acad1317 Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Tue, 18 Jun 2019 13:33:00 +0200 Subject: [PATCH 4/8] simplify tf probability version --- R/install.R | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/R/install.R b/R/install.R index d1ca9d2a..9565580a 100644 --- a/R/install.R +++ b/R/install.R @@ -56,26 +56,12 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), default_packages <- c("tensorflow-hub") - # install tensorflow-probability + # Resolve TF probability version. if (!is.na(version) && substr(version, 1, 4) %in% c("1.12", "1.13", "1.14")) { default_packages <- c(default_packages, "tensorflow-probability") - # extra_packages cannot contain the nightly version of tfp. - if ("tfp-nightly" %in% extra_packages) { - warning("tfp-nightly won't be installed since it requires TensorFlow nightly. ", - "tensorflow-probability will be installed instead.") - extra_packages <- setdiff(extra_packages, "tfp-nightly") - } - # install tfp-nightly - } else if (is.na(version) || - (substr(version, 1, 4) %in% c("2.0.") || version == "nightly")) { + } else if (is.na(version) ||(substr(version, 1, 4) %in% c("2.0.") || version == "nightly")) { default_packages <- c(default_packages, "tfp-nightly") - # extra_packages cannot contain tensorflow-probability version - if ("tensorflow-probability" %in% extra_packages) { - warning("tensorflow-probability won't be installed since it requires TensorFlow nightly. ", - "tfp-nightly will be installed instead.") - extra_packages <- setdiff(extra_packages, "tfp-nightly") - } } extra_packages <- unique(c(default_packages, extra_packages)) @@ -260,6 +246,3 @@ install_tensorflow_extras <- function(packages, conda = "auto") { "Use the extra_packages argument to install_tensorflow() to ", "install additional packages.") } - - - From abd8283c189d1df5ad55f5242278c69d7dcafd54 Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Tue, 18 Jun 2019 13:37:44 +0200 Subject: [PATCH 5/8] add ellipsis to pass additional args to conda_install or virtualenv_install --- R/install.R | 33 ++++++++++++++++++++++----------- man/install_tensorflow.Rd | 7 +++++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/R/install.R b/R/install.R index 9565580a..a7d8e90d 100644 --- a/R/install.R +++ b/R/install.R @@ -27,6 +27,9 @@ #' @param restart_session Restart R session after installing (note this will #' only occur within RStudio). #' +#' @param ... other arguments passed to [reticulate::conda_install()] or +#' [reticulate::virtualenv_install()]. +#' #' @importFrom jsonlite fromJSON #' #' @export @@ -35,7 +38,8 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), version = "default", envname = "r-tensorflow", extra_packages = NULL, - restart_session = TRUE) { + restart_session = TRUE, + ...) { # verify 64-bit if (.Machine$sizeof.pointer != 8) { @@ -74,13 +78,15 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), package = package, extra_packages = extra_packages, envname = envname, - conda = conda + conda = conda, + ... ) } else if (method == "virtualenv" || method == "auto") { install_virtualenv( package = package, extra_packages = extra_packages, - envname = envname + envname = envname, + ... ) } @@ -95,7 +101,8 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), package = package, extra_packages = extra_packages, envname = envname, - conda = conda + conda = conda, + ... ) } @@ -113,9 +120,7 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), invisible(NULL) } -install_conda <- function(package, extra_packages, envname, conda) { - - +install_conda <- function(package, extra_packages, envname, conda, ...) { # find if environment exists envname_exists <- envname %in% reticulate::conda_list(conda = conda)$name @@ -135,25 +140,31 @@ install_conda <- function(package, extra_packages, envname, conda) { envname = envname, packages = c(package, extra_packages), conda = conda, - pip = TRUE # always use pip since it's the recommend way. + pip = TRUE, # always use pip since it's the recommend way. + ... ) } -install_virtualenv <- function(package, extra_packages, envname) { +install_virtualenv <- function(package, extra_packages, envname, ...) { # find if environment exists envname_exists <- envname %in% reticulate::virtualenv_list() # remove environment - if (envname_exists) + if (envname_exists) { + cat("Removing ", envname, " virtualenv environment... \n") reticulate::virtualenv_remove(envname = envname, confirm = FALSE) + } + cat("Creating ", envname, " virtualenv environment... \n") reticulate::virtualenv_create(envname = envname) + cat("Installing python modules...\n") reticulate::virtualenv_install( envname = envname, - packages = c(package, extra_packages) + packages = c(package, extra_packages), + ... ) } diff --git a/man/install_tensorflow.Rd b/man/install_tensorflow.Rd index 832fc248..03c0b483 100644 --- a/man/install_tensorflow.Rd +++ b/man/install_tensorflow.Rd @@ -4,9 +4,9 @@ \alias{install_tensorflow} \title{Install TensorFlow and its dependencies} \usage{ -install_tensorflow(method = c("auto", "virtualenv", "conda", "system"), +install_tensorflow(method = c("auto", "virtualenv", "conda"), conda = "auto", version = "default", envname = "r-tensorflow", - extra_packages = NULL, restart_session = TRUE) + extra_packages = NULL, restart_session = TRUE, ...) } \arguments{ \item{method}{Installation method. By default, "auto" automatically finds a @@ -36,6 +36,9 @@ TensorFlow.} \item{restart_session}{Restart R session after installing (note this will only occur within RStudio).} + +\item{...}{other arguments passed to \code{\link[reticulate:conda_install]{reticulate::conda_install()}} or +\code{\link[reticulate:virtualenv_install]{reticulate::virtualenv_install()}}.} } \description{ Install TensorFlow and its dependencies From d71d12f1fd539d1dda729b4ef6d8c92c22b21f79 Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Wed, 19 Jun 2019 23:00:52 +0200 Subject: [PATCH 6/8] try installing libgcc too --- R/install.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index a7d8e90d..3e051113 100644 --- a/R/install.R +++ b/R/install.R @@ -133,7 +133,10 @@ install_conda <- function(package, extra_packages, envname, conda, ...) { cat("Creating ", envname, " conda environment... \n") - reticulate::conda_create(envname = envname, conda = conda) + reticulate::conda_create( + envname = envname, conda = conda, + packages = c("python", "libgcc") + ) cat("Installing python modules...\n") reticulate::conda_install( From 2262374efbadab850bbcf7d1c4b4334849b75d20 Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Thu, 20 Jun 2019 00:20:58 +0200 Subject: [PATCH 7/8] try installation with python 3.6.8 --- R/install.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index 3e051113..fe541076 100644 --- a/R/install.R +++ b/R/install.R @@ -135,7 +135,7 @@ install_conda <- function(package, extra_packages, envname, conda, ...) { cat("Creating ", envname, " conda environment... \n") reticulate::conda_create( envname = envname, conda = conda, - packages = c("python", "libgcc") + packages = c("python=3.6") ) cat("Installing python modules...\n") From 4c58d7e8b24629963a9e9ee7660e2196fd98432a Mon Sep 17 00:00:00 2001 From: Daniel Falbel Date: Thu, 20 Jun 2019 00:42:11 +0200 Subject: [PATCH 8/8] controll the python version argument --- R/install.R | 10 ++++++++-- man/install_tensorflow.Rd | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/R/install.R b/R/install.R index fe541076..0c382a46 100644 --- a/R/install.R +++ b/R/install.R @@ -27,6 +27,9 @@ #' @param restart_session Restart R session after installing (note this will #' only occur within RStudio). #' +#' @param conda_python_version the python version installed in the created conda +#' environment. Python 3.6 is installed by default. +#' #' @param ... other arguments passed to [reticulate::conda_install()] or #' [reticulate::virtualenv_install()]. #' @@ -39,6 +42,7 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), envname = "r-tensorflow", extra_packages = NULL, restart_session = TRUE, + conda_python_version = "3.6", ...) { # verify 64-bit @@ -79,6 +83,7 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), extra_packages = extra_packages, envname = envname, conda = conda, + conda_python_version = conda_python_version, ... ) } else if (method == "virtualenv" || method == "auto") { @@ -102,6 +107,7 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), extra_packages = extra_packages, envname = envname, conda = conda, + conda_python_version = conda_python_version, ... ) @@ -120,7 +126,7 @@ install_tensorflow <- function(method = c("auto", "virtualenv", "conda"), invisible(NULL) } -install_conda <- function(package, extra_packages, envname, conda, ...) { +install_conda <- function(package, extra_packages, envname, conda, conda_python_version, ...) { # find if environment exists envname_exists <- envname %in% reticulate::conda_list(conda = conda)$name @@ -135,7 +141,7 @@ install_conda <- function(package, extra_packages, envname, conda, ...) { cat("Creating ", envname, " conda environment... \n") reticulate::conda_create( envname = envname, conda = conda, - packages = c("python=3.6") + packages = paste0("python=", conda_python_version) ) cat("Installing python modules...\n") diff --git a/man/install_tensorflow.Rd b/man/install_tensorflow.Rd index 03c0b483..5d73e794 100644 --- a/man/install_tensorflow.Rd +++ b/man/install_tensorflow.Rd @@ -6,7 +6,8 @@ \usage{ install_tensorflow(method = c("auto", "virtualenv", "conda"), conda = "auto", version = "default", envname = "r-tensorflow", - extra_packages = NULL, restart_session = TRUE, ...) + extra_packages = NULL, restart_session = TRUE, + conda_python_version = "3.6", ...) } \arguments{ \item{method}{Installation method. By default, "auto" automatically finds a @@ -37,6 +38,9 @@ TensorFlow.} \item{restart_session}{Restart R session after installing (note this will only occur within RStudio).} +\item{conda_python_version}{the python version installed in the created conda +environment. Python 3.6 is installed by default.} + \item{...}{other arguments passed to \code{\link[reticulate:conda_install]{reticulate::conda_install()}} or \code{\link[reticulate:virtualenv_install]{reticulate::virtualenv_install()}}.} }