Skip to content

Commit e22dc72

Browse files
authored
Refactor to inference device endpoint (#5093)
1 parent f5940f8 commit e22dc72

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

application/backend/app/api/routers/system.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
router = APIRouter(prefix="/api")
1515

1616

17-
@router.get("/system/devices")
18-
async def get_devices(
17+
@router.get("/system/devices/inference")
18+
async def get_inference_devices(
1919
system_service: Annotated[SystemService, Depends(get_system_service)],
2020
) -> list[DeviceInfo]:
21-
"""Returns the list of available compute devices (CPU, Intel XPU, NVIDIA CUDA)."""
22-
return system_service.get_devices()
21+
"""Returns the list of available compute devices (CPU, Intel XPU)."""
22+
return system_service.get_inference_devices()
2323

2424

2525
@router.get("/system/metrics/memory")

application/backend/app/services/system_service.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ def get_devices() -> list[DeviceInfo]:
7575

7676
return devices
7777

78+
def get_inference_devices(self) -> list[DeviceInfo]:
79+
"""
80+
Get available compute devices for inference (CPU, XPU, ...)
81+
82+
Returns:
83+
list[DeviceInfo]: List of available devices
84+
"""
85+
return [device for device in self.get_devices() if device.type != DeviceType.CUDA]
86+
7887
def validate_device(self, device_str: str) -> bool:
7988
"""
8089
Validate if a device string is available on the system.

application/backend/tests/unit/routers/test_system.py

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def fxt_system_service() -> Mock:
2626

2727

2828
class TestSystemEndpoints:
29-
def test_get_devices_cpu_only(self, fxt_system_service: Mock, fxt_client: TestClient):
30-
"""Test GET /api/system/devices with CPU only"""
31-
fxt_system_service.get_devices.return_value = [
29+
def test_get_inference_devices_cpu_only(self, fxt_system_service: Mock, fxt_client: TestClient):
30+
"""Test GET /api/system/devices/inference with CPU only"""
31+
fxt_system_service.get_inference_devices.return_value = [
3232
DeviceInfo(type=DeviceType.CPU, name="CPU", memory=None, index=None),
3333
]
3434

35-
response = fxt_client.get("/api/system/devices")
35+
response = fxt_client.get("/api/system/devices/inference")
3636

3737
assert response.status_code == status.HTTP_200_OK
3838
devices = response.json()
@@ -42,14 +42,14 @@ def test_get_devices_cpu_only(self, fxt_system_service: Mock, fxt_client: TestCl
4242
assert devices[0]["memory"] is None
4343
assert devices[0]["index"] is None
4444

45-
def test_get_devices_with_xpu(self, fxt_system_service: Mock, fxt_client: TestClient):
46-
"""Test GET /api/system/devices with Intel XPU"""
47-
fxt_system_service.get_devices.return_value = [
45+
def test_get_inference_devices_with_xpu(self, fxt_system_service: Mock, fxt_client: TestClient):
46+
"""Test GET /api/system/devices/inference with Intel XPU"""
47+
fxt_system_service.get_inference_devices.return_value = [
4848
DeviceInfo(type=DeviceType.CPU, name="CPU", memory=None, index=None),
4949
DeviceInfo(type=DeviceType.XPU, name="Intel(R) Graphics [0x7d41]", memory=36022263808, index=0),
5050
]
5151

52-
response = fxt_client.get("/api/system/devices")
52+
response = fxt_client.get("/api/system/devices/inference")
5353

5454
assert response.status_code == status.HTTP_200_OK
5555
devices = response.json()
@@ -60,41 +60,6 @@ def test_get_devices_with_xpu(self, fxt_system_service: Mock, fxt_client: TestCl
6060
assert devices[1]["memory"] == 36022263808
6161
assert devices[1]["index"] == 0
6262

63-
def test_get_devices_with_cuda(self, fxt_system_service: Mock, fxt_client: TestClient):
64-
"""Test GET /api/system/devices with NVIDIA CUDA"""
65-
fxt_system_service.get_devices.return_value = [
66-
DeviceInfo(type=DeviceType.CPU, name="CPU", memory=None, index=None),
67-
DeviceInfo(type=DeviceType.CUDA, name="NVIDIA GeForce RTX 4090", memory=25769803776, index=0),
68-
]
69-
70-
response = fxt_client.get("/api/system/devices")
71-
72-
assert response.status_code == status.HTTP_200_OK
73-
devices = response.json()
74-
assert len(devices) == 2
75-
assert devices[0]["type"] == "cpu"
76-
assert devices[1]["type"] == "cuda"
77-
assert devices[1]["name"] == "NVIDIA GeForce RTX 4090"
78-
assert devices[1]["memory"] == 25769803776
79-
assert devices[1]["index"] == 0
80-
81-
def test_get_devices_with_all_devices(self, fxt_system_service: Mock, fxt_client: TestClient):
82-
"""Test GET /api/system/devices with all device types"""
83-
fxt_system_service.get_devices.return_value = [
84-
DeviceInfo(type=DeviceType.CPU, name="CPU", memory=None, index=None),
85-
DeviceInfo(type=DeviceType.XPU, name="Intel(R) Graphics [0x7d41]", memory=36022263808, index=0),
86-
DeviceInfo(type=DeviceType.CUDA, name="NVIDIA GeForce RTX 4090", memory=25769803776, index=0),
87-
]
88-
89-
response = fxt_client.get("/api/system/devices")
90-
91-
assert response.status_code == status.HTTP_200_OK
92-
devices = response.json()
93-
assert len(devices) == 3
94-
assert devices[0]["type"] == "cpu"
95-
assert devices[1]["type"] == "xpu"
96-
assert devices[2]["type"] == "cuda"
97-
9863
def test_get_memory(self, fxt_system_service: Mock, fxt_client: TestClient):
9964
"""Test GET /api/system/metrics/memory"""
10065
fxt_system_service.get_memory_usage.return_value = (1024.5, 8192.0)

application/backend/tests/unit/services/test_system_service.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,32 @@ def test_validate_device_cuda_not_available(self, fxt_system_service: SystemServ
169169
assert fxt_system_service.validate_device("cuda") is False
170170
assert fxt_system_service.validate_device("cuda-0") is False
171171

172+
def test_get_inference_devices_with_multiple_devices(self, fxt_system_service: SystemService):
173+
"""Test getting inference devices when multiple GPUs are available"""
174+
with patch("app.services.system_service.torch") as mock_torch:
175+
# Mock XPU device
176+
mock_xpu_dp = MagicMock()
177+
mock_xpu_dp.name = "Intel(R) Graphics [0x7d41]"
178+
mock_xpu_dp.total_memory = 36022263808
179+
180+
mock_torch.xpu.is_available.return_value = True
181+
mock_torch.xpu.device_count.return_value = 1
182+
mock_torch.xpu.get_device_properties.return_value = mock_xpu_dp
183+
184+
# Mock CUDA device
185+
mock_cuda_dp = MagicMock()
186+
mock_cuda_dp.name = "NVIDIA GeForce RTX 4090"
187+
mock_cuda_dp.total_memory = 25769803776
188+
189+
mock_torch.cuda.is_available.return_value = True
190+
mock_torch.cuda.device_count.return_value = 1
191+
mock_torch.cuda.get_device_properties.return_value = mock_cuda_dp
192+
193+
inference_devices = fxt_system_service.get_inference_devices()
194+
195+
assert len(inference_devices) == 2
196+
assert not any(device.type == "cuda" for device in inference_devices)
197+
172198
def test_validate_device_invalid_type(self, fxt_system_service: SystemService):
173199
"""Test validating invalid device types"""
174200
with patch("app.services.system_service.torch") as mock_torch, pytest.raises(ValueError):

0 commit comments

Comments
 (0)