Skip to content
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

Add ONNX support for RegNet #833

Merged
merged 6 commits into from
Mar 4, 2023
Merged

Conversation

asrimanth
Copy link
Contributor

@asrimanth asrimanth commented Feb 28, 2023

Fixes #555

I've made the changes, however, is there any way I can verify whether my approach is correct? In other words, could anyone give me a few pointers as to how to test the integration locally?

  • Did you write any new necessary tests?

@fxmarty
Copy link
Contributor

fxmarty commented Mar 1, 2023

Thank you @asrimanth ! You can try:

pip install -e .[tests,exporters]
pytest tests/exporters/onnx/test_*.py -k "regnet" -s

For slow tests:

pip install -e .[tests,exporters]
RUN_SLOW=1 pytest tests/exporters/onnx/test_*.py -k "regnet" -s -m "run_slow"

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 1, 2023

The documentation is not available anymore as the PR was closed or merged.

@asrimanth
Copy link
Contributor Author

@fxmarty The test environment works locally now, thank you!

In the exporter_utils.py file, I wrote this earlier:

"regnet": "hf-internal-testing/tiny-random-RegNet"

However, this page (https://huggingface.co/hf-internal-testing/tiny-random-RegNet) does not exist. So I changed it to

"regnet": "facebook/regnet-y-040",

Even though the URL exists (https://huggingface.co/facebook/regnet-y-040), I keep running into errors when I test locally. Please advise.

@fxmarty
Copy link
Contributor

fxmarty commented Mar 3, 2023

Hi @asrimanth you can maybe try https://huggingface.co/hf-internal-testing/tiny-random-RegNetModel

What errors are you running into?

@asrimanth
Copy link
Contributor Author

Hello @fxmarty, Here's the error I'm getting:

=============================================================================== short test summary info ================================================================================
FAILED tests/exporters/onnx/test_exporters_onnx_cli.py::OnnxCLIExportTestCase::test_exporters_cli_pytorch_289_regnet_default - subprocess.CalledProcessError: Command 'python3 -m optimum.exporters.onnx --model hf-internal-testing/tiny-random-RegNetModel  --task default /var/folders/m4/tk3w_g0119d3bxf4s_zrn...
FAILED tests/exporters/onnx/test_exporters_onnx_cli.py::OnnxCLIExportTestCase::test_exporters_cli_pytorch_290_regnet_image_classification - subprocess.CalledProcessError: Command 'python3 -m optimum.exporters.onnx --model hf-internal-testing/tiny-random-RegNetModel  --task image-classification /var/folders/m4/tk3w_g01...
FAILED tests/exporters/onnx/test_exporters_onnx_cli.py::OnnxCLIExportTestCase::test_exporters_cli_pytorch_291_regnet_no_task - subprocess.CalledProcessError: Command 'python3 -m optimum.exporters.onnx --model hf-internal-testing/tiny-random-RegNetModel  /var/folders/m4/tk3w_g0119d3bxf4s_zrnsjr0000gn/T/tmp...

I think these are the errors caused by the line in the exporter_utils.py file. I've made the changes as follows:

"regnet": "hf-internal-testing/tiny-random-RegNetModel",

Since I'm new to open source, I'm open to advice.

@regisss
Copy link
Contributor

regisss commented Mar 3, 2023

Hi @asrimanth! Can you show us the detailed errors that are printed before this summary please? Unfortunately this test summary info cut the error messages so it is hard to know how to solve it.

@asrimanth
Copy link
Contributor Author

Hello @regisss, thank you for your comment!

You can see the full log in this commit here: 510845f

If not, I've attached the local output as follows:
log.txt

@fxmarty
Copy link
Contributor

fxmarty commented Mar 3, 2023

@asrimanth Yes the last error in the trace is not informative unfortunately. We need to refactor a bit as for now we currently spawn a subprocess to do the ONNX export.

To see the full trace and where the issue comes from, you can try: pytest tests/exporters/onnx/test_exporters_onnx_cli.py -k "test_exporters_cli_pytorch_290_regnet_image_classification" -s --full-trace.

You could also simply try:

optimum-cli export onnx --model facebook/regnet-y-040 regnet_onnx/

@asrimanth
Copy link
Contributor Author

I've tried facebook/regnet-y-040 and hf-internal-testing/tiny-random-RegNetModel using the full trace. Both of them show output as follows:

log.txt

As for the command: optimum-cli export onnx --model facebook/regnet-y-040 regnet_onnx/, it throws the following error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/Users/srimanth/Documents/TakeHome/optimum/optimum/exporters/__init__.py", line 15, in <module>
    from . import onnx  # noqa
  File "/Users/srimanth/Documents/TakeHome/optimum/optimum/exporters/onnx/__init__.py", line 16, in <module>
    from .base import OnnxConfig, OnnxConfigWithLoss, OnnxConfigWithPast, OnnxSeq2SeqConfigWithPast  # noqa
  File "/Users/srimanth/Documents/TakeHome/optimum/optimum/exporters/onnx/base.py", line 29, in <module>
    import onnx
ModuleNotFoundError: No module named 'onnx'
Traceback (most recent call last):
  File "/opt/homebrew/bin/optimum-cli", line 8, in <module>
    sys.exit(main())
  File "/Users/srimanth/Documents/TakeHome/optimum/optimum/commands/optimum_cli.py", line 40, in main
    service.run()
  File "/Users/srimanth/Documents/TakeHome/optimum/optimum/commands/export/onnx.py", line 173, in run
    subprocess.run(full_command, shell=True, check=True)
  File "/opt/homebrew/Cellar/[email protected]/3.10.10_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'python3 -m optimum.exporters.onnx --model facebook/regnet-y-040 regnet_onnx/' returned non-zero exit status 1.

@regisss
Copy link
Contributor

regisss commented Mar 3, 2023

The package onnx is not installed in your environment it seems. Could you make sure you installed Optimum with the following command please?

pip install -e .[tests,exporters]

@asrimanth
Copy link
Contributor Author

asrimanth commented Mar 3, 2023

Hello @regisss, I've tried that on my local M1 Macbook air and it doesn't work. So I tried it on a different linux machine and all of the tests pass with 1 warning. You can see the output below:

> pytest tests/exporters/onnx/test_*.py -k "regnet" -s
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.10.5, pytest-7.2.1, pluggy-1.0.0
rootdir: /nfs/nfs2/home/sragas/optimum, configfile: pyproject.toml
plugins: xdist-3.2.0, anyio-3.5.0
collected 1143 items / 1143 deselected / 0 selected                                                                                                                             

=============================================================================== warnings summary ================================================================================
<frozen importlib._bootstrap>:283
  <frozen importlib._bootstrap>:283: DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
====================================================================== 1143 deselected, 1 warning in 5.77s ======================================================================

Can you please approve the workflow in order to run CI/CD tests? Or should I make any more changes? Please let me know.

@fxmarty
Copy link
Contributor

fxmarty commented Mar 3, 2023

Hi @asrimanth the tests indeed pass! There remains a conflict in docs/source/exporters/onnx/package_reference/configuration.mdx, could you solve it?

For that, you would need to pull on main, and merge or rebase into your branch, solving the conflict. It's probably just due to the fact that I moved the list of supported models in the doc to the overview page.

@asrimanth
Copy link
Contributor Author

@fxmarty, @regisss, I've resolved the merge conflicts and made the final changes. Where can I add the support for RegNet model in the documentation? Please let me know.

@asrimanth asrimanth changed the title Regnet onnx Add ONNX support for RegNet Mar 3, 2023
@fxmarty
Copy link
Contributor

fxmarty commented Mar 3, 2023

@asrimanth
Copy link
Contributor Author

asrimanth commented Mar 3, 2023

I've made the documentation changes. Thank you @fxmarty, @regisss for helping me with my first open-source contribution!

@asrimanth
Copy link
Contributor Author

@fxmarty, @regisss, The build failed in macOS. Any ideas on how to debug this?

Copy link
Contributor

@fxmarty fxmarty left a comment

Choose a reason for hiding this comment

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

The failing test is unrelated. Thank you for the addition!

@fxmarty fxmarty merged commit 81e56a4 into huggingface:main Mar 4, 2023
@sushmanthreddy
Copy link

@asrimanth can u tell me , how to find supported_task_maping for a model?i have been trying to understand but I am quite confused.. pls tell me how to find supported_task_maping for a model??

@fxmarty
Copy link
Contributor

fxmarty commented Apr 24, 2023

Hi @sushmanthreddy , you can use this method: https://huggingface.co/docs/optimum/main/en/exporters/task_manager#optimum.exporters.TasksManager.get_supported_tasks_for_model_type

from optimum.exporters import TasksManager

print(TasksManager.get_supported_tasks_for_model_type("bert", exporter="onnx").keys())

Are you asking in order to add a new architecture?

@sushmanthreddy
Copy link

@fxmarty yes I was asking for adding new architecture
BLIP-2 MODEL

@fxmarty
Copy link
Contributor

fxmarty commented Apr 24, 2023

Awesome, you could add it in the mapping here:

"bloom": supported_tasks_mapping(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Community contribution - optimum.exporters.onnx support for new models!
5 participants