|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2025-present WATonomous. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +Launch test for deep_sample with TensorRT backend. |
| 18 | +
|
| 19 | +This test should ONLY be run locally on a machine with a GPU and TensorRT. |
| 20 | +It will be skipped in CI environments. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import time |
| 25 | +import unittest |
| 26 | + |
| 27 | +import launch |
| 28 | +import launch_ros.actions |
| 29 | +import launch_testing |
| 30 | +import launch_testing.actions |
| 31 | +import launch_testing.asserts |
| 32 | +import pytest |
| 33 | +import rclpy |
| 34 | +from sensor_msgs.msg import Image |
| 35 | +from std_msgs.msg import Float32MultiArray |
| 36 | +import numpy as np |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.launch_test |
| 40 | +def generate_test_description(): |
| 41 | + """Generate launch description for TensorRT backend test.""" |
| 42 | + from ament_index_python.packages import get_package_share_directory |
| 43 | + |
| 44 | + # Path to TensorRT config file |
| 45 | + config_file = os.path.join( |
| 46 | + get_package_share_directory("deep_sample"), |
| 47 | + "config", |
| 48 | + "sample_node_tensorrt_config.yaml", |
| 49 | + ) |
| 50 | + |
| 51 | + # Sample inference node with TensorRT backend |
| 52 | + sample_node = launch_ros.actions.Node( |
| 53 | + package="deep_sample", |
| 54 | + executable="sample_inference_node", |
| 55 | + name="sample_inference_node", |
| 56 | + parameters=[config_file], |
| 57 | + output="screen", |
| 58 | + ) |
| 59 | + |
| 60 | + # Lifecycle manager |
| 61 | + lifecycle_manager = launch_ros.actions.Node( |
| 62 | + package="nav2_lifecycle_manager", |
| 63 | + executable="lifecycle_manager", |
| 64 | + name="lifecycle_manager", |
| 65 | + parameters=[{"node_names": ["sample_inference_node"], "autostart": True}], |
| 66 | + output="screen", |
| 67 | + ) |
| 68 | + |
| 69 | + return ( |
| 70 | + launch.LaunchDescription( |
| 71 | + [sample_node, lifecycle_manager, launch_testing.actions.ReadyToTest()] |
| 72 | + ), |
| 73 | + { |
| 74 | + "sample_node": sample_node, |
| 75 | + "lifecycle_manager": lifecycle_manager, |
| 76 | + }, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +class TestTensorRTBackend(unittest.TestCase): |
| 81 | + """Test TensorRT backend functionality.""" |
| 82 | + |
| 83 | + @classmethod |
| 84 | + def setUpClass(cls): |
| 85 | + """Initialize ROS context.""" |
| 86 | + rclpy.init() |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def tearDownClass(cls): |
| 90 | + """Shutdown ROS context.""" |
| 91 | + rclpy.shutdown() |
| 92 | + |
| 93 | + def setUp(self): |
| 94 | + """Set up test fixtures.""" |
| 95 | + self.node = rclpy.create_node("test_tensorrt_backend") |
| 96 | + |
| 97 | + def tearDown(self): |
| 98 | + """Clean up test fixtures.""" |
| 99 | + self.node.destroy_node() |
| 100 | + |
| 101 | + def test_node_starts(self, proc_output): |
| 102 | + """Test that the sample node starts successfully.""" |
| 103 | + proc_output.assertWaitFor("SampleInferenceNode constructor", timeout=10) |
| 104 | + |
| 105 | + def test_backend_loads(self, proc_output): |
| 106 | + """Test that GPU backend plugin loads.""" |
| 107 | + proc_output.assertWaitFor("Loading plugin: onnxruntime_gpu", timeout=10) |
| 108 | + proc_output.assertWaitFor( |
| 109 | + "Successfully loaded plugin: onnxruntime_gpu", timeout=10 |
| 110 | + ) |
| 111 | + |
| 112 | + def test_tensorrt_provider_configured(self, proc_output): |
| 113 | + """Test that TensorRT execution provider is configured.""" |
| 114 | + proc_output.assertWaitFor( |
| 115 | + "Configuring TensorRT execution provider on device 0", timeout=10 |
| 116 | + ) |
| 117 | + proc_output.assertWaitFor( |
| 118 | + "TensorRT provider registered successfully", timeout=10 |
| 119 | + ) |
| 120 | + |
| 121 | + def test_model_loads(self, proc_output): |
| 122 | + """Test that the model loads successfully with TensorRT backend.""" |
| 123 | + proc_output.assertWaitFor("Loading model:", timeout=15) |
| 124 | + proc_output.assertWaitFor("Successfully loaded model:", timeout=15) |
| 125 | + |
| 126 | + def test_node_activates(self, proc_output): |
| 127 | + """Test that the node activates successfully with TensorRT backend.""" |
| 128 | + proc_output.assertWaitFor( |
| 129 | + "SampleInferenceNode activated with backend: onnxruntime_gpu", timeout=20 |
| 130 | + ) |
| 131 | + |
| 132 | + def test_no_tensorrt_errors(self, proc_output): |
| 133 | + """Test that there are no TensorRT-related errors.""" |
| 134 | + # This will fail if any TensorRT errors appear in the output |
| 135 | + time.sleep(2) # Give time for any errors to appear |
| 136 | + # If we get here without exceptions from previous assertions, no TensorRT errors occurred |
| 137 | + |
| 138 | + def test_tensorrt_inference_with_dummy_image(self, proc_output): |
| 139 | + """Test end-to-end TensorRT inference by publishing a dummy image and verifying output.""" |
| 140 | + # Wait for node to be fully activated |
| 141 | + proc_output.assertWaitFor( |
| 142 | + "SampleInferenceNode activated with backend: onnxruntime_gpu", timeout=20 |
| 143 | + ) |
| 144 | + time.sleep(1) |
| 145 | + |
| 146 | + # Create publisher for dummy images |
| 147 | + image_pub = self.node.create_publisher(Image, "/camera/image_raw", 10) |
| 148 | + |
| 149 | + # Variable to track if we received output |
| 150 | + received_output = [] |
| 151 | + |
| 152 | + def output_callback(msg): |
| 153 | + received_output.append(msg) |
| 154 | + self.node.get_logger().info( |
| 155 | + f"Received TensorRT inference output with {len(msg.data)} elements" |
| 156 | + ) |
| 157 | + |
| 158 | + # Create subscriber for inference output |
| 159 | + self.output_sub = self.node.create_subscription( |
| 160 | + Float32MultiArray, "/inference/output", output_callback, 10 |
| 161 | + ) |
| 162 | + |
| 163 | + # Wait for publisher/subscriber to be ready |
| 164 | + time.sleep(1) |
| 165 | + |
| 166 | + # Create a dummy 32x32 RGB image with float32 data (tiny_model expects 32x32) |
| 167 | + dummy_image = Image() |
| 168 | + dummy_image.header.stamp = self.node.get_clock().now().to_msg() |
| 169 | + dummy_image.header.frame_id = "camera" |
| 170 | + dummy_image.height = 32 |
| 171 | + dummy_image.width = 32 |
| 172 | + dummy_image.encoding = "32FC3" # float32, 3 channels |
| 173 | + dummy_image.is_bigendian = 0 |
| 174 | + dummy_image.step = 32 * 3 * 4 # width * channels * bytes_per_channel |
| 175 | + dummy_image.data = np.random.rand(32, 32, 3).astype(np.float32).tobytes() |
| 176 | + |
| 177 | + # Publish dummy image |
| 178 | + self.node.get_logger().info( |
| 179 | + "Publishing dummy image for TensorRT inference test" |
| 180 | + ) |
| 181 | + image_pub.publish(dummy_image) |
| 182 | + |
| 183 | + # Spin to process callbacks |
| 184 | + start_time = time.time() |
| 185 | + timeout = 5.0 |
| 186 | + while len(received_output) == 0 and (time.time() - start_time) < timeout: |
| 187 | + rclpy.spin_once(self.node, timeout_sec=0.1) |
| 188 | + |
| 189 | + # Verify we received output |
| 190 | + self.assertGreater( |
| 191 | + len(received_output), |
| 192 | + 0, |
| 193 | + "Should receive inference output after publishing image", |
| 194 | + ) |
| 195 | + self.assertGreater( |
| 196 | + len(received_output[0].data), 0, "Inference output should contain data" |
| 197 | + ) |
| 198 | + self.node.get_logger().info( |
| 199 | + f"TensorRT inference test passed! Received {len(received_output[0].data)} output values" |
| 200 | + ) |
0 commit comments