GitHub
4 年前
当前提交
457ed0b8
共有 8 个文件被更改,包括 103 次插入 和 14 次删除
-
3com.unity.ml-agents/CHANGELOG.md
-
10docs/Training-ML-Agents.md
-
1ml-agents/mlagents/torch_utils/__init__.py
-
37ml-agents/mlagents/torch_utils/torch.py
-
15ml-agents/mlagents/trainers/cli_utils.py
-
1ml-agents/mlagents/trainers/learn.py
-
9ml-agents/mlagents/trainers/settings.py
-
41ml-agents/mlagents/trainers/tests/test_torch_utils.py
|
|||
from mlagents.torch_utils.torch import torch as torch # noqa |
|||
from mlagents.torch_utils.torch import nn # noqa |
|||
from mlagents.torch_utils.torch import set_torch_config # noqa |
|||
from mlagents.torch_utils.torch import default_device # noqa |
|
|||
import pytest |
|||
from unittest import mock |
|||
|
|||
import torch # noqa I201 |
|||
|
|||
from mlagents.torch_utils import set_torch_config, default_device |
|||
from mlagents.trainers.settings import TorchSettings |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"device_str, expected_type, expected_index, expected_tensor_type", |
|||
[ |
|||
("cpu", "cpu", None, torch.FloatTensor), |
|||
("cuda", "cuda", None, torch.cuda.FloatTensor), |
|||
("cuda:42", "cuda", 42, torch.cuda.FloatTensor), |
|||
("opengl", "opengl", None, torch.FloatTensor), |
|||
], |
|||
) |
|||
@mock.patch.object(torch, "set_default_tensor_type") |
|||
def test_set_torch_device( |
|||
mock_set_default_tensor_type, |
|||
device_str, |
|||
expected_type, |
|||
expected_index, |
|||
expected_tensor_type, |
|||
): |
|||
try: |
|||
torch_settings = TorchSettings(device=device_str) |
|||
set_torch_config(torch_settings) |
|||
assert default_device().type == expected_type |
|||
if expected_index is None: |
|||
assert default_device().index is None |
|||
else: |
|||
assert default_device().index == expected_index |
|||
mock_set_default_tensor_type.assert_called_once_with(expected_tensor_type) |
|||
except Exception: |
|||
raise |
|||
finally: |
|||
# restore the defaults |
|||
torch_settings = TorchSettings(device=None) |
|||
set_torch_config(torch_settings) |
撰写
预览
正在加载...
取消
保存
Reference in new issue