Skip to content

Commit

Permalink
UPDATE 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
davidycliao committed Nov 3, 2023
1 parent 5000e59 commit eef1f28
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 94 deletions.
63 changes: 16 additions & 47 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,59 +88,28 @@ show_flair_cache <- function() {
}
}

#'
#' #' @title Create or Use Python environment for Flair
#' #'
#' #' @description
#' #' This function checks whether the Flair Python library is installed in the
#' #' current Python environment. If it is not, it attempts to install it either
#' #' in the current conda environment or creates a new one.
#' #'
#' #' @param env The name of the conda environment to be used or
#' #' created (default is "r-reticulate").
#' #'
#' #' @return Nothing is returned. The function primarily ensures that the Python
#' #' library Flair is installed and available.
#' #' @export
#' #' @importFrom reticulate import py_config use_condaenv
#' #' @importFrom rstudioapi restartSession
#' create_flair_env <- function(env = "r-reticulate") {
#' # check if flair is already installed in the current Python environment
#' if (reticulate::py_module_available("flair")) {
#' message("Environment creation stopped.", "\n", "Flair is already installed in ", reticulate::py_config()$python)
#' message(sprintf("Using Flair: %-48s", reticulate::import("flair")$`__version__`))
#' return(invisible(NULL))
#' }
#' paths <- reticulate::conda_list()
#' env_path <- paths[grep("envs/", paths$python), "python"][1]
#' if (grepl("envs/", env_path)) {
#' message("you already created:", length(paths[grep("envs/", paths$python), "python"]))
#' message("you can run use_condaenv(",as.character(env_path),") to activate the enviroment in your R." )
#' reticulate::use_condaenv(env)
#' } else {
#' # No conda environment found or active, so create one
#' reticulate::conda_create(env)
#' message("No conda environment found. Creating a new environment named '", env, "'. ", "After restarting the R session, please run create_flair_env() again.")
#' rstudioapi::restartSession()
#' }
#' }
#'

#' @title Check the Device for Accelerating PyTorch
#'
#' @description This function verifies if the specified device is available for PyTorch.
#' If CUDA is not available, a message is shown. Additionally, if the system
#' is running on a Mac M1, MPS will be used instead of CUDA.
#' is running on a Mac M1, MPS will be used instead of CUDA. Checks if the specified device is compatible with the current system's
#' hardware and operating system configuration, particularly for Mac systems
#' with Apple M1/M2 silicon using Metal Performance Shaders (MPS).
#'
#' @details
#' If MPS is available and the system meets the requirements, a device of type
#' MPS will be returned. Otherwise, a CPU device will be used. The requirements
#' for using MPS are as follows:\\cr
#' - Mac computers with Apple silicon or AMD GPUs\\cr
#' - macOS 12.3 or later\\cr
#' - Python 3.7 or later\\cr
#' - Xcode command-line tools installed (`xcode-select --install`)\\cr
#' More information at: \url{https://developer.apple.com/metal/pytorch/}.
#'
#' @param device A character string specifying the device type.
#'
#' @note Flair NLP operates under the [PyTorch](https://pytorch.org) framework.
#' As such, we can use the `$to` method to set the device for the Flair Python
#' library. `flair_device("cpu")` allows you to select whether to use the CPU,
#' CUDA devices (like cuda:0, cuda:1, cuda:2), or specific MPS devices on Mac
#' (such as mps:0, mps:1, mps:2). For information on Accelerated PyTorch
#' training on Mac, please refer to https://developer.apple.com/metal/pytorch/.
#' For more about CUDA, please visit: https://developer.nvidia.com/cuda-zone.
#' @return A PyTorch device object.
#'
#' @param device Character. The device to be set for PyTorch.
#' @importFrom reticulate import
#' @keywords internal
check_device <- function(device) {
Expand Down
1 change: 0 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ reference:
- map_entities
- embeddings_to_matrix
- clear_flair_cache
- create_flair_env
- show_flair_cache


Expand Down
61 changes: 15 additions & 46 deletions man/check_device.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eef1f28

Please sign in to comment.