mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-30 13:09:40 +00:00
e36783253a
* fix(utils): raise ValueError from get_safe_torch_device Bare asserts vanish under python -O and look like programmer bugs. Convert unavailable CUDA/MPS/XPU requests into clear ValueErrors. * style: combine nested with in device util tests (ruff) --------- Co-authored-by: Steven Palma <imstevenpmwork@ieee.org>
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
#!/usr/bin/env python
|
|
# Copyright 2026 The HuggingFace Inc. team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from lerobot.utils.device_utils import get_safe_torch_device, is_torch_device_available
|
|
|
|
|
|
def test_cpu_always_available():
|
|
assert get_safe_torch_device("cpu") == torch.device("cpu")
|
|
assert is_torch_device_available("cpu")
|
|
|
|
|
|
def test_missing_cuda_raises_valueerror():
|
|
with patch("torch.cuda.is_available", return_value=False), pytest.raises(ValueError, match="CUDA"):
|
|
get_safe_torch_device("cuda")
|
|
|
|
|
|
def test_missing_mps_raises_valueerror():
|
|
with patch("torch.backends.mps.is_available", return_value=False), pytest.raises(ValueError, match="MPS"):
|
|
get_safe_torch_device("mps")
|