Skip to content

[Example] Yolo12 Detection sample with OpenVINO/XNNPACK backend #10156

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

Merged
merged 8 commits into from
Jul 1, 2025

Conversation

daniil-lyakhov
Copy link
Contributor

@daniil-lyakhov daniil-lyakhov commented Apr 14, 2025

Summary

Ultralytics Yolo12 Detection sample to expand example models with the detection task:

  1. Export script to get executorch yolo model lowered to OpenVINO/XNNPACK
  2. Demo app which gets a model and a video as an input and outputs an annotated video

Changes in the OpenVINO backend:

  • OpenVINO repo branch is updated to support yolo12 models (Commit to Openvino master WIP)
  • Minor fixes in quantizer.py

Test plan

Warning:

OpenCV should be installed in the environment.

OpenVINO:

./.ci/scripts/test_yolo12.sh  -model yolo12n -mode openvino -pt2e_quantize OFF -upload tmp_ov_run -video_path  path/to/mp4/file

XNNPACK:

./.ci/scripts/test_yolo12.sh  -model yolo12n -mode xnnpack -pt2e_quantize OFF -upload tmp_xnnpack_run -video_path  path/to/mp4/file

Issues:

  • Quantization does not work in both backends, issues WIP. Marked by NonImplemented error by now
  • OpenVINO is being build twice because it is not present in the executorch-config.cmake file. Perhaps we could add the OpenVINO backend to the config file?

CC: @ynimmaga @suryasidd @alexsu52

Copy link

pytorch-bot bot commented Apr 14, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/10156

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit 9e1d95f with merge base f0a7d10 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 14, 2025
@GregoryComer
Copy link
Member

FYI On the mlock error, you can try switching the load mode in the Module constructor (or data loader) to Mmap (from Mlock). IMO that should be the default.

@ynimmaga ynimmaga added partner: intel For backend delegation, kernels, demo, etc. from the 3rd-party partner, Intel release notes: openvino OpenVino backend related issues, features, bugs etc. labels Apr 14, 2025
Copy link
Contributor

@alexsu52 alexsu52 left a comment

Choose a reason for hiding this comment

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

I think some sanity checking needs to be added to this example.

@@ -88,7 +88,7 @@ def set_ignored_scope(
names: Optional[List[str]] = None,
patterns: Optional[List[str]] = None,
types: Optional[List[str]] = None,
subgraphs: Optional[List[Tuple[List[str], List[str]]]] = None,
subgraphs: Optional[List[nncf.Subgraph]] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you manage subgraphs without changing the function signature?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@daniil-lyakhov
Copy link
Contributor Author

FYI On the mlock error, you can try switching the load mode in the Module constructor (or data loader) to Mmap (from Mlock). IMO that should be the default.

This helped, thanks!

@daniil-lyakhov daniil-lyakhov force-pushed the dl/yolo12_main branch 2 times, most recently from 4c302d6 to 60d9cfb Compare April 22, 2025 13:23
@daniil-lyakhov daniil-lyakhov requested a review from alexsu52 April 23, 2025 09:15
@daniil-lyakhov
Copy link
Contributor Author

@lucylq, @jackzhxng, could you please take a look?

@@ -0,0 +1,198 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be running in CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is copy-paste from the other similar .sh files like this https://github.com/pytorch/executorch/blob/main/.ci/scripts/test_openvino.sh

Copy link
Contributor

Choose a reason for hiding this comment

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

No my question is this script is not actually running in ci. If so why is it inside .ci folder

Comment on lines 46 to 47
git clone https://github.com/daniil-lyakhov/openvino.git
cd openvino && git checkout dl/executorch/yolo12
Copy link
Contributor

Choose a reason for hiding this comment

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

is this meant to be left like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are working on a fix in the openvino itegration, I'll update the branch as soon as we will have it merged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@@ -0,0 +1,168 @@
#include "inference.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

@larryliu0820 have we setup examples repo already? I am wondeirng if we should move it there.

@@ -0,0 +1,151 @@
#ifndef INFERENCE_H
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe orgnaize folders into export and inference

Copy link
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

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

Left some comments. Didnt review in details since this is largely in examples folder. At high level can you include some performance numbers in the summary

@kimishpatel
Copy link
Contributor

@guangy10 to see if this can be done also via optimum-et

@guangy10
Copy link
Contributor

guangy10 commented Jun 3, 2025

@guangy10 to see if this can be done also via optimum-et

HF Transformers seem to have a different one yolos https://github.com/huggingface/transformers/tree/main/src/transformers/models/yolos. Should we try that in optimum-et?

@daniil-lyakhov
Copy link
Contributor Author

Left some comments. Didnt review in details since this is largely in examples folder. At high level can you include some performance numbers in the summary

Please take a look https://github.com/pytorch/executorch/pull/10156/files#diff-a2182fd228e1b70702adceb8eacfcb42462b65dab17a66113a43ba511ee6988aR15-R22

@kimishpatel
Copy link
Contributor

WIll take a look. Please do ping in case this slips through. I apologize that this has been waiting for so long

from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
from torch.export.exported_program import ExportedProgram
from torch.fx.passes.graph_drawer import FxGraphDrawer
from ultralytics import YOLO
Copy link
Contributor

Choose a reason for hiding this comment

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

is this different from hf yolo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please share a link to the HF yolo? I believe that Ultralitics is the primary source of the Yolo12 models

Copy link
Contributor

Choose a reason for hiding this comment

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

Aah. I see. Yeah while looking for yolo12 + hf I am also coming across mentions of ultralitics.

I was trying to find the source code of it. If you can point me to that, it would be great. I am trying to understand how bounding box related logic is handled in the model and its exportability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ultralytics Yolo repo is quite complex due to the fact that a single class (YOLO) is handling all available yolo versions + training/evaluation/inference code as well.
The inference pipeline could be found here
https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/validator.py#L202-L226
With preprocessing https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/val.py#L66-L81
and postprocessing
https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/val.py#L113-L133

The example code is based on the YOLOv8-CPP-Inference example (the bounding box logic could be found here https://github.com/ultralytics/ultralytics/blob/main/examples/YOLOv8-CPP-Inference/inference.cpp#L16)

Summarizing my work, I've replaced the ONNX backend with the ExecuTorch and made sure everything is working

@@ -45,8 +45,9 @@ Before you begin, ensure you have openvino installed and configured on your syst
### Build OpenVINO from Source

```bash
git clone https://github.com/openvinotoolkit/openvino.git
cd openvino && git checkout releases/2025/1
git clone https://github.com/daniil-lyakhov/openvino.git
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets wait till the fix is landed? I prefer that we dont have to do this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix is finally landed on the master branch! The PR is updated

Copy link
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

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

lets wait till the fix is landed so that we dont have to patch to your personal repo + branch

@daniil-lyakhov
Copy link
Contributor Author

lets wait till the fix is landed so that we dont have to patch to your personal repo + branch

The fix is finally merged to the openvino master! The README and the test script are updated accordingly

@kimishpatel
Copy link
Contributor

lets wait till the fix is landed so that we dont have to patch to your personal repo + branch

The fix is finally merged to the openvino master! The README and the test script are updated accordingly

Thank you. Accepted. Will wait for rest of the CI and merge

@kimishpatel
Copy link
Contributor

Just realized, i didnt approve the workflows. Just did

@daniil-lyakhov
Copy link
Contributor Author

Linters and some other jobs were failed most likely due to the old base commit. Rebased the PR and fixed the linter

@kimishpatel
Copy link
Contributor

kicked off the run

@kimishpatel
Copy link
Contributor

some tests are failing. not sure if they are realted

@daniil-lyakhov
Copy link
Contributor Author

daniil-lyakhov commented Jun 30, 2025

some tests are failing. not sure if they are realted

Looks like some sporicidal problems with the XNNPACKQuantizer

FAILED backends/xnnpack/test/ops/test_exp.py::TestExp::test_fp16_exp - AssertionError: Output 0 does not match reference output.
FAILED backends/xnnpack/test/ops/test_conv1d.py::TestConv1d::test_qs8_conv1d_batchnorm_seq - AssertionError: Output 0 does not match reference output.

and a problem with access to a json file for the llama tests on a mac os

Starting to run llama runner at 23:25:16
+ cmake-out/examples/models/llama/llama_main --model_path=tinyllama_coreml_fp32.pte --tokenizer_path=tokenizer.bin --prompt=Once --temperature=0 --seq_len=10 --warmup=1
I tokenizers:regex.cpp:27] Registering override fallback regex
Error parsing json file: [json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON
invalid charcouldn't load tokenizer.bin. 
Error message: 
could not parse ModelProto from tokenizer.bin
It is likely that the tokenizer artifact is broken or of a different format.++ date +%H:%M:%S

Most likely this commits will fix the problems #12096 #12089. Merging the main branch to my PR

@kimishpatel
Copy link
Contributor

Unrelated failures. merging

@kimishpatel kimishpatel merged commit 00df64c into pytorch:main Jul 1, 2025
189 of 190 checks passed
metascroy added a commit that referenced this pull request Jul 1, 2025
metascroy added a commit that referenced this pull request Jul 1, 2025
…nd" (#12136)

Reverts #10156

@daniil-lyakhov I'm reverting this PR because the gif file is large.

Can you please remove the gif file and re-land?
daniil-lyakhov added a commit to daniil-lyakhov/executorch that referenced this pull request Jul 2, 2025
…rch#10156)

### Summary
Ultralytics Yolo12 Detection sample to expand example models with the
detection task:

1) Export script to get executorch yolo model lowered to
OpenVINO/XNNPACK
2) Demo app which gets a model and a video as an input and outputs an
annotated video

Changes in the OpenVINO backend:
* OpenVINO repo branch is updated to support yolo12 models (Commit to
Openvino master WIP)
* Minor fixes in quantizer.py

### Test plan

#### Warning:
OpenCV should be installed in the environment.

OpenVINO:
```
./.ci/scripts/test_yolo12.sh  -model yolo12n -mode openvino -pt2e_quantize OFF -upload tmp_ov_run -video_path  path/to/mp4/file
```
XNNPACK:
```
./.ci/scripts/test_yolo12.sh  -model yolo12n -mode xnnpack -pt2e_quantize OFF -upload tmp_xnnpack_run -video_path  path/to/mp4/file
```

### Issues:

* Quantization does not work in both backends, issues WIP. Marked by
NonImplemented error by now
* OpenVINO is being build twice because it is not present in the
[executorch-config.cmake](https://github.com/pytorch/executorch/blob/main/tools/cmake/executorch-config.cmake)
file. Perhaps we could add the OpenVINO backend to the config file?


CC: @ynimmaga @suryasidd @alexsu52
kimishpatel pushed a commit that referenced this pull request Jul 8, 2025
### Summary
Follow up #12136 
Revert of the revert #10156 
The big gif is removed from the PR

CC:  @kimishpatel
Tanish2101 pushed a commit to Tanish2101/executorch that referenced this pull request Jul 9, 2025
…rch#10156)

### Summary
Ultralytics Yolo12 Detection sample to expand example models with the
detection task:

1) Export script to get executorch yolo model lowered to
OpenVINO/XNNPACK
2) Demo app which gets a model and a video as an input and outputs an
annotated video

Changes in the OpenVINO backend:
* OpenVINO repo branch is updated to support yolo12 models (Commit to
Openvino master WIP)
* Minor fixes in quantizer.py

### Test plan

#### Warning:
OpenCV should be installed in the environment.

OpenVINO:
```
./.ci/scripts/test_yolo12.sh  -model yolo12n -mode openvino -pt2e_quantize OFF -upload tmp_ov_run -video_path  path/to/mp4/file
```
XNNPACK:
```
./.ci/scripts/test_yolo12.sh  -model yolo12n -mode xnnpack -pt2e_quantize OFF -upload tmp_xnnpack_run -video_path  path/to/mp4/file
```

### Issues:

* Quantization does not work in both backends, issues WIP. Marked by
NonImplemented error by now
* OpenVINO is being build twice because it is not present in the
[executorch-config.cmake](https://github.com/pytorch/executorch/blob/main/tools/cmake/executorch-config.cmake)
file. Perhaps we could add the OpenVINO backend to the config file?


CC: @ynimmaga @suryasidd @alexsu52
Tanish2101 pushed a commit to Tanish2101/executorch that referenced this pull request Jul 9, 2025
…nd" (pytorch#12136)

Reverts pytorch#10156

@daniil-lyakhov I'm reverting this PR because the gif file is large.

Can you please remove the gif file and re-land?
Tanish2101 pushed a commit to Tanish2101/executorch that referenced this pull request Jul 9, 2025
### Summary
Follow up pytorch#12136 
Revert of the revert pytorch#10156 
The big gif is removed from the PR

CC:  @kimishpatel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. partner: intel For backend delegation, kernels, demo, etc. from the 3rd-party partner, Intel release notes: openvino OpenVino backend related issues, features, bugs etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants