├── tests
├── __init__.py
├── exporters
│ ├── __init__.py
│ ├── onnx
│ │ └── __init__.py
│ ├── tflite
│ │ └── __init__.py
│ └── Dockerfile_exporters_gpu
├── assets
│ ├── hub
│ │ └── config.json
│ └── onnx
│ │ ├── model.onnx
│ │ └── config.json
├── run_doctest.sh
├── README.md
├── gptq
│ └── Dockerfile_quantization_gpu
├── bettertransformer
│ └── Dockerfile_bettertransformer_gpu
├── onnxruntime
│ ├── docker
│ │ └── Dockerfile_onnxruntime_gpu
│ ├── ds_configs
│ │ ├── ds_config_zero_stage_inifinity.json
│ │ ├── ds_config_zero_stage_1.json
│ │ ├── ds_config_zero_stage_2.json
│ │ └── ds_config_zero_stage_3.json
│ └── test_utils.py
├── cli
│ └── cli_with_custom_command.py
├── test_modeling_base.py
├── onnx
│ └── test_onnx_export_custom_module.py
└── benchmark
│ └── memory_tracker.py
├── optimum
├── onnxruntime
│ ├── models
│ │ ├── __init__.py
│ │ └── bloom.py
│ ├── runs
│ │ └── utils.py
│ ├── io_binding
│ │ └── __init__.py
│ ├── preprocessors
│ │ ├── __init__.py
│ │ ├── passes
│ │ │ ├── __init__.py
│ │ │ ├── fully_connected.py
│ │ │ ├── gelu.py
│ │ │ └── layernorm.py
│ │ └── quantization.py
│ ├── constants.py
│ ├── graph.py
│ └── training_args_seq2seq.py
├── utils
│ ├── dummy_bettertransformer_objects.py
│ ├── constant.py
│ ├── preprocessing
│ │ ├── __init__.py
│ │ └── task_processors_manager.py
│ ├── modeling_utils.py
│ ├── doc.py
│ ├── dummy_diffusers_objects.py
│ └── __init__.py
├── commands
│ ├── register
│ │ ├── __init__.py
│ │ └── README.md
│ ├── export
│ │ ├── __init__.py
│ │ └── base.py
│ ├── onnxruntime
│ │ ├── __init__.py
│ │ └── base.py
│ ├── __init__.py
│ └── env.py
├── version.py
├── gptq
│ ├── __init__.py
│ └── constants.py
├── fx
│ ├── __init__.py
│ ├── quantization
│ │ └── __init__.py
│ ├── optimization
│ │ └── __init__.py
│ └── utils.py
├── exporters
│ ├── __init__.py
│ ├── base.py
│ ├── error_utils.py
│ ├── onnx
│ │ ├── constants.py
│ │ └── __init__.py
│ └── tflite
│ │ ├── __init__.py
│ │ └── config.py
├── bettertransformer
│ └── __init__.py
├── pipelines
│ ├── __init__.py
│ └── diffusers
│ │ └── watermark.py
├── quantization_base.py
├── onnx
│ └── __init__.py
└── conftest.py
├── docs
├── source
│ ├── notebooks.md
│ ├── nvidia_overview.mdx
│ ├── torch_fx
│ │ ├── concept_guides
│ │ │ └── symbolic_tracer.mdx
│ │ ├── package_reference
│ │ │ └── optimization.mdx
│ │ └── overview.mdx
│ ├── onnxruntime
│ │ ├── package_reference
│ │ │ ├── optimization.mdx
│ │ │ ├── quantization.mdx
│ │ │ ├── trainer.mdx
│ │ │ └── configuration.mdx
│ │ ├── concept_guides
│ │ │ └── onnx.mdx
│ │ └── overview.mdx
│ ├── exporters
│ │ ├── overview.mdx
│ │ ├── tflite
│ │ │ ├── package_reference
│ │ │ │ ├── export.mdx
│ │ │ │ └── configuration.mdx
│ │ │ ├── usage_guides
│ │ │ │ └── contribute.mdx
│ │ │ └── overview.mdx
│ │ └── onnx
│ │ │ └── package_reference
│ │ │ ├── export.mdx
│ │ │ └── configuration.mdx
│ ├── _redirects.yml
│ └── utils
│ │ ├── dummy_input_generators.mdx
│ │ └── normalized_config.mdx
├── Dockerfile
└── conftest.py
├── examples
└── onnxruntime
│ ├── quantization
│ ├── image-classification
│ │ ├── requirements.txt
│ │ └── README.md
│ ├── question-answering
│ │ ├── requirements.txt
│ │ └── README.md
│ ├── token-classification
│ │ ├── requirements.txt
│ │ └── README.md
│ ├── multiple-choice
│ │ ├── requirements.txt
│ │ └── README.md
│ └── text-classification
│ │ ├── requirements.txt
│ │ └── README.md
│ ├── optimization
│ ├── question-answering
│ │ ├── requirements.txt
│ │ └── README.md
│ ├── token-classification
│ │ ├── requirements.txt
│ │ └── README.md
│ ├── multiple-choice
│ │ ├── requirements.txt
│ │ └── README.md
│ └── text-classification
│ │ ├── requirements.txt
│ │ └── README.md
│ └── training
│ ├── text-classification
│ ├── requirements.txt
│ └── zero_stage_2.json
│ ├── translation
│ └── requirements.txt
│ ├── question-answering
│ ├── requirements.txt
│ └── README.md
│ ├── stable-diffusion
│ └── text-to-image
│ │ └── requirements.txt
│ ├── image-classification
│ ├── requirements.txt
│ └── README.md
│ ├── token-classification
│ ├── requirements.txt
│ └── README.md
│ ├── summarization
│ ├── requirements.txt
│ └── README.md
│ ├── language-modeling
│ ├── requirements.txt
│ └── README.md
│ └── docker
│ └── Dockerfile-ort-nightly-rocm57
├── .github
├── ISSUE_TEMPLATE
│ ├── config.yml
│ ├── feature-request.yml
│ └── bug-report.yml
├── workflows
│ ├── upload_pr_documentation.yml
│ ├── test_bettertransformer_gpu.yml
│ ├── doctests.yml
│ ├── test_exporters_common.yml
│ ├── test_onnx.yml
│ ├── test_onnxruntime_slow.yml
│ ├── test_export_onnx_timm.yml
│ ├── test_onnxruntime_train.yml
│ ├── test_fx.yml
│ ├── test_export_onnx_cli_timm.yml
│ ├── test_export_onnx_cli.yml
│ ├── test_exporters_gpu.yml
│ ├── test_gptq.yml
│ ├── test_export_tflite.yml
│ ├── test_onnxruntime_gpu.yml
│ ├── test_export_tflite_cli.yml
│ ├── dev_test_exporters.yml
│ ├── test_export_tflite_cli_quantization_fp16.yml
│ ├── test_benckmark.yml
│ ├── dev_test_onnx.yml
│ ├── test_export_tflite_cli_dynamic_quantization_int8.yml
│ ├── test_dummy_inputs.yml
│ ├── test_export_tflite_cli_quantization_int8x16.yml
│ ├── dev_test_benckmark.yml
│ ├── test_cli.yml
│ ├── test_export_tflite_cli_quantization_full_int8.yml
│ ├── dev_test_dummy_inputs.yml
│ ├── test_export_tflite_cli_quantization_int8_custom_dataset.yml
│ ├── test_export_tflite_cli_quantization_int8_default_dataset.yml
│ ├── dev_test_fx.yml
│ ├── test_exporters_slow.yml
│ ├── dev_test_bettertransformer.yml
│ ├── dev_test_onnxruntime.yml
│ ├── test_export_onnx.yml
│ ├── check_code_quality.yml
│ ├── test_bettertransformer.yml
│ ├── test_optimum_common.yml
│ ├── test_offline.yml
│ └── dev_test_optimum_common.yml
├── PULL_REQUEST_TEMPLATE.md
└── generate_dev_tests.py
├── setup.cfg
├── MANIFEST.in
├── pyproject.toml
├── Makefile
└── .gitignore
/tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/exporters/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/exporters/onnx/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/exporters/tflite/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/models/__init__.py:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/tests/assets/hub/config.json:
--------------------------------------------------------------------------------
1 | {"from_local":true}
--------------------------------------------------------------------------------
/docs/source/notebooks.md:
--------------------------------------------------------------------------------
1 | ../../notebooks/README.md
--------------------------------------------------------------------------------
/tests/assets/onnx/model.onnx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/merveenoyan/optimum/main/tests/assets/onnx/model.onnx
--------------------------------------------------------------------------------
/examples/onnxruntime/quantization/image-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | torch>=1.5.0
2 | torchvision>=0.6.0
3 | datasets>=1.17.0
4 |
--------------------------------------------------------------------------------
/examples/onnxruntime/optimization/question-answering/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | torch >= 1.9.0
3 | onnx
4 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/quantization/question-answering/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | torch >= 1.9.0
3 | onnx
4 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/optimization/token-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | seqeval
2 | datasets >= 1.18.0
3 | torch >= 1.9
4 | onnx
5 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/quantization/token-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | seqeval
2 | datasets >= 1.8.0
3 | torch >= 1.9
4 | onnx
5 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/training/text-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf
6 |
--------------------------------------------------------------------------------
/docs/source/nvidia_overview.mdx:
--------------------------------------------------------------------------------
1 | # 🤗 Optimum Nvidia
2 |
3 | Find more information about 🤗 Optimum Nvidia [here](https://github.com/huggingface/optimum-nvidia).
4 |
--------------------------------------------------------------------------------
/examples/onnxruntime/training/translation/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.18.0
2 | sentencepiece != 0.1.92
3 | protobuf
4 | sacrebleu >= 1.4.12
5 | py7zr
6 | torch >= 1.8
--------------------------------------------------------------------------------
/docs/source/torch_fx/concept_guides/symbolic_tracer.mdx:
--------------------------------------------------------------------------------
1 | # Symbolic tracer
2 |
3 | In Torch FX, the symbolic tracer feeds dummy values through the code to record the underlying operations.
--------------------------------------------------------------------------------
/examples/onnxruntime/training/question-answering/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf
6 | torch >= 1.9.0
7 | torch-ort
8 |
--------------------------------------------------------------------------------
/examples/onnxruntime/optimization/multiple-choice/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf
6 | torch >= 1.9
7 | onnx
8 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/quantization/multiple-choice/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf
6 | torch >= 1.9
7 | onnx
8 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/optimization/text-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf
6 | torch >= 1.9
7 | onnx
8 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/quantization/text-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf
6 | torch >= 1.9
7 | onnx
8 | onnxruntime >= 1.9.0
--------------------------------------------------------------------------------
/examples/onnxruntime/training/stable-diffusion/text-to-image/requirements.txt:
--------------------------------------------------------------------------------
1 | accelerate>=0.16.0
2 | transformers>=4.25.1
3 | datasets
4 | git+https://github.com/huggingface/diffusers
5 | ftfy
6 | tensorboard
7 | Jinja2
8 |
--------------------------------------------------------------------------------
/examples/onnxruntime/training/image-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | accelerate>=0.12.0
2 | torch>=1.5.0
3 | torchvision>=0.6.0
4 | datasets>=1.17.0
5 | evaluate
6 | onnx>=1.9.0
7 | onnxruntime-training>=1.9.0
8 | torch-ort
9 |
--------------------------------------------------------------------------------
/examples/onnxruntime/training/token-classification/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.18.3
2 | scipy
3 | scikit-learn
4 | sentencepiece != 0.1.92
5 | seqeval
6 | torch >= 1.8.1
7 | seqeval
8 | sentencepiece != 0.1.92
9 | torch >= 1.9
10 | torch-ort
11 |
--------------------------------------------------------------------------------
/examples/onnxruntime/training/summarization/requirements.txt:
--------------------------------------------------------------------------------
1 | accelerate
2 | evaluate
3 | datasets >= 1.8.0
4 | sentencepiece != 0.1.92
5 | scipy
6 | scikit-learn
7 | protobuf
8 | rouge-score
9 | nltk
10 | py7zr
11 | torch >= 1.9.0
12 | torch-ort
13 |
--------------------------------------------------------------------------------
/examples/onnxruntime/training/language-modeling/requirements.txt:
--------------------------------------------------------------------------------
1 | datasets >= 1.8.0
2 | sentencepiece != 0.1.92
3 | scipy
4 | scikit-learn
5 | protobuf == 3.20.2
6 | torch >= 1.9.0
7 | transformers>=4.16.0
8 | onnx>=1.9.0
9 | onnxruntime-training>=1.9.0
10 | torch-ort
11 |
--------------------------------------------------------------------------------
/optimum/utils/dummy_bettertransformer_objects.py:
--------------------------------------------------------------------------------
1 | from .import_utils import DummyObject, requires_backends
2 |
3 |
4 | class BarkSelfAttention(metaclass=DummyObject):
5 | _backends = ["transformers_431"]
6 |
7 | def __init__(self, *args, **kwargs):
8 | requires_backends(self, ["transformers_431"])
9 |
--------------------------------------------------------------------------------
/tests/run_doctest.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | pip install accelerate
3 | pip install .[benchmark]
4 | touch optimum/__init__.py
5 | python tests/utils/prepare_for_doc_test.py optimum docs
6 | pytest --verbose -s --doctest-modules $(cat tests/utils/documentation_tests.txt) --doctest-continue-on-failure --doctest-glob='*.mdx'
7 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/config.yml:
--------------------------------------------------------------------------------
1 | blank_issues_enabled: true
2 | version: 2.1
3 | contact_links:
4 | - name: Website Related
5 | url: https://github.com/huggingface/hub-docs/issues
6 | about: Feature requests and bug reports related to the website
7 | - name: Forum
8 | url: https://discuss.huggingface.co/
9 | about: General usage questions and community discussions
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [isort]
2 | default_section = FIRSTPARTY
3 | ensure_newline_before_comments = True
4 | force_grid_wrap = 0
5 | include_trailing_comma = True
6 | known_first_party = optimum
7 | line_length = 119
8 | lines_after_imports = 2
9 | multi_line_output = 3
10 | use_parentheses = True
11 |
12 | [flake8]
13 | ignore = E203, E501, E741, W503, W605
14 | max-line-length = 119
15 |
16 | [tool:pytest]
17 | doctest_optionflags=NUMBER NORMALIZE_WHITESPACE ELLIPSIS
18 |
--------------------------------------------------------------------------------
/examples/onnxruntime/training/text-classification/zero_stage_2.json:
--------------------------------------------------------------------------------
1 | {
2 | "steps_per_print": 100,
3 | "zero_optimization": {
4 | "stage": 2
5 | },
6 | "zero_allow_untested_optimizer": true,
7 | "fp16": {
8 | "enabled": true,
9 | "initial_scale_power": 12
10 | },
11 | "tensorboard":{
12 | "enabled": false
13 | },
14 | "train_micro_batch_size_per_gpu": "auto",
15 | "gradient_accumulation_steps": "auto"
16 | }
--------------------------------------------------------------------------------
/docs/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM nikolaik/python-nodejs:python3.8-nodejs18
2 |
3 | ARG commit_sha
4 | ARG clone_url
5 |
6 | RUN apt -y update
7 | RUN python3 -m pip install --no-cache-dir --upgrade pip
8 | RUN python3 -m pip install --no-cache-dir git+https://github.com/huggingface/doc-builder.git
9 |
10 | RUN git clone $clone_url && cd optimum && git checkout $commit_sha
11 | RUN python3 -m pip install --no-cache-dir ./optimum[onnxruntime,benchmark,quality,exporters-tf,doc-build,diffusers]
12 |
--------------------------------------------------------------------------------
/.github/workflows/upload_pr_documentation.yml:
--------------------------------------------------------------------------------
1 | name: Upload PR Documentation
2 |
3 | on:
4 | workflow_run:
5 | workflows: ["Build PR documentation"]
6 | types:
7 | - completed
8 |
9 | jobs:
10 | build:
11 | uses: huggingface/doc-builder/.github/workflows/upload_pr_documentation.yml@main
12 | with:
13 | package_name: optimum
14 | secrets:
15 | hf_token: ${{ secrets.HF_DOC_BUILD_PUSH }}
16 | comment_bot_token: ${{ secrets.COMMENT_BOT_TOKEN }}
17 |
--------------------------------------------------------------------------------
/tests/README.md:
--------------------------------------------------------------------------------
1 | # Helpful tips for testing & debugging optimum
2 |
3 | ## VSCODE
4 |
5 | If you are using vscode you might have hard time discovering the test for the "testing" menu to run tests individually or debug them. You can copy the snippet below into `.vscode/settings.json`.
6 |
7 | ```json
8 | {
9 | "python.testing.pytestArgs": [
10 | "tests/onnxruntime",
11 | "tests/test_*"
12 | ],
13 | "python.testing.unittestEnabled": false,
14 | "python.testing.pytestEnabled": true
15 | }
16 | ```
17 |
18 | This snippet will discover all base tests and the tests inside the `tests/onnxruntime` folder.
19 |
--------------------------------------------------------------------------------
/optimum/commands/register/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 The HuggingFace Team. 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 |
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | # Copyright 2021 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | include README.md
16 | include LICENSE
17 |
--------------------------------------------------------------------------------
/optimum/version.py:
--------------------------------------------------------------------------------
1 | # Copyright 2021 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | __version__ = "1.20.0.dev0"
16 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/runs/utils.py:
--------------------------------------------------------------------------------
1 | from ..modeling_decoder import ORTModelForCausalLM
2 | from ..modeling_ort import (
3 | ORTModelForFeatureExtraction,
4 | ORTModelForImageClassification,
5 | ORTModelForQuestionAnswering,
6 | ORTModelForSequenceClassification,
7 | ORTModelForTokenClassification,
8 | )
9 |
10 |
11 | task_ortmodel_map = {
12 | "text-generation": ORTModelForCausalLM,
13 | "feature-extraction": ORTModelForFeatureExtraction,
14 | "image-classification": ORTModelForImageClassification,
15 | "question-answering": ORTModelForQuestionAnswering,
16 | "text-classification": ORTModelForSequenceClassification,
17 | "token-classification": ORTModelForTokenClassification,
18 | }
19 |
--------------------------------------------------------------------------------
/optimum/gptq/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 HuggingFace Inc. team.
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 | from .quantizer import GPTQQuantizer, load_quantized_model
16 |
--------------------------------------------------------------------------------
/optimum/fx/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2022 The HuggingFace Team. 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 | from . import optimization, quantization
16 |
--------------------------------------------------------------------------------
/optimum/fx/quantization/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | from .functions import fuse_fx, prepare_fx, prepare_qat_fx
15 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/io_binding/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | from .io_binding_helper import IOBindingHelper, TypeHelper
15 |
--------------------------------------------------------------------------------
/optimum/exporters/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2022 The HuggingFace Team. 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 | from . import onnx # noqa
16 | from .tasks import TasksManager # noqa
17 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/preprocessors/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from .quantization import PreprocessorPass, QuantizationPreprocessor
16 |
--------------------------------------------------------------------------------
/.github/workflows/test_bettertransformer_gpu.yml:
--------------------------------------------------------------------------------
1 | name: BetterTransformer GPU / Python - Test
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 1 */3 * * # at 1am every 3 days
7 |
8 | jobs:
9 | do-the-job:
10 | name: Start self-hosted EC2 runner
11 | runs-on: [single-gpu, nvidia-gpu, t4, ci]
12 | env:
13 | AWS_REGION: us-east-1
14 | steps:
15 | - name: Checkout
16 | uses: actions/checkout@v2
17 | - name: Build image
18 | run: |
19 | docker build -f tests/bettertransformer/Dockerfile_bettertransformer_gpu -t bettertransformer-gpu .
20 | - name: Test with unittest within docker container
21 | run: |
22 | docker run --rm --gpus all --workdir=/workspace/optimum/tests bettertransformer-gpu:latest
23 |
--------------------------------------------------------------------------------
/optimum/bettertransformer/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace and Meta Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | from .models import BetterTransformerManager
15 | from .transformation import BetterTransformer
16 |
--------------------------------------------------------------------------------
/docs/source/onnxruntime/package_reference/optimization.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Optimization
14 |
15 | ## ORTOptimizer
16 |
17 | [[autodoc]] onnxruntime.optimization.ORTOptimizer
18 | - all
--------------------------------------------------------------------------------
/docs/source/onnxruntime/package_reference/quantization.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Quantization
14 |
15 | ## ORTQuantizer
16 |
17 | [[autodoc]] onnxruntime.quantization.ORTQuantizer
18 | - all
19 |
--------------------------------------------------------------------------------
/optimum/exporters/base.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2022 The HuggingFace Team. 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 | """Base exporters config."""
16 |
17 | from abc import ABC
18 |
19 |
20 | class ExportConfig(ABC):
21 | pass
22 |
--------------------------------------------------------------------------------
/optimum/commands/export/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | from .base import ExportCommand
17 | from .onnx import ONNXExportCommand
18 | from .tflite import TFLiteExportCommand
19 |
--------------------------------------------------------------------------------
/.github/workflows/doctests.yml:
--------------------------------------------------------------------------------
1 | name: Optimum common / Doctest
2 | # Note: this test uses transformers stable and optimum dev
3 |
4 | on:
5 | workflow_dispatch:
6 | schedule:
7 | - cron: 0 1 * * 0 # every sunday at 1am
8 |
9 | jobs:
10 | do-the-job:
11 | name: Start self-hosted EC2 runner
12 | runs-on: [single-gpu, nvidia-gpu, t4, ci]
13 | env:
14 | AWS_REGION: us-east-1
15 | steps:
16 | - name: Checkout
17 | uses: actions/checkout@v2
18 | - name: Build image
19 | run: |
20 | docker build -f tests/onnxruntime/Dockerfile_onnxruntime_gpu -t onnxruntime-gpu .
21 | - name: Test with unittest within docker container
22 | run: |
23 | docker run --rm --gpus all --workdir=/workspace/optimum/ onnxruntime-gpu:latest /bin/bash tests/run_doctest.sh
24 |
--------------------------------------------------------------------------------
/optimum/commands/onnxruntime/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 The HuggingFace Team. 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 | from .base import ONNXRuntimeCommand
17 | from .optimize import ONNXRuntimeOptimizeCommand
18 | from .quantize import ONNXRuntimeQuantizeCommand
19 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/preprocessors/passes/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from .excluders import ExcludeNodeAfter, ExcludeNodeFollowedBy
16 | from .gelu import ExcludeGeLUNodes
17 | from .layernorm import ExcludeLayerNormNodes
18 |
--------------------------------------------------------------------------------
/docs/source/exporters/overview.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Overview
14 |
15 | 🤗 Optimum enables exporting models from PyTorch or TensorFlow to different formats through its `exporters` module. For now, two exporting format are supported: ONNX and TFLite (TensorFlow Lite).
16 |
--------------------------------------------------------------------------------
/optimum/pipelines/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 The HuggingFace Team. 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 | from .pipelines_base import (
17 | MAPPING_LOADING_FUNC,
18 | ORT_SUPPORTED_TASKS,
19 | load_bettertransformer,
20 | load_ort_pipeline,
21 | pipeline,
22 | )
23 |
--------------------------------------------------------------------------------
/docs/source/exporters/tflite/package_reference/export.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Export functions
14 |
15 | ## Main functions
16 |
17 | [[autodoc]] exporters.tflite.convert.export
18 |
19 | ## Utility functions
20 |
21 | [[autodoc]] exporters.tflite.convert.validate_model_outputs
22 |
--------------------------------------------------------------------------------
/tests/assets/onnx/config.json:
--------------------------------------------------------------------------------
1 | {
2 | "_name_or_path": "tiny-distilbert-classification",
3 | "activation": "gelu",
4 | "architectures": [
5 | "DistilBertForSequenceClassification"
6 | ],
7 | "attention_dropout": 0.1,
8 | "dim": 2,
9 | "dropout": 0.1,
10 | "finetuning_task": "sst-2",
11 | "hidden_dim": 2,
12 | "id2label": {
13 | "0": "NEGATIVE",
14 | "1": "POSITIVE"
15 | },
16 | "initializer_range": 0.02,
17 | "label2id": {
18 | "NEGATIVE": 0,
19 | "POSITIVE": 1
20 | },
21 | "max_position_embeddings": 512,
22 | "model_type": "distilbert",
23 | "n_heads": 2,
24 | "n_layers": 2,
25 | "output_past": true,
26 | "pad_token_id": 0,
27 | "qa_dropout": 0.1,
28 | "seq_classif_dropout": 0.2,
29 | "sinusoidal_pos_embds": false,
30 | "tie_weights_": true,
31 | "torch_dtype": "float32",
32 | "transformers_version": "4.10.0.dev0",
33 | "vocab_size": 30522
34 | }
35 |
--------------------------------------------------------------------------------
/optimum/gptq/constants.py:
--------------------------------------------------------------------------------
1 | # Copyright 2023 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | SEQLEN_KEYS_TRANFORMERS = ["max_position_embeddings", "seq_length", "n_positions"]
16 | BLOCK_PATTERNS = [
17 | "transformer.h",
18 | "model.decoder.layers",
19 | "gpt_neox.layers",
20 | "model.layers",
21 | ]
22 |
23 | GPTQ_CONFIG = "quantize_config.json"
24 |
--------------------------------------------------------------------------------
/.github/workflows/test_exporters_common.yml:
--------------------------------------------------------------------------------
1 | name: Exporters Common / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | concurrency:
10 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11 | cancel-in-progress: true
12 |
13 | jobs:
14 | build:
15 | strategy:
16 | fail-fast: false
17 | matrix:
18 | python-version: [3.8, 3.9]
19 | os: [ubuntu-20.04]
20 |
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies
29 | run: |
30 | pip install .[tests,exporters-tf]
31 | - name: Test with unittest
32 | working-directory: tests
33 | run: |
34 | pytest exporters/common/ -s --durations=0
35 |
--------------------------------------------------------------------------------
/.github/workflows/test_onnx.yml:
--------------------------------------------------------------------------------
1 | name: ONNX / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | concurrency:
10 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11 | cancel-in-progress: true
12 |
13 | jobs:
14 | build:
15 | strategy:
16 | fail-fast: false
17 | matrix:
18 | python-version: [3.8, 3.9]
19 | os: [ubuntu-20.04, macos-13]
20 |
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies
29 | run: |
30 | pip install .[tests,onnxruntime,exporters-tf]
31 | - name: Test with unittest
32 | working-directory: tests
33 | run: |
34 | python -m unittest discover -s onnx -p 'test_*.py'
35 |
--------------------------------------------------------------------------------
/optimum/fx/optimization/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2022 The HuggingFace Team. 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 | from .transformations import ( # noqa
16 | ChangeTrueDivToMulByInverse,
17 | FuseBatchNorm1dInLinear,
18 | FuseBatchNorm2dInConv2d,
19 | FuseBiasInLinear,
20 | MergeLinears,
21 | ReversibleTransformation,
22 | Transformation,
23 | compose,
24 | )
25 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/constants.py:
--------------------------------------------------------------------------------
1 | # Copyright 2023 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | ENCODER_ONNX_FILE_PATTERN = r"(.*)?encoder(.*)?\.onnx"
16 | DECODER_ONNX_FILE_PATTERN = r"(.*)?decoder((?!(with_past|merged)).)*?\.onnx"
17 | DECODER_WITH_PAST_ONNX_FILE_PATTERN = r"(.*)?decoder(.*)?with_past(.*)?\.onnx"
18 | DECODER_MERGED_ONNX_FILE_PATTERN = r"(.*)?decoder(.*)?merged(.*)?\.onnx"
19 |
--------------------------------------------------------------------------------
/.github/workflows/test_onnxruntime_slow.yml:
--------------------------------------------------------------------------------
1 | name: ONNX Runtime slow / Python - Test
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 7 * * * # every day at 7am
7 |
8 | concurrency:
9 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
10 | cancel-in-progress: true
11 |
12 | jobs:
13 | build:
14 | strategy:
15 | fail-fast: false
16 | matrix:
17 | python-version: [3.8, 3.9]
18 | os: [ubuntu-20.04]
19 |
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies for export
28 | run: |
29 | pip install .[tests,onnxruntime]
30 | - name: Test with unittest
31 | working-directory: tests
32 | run: |
33 | RUN_SLOW=1 pytest onnxruntime -s -m "run_slow" --durations=0
34 |
--------------------------------------------------------------------------------
/tests/gptq/Dockerfile_quantization_gpu:
--------------------------------------------------------------------------------
1 | FROM nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04
2 | CMD nvidia-smi
3 |
4 | # Ignore interactive questions during `docker build`
5 | ENV DEBIAN_FRONTEND noninteractive
6 |
7 | # Install and update tools to minimize security vulnerabilities
8 | RUN apt-get update
9 | RUN apt-get install -y software-properties-common wget apt-utils patchelf git libprotobuf-dev protobuf-compiler cmake \
10 | bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 mercurial subversion libopenmpi-dev python3-pip && \
11 | apt-get clean
12 | RUN unattended-upgrade
13 | RUN apt-get autoremove -y
14 |
15 | RUN python3 -m pip install -U pip
16 |
17 | RUN pip install torch torchvision torchaudio
18 | RUN pip install transformers accelerate auto-gptq datasets
19 |
20 | # Install Optimum
21 | COPY . /workspace/optimum
22 | RUN pip install /workspace/optimum[tests]
23 |
24 | ENV RUN_SLOW=1
25 | WORKDIR /workspace/optimum/tests/
26 | CMD pytest gptq/test_*.py --durations=0 -s -vvvvv
27 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_onnx_timm.yml:
--------------------------------------------------------------------------------
1 | name: Exporters ONNX Timm / Python - Test
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 7 * * * # every day at 7am
7 |
8 | concurrency:
9 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
10 | cancel-in-progress: true
11 |
12 | jobs:
13 | build:
14 | strategy:
15 | fail-fast: false
16 | matrix:
17 | python-version: [3.8, 3.9]
18 | os: [ubuntu-20.04]
19 |
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies for pytorch export
28 | run: |
29 | pip install .[tests,exporters]
30 | - name: Test with unittest
31 | working-directory: tests
32 | run: |
33 | RUN_SLOW=1 pytest exporters/onnx/ -s -n auto -k "timm" --durations=0
34 |
35 |
--------------------------------------------------------------------------------
/optimum/commands/register/README.md:
--------------------------------------------------------------------------------
1 | # Register commands in the Optimum CLI from a subpackage
2 |
3 | It is possible to register a command in the Optimum CLI, either as a command or a subcommand of an already existing command.
4 |
5 | Steps to follow:
6 |
7 | 1. Create a command as a subclass of `optimum.commands.BaseOptimumCLICommand`.
8 | 2. Create a Python file under `optimum/commands/register/`, and define a `REGISTER_COMMANDS` list variable there.
9 | 3. Fill the `REGISTER_COMMANDS` as follows:
10 |
11 | ```python
12 | # CustomCommand1 and CustomCommand2 could also be defined in this file actually.
13 | from ..my_custom_commands import CustomCommand1, CustomCommand2
14 | from ..export import ExportCommand
15 |
16 | REGISTER_COMMANDS = [
17 | # CustomCommand1 will be registered as a subcommand of the root Optimum CLI.
18 | CustomCommand1,
19 | # CustomCommand2 will be registered as a subcommand of the `optimum-cli export` command.
20 | (CustomCommand2, ExportCommand) # CustomCommand2 will be registered
21 | ]
22 | ```
23 |
--------------------------------------------------------------------------------
/tests/bettertransformer/Dockerfile_bettertransformer_gpu:
--------------------------------------------------------------------------------
1 | FROM nvidia/cuda:11.7.1-cudnn8-devel-ubuntu22.04
2 | CMD nvidia-smi
3 |
4 | # Ignore interactive questions during `docker build`
5 | ENV DEBIAN_FRONTEND noninteractive
6 |
7 | # Install and update tools to minimize security vulnerabilities
8 | RUN apt-get update
9 | RUN apt-get install -y software-properties-common wget apt-utils patchelf git libprotobuf-dev protobuf-compiler cmake \
10 | bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 mercurial subversion libopenmpi-dev python3-pip && \
11 | apt-get clean
12 | RUN unattended-upgrade
13 | RUN apt-get autoremove -y
14 |
15 | RUN python3 -m pip install -U pip
16 |
17 | RUN pip install torch torchvision torchaudio
18 | RUN pip install transformers accelerate datasets
19 |
20 | # Install Optimum
21 | COPY . /workspace/optimum
22 | RUN pip install /workspace/optimum[tests]
23 |
24 | ENV RUN_SLOW=1
25 | WORKDIR /workspace/optimum/tests/
26 | CMD pytest bettertransformer/test_*.py -s --durations=0 -m gpu_test
27 |
--------------------------------------------------------------------------------
/optimum/commands/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from .base import BaseOptimumCLICommand, CommandInfo, RootOptimumCLICommand
16 | from .env import EnvironmentCommand
17 | from .export import ExportCommand, ONNXExportCommand, TFLiteExportCommand
18 | from .onnxruntime import ONNXRuntimeCommand, ONNXRuntimeOptimizeCommand, ONNXRuntimeQuantizeCommand
19 | from .optimum_cli import register_optimum_cli_subcommand
20 |
--------------------------------------------------------------------------------
/optimum/utils/constant.py:
--------------------------------------------------------------------------------
1 | # Copyright 2023 The HuggingFace Inc. team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | CONFIG_NAME = "config.json"
17 | DIFFUSION_MODEL_UNET_SUBFOLDER = "unet"
18 | DIFFUSION_MODEL_TEXT_ENCODER_SUBFOLDER = "text_encoder"
19 | DIFFUSION_MODEL_VAE_DECODER_SUBFOLDER = "vae_decoder"
20 | DIFFUSION_MODEL_VAE_ENCODER_SUBFOLDER = "vae_encoder"
21 | DIFFUSION_MODEL_TEXT_ENCODER_2_SUBFOLDER = "text_encoder_2"
22 | ONNX_WEIGHTS_NAME = "model.onnx"
23 |
--------------------------------------------------------------------------------
/.github/workflows/test_onnxruntime_train.yml:
--------------------------------------------------------------------------------
1 | name: ONNX Runtime / Test ORTTrainer
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 1 */3 * * # at 1am every 3 days
7 | pull_request:
8 | types: [opened, synchronize, reopened, labeled]
9 |
10 | jobs:
11 | do-the-job:
12 | if: ${{ (github.event_name == 'workflow_dispatch') || (github.event_name == 'schedule') || contains( github.event.pull_request.labels.*.name, 'training')}}
13 | name: Run ORTTrainer test
14 | runs-on: [single-gpu, nvidia-gpu, t4, ci]
15 | env:
16 | AWS_REGION: us-east-1
17 | steps:
18 | - name: Checkout
19 | uses: actions/checkout@v2
20 | - name: Build image
21 | run: |
22 | docker build -f tests/onnxruntime/docker/Dockerfile_onnxruntime_trainer -t onnxruntime/train .
23 | - name: Run test within docker container
24 | run: |
25 | docker run --rm --gpus all -v /mnt/cache/.cache/huggingface:/root/.cache/huggingface --workdir=/workspace/optimum/tests onnxruntime/train:latest
--------------------------------------------------------------------------------
/docs/source/exporters/tflite/package_reference/configuration.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Configuration classes for TFLite export
14 |
15 | ## Base classes
16 |
17 | [[autodoc]] exporters.tflite.TFLiteConfig
18 | - inputs
19 | - outputs
20 | - generate_dummy_inputs
21 |
22 | ## Middle-end classes
23 |
24 | [[autodoc]] exporters.tflite.config.TextEncoderTFliteConfig
25 |
26 | [[autodoc]] exporters.tflite.config.VisionTFLiteConfig
27 |
--------------------------------------------------------------------------------
/.github/workflows/test_fx.yml:
--------------------------------------------------------------------------------
1 | name: FX / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | concurrency:
10 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11 | cancel-in-progress: true
12 |
13 | jobs:
14 | build:
15 | strategy:
16 | fail-fast: false
17 | matrix:
18 | python-version: [3.8, 3.9]
19 | os: [ubuntu-20.04, macos-13]
20 |
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies
29 | run: |
30 | pip install .[tests]
31 | pip install git+https://github.com/huggingface/transformers.git
32 | - name: Test with unittest
33 | working-directory: tests
34 | run: |
35 | python -m pytest fx/optimization/test_transformations.py --exitfirst
36 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_onnx_cli_timm.yml:
--------------------------------------------------------------------------------
1 | name: Exporters ONNX CLI Timm (scheduled) / Python - Test
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 7 * * * # every day at 7am
7 |
8 | concurrency:
9 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
10 | cancel-in-progress: true
11 |
12 | jobs:
13 | build:
14 | strategy:
15 | fail-fast: false
16 | matrix:
17 | python-version: [3.8, 3.9]
18 | os: [ubuntu-20.04]
19 |
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies for pytorch export
28 | run: |
29 | pip install .[tests,exporters]
30 | - name: Test with unittest
31 | working-directory: tests
32 | run: |
33 | RUN_SLOW=1 pytest exporters/onnx/test_exporters_onnx_cli.py -n auto -k "timm" -s --durations=0
34 |
--------------------------------------------------------------------------------
/optimum/onnxruntime/graph.py:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The HuggingFace Team. All rights reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | from typing import List
15 |
16 | from onnxruntime.transformers.onnx_model import OnnxModel
17 |
18 |
19 | def find_fully_connected_layers_nodes(model: OnnxModel) -> List[List[str]]:
20 | adds = model.get_nodes_by_op_type("Add")
21 | fc = list(filter(lambda graph: graph[1] is not None, ((add, model.match_parent(add, "MatMul")) for add in adds)))
22 |
23 | return fc
24 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_onnx_cli.yml:
--------------------------------------------------------------------------------
1 | name: Exporters ONNX CLI / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | concurrency:
10 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11 | cancel-in-progress: true
12 |
13 | jobs:
14 | build:
15 | strategy:
16 | fail-fast: false
17 | matrix:
18 | python-version: [3.8, 3.9]
19 | os: [ubuntu-20.04]
20 |
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies for pytorch export
29 | run: |
30 | pip install .[tests,exporters]
31 | - name: Test with unittest
32 | working-directory: tests
33 | run: |
34 | pytest exporters/onnx/test_exporters_onnx_cli.py -n auto -m "not tensorflow_test and not timm_test" -s --durations=0
35 |
--------------------------------------------------------------------------------
/optimum/quantization_base.py:
--------------------------------------------------------------------------------
1 | import logging
2 | from abc import ABC, abstractmethod
3 | from pathlib import Path
4 | from typing import Optional, Union
5 |
6 |
7 | logger = logging.getLogger(__name__)
8 |
9 |
10 | class OptimumQuantizer(ABC):
11 | @classmethod
12 | def from_pretrained(
13 | cls,
14 | model_or_path: Union[str, Path],
15 | file_name: Optional[str] = None,
16 | ):
17 | """Overwrite this method in subclass to define how to load your model from pretrained"""
18 | raise NotImplementedError(
19 | "Overwrite this method in subclass to define how to load your model from pretrained for quantization"
20 | )
21 |
22 | @abstractmethod
23 | def quantize(self, save_dir: Union[str, Path], file_prefix: Optional[str] = None, **kwargs):
24 | """Overwrite this method in subclass to define how to quantize your model for quantization"""
25 | raise NotImplementedError(
26 | "Overwrite this method in subclass to define how to quantize your model for quantization"
27 | )
28 |
--------------------------------------------------------------------------------
/optimum/utils/preprocessing/__init__.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2022 The HuggingFace Team. 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 | from .base import Preprocessor, TaskProcessor
17 | from .image_classification import ImageClassificationProcessing
18 | from .question_answering import QuestionAnsweringProcessing
19 | from .task_processors_manager import TaskProcessorsManager
20 | from .text_classification import TextClassificationProcessing
21 | from .token_classification import TokenClassificationProcessing
22 |
--------------------------------------------------------------------------------
/tests/onnxruntime/docker/Dockerfile_onnxruntime_gpu:
--------------------------------------------------------------------------------
1 | # use version with CUDA 11.8 and TensorRT 8.5.1.7 to match ORT 1.14 requirements
2 | FROM nvcr.io/nvidia/tensorrt:22.12-py3
3 | CMD nvidia-smi
4 |
5 | # Ignore interactive questions during `docker build`
6 | ENV DEBIAN_FRONTEND noninteractive
7 |
8 | # Install and update tools to minimize security vulnerabilities
9 | RUN apt-get update
10 | RUN apt-get install -y software-properties-common wget apt-utils patchelf git libprotobuf-dev protobuf-compiler cmake \
11 | bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 mercurial subversion libopenmpi-dev ffmpeg && \
12 | apt-get clean
13 | RUN unattended-upgrade
14 | RUN apt-get autoremove -y
15 |
16 | RUN python -m pip install -U pip
17 |
18 | RUN pip install transformers torch onnxruntime-gpu
19 | RUN pip install datasets evaluate diffusers scipy
20 |
21 | # Install Optimum
22 | COPY . /workspace/optimum
23 | RUN pip install /workspace/optimum[onnxruntime-gpu,tests]
24 |
25 | ENV TEST_LEVEL=1
26 | CMD pytest onnxruntime/test_*.py --durations=0 -s -vvvvv -m cuda_ep_test -m trt_ep_test
27 |
--------------------------------------------------------------------------------
/.github/workflows/test_exporters_gpu.yml:
--------------------------------------------------------------------------------
1 | name: Exporters / Test GPU
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 1 */3 * * # at 1am every 3 days
7 | pull_request:
8 | types: [labeled]
9 | # uncomment to enable on PR merge on main branch:
10 | #push:
11 | # branches:
12 | # - main
13 |
14 | jobs:
15 | do-the-job:
16 | if: ${{ (github.event_name == 'workflow_dispatch') || (github.event_name == 'schedule') || contains( github.event.pull_request.labels.*.name, 'gpu-test') }}
17 | name: Start self-hosted EC2 runner
18 | runs-on: [single-gpu, nvidia-gpu, t4, ci]
19 | env:
20 | AWS_REGION: us-east-1
21 | steps:
22 | - name: Checkout
23 | uses: actions/checkout@v2
24 | - name: Build image
25 | run: |
26 | docker build -f tests/exporters/Dockerfile_exporters_gpu -t exporters-gpu .
27 | - name: Test with unittest within docker container
28 | run: |
29 | docker run --rm --gpus all -v /mnt/cache/.cache/huggingface:/root/.cache/huggingface --workdir=/workspace/optimum/tests exporters-gpu:latest
30 |
--------------------------------------------------------------------------------
/.github/workflows/test_gptq.yml:
--------------------------------------------------------------------------------
1 | name: GPTQ Quantization / Test GPU
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 1 */3 * * # at 1am every 3 days
7 | pull_request:
8 | types: [opened, synchronize, reopened, labeled]
9 | # uncomment to enable on PR merge on main branch:
10 | #push:
11 | # branches:
12 | # - main
13 |
14 | jobs:
15 | do-the-job:
16 | if: ${{ (github.event_name == 'workflow_dispatch') || (github.event_name == 'schedule') || contains( github.event.pull_request.labels.*.name, 'gpu-test') }}
17 | name: Start self-hosted EC2 runner
18 | runs-on: [single-gpu, nvidia-gpu, t4, ci]
19 | env:
20 | AWS_REGION: us-east-1
21 | steps:
22 | - name: Checkout
23 | uses: actions/checkout@v2
24 | - name: Build image
25 | run: |
26 | docker build -f tests/gptq/Dockerfile_quantization_gpu -t gptq-gpu .
27 | - name: Test with unittest within docker container
28 | run: |
29 | docker run --rm --gpus all -v $(pwd)/hf_cache:/root/.cache/huggingface --workdir=/workspace/optimum/tests gptq-gpu:latest
30 |
--------------------------------------------------------------------------------
/tests/exporters/Dockerfile_exporters_gpu:
--------------------------------------------------------------------------------
1 | # use version with cudnn 8.5 to match torch==1.13.1 that uses 8.5.0.96
2 | # has Python 3.8.10
3 | FROM nvcr.io/nvidia/tensorrt:22.08-py3
4 | CMD nvidia-smi
5 |
6 | # Ignore interactive questions during `docker build`
7 | ENV DEBIAN_FRONTEND noninteractive
8 |
9 | # Install and update tools to minimize security vulnerabilities
10 | RUN apt-get update
11 | RUN apt-get install -y software-properties-common wget apt-utils patchelf git libprotobuf-dev protobuf-compiler cmake \
12 | bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 mercurial subversion libopenmpi-dev && \
13 | apt-get clean
14 | RUN unattended-upgrade
15 | RUN apt-get autoremove -y
16 |
17 | RUN python -m pip install -U pip
18 |
19 | RUN pip install torch scipy datasets evaluate diffusers
20 |
21 | RUN pip install transformers
22 | RUN pip install onnxruntime-gpu
23 |
24 | # Install Optimum
25 | COPY . /workspace/optimum
26 | RUN pip install /workspace/optimum[onnxruntime-gpu,tests,exporters-gpu]
27 |
28 | ENV TEST_LEVEL=1
29 | ENV RUN_SLOW=1
30 | CMD pytest exporters --durations=0 -s -vvvvv -m gpu_test
31 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature-request.yml:
--------------------------------------------------------------------------------
1 | name: "\U0001F680 Feature request"
2 | description: Submit a proposal/request for a new optimum feature
3 | labels: [ "feature" ]
4 | body:
5 | - type: textarea
6 | id: feature-request
7 | validations:
8 | required: true
9 | attributes:
10 | label: Feature request
11 | description: |
12 | A clear and concise description of the feature proposal. Please provide a link to the paper and code in case they exist.
13 |
14 | - type: textarea
15 | id: motivation
16 | validations:
17 | required: true
18 | attributes:
19 | label: Motivation
20 | description: |
21 | Please outline the motivation for the proposal. Is your feature request related to a problem? e.g., I'm always frustrated when [...]. If this is related to another GitHub issue, please link here too.
22 |
23 |
24 | - type: textarea
25 | id: contribution
26 | validations:
27 | required: true
28 | attributes:
29 | label: Your contribution
30 | description: |
31 | Is there any way that you could help, e.g. by submitting a PR?
32 |
--------------------------------------------------------------------------------
/docs/source/exporters/tflite/usage_guides/contribute.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Adding support for an unsupported architecture
14 |
15 | If you wish to export a model whose architecture is not already supported by the library, the PR [#813 Adds support for ResNet](https://github.com/huggingface/optimum/pull/813 ) can be used as a reference.
16 |
17 | You can make sure tests pass for the new `my_new_modeltype` model type by running:
18 |
19 | ```bash
20 | pytest tests/exporters/tflite/test_*.py -k "my_new_modeltype" -s --exitfirst
21 | ```
22 |
--------------------------------------------------------------------------------
/optimum/exporters/error_utils.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 The HuggingFace Team. 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 | """Utilities related to error handling."""
16 |
17 |
18 | class ShapeError(ValueError):
19 | pass
20 |
21 |
22 | class AtolError(ValueError):
23 | pass
24 |
25 |
26 | class OutputMatchError(ValueError):
27 | pass
28 |
29 |
30 | class NumberOfInputsMatchError(ValueError):
31 | pass
32 |
33 |
34 | class NumberOfOutputsMatchError(ValueError):
35 | pass
36 |
37 |
38 | class MinimumVersionError(ValueError):
39 | pass
40 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_tflite_*.py -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/test_onnxruntime_gpu.yml:
--------------------------------------------------------------------------------
1 | name: ONNX Runtime / Test GPU
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 1 */3 * * # at 1am every 3 days
7 | pull_request:
8 | types: [opened, synchronize, reopened, labeled]
9 | # uncomment to enable on PR merge on main branch:
10 | #push:
11 | # branches:
12 | # - main
13 |
14 | jobs:
15 | do-the-job:
16 | if: ${{ (github.event_name == 'workflow_dispatch') || (github.event_name == 'schedule') || contains( github.event.pull_request.labels.*.name, 'gpu-test') }}
17 | name: Start self-hosted EC2 runner
18 | runs-on: [single-gpu, nvidia-gpu, t4, ci]
19 | env:
20 | AWS_REGION: us-east-1
21 | steps:
22 | - name: Checkout
23 | uses: actions/checkout@v2
24 | - name: Build image
25 | run: |
26 | docker build -f tests/onnxruntime/docker/Dockerfile_onnxruntime_gpu -t onnxruntime-gpu .
27 | - name: Test with unittest within docker container
28 | run: |
29 | docker run --rm --gpus all -v /mnt/cache/.cache/huggingface:/root/.cache/huggingface --workdir=/workspace/optimum/tests onnxruntime-gpu:latest
30 |
--------------------------------------------------------------------------------
/docs/source/onnxruntime/package_reference/trainer.mdx:
--------------------------------------------------------------------------------
1 |
12 |
13 | # Trainer
14 |
15 | ## ORTTrainer
16 |
17 | [[autodoc]] onnxruntime.trainer.ORTTrainer
18 | - all
19 |
20 | ## ORTSeq2SeqTrainer
21 |
22 | [[autodoc]] onnxruntime.trainer_seq2seq.ORTSeq2SeqTrainer
23 | - evaluate
24 | - predict
25 |
26 | ## ORTTrainingArguments
27 |
28 | [[autodoc]] onnxruntime.training_args.ORTTrainingArguments
29 | - all
30 |
31 | ## ORTSeq2SeqTrainingArguments
32 |
33 | [[autodoc]] onnxruntime.training_args_seq2seq.ORTSeq2SeqTrainingArguments
34 | - all
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -m "not quantization" -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_exporters.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_Exporters / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | - 3.9
18 | os:
19 | - ubuntu-20.04
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies
28 | run: |
29 | pip install .[tests,exporters-tf]
30 | pip install -U git+https://github.com/huggingface/evaluate
31 | pip install -U git+https://github.com/huggingface/diffusers
32 | pip install -U git+https://github.com/huggingface/transformers
33 | - name: Test with unittest
34 | working-directory: tests
35 | run: |
36 | pytest exporters -s --durations=0
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli_quantization_fp16.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI Quantization FP16 / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -k "float16_quantization" -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/test_benckmark.yml:
--------------------------------------------------------------------------------
1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3 | name: Benchmark suite / Python - Test
4 |
5 | on:
6 | push:
7 | branches: [ main ]
8 | pull_request:
9 | branches: [ main ]
10 |
11 | concurrency:
12 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13 | cancel-in-progress: true
14 |
15 | jobs:
16 | build:
17 | strategy:
18 | fail-fast: false
19 | matrix:
20 | python-version: [3.8, 3.9]
21 | os: [ubuntu-20.04]
22 |
23 | runs-on: ${{ matrix.os }}
24 | steps:
25 | - uses: actions/checkout@v2
26 | - name: Setup Python ${{ matrix.python-version }}
27 | uses: actions/setup-python@v2
28 | with:
29 | python-version: ${{ matrix.python-version }}
30 | - name: Install dependencies
31 | run: |
32 | pip install wheel
33 | pip install .[tests,onnxruntime,benchmark]
34 | - name: Test with unittest
35 | run: |
36 | python -m unittest discover --start-directory tests/benchmark --pattern 'test_*.py'
37 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_onnx.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_ONNX / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | - 3.9
18 | os:
19 | - ubuntu-20.04
20 | - macos-13
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies
29 | run: |
30 | pip install .[tests,onnxruntime] tensorflow tf2onnx
31 | pip install -U git+https://github.com/huggingface/evaluate
32 | pip install -U git+https://github.com/huggingface/diffusers
33 | pip install -U git+https://github.com/huggingface/transformers
34 | - name: Test with unittest
35 | working-directory: tests
36 | run: |
37 | python -m unittest discover -s onnx -p test_*.py
38 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli_dynamic_quantization_int8.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI Dynamic Quantization INT8 / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -k "int8_dynamic_quantization" -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/test_dummy_inputs.yml:
--------------------------------------------------------------------------------
1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3 | name: Dummy inputs / Python - Test
4 |
5 | on:
6 | push:
7 | branches: [ main ]
8 | pull_request:
9 | branches: [ main ]
10 |
11 | concurrency:
12 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13 | cancel-in-progress: true
14 |
15 | jobs:
16 | build:
17 | strategy:
18 | fail-fast: false
19 | matrix:
20 | python-version: [3.8, 3.9]
21 | os: [ubuntu-20.04, macos-13]
22 |
23 | runs-on: ${{ matrix.os }}
24 | steps:
25 | - uses: actions/checkout@v2
26 | - name: Setup Python ${{ matrix.python-version }}
27 | uses: actions/setup-python@v2
28 | with:
29 | python-version: ${{ matrix.python-version }}
30 | - name: Install dependencies
31 | run: |
32 | python -m pip install --upgrade pip
33 | pip install .[tests]
34 | - name: Test with unittest
35 | working-directory: tests
36 | run: |
37 | python -m unittest discover -s utils -p 'test_*.py'
38 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli_quantization_int8x16.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI Quantization INT8x16 / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -k "tflite_int8x16_quantization_with_default_dataset" -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_benckmark.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_Benchmark suite / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | - 3.9
18 | os:
19 | - ubuntu-20.04
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies
28 | run: |
29 | pip install wheel
30 | pip install .[tests,onnxruntime,benchmark]
31 | pip install -U git+https://github.com/huggingface/evaluate
32 | pip install -U git+https://github.com/huggingface/diffusers
33 | pip install -U git+https://github.com/huggingface/transformers
34 | - name: Test with unittest
35 | run: |
36 | python -m unittest discover --start-directory tests/benchmark --pattern
37 | test_*.py
--------------------------------------------------------------------------------
/.github/workflows/test_cli.yml:
--------------------------------------------------------------------------------
1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3 | name: Optimum CLI / Python - Test
4 |
5 | on:
6 | push:
7 | branches: [ main ]
8 | pull_request:
9 | branches: [ main ]
10 |
11 | concurrency:
12 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13 | cancel-in-progress: true
14 |
15 | jobs:
16 | build:
17 | strategy:
18 | fail-fast: false
19 | matrix:
20 | python-version: [3.8, 3.9]
21 | os: [ubuntu-20.04, macos-13]
22 |
23 | runs-on: ${{ matrix.os }}
24 | steps:
25 | - uses: actions/checkout@v2
26 | - name: Setup Python ${{ matrix.python-version }}
27 | uses: actions/setup-python@v2
28 | with:
29 | python-version: ${{ matrix.python-version }}
30 | - name: Install dependencies
31 | run: |
32 | python -m pip install --upgrade pip
33 | pip install .[tests,exporters,exporters-tf]
34 | - name: Test with unittest
35 | working-directory: tests
36 | run: |
37 | python -m unittest discover -s cli -p 'test_*.py'
38 |
39 |
--------------------------------------------------------------------------------
/tests/onnxruntime/ds_configs/ds_config_zero_stage_inifinity.json:
--------------------------------------------------------------------------------
1 | {
2 | "zero_optimization": {
3 | "stage": 3,
4 | "offload_optimizer": {
5 | "device": "nvme",
6 | "nvme_path": "/dev/nvme1n1",
7 | "pin_memory": true,
8 | "buffer_count": 4,
9 | "fast_init": false
10 | },
11 | "offload_param": {
12 | "device": "nvme",
13 | "nvme_path": "/dev/nvme1n1",
14 | "pin_memory": true,
15 | "buffer_count": 5,
16 | "buffer_size": 1e8,
17 | "max_in_cpu": 1e9
18 | },
19 | "aio": {
20 | "block_size": 262144,
21 | "queue_depth": 32,
22 | "thread_count": 1,
23 | "single_submit": false,
24 | "overlap_events": true
25 | },
26 | "overlap_comm": true,
27 | "contiguous_gradients": true,
28 | "sub_group_size": 1e9,
29 | "reduce_bucket_size": "auto",
30 | "stage3_prefetch_bucket_size": "auto",
31 | "stage3_param_persistence_threshold": "auto",
32 | "stage3_max_live_parameters": 1e9,
33 | "stage3_max_reuse_distance": 1e9,
34 | "stage3_gather_16bit_weights_on_model_save": true
35 | }
36 | }
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli_quantization_full_int8.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI Quantization Full INT8 Default Dataset / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -k "full_int8_quantization_with_default_dataset" -s --durations=0
40 |
--------------------------------------------------------------------------------
/optimum/pipelines/diffusers/watermark.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from imwatermark import WatermarkEncoder
3 |
4 |
5 | WATERMARK_MESSAGE = 0b101100111110110010010000011110111011000110011110
6 | WATERMARK_BITS = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]]
7 |
8 |
9 | # Adapted from https://github.com/huggingface/diffusers/blob/v0.18.1/src/diffusers/pipelines/stable_diffusion_xl/watermark.py#L12
10 | class StableDiffusionXLWatermarker:
11 | def __init__(self):
12 | self.watermark = WATERMARK_BITS
13 | self.encoder = WatermarkEncoder()
14 | self.encoder.set_watermark("bits", self.watermark)
15 |
16 | def apply_watermark(self, images: np.array):
17 | # can't encode images that are smaller than 256
18 | if images.shape[-1] < 256:
19 | return images
20 |
21 | # cv2 doesn't support float16
22 | if images.dtype == np.float16:
23 | images = images.astype(np.float32)
24 |
25 | images = (255 * (images / 2 + 0.5)).transpose((0, 2, 3, 1))
26 |
27 | images = np.array([self.encoder.encode(image, "dwtDct") for image in images]).transpose((0, 3, 1, 2))
28 |
29 | np.clip(2 * (images / 255 - 0.5), -1.0, 1.0, out=images)
30 |
31 | return images
32 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_dummy_inputs.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_Dummy inputs / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | - 3.9
18 | os:
19 | - ubuntu-20.04
20 | - macos-13
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies
29 | run: |
30 | python -m pip install --upgrade pip
31 | pip install .[tests]
32 | pip install -U git+https://github.com/huggingface/evaluate
33 | pip install -U git+https://github.com/huggingface/diffusers
34 | pip install -U git+https://github.com/huggingface/transformers
35 | - name: Test with unittest
36 | working-directory: tests
37 | run: |
38 | python -m unittest discover -s utils -p test_*.py
39 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli_quantization_int8_custom_dataset.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI Quantization INT8 Custom Dataset / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -k "tflite_int8_quantization_with_custom_dataset" -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_tflite_cli_quantization_int8_default_dataset.yml:
--------------------------------------------------------------------------------
1 | name: Exporters TFLite CLI Quantization INT8 Default Dataset / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 | paths:
9 | - "optimum/exporters/tasks.py"
10 | - "optimum/exporters/tflite/**.py"
11 | - "tests/exporters/**.py"
12 | - "setup.py"
13 |
14 | concurrency:
15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | python-version: [3.8, 3.9]
24 | os: [ubuntu-20.04]
25 |
26 | runs-on: ${{ matrix.os }}
27 | steps:
28 | - uses: actions/checkout@v2
29 | - name: Setup Python ${{ matrix.python-version }}
30 | uses: actions/setup-python@v2
31 | with:
32 | python-version: ${{ matrix.python-version }}
33 | - name: Install dependencies
34 | run: |
35 | pip install .[tests,exporters-tf]
36 | - name: Test with unittest
37 | working-directory: tests
38 | run: |
39 | pytest exporters/tflite/test_exporters_tflite_cli.py -k "tflite_int8_quantization_with_default_dataset" -s --durations=0
40 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_fx.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_FX / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | - 3.9
18 | os:
19 | - ubuntu-20.04
20 | - macos-13
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies
29 | run: |
30 | pip install .[tests]
31 | pip install git+https://github.com/huggingface/transformers.git
32 | pip install -U git+https://github.com/huggingface/evaluate
33 | pip install -U git+https://github.com/huggingface/diffusers
34 | pip install -U git+https://github.com/huggingface/transformers
35 | - name: Test with unittest
36 | working-directory: tests
37 | run: |
38 | python -m pytest fx/optimization/test_transformations.py --exitfirst
39 |
--------------------------------------------------------------------------------
/.github/workflows/test_exporters_slow.yml:
--------------------------------------------------------------------------------
1 | name: Exporters slow / Python - Test
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: 0 7 * * * # every day at 7am
7 |
8 | concurrency:
9 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
10 | cancel-in-progress: true
11 |
12 | jobs:
13 | build:
14 | strategy:
15 | fail-fast: false
16 | matrix:
17 | python-version: [3.8, 3.9]
18 | os: [ubuntu-20.04]
19 |
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies for pytorch export
28 | run: |
29 | pip install .[tests,exporters]
30 | - name: Test with unittest
31 | working-directory: tests
32 | run: |
33 | RUN_SLOW=1 pytest exporters -s -m "not tensorflow_test and run_slow" --durations=0
34 | - name: Install dependencies for tensorflow export
35 | run: |
36 | pip install .[tests,exporters-tf]
37 | - name: Test with unittest
38 | working-directory: tests
39 | run: |
40 | RUN_SLOW=1 pytest exporters -s -m "tensorflow_test and run_slow" --durations=0
41 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_bettertransformer.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_BetterTransformer / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | os:
18 | - ubuntu-20.04
19 | - macos-13
20 | runs-on: ${{ matrix.os }}
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Setup Python ${{ matrix.python-version }}
24 | uses: actions/setup-python@v2
25 | with:
26 | python-version: ${{ matrix.python-version }}
27 | - name: Install dependencies
28 | run: |
29 | pip install .[tests]
30 | pip3 install --upgrade torch torchvision torchaudio
31 | pip install accelerate
32 | pip install -U git+https://github.com/huggingface/evaluate
33 | pip install -U git+https://github.com/huggingface/diffusers
34 | pip install -U git+https://github.com/huggingface/transformers
35 | - name: Test with unittest
36 | working-directory: tests
37 | run: |
38 | python -m unittest discover -s bettertransformer -p test_*.py
39 |
--------------------------------------------------------------------------------
/optimum/exporters/onnx/constants.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 The HuggingFace Team. 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 | # 2 GB
17 | EXTERNAL_DATA_FORMAT_SIZE_LIMIT = 2 * 1024 * 1024 * 1024
18 |
19 | ONNX_ENCODER_NAME = "encoder_model"
20 | ONNX_DECODER_NAME = "decoder_model"
21 | ONNX_DECODER_WITH_PAST_NAME = "decoder_with_past_model"
22 | ONNX_DECODER_MERGED_NAME = "decoder_model_merged"
23 |
24 | UNPICKABLE_ARCHS = [
25 | "encodec",
26 | "hubert",
27 | "sew",
28 | "sew-d",
29 | "speecht5",
30 | "unispeech",
31 | "unispeech-sat",
32 | "wav2vec2",
33 | "wav2vec2-conformer",
34 | "wavlm",
35 | ]
36 |
37 | SDPA_ARCHS_ONNX_EXPORT_NOT_SUPPORTED = [
38 | "bart",
39 | "musicgen",
40 | "whisper",
41 | ]
42 |
--------------------------------------------------------------------------------
/.github/workflows/dev_test_onnxruntime.yml:
--------------------------------------------------------------------------------
1 | # This yml file is autogenerated. Do not edit.
2 |
3 | name: dev_ONNX Runtime / Python - Test
4 | on:
5 | schedule:
6 | - cron: 0 7 * * *
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9 | cancel-in-progress: true
10 | jobs:
11 | build:
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | python-version:
16 | - 3.8
17 | - 3.9
18 | os:
19 | - ubuntu-20.04
20 | - windows-2019
21 | - macos-13
22 | runs-on: ${{ matrix.os }}
23 | steps:
24 | - uses: actions/checkout@v2
25 | - name: Setup Python ${{ matrix.python-version }}
26 | uses: actions/setup-python@v2
27 | with:
28 | python-version: ${{ matrix.python-version }}
29 | - name: Install dependencies
30 | run: |
31 | pip install .[tests,onnxruntime]
32 | pip install -U git+https://github.com/huggingface/evaluate
33 | pip install -U git+https://github.com/huggingface/diffusers
34 | pip install -U git+https://github.com/huggingface/transformers
35 | - name: Test with pytest
36 | working-directory: tests
37 | run: |
38 | python -m pytest -n auto -m "not run_in_series" onnxruntime
39 | python -m pytest -m "run_in_series" onnxruntime
40 |
--------------------------------------------------------------------------------
/tests/cli/cli_with_custom_command.py:
--------------------------------------------------------------------------------
1 | # coding=utf-8
2 | # Copyright 2023 The HuggingFace Team. 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 | import os
17 |
18 | from optimum.commands import BaseOptimumCLICommand, CommandInfo, ExportCommand
19 |
20 |
21 | class MyCustomCommand(BaseOptimumCLICommand):
22 | COMMAND = CommandInfo(name="blablabla", help="Says something.")
23 |
24 | def run(self):
25 | print("If the CI can read this, it means it worked!")
26 |
27 |
28 | parent_command_cls = os.environ.get("TEST_REGISTER_COMMAND_WITH_SUBCOMMAND", None)
29 |
30 | if parent_command_cls == "true":
31 | REGISTER_COMMANDS = [
32 | (MyCustomCommand, ExportCommand),
33 | ]
34 | else:
35 | REGISTER_COMMANDS = [
36 | MyCustomCommand,
37 | ]
38 |
--------------------------------------------------------------------------------
/.github/workflows/test_export_onnx.yml:
--------------------------------------------------------------------------------
1 | name: Exporters ONNX / Python - Test
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | concurrency:
10 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11 | cancel-in-progress: true
12 |
13 | jobs:
14 | build:
15 | strategy:
16 | fail-fast: false
17 | matrix:
18 | python-version: [3.8, 3.9]
19 | os: [ubuntu-20.04]
20 |
21 | runs-on: ${{ matrix.os }}
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Setup Python ${{ matrix.python-version }}
25 | uses: actions/setup-python@v2
26 | with:
27 | python-version: ${{ matrix.python-version }}
28 | - name: Install dependencies for pytorch export
29 | run: |
30 | pip install .[tests,exporters]
31 | - name: Test with unittest
32 | working-directory: tests
33 | run: |
34 | pytest exporters/onnx/test_onnx_*.py -s -n auto -m "not tensorflow_test and not timm_test" --durations=0
35 | - name: Install dependencies for tensorflow export
36 | run: |
37 | pip install .[tests,exporters-tf]
38 | - name: Test with unittest
39 | working-directory: tests
40 | run: |
41 | pytest exporters/onnx/test_onnx_*.py -n auto -m "tensorflow_test" -s --durations=0
42 |
--------------------------------------------------------------------------------
/docs/source/onnxruntime/concept_guides/onnx.mdx:
--------------------------------------------------------------------------------
1 | # ONNX 🤝 ONNX Runtime
2 |
3 | ONNX is an open standard that defines a common set of operators and a common file format to represent deep learning models in a wide variety of frameworks, including PyTorch and TensorFlow. When a model is exported to the ONNX format, these operators are used to construct a computational graph (often called an _intermediate representation_) that represents the flow of data through the neural network.
4 |
5 |
Practical guides to help you achieve a specific goal. Take a look at these guides to learn how to use 🤗 Optimum to solve real-world problems.
10 | 11 |High-level explanations for building a better understanding about important topics such as quantization and graph optimization.
14 | 15 |Technical descriptions of how the ONNX Runtime classes and methods of 🤗 Optimum work.
18 | 19 |Practical guides to help you achieve a specific goal. Take a look at these guides to learn how to use 🤗 Optimum to solve real-world problems.
10 | 11 |High-level explanations for building a better understanding about important topics such as quantization and graph optimization.
14 | 15 |Technical descriptions of how the Torch FX classes and methods of 🤗 Optimum work.
18 | 19 |