[rllib] Add test case that we don't have a hard torch dependency (#7926)

This commit is contained in:
Eric Liang
2020-04-07 18:07:39 -07:00
committed by GitHub
parent 85481d635d
commit e8c19aba41
4 changed files with 42 additions and 8 deletions
+11 -6
View File
@@ -101,7 +101,10 @@ def try_import_tfp(error=False):
# Fake module for torch.nn.
class NNStub:
pass
def __init__(self, *a, **kw):
# Fake nn.functional module within torch.nn.
self.functional = None
self.Module = ModuleStub
# Fake class for torch.nn.Module to allow it to be inherited from.
@@ -120,7 +123,7 @@ def try_import_torch(error=False):
"""
if "RLLIB_TEST_NO_TORCH_IMPORT" in os.environ:
logger.warning("Not importing Torch for test purposes.")
return None, None
return _torch_stubs()
try:
import torch
@@ -129,10 +132,12 @@ def try_import_torch(error=False):
except ImportError as e:
if error:
raise e
return _torch_stubs()
nn = NNStub()
nn.Module = ModuleStub
return None, nn
def _torch_stubs():
nn = NNStub()
return None, nn
def get_variable(value,
@@ -165,7 +170,7 @@ def get_variable(value,
return tf.compat.v1.get_variable(
tf_name, initializer=value, dtype=dtype, trainable=trainable)
elif framework == "torch" and torch_tensor is True:
import torch
torch, _ = try_import_torch()
var_ = torch.from_numpy(value)
var_.requires_grad = trainable
return var_