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
17 changes: 15 additions & 2 deletions .github/workflows/perception-images.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Builds the perception images (CPU and GPU), runs their deploy tests, and on main
# pushes them to ghcr.io/we-autopilot/ap1/perception. See docker/README.md.
# Builds the perception images (CPU and GPU), runs their unit and deploy tests,
# and on main pushes them to ghcr.io/we-autopilot/ap1/perception. See docker/README.md.
name: Perception images

on:
Expand Down Expand Up @@ -80,6 +80,19 @@ jobs:
targets: perception-${{ matrix.variant }}
load: true

# Unit tests aren't installed in the image (setup.py ships only test/deploy),
# so they're mounted from the checkout. Read-only mount, hence no pytest cache.
# The launch_testing plugins are off because launch_testing 3.4.13's pytest
# hook disables --ignore (ros2/launch#1047); drop the two -p flags once the
# image has a newer launch_testing. The unit tests don't use these plugins.
- name: Unit tests
run: |
docker run --rm \
-v "$PWD/src/perception/test:/opt/ap1/unit-tests:ro" \
"$IMAGE:${{ matrix.variant }}" \
pytest -p no:cacheprovider -p no:launch_testing -p no:launch_ros \
/opt/ap1/unit-tests --ignore=/opt/ap1/unit-tests/deploy

# Hosted runners have no GPU: the GPU image runs inference on CPU here, but
# the tests still check its onnxruntime has CUDA support.
- name: Deploy tests
Expand Down
17 changes: 16 additions & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ the exact image that runs on the car:
ROS, and checks the `Detection2DArray` on `ap1/perception/objects_2d`
(class, frame, stamp, pixel box; empty when nothing is detected). The photo is downloaded to a temporary folder and deleted once
loaded, so this test needs internet.
- `test_launch_defaults.py`: the image supports the `device` each launch file
defaults to (e.g. TensorRT is installed if a launch file defaults to
`tensorrt`).

The other tests in `test/` are unit tests that only run in dev containers.
The other tests in `test/` are unit tests. They aren't installed in the image;
CI mounts them from the checkout and runs them in both deploy images.

The tests launch their own nodes in a ROS domain no other test run uses, so
they never talk to running containers or to a teammate's nodes. They run on
Expand All @@ -177,6 +181,17 @@ Against a deploy image, through the deploy service so the model is mounted:
docker compose run --rm perception-cpu pytest /opt/ap1/share/ap1_perception/test/deploy
```

The unit tests against a deploy image, as CI runs them (from the repo root):

```bash
docker compose run --rm -v "$PWD/src/perception/test:/opt/ap1/unit-tests:ro" perception-cpu \
pytest -p no:cacheprovider -p no:launch_testing -p no:launch_ros \
/opt/ap1/unit-tests --ignore=/opt/ap1/unit-tests/deploy
```

The `-p no:launch_*` flags work around launch_testing 3.4.13, whose pytest
plugin disables `--ignore` ([ros2/launch#1047](https://github.com/ros2/launch/issues/1047)).

Use `docker compose run`, not `exec`: `exec` skips the image's entrypoint, so ROS
isn't sourced. The same commands work in the all-modules containers
(`all-dev-cpu` and `all-dev-gpu`, `all-perception`).
Expand Down
126 changes: 126 additions & 0 deletions src/perception/test/deploy/test_launch_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""Checks that the image can run the device each launch file defaults to.

The launch files pick their default `device` from AP1_GPU. A default the image
can't run makes the node fail at startup, and the other deploy tests never
notice: they choose the device themselves with pick_device().

Needs no camera data, model or GPU.
"""
import ctypes.util
from collections.abc import Callable
from pathlib import Path
from typing import NamedTuple

import onnxruntime
import pytest
from ament_index_python.packages import get_package_share_directory
from launch import LaunchContext
from launch.launch_description_sources import get_launch_description_from_python_launch_file
from launch.utilities import perform_substitutions

CPU_PROVIDER = 'CPUExecutionProvider'
CUDA_PROVIDER = 'CUDAExecutionProvider'
TRT_PROVIDER = 'TensorrtExecutionProvider'


class Requirement(NamedTuple):
providers: list[str] # onnxruntime execution providers the device needs
libraries: list[str] # shared libraries, as passed to ctypes.util.find_library


# onnxruntime-gpu lists the TensorRT provider even when TensorRT isn't installed,
# so TensorRT's own library is checked too
DEVICE_REQUIREMENTS = {
'auto': Requirement([], []), # falls back to CPU
'cpu': Requirement([CPU_PROVIDER], []),
'cuda': Requirement([CUDA_PROVIDER], []),
'tensorrt': Requirement([TRT_PROVIDER], ['nvinfer']),
}


def device_problems(device: str, available: list[str],
find_library: Callable[[str], str | None]) -> list[str]:
"""Why an image can't run `device`; an empty list means it can.

`available` is onnxruntime.get_available_providers() and `find_library` is
ctypes.util.find_library, passed in so the rules can be tested without them.
Every unmet requirement is listed, not just the first.
"""
if device not in DEVICE_REQUIREMENTS:
return [f"unknown device '{device}', expected one of {sorted(DEVICE_REQUIREMENTS)}"]
requirement = DEVICE_REQUIREMENTS[device]
problems = [f'onnxruntime has no {p} (available: {available})'
for p in requirement.providers if p not in available]
problems += [f"library '{lib}' not found"
for lib in requirement.libraries if find_library(lib) is None]
return problems


def launch_default_devices() -> dict[str, str]:
"""Default of the `device` argument of every installed launch file that declares one."""
launch_dir = Path(get_package_share_directory('ap1_perception')) / 'launch'
defaults = {}
for path in sorted(launch_dir.glob('*.launch.py')):
description = get_launch_description_from_python_launch_file(str(path))
for arg in description.get_launch_arguments():
if arg.name == 'device':
default = perform_substitutions(LaunchContext(), arg.default_value or [])
defaults[path.name] = default
return defaults


def test_image_runs_launch_default_devices():
defaults = launch_default_devices()
assert defaults, 'no installed launch file declares a device argument'
available = onnxruntime.get_available_providers()
problems = [f'{name}: device={device!r}: {problem}'
for name, device in defaults.items()
for problem in device_problems(device, available, ctypes.util.find_library)]
assert not problems, '\n'.join(problems)


def no_libraries(_name):
return None


def all_libraries(name):
return f'lib{name}.so'


@pytest.mark.parametrize('device, available, find_library', [
('auto', [CPU_PROVIDER], no_libraries),
('cpu', [CPU_PROVIDER], no_libraries),
('cuda', [CUDA_PROVIDER, CPU_PROVIDER], no_libraries),
('tensorrt', [TRT_PROVIDER, CUDA_PROVIDER, CPU_PROVIDER], all_libraries),
])
def test_supported_device_has_no_problems(device, available, find_library):
assert device_problems(device, available, find_library) == []


def test_cuda_without_cuda_provider():
problems = device_problems('cuda', [CPU_PROVIDER], all_libraries)
assert len(problems) == 1
assert CUDA_PROVIDER in problems[0]


def test_cpu_without_cpu_provider():
assert len(device_problems('cpu', [], all_libraries)) == 1


def test_tensorrt_listed_but_not_installed():
# The GPU image today: onnxruntime-gpu lists the provider, TensorRT is missing
available = [TRT_PROVIDER, CUDA_PROVIDER, CPU_PROVIDER]
problems = device_problems('tensorrt', available, no_libraries)
assert len(problems) == 1
assert 'nvinfer' in problems[0]


def test_every_unmet_requirement_is_listed():
problems = device_problems('tensorrt', [CPU_PROVIDER], no_libraries)
assert len(problems) == 2


def test_unknown_device():
problems = device_problems('tpu', [CPU_PROVIDER], all_libraries)
assert len(problems) == 1
assert 'tpu' in problems[0]