Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions brainchip_utils/hardware_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def get_akida_device(target_version=None):
print(str(len(devices)) + ' Akida devices found. Using the first device detected.')
return devices[0]
else:
for dd in akida.devices():
for dd in devices:
if dd.ip_version == target_version:
print('Target Akida device found')
return dd
print('Connected Akida Device does not match the requested IPVersion.')
print('Calls to akida will run on the software backend.')
return None
print('Connected Akida Device does not match the requested IPVersion.')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the fix, but we could improve error message.

available_versions = [dd.ip_version for dd in devices]
print(f'Connected Akida Device does not match the requested IPVersion. '
      f'Requested: {target_version}, available: {available_versions}')

print('Calls to akida will run on the software backend.')
return None

#----------------------------------------------------------------------------------
#
Expand Down
27 changes: 27 additions & 0 deletions test/test_hardware_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Unit tests for brainchip_utils.hardware_utils (no hardware required)."""
import akida

from brainchip_utils.hardware_utils import get_akida_device


class _FakeDevice:
def __init__(self, ip_version):
self.ip_version = ip_version


def test_matching_device_after_the_first_is_found(monkeypatch):
"""A device matching target_version must be found wherever it sits in the list."""
first, second = _FakeDevice(akida.IpVersion.v2), _FakeDevice(akida.IpVersion.v1)
monkeypatch.setattr(akida, "devices", lambda: [first, second])
assert get_akida_device(target_version=akida.IpVersion.v1) is second


def test_returns_none_when_no_device_matches(monkeypatch):
monkeypatch.setattr(akida, "devices", lambda: [_FakeDevice(akida.IpVersion.v2)])
assert get_akida_device(target_version=akida.IpVersion.v1) is None


def test_returns_first_device_when_no_target_requested(monkeypatch):
first = _FakeDevice(akida.IpVersion.v1)
monkeypatch.setattr(akida, "devices", lambda: [first, _FakeDevice(akida.IpVersion.v2)])
assert get_akida_device() is first
Comment on lines +12 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not mandatory but you are able to parametrize the test

Suggested change
def test_matching_device_after_the_first_is_found(monkeypatch):
"""A device matching target_version must be found wherever it sits in the list."""
first, second = _FakeDevice(akida.IpVersion.v2), _FakeDevice(akida.IpVersion.v1)
monkeypatch.setattr(akida, "devices", lambda: [first, second])
assert get_akida_device(target_version=akida.IpVersion.v1) is second
def test_returns_none_when_no_device_matches(monkeypatch):
monkeypatch.setattr(akida, "devices", lambda: [_FakeDevice(akida.IpVersion.v2)])
assert get_akida_device(target_version=akida.IpVersion.v1) is None
def test_returns_first_device_when_no_target_requested(monkeypatch):
first = _FakeDevice(akida.IpVersion.v1)
monkeypatch.setattr(akida, "devices", lambda: [first, _FakeDevice(akida.IpVersion.v2)])
assert get_akida_device() is first
import pytest
_device_ip_v1=_FakeDevice(akida.IpVersion.v1)
_device_ip_v2=_FakeDevice(akida.IpVersion.v2)
@pytest.mark.parametrize("list_devices, target_version, expected",
[([_device_ip_v1, _device_ip_v2], akida.IpVersion.v1, _device_ip_v2), ...])
def test_returns_first_device_when_no_target_requested(monkeypatch, list_devices, target_version, expected):
monkeypatch.setattr(akida, "devices", lambda: list_devices)
assert get_akida_device(target_version=target_version) is expected