-
Notifications
You must be signed in to change notification settings - Fork 120
yolo26 example #4639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alexsu52
wants to merge
1
commit into
ROCm:develop
Choose a base branch
from
alexsu52:as/example_yolo26
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+666
−1
Draft
yolo26 example #4639
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| # YOLO Model Benchmarking with MIGraphX | ||
|
|
||
| Automated pipeline for exporting, compiling, validating, and benchmarking YOLO models from Ultralytics using AMD's MIGraphX inference runtime on ROCm-enabled GPUs. | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| - [Overview](#overview) | ||
| - [Features](#features) | ||
| - [Installation](#installation) | ||
| - [Usage](#usage) | ||
| - [Command Line Arguments](#command-line-arguments) | ||
| - [Standalone Model Benchmarking](#standalone-model-benchmarking) | ||
|
|
||
| ## Overview | ||
|
|
||
| This example provides an end-to-end pipeline for preparing YOLO models for optimized inference on AMD GPUs. It automates the entire process from PyTorch model export to final performance benchmarking, including FP16 quantization and accuracy validation. | ||
|
|
||
| ## Features | ||
|
|
||
| - **ONNX Export**: Export YOLO PyTorch models from Ultralytics to ONNX. | ||
| - **FP16 Quantization**: Applies MIGraphX FP16 quantization for faster inference. | ||
| - **GPU Compilation**: Compiles models for AMD GPU targets. | ||
| - **Accuracy Validation**: Validates compiled models on COCO128 dataset with mAP metrics. | ||
| - **Performance Benchmarking**: Measures inference throughput (FPS) using migraphx-driver. | ||
| - **Flexible Configuration**: Supports custom batch sizes and image dimensions. | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Requirements | ||
|
|
||
| - **Python Version**: Python 3.10 or lower (MIGraphX currently supports up to Python 3.10) | ||
| - **ROCm**: 7.1 or later | ||
|
|
||
| ### Option 1: Using Docker (Recommended) | ||
|
|
||
| The easiest way to get started is using the official ROCm PyTorch Docker image: | ||
|
|
||
| ```bash | ||
| docker run -it \ | ||
| --device=/dev/kfd \ | ||
| --device=/dev/dri \ | ||
| --network host \ | ||
| --ipc host \ | ||
| --group-add video \ | ||
| --cap-add=SYS_PTRACE \ | ||
| --security-opt seccomp=unconfined \ | ||
| -v $(pwd):/workspace \ | ||
| --shm-size 16G \ | ||
| rocm/pytorch:rocm7.1_ubuntu22.04_py3.10_pytorch_release_2.9.1 | ||
| ``` | ||
|
|
||
| Inside the container, MIGraphX should already be available. Verify with: | ||
|
|
||
| ```bash | ||
| export PYTHONPATH=/opt/rocm/lib | ||
| python -c "import migraphx" | ||
| ``` | ||
|
|
||
| Then install the additional dependencies: | ||
|
|
||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| ### Option 2: Manual Installation | ||
|
|
||
| ### 1. Install ROCm 7.1 | ||
|
|
||
| Follow the official AMD ROCm installation guide: | ||
| [ROCm Installation Quick Start Guide](https://rocm.docs.amd.com/projects/install-on-linux/en/latest/install/quick-start.html) | ||
|
|
||
| ### 2. Install PyTorch with ROCm Support | ||
|
|
||
| ```bash | ||
| # Create a virtual environment with Python 3.10 (recommended) | ||
| python3.10 -m venv ~/env | ||
| source ~/env/bin/activate | ||
|
|
||
| # Install PyTorch with ROCm 7.1 support | ||
| pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/rocm7.1 | ||
| ``` | ||
|
|
||
| ### 3. Install MIGraphX | ||
|
|
||
| ```bash | ||
| # MIGraphX should be installed with ROCm | ||
| # Verify installation | ||
| export PYTHONPATH=/opt/rocm/lib | ||
| python -c "import migraphx" | ||
| ``` | ||
|
|
||
| ### 4. Install Additional Dependencies | ||
|
|
||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| > **Note**: Always use `HIP_VISIBLE_DEVICES=0` to restrict execution to a single GPU. MIGraphX models compiled with this tool should run on only one GPU device. | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| ```bash | ||
| # Export and benchmark YOLOv12x with default settings (batch_size=1, imgsz=640x640) | ||
| HIP_VISIBLE_DEVICES=0 python main.py --model-name yolo26x | ||
| ``` | ||
|
|
||
| ### Advanced Examples | ||
|
|
||
| **Large batch for throughput testing:** | ||
| ```bash | ||
| HIP_VISIBLE_DEVICES=0 python main.py --model-name yolo26x --batch-size 32 --imgsz 640 640 | ||
| ``` | ||
|
|
||
| ## Command Line Arguments | ||
|
|
||
| | Argument | Type | Default | Description | | ||
| |----------|------|---------|-------------| | ||
| | `--model-name` | str | `"yolo26x"` | YOLO model name (e.g., yolo26x, yolo26l, yolo26m, yolo26s) | | ||
| | `--batch-size` | int | `1` | Batch size for inference | | ||
| | `--imgsz` | int int | `640 640` | Image dimensions as HEIGHT WIDTH (must be divisible by 32) | | ||
|
|
||
| ## Standalone Model Benchmarking | ||
|
|
||
| Once you have a compiled MIGraphX model (`.mxr` file), you can benchmark it directly using the `migraphx-driver` command-line tool without running Python scripts. | ||
|
|
||
| ### Using migraphx-driver | ||
|
|
||
| The `migraphx-driver` is a powerful command-line utility included with MIGraphX that provides various operations for model testing and benchmarking. | ||
|
|
||
| **Basic benchmarking:** | ||
|
|
||
| ```bash | ||
| migraphx-driver perf model.mxr | ||
| ``` | ||
|
|
||
| This will run performance benchmarks and output detailed statistics including: | ||
| - Inference rate (inferences/sec) | ||
| - Total execution time | ||
| - Per-layer profiling information | ||
| - Memory usage statistics | ||
|
|
||
| **Example output:** | ||
|
|
||
| ``` | ||
| Compiling ... | ||
| Reading: model.mxr | ||
| Summary: | ||
| Rate: 245.678 inferences/sec | ||
| Total time: 4.567s | ||
| Total instructions time: 4.123s | ||
| ``` | ||
|
|
||
| For complete documentation on `migraphx-driver` and its capabilities, refer to: | ||
| - [MIGraphX GitHub Repository](https://github.com/ROCm/AMDMIGraphX) | ||
| - [MIGraphX Driver Documentation](https://github.com/ROCm/AMDMIGraphX/blob/develop/tools/README.md) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import argparse | ||
| import re | ||
| import subprocess | ||
| from pathlib import Path | ||
| from typing import Dict, Tuple | ||
|
|
||
| import onnx | ||
| import torch | ||
| from onnxruntime.tools.onnx_model_utils import make_input_shape_fixed | ||
| from rich.progress import track | ||
| from ultralytics import YOLO | ||
| from ultralytics.data.utils import check_det_dataset | ||
| from ultralytics.engine.validator import BaseValidator as Validator | ||
| from ultralytics.utils.metrics import ConfusionMatrix | ||
| import migraphx | ||
|
|
||
| ROOT = Path(__file__).parent.resolve() | ||
|
|
||
|
|
||
| def prepare_validation( | ||
| model: YOLO, dataset_yaml_path: str, batch_size: int = 1 | ||
| ) -> Tuple[Validator, torch.utils.data.DataLoader]: | ||
| custom = {"rect": False, "batch": batch_size} # method defaults | ||
| args = { | ||
| **model.overrides, | ||
| **custom, | ||
| "mode": "val", | ||
| "workers": 0, | ||
| } # highest priority args on the right | ||
|
|
||
| validator = model._smart_load("validator")(args=args, _callbacks=model.callbacks) | ||
| validator.class_map = list(range(1, len(model.names) + 1)) | ||
| validator.names = model.names | ||
| validator.nc = len(model.names) | ||
| validator.end2end = False | ||
| validator.metrics.names = model.names | ||
| validator.stride = 32 # used in get_dataloader() for padding | ||
| validator.data = check_det_dataset(dataset_yaml_path) | ||
|
|
||
| data_loader = validator.get_dataloader( | ||
| validator.data.get(validator.args.split), validator.args.batch | ||
| ) | ||
|
|
||
| return validator, data_loader | ||
|
|
||
|
|
||
| def validate_model( | ||
| mxr_model: migraphx.program, | ||
| validator: Validator, | ||
| data_loader: torch.utils.data.DataLoader, | ||
| ) -> Dict[str, float]: | ||
| print(f"Start validation...") | ||
| validator.device = torch.device("cuda") | ||
| validator.seen = 0 | ||
| validator.jdict = [] | ||
| validator.confusion_matrix = ConfusionMatrix( | ||
| names=validator.names, save_matches=False | ||
| ) | ||
|
|
||
| input_name = "images" | ||
| input_shape = mxr_model.get_parameter_shapes()[input_name] | ||
| output_name = next(iter(mxr_model.get_parameter_shapes())) | ||
| output_shape = mxr_model.get_parameter_shapes()[output_name] | ||
|
|
||
| curr_stream = torch.cuda.current_stream() | ||
|
|
||
| tensor = torch.empty_strided( | ||
| output_shape.lens(), | ||
| output_shape.strides(), | ||
| dtype=torch.float32, | ||
| device=torch.device("cuda"), | ||
| ) | ||
| mgx_buffers = {} | ||
| mgx_buffers[output_name] = migraphx.argument_from_pointer( | ||
| output_shape, tensor.data_ptr() | ||
| ) | ||
|
|
||
| for batch in track(data_loader, description="Processing items..."): | ||
| batch = validator.preprocess(batch) | ||
|
|
||
| input_argument = migraphx.argument_from_pointer( | ||
| input_shape, batch["img"].data_ptr() | ||
| ) | ||
| mgx_buffers[input_name] = input_argument | ||
| mxr_model.run_async(mgx_buffers, curr_stream.cuda_stream, "ihipStream_t") | ||
|
|
||
| preds = validator.postprocess(tensor) | ||
| validator.update_metrics(preds, batch) | ||
| stats = validator.get_stats() | ||
| return stats | ||
|
|
||
|
|
||
| def run_benchmark(model_path: str, verbose: bool = True) -> float: | ||
| cmd = ["migraphx-driver", "perf", model_path] | ||
| cmd_output = subprocess.check_output(cmd, text=True) # nosec | ||
| if verbose: | ||
| print(*cmd_output.splitlines()[-9:], sep="\n") | ||
| match = re.search(r"Rate\: (.+?) inferences/sec", cmd_output) | ||
| return float(match.group(1)) | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser( | ||
| description="Export and benchmark YOLO models with MIGraphX" | ||
| ) | ||
| parser.add_argument( | ||
| "--model-name", | ||
| type=str, | ||
| default="yolo26x", | ||
| help="Model name (e.g., yolo26x, yolo26l)", | ||
| ) | ||
| parser.add_argument( | ||
| "--batch-size", | ||
| type=int, | ||
| default=1, | ||
| help="Batch size for inference", | ||
| ) | ||
| parser.add_argument( | ||
| "--imgsz", | ||
| type=int, | ||
| nargs=2, | ||
| default=[640, 640], | ||
| metavar=("HEIGHT", "WIDTH"), | ||
| help="Image size as height width (e.g., 640 640)", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main() -> Tuple[float, float]: | ||
| args = parse_args() | ||
|
|
||
| model_name = args.model_name | ||
| batch_size = args.batch_size | ||
| imgsz = args.imgsz | ||
|
|
||
| model_input_id = f"{batch_size}_{'_'.join(str(x) for x in imgsz)}" | ||
| model_path = ROOT / model_input_id | ||
|
|
||
| # export model to ONNX | ||
| model = YOLO(model_path / f"{model_name}.pt") | ||
| onnx_model_path = model.export(format="onnx", dynamic=True, imgsz=imgsz) | ||
| print(f"Model exported to ONNX: {onnx_model_path}") | ||
|
|
||
| onnx_model = onnx.load(onnx_model_path) | ||
|
|
||
| # Assumption: first input is the image tensor | ||
| # Ensure each dimension of imgsz is divisible by 32 | ||
| imgsz = [((dim + 31) // 32) * 32 for dim in imgsz] | ||
| input_name = onnx_model.graph.input[0].name | ||
| make_input_shape_fixed(onnx_model.graph, input_name, [batch_size, 3, imgsz[0], imgsz[1]]) | ||
|
|
||
| # Save the modified ONNX model | ||
| onnx.save(onnx_model, onnx_model_path) | ||
|
|
||
| print("MIGraphX: Loading ONNX model...") | ||
| mxr_model = migraphx.parse_onnx(onnx_model_path) | ||
|
|
||
| print("MIGraphX: Applying FP16 quantization...") | ||
| migraphx.quantize_fp16(mxr_model) | ||
|
|
||
| print("MIGraphX: Compilation...") | ||
| mxr_model.compile(t=migraphx.get_target("gpu"), offload_copy=False) | ||
|
|
||
| mxr_model_path = str(model_path / f"{model_name}_{model_input_id}.mxr") | ||
| migraphx.save(mxr_model, mxr_model_path) | ||
|
|
||
| print(f"\n✓ Successfully processed batch size {batch_size} and image size {imgsz}") | ||
| print(f" Output saved to: {mxr_model_path}\n") | ||
|
|
||
| map50_95 = None | ||
| if imgsz[0] == imgsz[1] and batch_size <= 128: | ||
| validator, data_loader = prepare_validation(model, "coco128.yaml", batch_size) | ||
| metrics = validate_model(mxr_model, validator, data_loader) | ||
| map50_95 = metrics['metrics/mAP50-95(B)'] | ||
| print(f"Metrics/mAP50-95(B): {map50_95}") | ||
|
|
||
| fps = run_benchmark(mxr_model_path) | ||
| print(f"FPS: {fps}") | ||
|
|
||
| return map50_95, fps | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| onnx | ||
| onnxruntime | ||
| ultralytics | ||
| rich | ||
| onnxslim |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These versions should be pinned.