[rllib] Set framework to tf by default and remove import checks; "Auto" option (#8748)

* tf by default

* Update rllib/agents/trainer.py

Co-authored-by: Sven Mika <sven@anyscale.io>

* remove it

* fix

* remove

* fix

* lint

Co-authored-by: Sven Mika <sven@anyscale.io>
This commit is contained in:
Eric Liang
2020-06-08 23:04:50 -07:00
committed by GitHub
co-authored by Sven Mika
parent f93bb008bb
commit 831b2fe51d
7 changed files with 11 additions and 106 deletions
-76
View File
@@ -3,8 +3,6 @@ import os
import sys
from typing import Any, Union
from ray.util import log_once
logger = logging.getLogger(__name__)
# Represents a generic tensor type.
@@ -14,80 +12,6 @@ TensorType = Any
TensorStructType = Union[TensorType, dict, tuple]
def get_auto_framework():
"""Returns the framework (str) when framework="auto" in the config.
If only PyTorch is installed, returns "torch", if only tf is installed,
returns "tf", if both are installed, raises an error.
"""
# PyTorch is installed.
if torch is not None:
# TF is not installed -> return torch.
if tf is None:
if log_once("get_auto_framework"):
logger.info(
"`framework=auto` found in config -> Detected PyTorch.")
return "torch"
# TF is also installed -> raise error.
else:
raise ValueError(
"framework='auto' (default value) is not allowed if both "
"TensorFlow AND PyTorch are installed! "
"Instead, use framework='tf|tfe|torch' explicitly.")
# PyTorch nor TF installed -> raise error.
if not tf:
raise ValueError(
"Neither TensorFlow nor PyTorch are installed! You must install "
"one of them by running either `pip install tensorflow` OR "
"`pip install torch torchvision`")
# Only TensorFlow installed -> return tf.
if log_once("get_auto_framework"):
logger.info("`framework=auto` found in config -> Detected TensorFlow.")
return "tf"
def check_framework(framework, allow_none=True):
"""Checks, whether the given framework is "valid".
Meaning, whether all necessary dependencies are installed.
Args:
framework (str): Once of "tf", "torch", or None.
allow_none (bool): Whether framework=None (e.g. numpy implementatiopn)
is allowed or not.
Returns:
str: The input framework string.
Raises:
ImportError: If given framework is not installed.
"""
# Resolve auto framework first.
if framework == "auto":
framework = get_auto_framework()
# Check, whether tf is installed.
if framework in ["tf", "tfe"]:
if tf is None:
raise ImportError(
"Could not import `tensorflow`. Try `pip install tensorflow`")
# Check, whether torch is installed.
elif framework == "torch":
if torch is None:
raise ImportError("Could not import `torch`. "
"Try `pip install torch torchvision`")
# Framework is None (use numpy version of the component).
elif framework is None:
if not allow_none:
raise ValueError("framework=None not allowed!")
# Invalid value.
else:
raise ValueError("Invalid framework='{}'. Use one of "
"[tf|tfe|torch|auto].".format(framework))
return framework
def try_import_tf(error=False):
"""Tries importing tf and returns the module (or None).