mirror of
https://github.com/wassname/ray.git
synced 2026-08-11 11:24:51 +08:00
[rllib] Add test case that we don't have a hard torch dependency (#7926)
This commit is contained in:
@@ -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_
|
||||
|
||||
Reference in New Issue
Block a user