Update quality tooling for formatting (#760)

This commit is contained in:
regisss
2023-02-09 14:12:19 +01:00
committed by GitHub
parent 8671303ee8
commit aa7e57b350
92 changed files with 306 additions and 408 deletions
-1
View File
@@ -1,4 +1,3 @@
import shutil
from pathlib import Path
import yaml
+2 -2
View File
@@ -47,7 +47,7 @@ jobs:
run: |
source venv/bin/activate
black --check .
- name: Check style with isort
- name: Check style with ruff
run: |
source venv/bin/activate
isort --check .
ruff .
+1 -1
View File
@@ -51,7 +51,7 @@ If you would like to work on any of the open Issues:
5. Develop the features on your branch.
6. Format your code. Run black and isort so that your newly added files look nice with the following command:
6. Format your code. Run black and ruff so that your newly added files look nice with the following command:
```bash
make style
+2 -2
View File
@@ -23,11 +23,11 @@ REAL_CLONE_URL = $(if $(CLONE_URL),$(CLONE_URL),$(DEFAULT_CLONE_URL))
# Run code quality checks
style_check:
black --check .
isort --check .
ruff .
style:
black .
isort .
ruff . --fix
# Run tests for the library
test:
@@ -25,20 +25,18 @@ import sys
from dataclasses import dataclass, field
from functools import partial
from itertools import chain
from typing import Optional, Union
from typing import Optional
import datasets
import numpy as np
import torch
import transformers
from datasets import load_dataset
from transformers import AutoTokenizer, HfArgumentParser, TrainingArguments
from transformers.tokenization_utils_base import PreTrainedTokenizerBase
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version
from optimum.onnxruntime import ORTModelForMultipleChoice, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig, ORTConfig
from optimum.onnxruntime.configuration import OptimizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -238,7 +236,6 @@ def main():
)
os.makedirs(training_args.output_dir, exist_ok=True)
model_path = os.path.join(training_args.output_dir, "model.onnx")
optimized_model_path = os.path.join(training_args.output_dir, "model_optimized.onnx")
tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name or model_args.model_name_or_path)
@@ -24,23 +24,21 @@ import os
import sys
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import Optional
import datasets
import transformers
from datasets import load_dataset
from evaluate import load
from transformers import AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TrainingArguments
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from evaluate import load
from optimum.onnxruntime import ORTModelForQuestionAnswering, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig, ORTConfig
from optimum.onnxruntime.model import ORTModel
from trainer_qa import QuestionAnsweringTrainer
from utils_qa import postprocess_qa_predictions
from optimum.onnxruntime import ORTModelForQuestionAnswering, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig
from optimum.onnxruntime.model import ORTModel
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.15.0")
@@ -307,7 +305,6 @@ def main():
)
os.makedirs(training_args.output_dir, exist_ok=True)
model_path = os.path.join(training_args.output_dir, "model.onnx")
optimized_model_path = os.path.join(training_args.output_dir, "model_optimized.onnx")
tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name or model_args.model_name_or_path)
@@ -492,7 +489,7 @@ def main():
metrics = compute_metrics(predictions)
# Save metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(metrics, f, indent=4, sort_keys=True)
# Prediction
@@ -527,7 +524,7 @@ def main():
metrics = compute_metrics(predictions)
# Save metrics
with open(os.path.join(training_args.output_dir, f"predict_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "predict_results.json"), "w") as f:
json.dump(metrics, f, indent=4, sort_keys=True)
@@ -23,15 +23,14 @@ import os
import sys
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import Optional
import datasets
import numpy as np
import transformers
from datasets import load_dataset
from evaluate import load
from transformers import (
AutoConfig,
AutoTokenizer,
EvalPrediction,
HfArgumentParser,
@@ -41,9 +40,8 @@ from transformers import (
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from evaluate import load
from optimum.onnxruntime import ORTModelForSequenceClassification, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig, ORTConfig
from optimum.onnxruntime.configuration import OptimizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -395,7 +393,7 @@ def main():
try:
eval_dataset = eval_dataset.align_labels_with_mapping(label2id=model.config.label2id, label_column="label")
except Exception as e:
except Exception:
logger.warning(
f"\nModel label mapping: {model.config.label2id}"
f"\nDataset label features: {eval_dataset.features['label']}"
@@ -418,7 +416,7 @@ def main():
)
outputs = ort_model.evaluation_loop(eval_dataset)
# Save metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
# Prediction
@@ -445,7 +443,7 @@ def main():
predictions = np.squeeze(outputs.predictions) if is_regression else np.argmax(outputs.predictions, axis=1)
# Save predictions
output_predictions_file = os.path.join(training_args.output_dir, f"prediction.txt")
output_predictions_file = os.path.join(training_args.output_dir, "prediction.txt")
with open(output_predictions_file, "w") as writer:
logger.info(f"***** Predict results {data_args.task_name} *****")
writer.write("index\tprediction\n")
@@ -31,13 +31,13 @@ import datasets
import numpy as np
import transformers
from datasets import ClassLabel, load_dataset
from evaluate import load
from transformers import AutoTokenizer, HfArgumentParser, PreTrainedTokenizer, TrainingArguments
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from evaluate import load
from optimum.onnxruntime import ORTModelForTokenClassification, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig, ORTConfig
from optimum.onnxruntime.configuration import OptimizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -276,7 +276,6 @@ def main():
)
os.makedirs(training_args.output_dir, exist_ok=True)
model_path = os.path.join(training_args.output_dir, "model.onnx")
optimized_model_path = os.path.join(training_args.output_dir, "model_optimized.onnx")
tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name or model_args.model_name_or_path)
@@ -357,7 +356,7 @@ def main():
eval_dataset = eval_dataset.align_labels_with_mapping(
label2id=model.config.label2id, label_column=label_column_name
)
except Exception as e:
except Exception:
logger.warning(
f"\nModel label mapping: {model.config.label2id}"
f"\nDataset label features: {eval_dataset.features[label_column_name]}"
@@ -489,7 +488,7 @@ def main():
outputs = ort_model.evaluation_loop(eval_dataset)
# Save evaluation metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
# Prediction
@@ -525,7 +524,7 @@ def main():
]
# Save test metrics
with open(os.path.join(training_args.output_dir, f"predict_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "predict_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
# Save predictions
@@ -30,13 +30,12 @@ import numpy as np
import torch
import transformers
from datasets import load_dataset
from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor
from transformers import AutoFeatureExtractor, EvalPrediction, HfArgumentParser, TrainingArguments
from transformers.onnx import FeaturesManager
from transformers.utils.versions import require_version
from evaluate import load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor
from transformers import AutoFeatureExtractor, EvalPrediction, HfArgumentParser, TrainingArguments
from transformers.utils.versions import require_version
from optimum.onnxruntime import ORTQuantizer
from optimum.onnxruntime.configuration import AutoCalibrationConfig, QuantizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -399,7 +398,7 @@ def main():
eval_dataset = eval_dataset.align_labels_with_mapping(
label2id=model.config.label2id, label_column=labels_column
)
except Exception as e:
except Exception:
logger.warning(
f"\nModel label mapping: {model.config.label2id}"
f"\nDataset label features: {eval_dataset.features[labels_column]}"
@@ -418,7 +417,7 @@ def main():
)
outputs = ort_model.evaluation_loop(eval_dataset)
# Save metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
@@ -25,19 +25,17 @@ import sys
from dataclasses import dataclass, field
from functools import partial
from itertools import chain
from typing import Optional, Union
from typing import Optional
import datasets
import numpy as np
import torch
import transformers
from datasets import load_dataset
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from transformers import AutoTokenizer, HfArgumentParser, TrainingArguments
from transformers.tokenization_utils_base import PreTrainedTokenizerBase
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from optimum.onnxruntime import ORTModelForMultipleChoice, ORTQuantizer
from optimum.onnxruntime.configuration import AutoCalibrationConfig, QuantizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -30,13 +30,13 @@ from typing import Optional
import datasets
import transformers
from datasets import load_dataset
from transformers import AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TrainingArguments
from transformers.onnx import FeaturesManager
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from evaluate import load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from transformers import AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TrainingArguments
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from utils_qa import postprocess_qa_predictions
from optimum.onnxruntime import ORTQuantizer
from optimum.onnxruntime.configuration import AutoCalibrationConfig, QuantizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -48,8 +48,6 @@ from optimum.onnxruntime.preprocessors.passes import (
ExcludeNodeAfter,
ExcludeNodeFollowedBy,
)
from trainer_qa import QuestionAnsweringTrainer
from utils_qa import postprocess_qa_predictions
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
@@ -676,7 +674,7 @@ def main():
metrics = compute_metrics(predictions)
# Save metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(metrics, f, indent=4, sort_keys=True)
# Prediction
@@ -693,7 +691,7 @@ def main():
metrics = compute_metrics(predictions)
# Save metrics
with open(os.path.join(training_args.output_dir, f"predict_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "predict_results.json"), "w") as f:
json.dump(metrics, f, indent=4, sort_keys=True)
@@ -30,20 +30,18 @@ import datasets
import numpy as np
import transformers
from datasets import load_dataset
from evaluate import load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from transformers import (
AutoConfig,
AutoTokenizer,
EvalPrediction,
HfArgumentParser,
PreTrainedTokenizer,
TrainingArguments,
)
from transformers.onnx import FeaturesManager
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from evaluate import load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from optimum.onnxruntime import ORTQuantizer
from optimum.onnxruntime.configuration import AutoCalibrationConfig, QuantizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -498,9 +496,9 @@ def main():
try:
eval_dataset = eval_dataset.align_labels_with_mapping(label2id=model.config.label2id, label_column="label")
except Exception as e:
except Exception:
logger.warning(
f"\nModel label mapping: {onnx_model.config.label2id}"
f"\nModel label mapping: {model.config.label2id}"
f"\nDataset label features: {eval_dataset.features['label']}"
f"\nCould not guarantee the model label mapping and the dataset labels match."
f" Evaluation results may suffer from a wrong matching."
@@ -514,7 +512,7 @@ def main():
)
outputs = ort_model.evaluation_loop(eval_dataset)
# Save metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
# Prediction
@@ -536,7 +534,7 @@ def main():
predictions = np.squeeze(outputs.predictions) if is_regression else np.argmax(outputs.predictions, axis=1)
# Save predictions
output_predictions_file = os.path.join(training_args.output_dir, f"prediction.txt")
output_predictions_file = os.path.join(training_args.output_dir, "prediction.txt")
with open(output_predictions_file, "w") as writer:
logger.info(f"***** Predict results {data_args.task_name} *****")
writer.write("index\tprediction\n")
@@ -32,13 +32,12 @@ import datasets
import numpy as np
import transformers
from datasets import ClassLabel, load_dataset
from evaluate import load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from transformers import AutoTokenizer, HfArgumentParser, PretrainedConfig, PreTrainedTokenizer, TrainingArguments
from transformers.onnx import FeaturesManager
from transformers.utils import check_min_version
from transformers.utils.versions import require_version
from evaluate import load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from optimum.onnxruntime import ORTQuantizer
from optimum.onnxruntime.configuration import AutoCalibrationConfig, QuantizationConfig
from optimum.onnxruntime.model import ORTModel
@@ -354,7 +353,7 @@ def main():
label2id=label2id,
label_column=label_column_name,
)
except Exception as e:
except Exception:
logger.warning(
f"\nModel label mapping: {label2id}"
f"\nDataset label features: {eval_dataset.features[label_column_name]}"
@@ -581,7 +580,7 @@ def main():
outputs = ort_model.evaluation_loop(eval_dataset)
# Save evaluation metrics
with open(os.path.join(training_args.output_dir, f"eval_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "eval_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
# Prediction
@@ -618,7 +617,7 @@ def main():
]
# Save test metrics
with open(os.path.join(training_args.output_dir, f"predict_results.json"), "w") as f:
with open(os.path.join(training_args.output_dir, "predict_results.json"), "w") as f:
json.dump(outputs.metrics, f, indent=4, sort_keys=True)
# Save predictions
@@ -19,6 +19,7 @@ import sys
from dataclasses import dataclass, field
from typing import Optional
import evaluate
import numpy as np
import torch
import transformers
@@ -45,7 +46,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime import ORTTrainer, ORTTrainingArguments
@@ -29,6 +29,7 @@ from itertools import chain
from typing import Optional
import datasets
import evaluate
import transformers
from datasets import load_dataset
from transformers import (
@@ -47,7 +48,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime import ORTTrainer, ORTTrainingArguments
@@ -30,6 +30,7 @@ from itertools import chain
from typing import Optional
import datasets
import evaluate
import transformers
from datasets import load_dataset
from transformers import (
@@ -47,7 +48,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime import ORTTrainer, ORTTrainingArguments
@@ -25,8 +25,10 @@ from dataclasses import dataclass, field
from typing import Optional
import datasets
import evaluate
import transformers
from datasets import load_dataset
from trainer_qa import QuestionAnsweringORTTrainer
from transformers import (
AutoConfig,
AutoModelForQuestionAnswering,
@@ -41,12 +43,10 @@ from transformers import (
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime.training_args import ORTTrainingArguments
from trainer_qa import QuestionAnsweringORTTrainer
from utils_qa import postprocess_qa_predictions
from optimum.onnxruntime.training_args import ORTTrainingArguments
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.23.0")
@@ -25,10 +25,12 @@ from dataclasses import dataclass, field
from typing import Optional
import datasets
import evaluate
import nltk # Here to have a nice missing dependency error message early on
import numpy as np
import transformers
from datasets import load_dataset
from filelock import FileLock
from transformers import (
AutoConfig,
AutoModelForSeq2SeqLM,
@@ -45,8 +47,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, is_offline_mode, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from filelock import FileLock
from optimum.onnxruntime import ORTSeq2SeqTrainer, ORTSeq2SeqTrainingArguments
@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import json
import logging
import os
@@ -23,7 +22,7 @@ from unittest.mock import patch
import torch
from transformers.file_utils import is_apex_available
from transformers.testing_utils import CaptureLogger, TestCasePlus, get_gpu_count, slow, torch_device
from transformers.testing_utils import TestCasePlus, get_gpu_count, slow, torch_device
SRC_DIRS = [
@@ -64,7 +63,6 @@ def is_cuda_and_apex_available():
class ExamplesTests(TestCasePlus):
# Text Classification Tests
def test_run_glue(self):
stream_handler = logging.StreamHandler(sys.stdout)
@@ -25,6 +25,7 @@ from dataclasses import dataclass, field
from typing import Optional
import datasets
import evaluate
import numpy as np
import transformers
from datasets import load_dataset
@@ -43,7 +44,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime import ORTTrainer, ORTTrainingArguments
@@ -26,6 +26,7 @@ from dataclasses import dataclass, field
from typing import Optional
import datasets
import evaluate
import numpy as np
import transformers
from datasets import ClassLabel, load_dataset
@@ -43,7 +44,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime import ORTTrainer
from optimum.onnxruntime.training_args import ORTTrainingArguments
@@ -25,6 +25,7 @@ from dataclasses import dataclass, field
from typing import Optional
import datasets
import evaluate
import numpy as np
import transformers
from datasets import load_dataset
@@ -46,7 +47,6 @@ from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
import evaluate
from optimum.onnxruntime import ORTSeq2SeqTrainer
from optimum.onnxruntime.training_args_seq2seq import ORTSeq2SeqTrainingArguments
+1 -1
View File
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from copy import deepcopy
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
+1 -1
View File
@@ -17,7 +17,7 @@ from typing import Dict, Optional
import torch
from ..utils import check_if_pytorch_greater, is_accelerate_available
from .models import BetterTransformerManager, warn_uncompatible_save
from .models import BetterTransformerManager
if is_accelerate_available():
+1 -2
View File
@@ -15,11 +15,10 @@
import platform
from argparse import ArgumentParser
import huggingface_hub
from transformers import __version__ as transformers_version
from transformers.utils import is_tf_available, is_torch_available
import huggingface_hub
from ..version import __version__ as version
from . import BaseOptimumCLICommand
-1
View File
@@ -1,4 +1,3 @@
from argparse import ArgumentParser
from pathlib import Path
from ...onnxruntime.configuration import AutoQuantizationConfig, ORTConfig
-1
View File
@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from argparse import ArgumentParser
from .env import EnvironmentCommand
-1
View File
@@ -15,7 +15,6 @@
"""Entry point to the optimum.exporters.onnx command line."""
from argparse import ArgumentParser
from pathlib import Path
from transformers import AutoTokenizer
+2 -7
View File
@@ -27,14 +27,12 @@ from abc import ABC, abstractmethod
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Mapping, Optional, Union
from transformers.utils import is_torch_available
import onnx
from onnxruntime import InferenceSession
from transformers.utils import is_torch_available
from ...utils import DEFAULT_DUMMY_SHAPES
from ...utils import DEFAULT_DUMMY_SHAPES, DummyInputGenerator, DummyTrainingLabelsInputGenerator, logging
from ...utils import TORCH_MINIMUM_VERSION as GLOBAL_MIN_TORCH_VERSION
from ...utils import DummyInputGenerator, DummyTrainingLabelsInputGenerator, logging
from ...utils.doc import add_dynamic_docstring
from ..base import ExportConfig
@@ -303,8 +301,6 @@ class OnnxConfig(ExportConfig, ABC):
for output in self.outputs.values():
allowed_dynamic_axes |= set(output.values())
from onnxruntime.capi import _ld_preload
if os.environ.get("ORT_CUDA_UNAVAILABLE", "0") == "1":
providers = ["CPUExecutionProvider"]
else:
@@ -770,7 +766,6 @@ class OnnxConfigWithLoss(OnnxConfig, ABC):
"end_positions": {0: "batch_size"},
},
"image-classification": {"labels": {0: "batch_size"}},
"seq2seq-lm": {"labels": {0: "batch_size", 1: "sequence_length"}},
}
_tasks_to_extra_outputs = {
"default": OrderedDict({"loss": {}}),
-1
View File
@@ -33,7 +33,6 @@ if TYPE_CHECKING:
from transformers import PretrainedConfig
from ...utils import DummyInputGenerator
from .base import PatchingSpec
logger = logging.get_logger(__name__)
+2 -3
View File
@@ -21,9 +21,8 @@ from pathlib import Path
from typing import Dict, Iterable, List, Optional, Tuple, Union
import numpy as np
from transformers.utils import is_tf_available, is_torch_available
import onnx
from transformers.utils import is_tf_available, is_torch_available
from ...onnx.utils import _get_onnx_external_data_tensors, check_model_uses_external_data
from ...utils import TORCH_MINIMUM_VERSION, is_diffusers_available, is_torch_onnx_support_available, logging
@@ -33,6 +32,7 @@ from .utils import recursive_to_device
if is_torch_available():
import torch
import torch.nn as nn
from transformers.modeling_utils import PreTrainedModel
from transformers.pytorch_utils import is_torch_less_than_1_11
@@ -354,7 +354,6 @@ def export_pytorch(
`Tuple[List[str], List[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from
the ONNX configuration.
"""
import torch
from torch.onnx import export as onnx_export
from torch.utils._pytree import tree_map
+1 -1
View File
@@ -21,7 +21,7 @@ import packaging
import torch
from transformers.utils import is_tf_available, is_torch_available
from ...utils import ORT_QUANTIZE_MINIMUM_VERSION, TORCH_MINIMUM_VERSION, is_diffusers_available
from ...utils import ORT_QUANTIZE_MINIMUM_VERSION, is_diffusers_available
from ..tasks import TasksManager
+2 -3
View File
@@ -20,11 +20,10 @@ from functools import partial
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, Union
import huggingface_hub
from transformers import PretrainedConfig, is_tf_available, is_torch_available
from transformers.utils import TF2_WEIGHTS_NAME, WEIGHTS_NAME, logging
import huggingface_hub
if TYPE_CHECKING:
from transformers import PreTrainedModel, TFPreTrainedModel
@@ -1017,7 +1016,7 @@ class TasksManager:
revision: Optional[str] = None,
framework: Optional[str] = None,
cache_dir: Optional[str] = None,
**model_kwargs
**model_kwargs,
) -> Union["PreTrainedModel", "TFPreTrainedModel"]:
"""
Retrieves a model from its name and the task to be enabled.
-1
View File
@@ -113,7 +113,6 @@ class TFLiteConfig(ExportConfig, ABC):
nb_max_frames: Optional[int] = None,
audio_sequence_length: Optional[int] = None,
):
self._config = config
self._normalized_config = self.NORMALIZED_CONFIG_CLASS(self._config)
self.mandatory_axes = ()
+1 -3
View File
@@ -122,9 +122,7 @@ def validate_model_outputs(
if shape_failures:
msg = "\n".join(f"- {t[0]}: got {t[1]} (reference) and {t[2]} (TFLite)" for t in shape_failures)
raise ShapeError(
f"Output shapes do not match between reference model and the TFLite exported model:\n" "{msg}"
)
raise ShapeError("Output shapes do not match between reference model and the TFLite exported model:\n" "{msg}")
if value_failures:
msg = "\n".join(f"- {t[0]}: max diff = {t[1]}" for t in value_failures)
+1 -2
View File
@@ -21,9 +21,8 @@ from abc import ABC, abstractmethod
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Union
from transformers import AutoConfig, add_start_docstrings
from huggingface_hub import HfApi, HfFolder
from transformers import AutoConfig, add_start_docstrings
from .utils import CONFIG_NAME
-1
View File
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional
+3 -5
View File
@@ -18,11 +18,6 @@ import onnx
from onnx import ModelProto
from ..utils import logging
logger = logging.get_logger()
from .transformations_utils import (
_create_name_sharing_dict,
_deduplicated_cross_model_initializers,
@@ -36,6 +31,9 @@ from .transformations_utils import (
)
logger = logging.get_logger()
def remove_duplicate_weights(model: ModelProto, inplace: bool = False) -> ModelProto:
"""
Finds and removes duplicate weights in a model by keeping only unique weights, and make the duplicate values point
+2 -2
View File
@@ -98,7 +98,7 @@ def _infer_output_shape(output: ValueInfoProto):
elif getattr(dim, "dim_value"):
output_shape.append(getattr(dim, "dim_value"))
else:
raise ValueError(f"Cannot find `dim_param` nor `dim_value` in the output dimension info.")
raise ValueError("Cannot find `dim_param` nor `dim_value` in the output dimension info.")
return output_shape
@@ -146,7 +146,7 @@ def _unify_onnx_outputs(model1: ModelProto, model2: ModelProto):
model_output_1 == model_output_2
for model_output_1, model_output_2 in zip(model1.graph.output, model2.graph.output)
):
raise RuntimeError(f"Failed to unify outputs of given ONNX model protos.")
raise RuntimeError("Failed to unify outputs of given ONNX model protos.")
def _get_all_inputs(model_list: List[ModelProto]) -> List[onnx.onnx_ml_pb2.ValueInfoProto]:
+5 -1
View File
@@ -14,7 +14,7 @@
"""Defines the base classes that are used to perform inference with ONNX Runtime of Transformers models."""
from abc import abstractmethod
from typing import Dict, List, Optional, Tuple
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
import torch
from transformers.modeling_outputs import BaseModelOutput, CausalLMOutputWithCrossAttentions, Seq2SeqLMOutput
@@ -24,6 +24,10 @@ from onnxruntime import InferenceSession
from ..utils import NormalizedConfigManager
if TYPE_CHECKING:
from .modeling_ort import ORTModel
class ORTModelPart:
"""
For multi-file ONNX models, such as encoder-decoder models, represents a part of the model.
-1
View File
@@ -90,7 +90,6 @@ class CalibrationConfig:
force_symmetric_range: bool = False,
augmented_model_name: str = "augmented_model.onnx",
) -> CalibraterBase:
kwargs = {
"model": onnx_model_path,
"op_types_to_calibrate": operators_to_quantize or [],
@@ -102,7 +102,7 @@ class IOBindingHelper:
else:
try:
return IOBindingHelper.to_pytorch_via_cupy(ort_value)
except Exception as e:
except Exception:
logging.error(traceback.format_exc())
logging.info("Unable to access output memory in CUDA, will offload to CPU")
return IOBindingHelper.to_pytorch_via_numpy(ort_value)
+2 -5
View File
@@ -14,18 +14,15 @@
import logging
import os
from pathlib import Path
from typing import Callable, Dict, List, Optional, Union
import numpy as np
from datasets import Dataset
from transformers import EvalPrediction
from transformers.onnx import OnnxConfig
from transformers.trainer_pt_utils import nested_concat
from transformers.trainer_utils import EvalLoopOutput
import onnx
from onnxruntime import InferenceSession, SessionOptions
from onnxruntime import InferenceSession
logger = logging.getLogger(__name__)
@@ -65,7 +62,7 @@ class ORTModel:
dataset (`datasets.Dataset`):
Dataset to use for the evaluation step.
"""
logger.info(f"***** Running evaluation *****")
logger.info("***** Running evaluation *****")
all_preds = None
all_labels = None
for step, inputs in enumerate(dataset):
+4 -5
View File
@@ -20,13 +20,13 @@ from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
import torch
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from transformers import AutoModelForCausalLM, GenerationConfig
from transformers.file_utils import add_start_docstrings_to_model_forward
from transformers.modeling_outputs import CausalLMOutputWithCrossAttentions
import onnxruntime
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from ..exporters import TasksManager
from ..exporters.onnx import export_models, get_decoder_models_for_export
@@ -129,7 +129,7 @@ class ORTModelDecoder(ORTModel):
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
preprocessors: Optional[List] = None,
generation_config: Optional[GenerationConfig] = None,
**kwargs
**kwargs,
):
"""
Args:
@@ -374,7 +374,7 @@ class ORTModelDecoder(ORTModel):
# try download external data
try:
model_data_cache_path = hf_hub_download(
hf_hub_download(
repo_id=model_id,
subfolder=subfolder,
filename=filename + "_data",
@@ -546,7 +546,6 @@ class ORTModelForCausalLM(ORTModelDecoder, GenerationMixin):
labels: Optional[torch.LongTensor] = None,
**kwargs,
) -> CausalLMOutputWithCrossAttentions:
if past_key_values is None or self.decoder_with_past is None:
outputs = self.decoder(
input_ids=input_ids,
+5 -5
View File
@@ -19,10 +19,12 @@ import shutil
from inspect import signature
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Literal, Optional, Set, Tuple, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple, Union
import numpy as np
import torch
from huggingface_hub import HfApi, HfFolder, hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from transformers import (
AutoConfig,
AutoModel,
@@ -48,8 +50,6 @@ from transformers.modeling_outputs import (
)
import onnxruntime as ort
from huggingface_hub import HfApi, HfFolder, hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from ..exporters import TasksManager
from ..exporters.onnx import export
@@ -483,7 +483,7 @@ class ORTModel(OptimizedModel):
# try download external data
try:
model_data_cache_path = hf_hub_download(
hf_hub_download(
repo_id=model_id,
subfolder=subfolder,
filename=file_name + "_data",
@@ -678,7 +678,7 @@ class ORTModel(OptimizedModel):
*model_inputs: torch.Tensor,
known_output_shapes: Optional[Dict[str, Tuple[int]]] = None,
forward_function: Optional[Callable[..., Any]] = None,
outputs_to_not_bind: Optional[Union[Set[str], str]] = None
outputs_to_not_bind: Optional[Union[Set[str], str]] = None,
) -> Tuple[ort.IOBinding, Dict[str, Tuple[int]], Dict[str, torch.Tensor]]:
"""
Prepares IO binding for ONNX Runtime.
+8 -14
View File
@@ -24,13 +24,12 @@ from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
import torch
from huggingface_hub import hf_hub_download
from transformers import AutoModelForSeq2SeqLM, AutoModelForSpeechSeq2Seq, AutoModelForVision2Seq, GenerationConfig
from transformers.file_utils import add_start_docstrings_to_model_forward
from transformers.modeling_outputs import BaseModelOutput, Seq2SeqLMOutput
import onnxruntime as ort
from huggingface_hub import hf_hub_download
from onnxruntime import InferenceSession
from ..exporters.onnx import export_models, get_encoder_decoder_models_for_export
from ..exporters.tasks import TasksManager
@@ -39,7 +38,7 @@ from ..utils import check_if_transformers_greater
from ..utils.file_utils import validate_file_exists
from ..utils.normalized_config import NormalizedConfigManager
from ..utils.save_utils import maybe_load_preprocessors, maybe_save_preprocessors
from .base import ORTDecoder, ORTDecoderForSeq2Seq, ORTEncoder
from .base import ORTDecoderForSeq2Seq, ORTEncoder
from .modeling_ort import ORTModel
from .utils import (
ONNX_DECODER_NAME,
@@ -414,6 +413,7 @@ class ORTModelForConditionalGeneration(ORTModel, ABC):
The generation configuration used by default when calling `generate()`.
Refer to https://huggingface.co/docs/transformers/main/en/main_classes/text_generation#transformers.GenerationMixin.generate.
"""
# TODO: remove at version 2.0
def show_deprecated_argument(arg_name):
if kwargs.pop(arg_name, None) is not None:
@@ -685,7 +685,7 @@ class ORTModelForConditionalGeneration(ORTModel, ABC):
)
# try download external data
try:
model_data_cache_path = hf_hub_download(
hf_hub_download(
repo_id=model_id,
subfolder=subfolder,
filename=filename + "_data",
@@ -871,7 +871,6 @@ class ORTModelForSeq2SeqLM(ORTModelForConditionalGeneration, GenerationMixin):
labels: Optional[torch.LongTensor] = None,
**kwargs,
) -> Seq2SeqLMOutput:
# Encode if needed : first prediction pass
if encoder_outputs is None:
encoder_outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
@@ -909,9 +908,8 @@ class ORTModelForSeq2SeqLM(ORTModelForConditionalGeneration, GenerationMixin):
cross_attn_head_mask=None,
use_cache=None,
encoder_outputs=None,
**kwargs
**kwargs,
) -> Dict:
return {
"decoder_input_ids": input_ids,
"past_key_values": past_key_values,
@@ -980,7 +978,6 @@ class ORTModelForSpeechSeq2Seq(ORTModelForConditionalGeneration, GenerationMixin
labels: Optional[torch.LongTensor] = None,
**kwargs,
) -> Seq2SeqLMOutput:
# Encode if needed : first prediction pass
if encoder_outputs is None:
encoder_outputs = self.encoder(input_features=input_features)
@@ -1015,9 +1012,8 @@ class ORTModelForSpeechSeq2Seq(ORTModelForConditionalGeneration, GenerationMixin
cross_attn_head_mask=None,
use_cache=None,
encoder_outputs=None,
**kwargs
**kwargs,
) -> Dict:
return {
"decoder_input_ids": input_ids,
"past_key_values": past_key_values,
@@ -1065,7 +1061,7 @@ class ORTModelForVision2Seq(ORTModelForConditionalGeneration, GenerationMixin):
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
preprocessors: Optional[List] = None,
generation_config: Optional[GenerationConfig] = None,
**kwargs
**kwargs,
):
super().__init__(
encoder_session,
@@ -1108,7 +1104,6 @@ class ORTModelForVision2Seq(ORTModelForConditionalGeneration, GenerationMixin):
labels: Optional[torch.LongTensor] = None,
**kwargs,
) -> Seq2SeqLMOutput:
# Encode if needed : first prediction pass
if encoder_outputs is None:
encoder_outputs = self.encoder(pixel_values=pixel_values)
@@ -1143,9 +1138,8 @@ class ORTModelForVision2Seq(ORTModelForConditionalGeneration, GenerationMixin):
cross_attn_head_mask=None,
use_cache=None,
encoder_outputs=None,
**kwargs
**kwargs,
) -> Dict:
return {
"decoder_input_ids": input_ids,
"past_key_values": past_key_values,
+1 -1
View File
@@ -18,9 +18,9 @@ import os
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Union
from onnx import load_model
from transformers.models.auto.configuration_auto import AutoConfig
from onnx import load_model
from onnxruntime.transformers.onnx_model_bert import BertOnnxModel
from onnxruntime.transformers.optimizer import optimize_model
@@ -14,6 +14,7 @@
from typing import Set, Tuple
from onnx import ModelProto
from onnxruntime.transformers.onnx_model import OnnxModel
from .. import PreprocessorPass
@@ -14,6 +14,7 @@
from typing import Set, Tuple
from onnx import ModelProto
from onnxruntime.transformers.onnx_model import OnnxModel
from .. import PreprocessorPass
@@ -14,6 +14,7 @@
from typing import Set, Tuple
from onnx import ModelProto
from onnxruntime.transformers.onnx_model import OnnxModel
from .. import PreprocessorPass
@@ -14,6 +14,7 @@
from typing import Set, Tuple
from onnx import ModelProto
from onnxruntime.transformers.onnx_model import OnnxModel
from .. import PreprocessorPass
@@ -18,6 +18,7 @@ from pathlib import Path
from typing import Optional, Set, Tuple, Union
from onnx import ModelProto, load_model
from onnxruntime.transformers.onnx_model import OnnxModel
+1 -1
View File
@@ -19,11 +19,11 @@ from collections import defaultdict
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union
import onnx
from datasets import Dataset, load_dataset
from packaging.version import Version, parse
from transformers import AutoConfig
import onnx
from onnxruntime import __version__ as ort_version
from onnxruntime.quantization import CalibrationDataReader, QuantFormat, QuantizationMode, QuantType
from onnxruntime.quantization.onnx_quantizer import ONNXQuantizer
+7 -14
View File
@@ -24,29 +24,28 @@ import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, Union
from tqdm.auto import tqdm
# Integrations must be imported before ML frameworks:
from transformers.integrations import ( # isort: split
# isort: off
from transformers.integrations import (
hp_params,
is_fairscale_available,
)
# isort: on
import numpy as np
import torch
import torch.distributed as dist
from torch import nn
from torch.utils.data import DataLoader, Dataset, RandomSampler
from torch.utils.data.distributed import DistributedSampler
from transformers import PreTrainedModel, __version__
from tqdm.auto import tqdm
from transformers.data.data_collator import DataCollator
from transformers.debug_utils import DebugOption, DebugUnderflowOverflow
from transformers.deepspeed import deepspeed_init, is_deepspeed_zero3_enabled
from transformers.dependency_versions_check import dep_version_check
from transformers.file_utils import (
CONFIG_NAME,
WEIGHTS_NAME,
is_apex_available,
is_sagemaker_dp_enabled,
is_sagemaker_mp_enabled,
@@ -118,7 +117,6 @@ if is_apex_available():
if is_fairscale_available():
dep_version_check("fairscale")
import fairscale
from fairscale.nn.data_parallel import ShardedDataParallel as ShardedDDP
from fairscale.optim import OSS
@@ -267,7 +265,6 @@ class ORTTrainer(Trainer):
preprocess_logits_for_metrics: Optional[Callable[[torch.Tensor, torch.Tensor], torch.Tensor]] = None,
onnx_model_path: Union[str, os.PathLike] = None,
):
super().__init__(
model=model,
args=args,
@@ -415,7 +412,6 @@ class ORTTrainer(Trainer):
num_train_epochs = math.ceil(args.num_train_epochs)
num_train_samples = self.num_examples(train_dataloader) * args.num_train_epochs
elif args.max_steps > 0: # Rely on max_steps when dataloader does not have a working size
max_steps = args.max_steps
# Setting a very large number of epochs so we go as many times as necessary over the iterator.
num_train_epochs = sys.maxsize
@@ -619,7 +615,6 @@ class ORTTrainer(Trainer):
step = -1
for step, inputs in enumerate(train_dataloader):
# Skip past any already trained steps if resuming training
if steps_trained_in_current_epoch > 0:
steps_trained_in_current_epoch -= 1
@@ -951,7 +946,6 @@ class ORTTrainer(Trainer):
ignore_keys: Optional[List[str]] = None,
metric_key_prefix: str = "eval",
) -> EvalLoopOutput:
"""
Prediction/evaluation loop, shared by `ORTTrainer.evaluate()` and `ORTTrainer.predict()`.
@@ -1559,7 +1553,7 @@ class ORTTrainer(Trainer):
if self.use_apex and training:
model, self.optimizer = amp.initialize(model, self.optimizer, opt_level=self.args.fp16_opt_level)
if args.fp16:
if self.args.fp16:
from onnxruntime.training.optim.fp16_optimizer import FP16_Optimizer
self.optimizer = FP16_Optimizer(self.optimizer)
@@ -1592,9 +1586,8 @@ class ORTTrainer(Trainer):
# Distributed training using PyTorch FSDP
elif self.fsdp is not None:
# PyTorch FSDP!
from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload
from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload, MixedPrecision
from torch.distributed.fsdp.fully_sharded_data_parallel import FullyShardedDataParallel as FSDP
from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision
from torch.distributed.fsdp.wrap import size_based_auto_wrap_policy, transformer_auto_wrap_policy
if FSDPOption.OFFLOAD in self.args.fsdp:
+2 -8
View File
@@ -19,7 +19,6 @@ from typing import Any, Dict, List, Optional, Tuple, Union
import numpy as np
import torch
from packaging import version
from torch import nn
from torch.utils.data import DataLoader, Dataset
from transformers.deepspeed import is_deepspeed_zero3_enabled
@@ -50,10 +49,6 @@ from .trainer import ORTTrainer
from .utils import ONNX_DECODER_NAME, ONNX_DECODER_WITH_PAST_NAME, ONNX_ENCODER_NAME, wrap_onnx_config_for_loss
if version.parse(torch.__version__) >= version.parse("1.8"):
from torch.cuda.amp import autocast
logger = logging.get_logger(__name__)
@@ -64,7 +59,7 @@ class ORTSeq2SeqTrainer(ORTTrainer):
ignore_keys: Optional[List[str]] = None,
metric_key_prefix: str = "eval",
inference_with_ort: bool = False,
**gen_kwargs
**gen_kwargs,
) -> Dict[str, float]:
"""
Run evaluation with ONNX Runtime or PyTorch backend and returns metrics.
@@ -107,7 +102,7 @@ class ORTSeq2SeqTrainer(ORTTrainer):
ignore_keys: Optional[List[str]] = None,
metric_key_prefix: str = "eval",
inference_with_ort: bool = False,
**gen_kwargs
**gen_kwargs,
) -> PredictionOutput:
"""
Run prediction and returns predictions and potential metrics.
@@ -165,7 +160,6 @@ class ORTSeq2SeqTrainer(ORTTrainer):
ignore_keys: Optional[List[str]] = None,
metric_key_prefix: str = "eval",
) -> EvalLoopOutput:
"""
Prediction/evaluation loop, shared by `ORTTrainer.evaluate()` and `ORTTrainer.predict()`.
+4 -5
View File
@@ -16,7 +16,7 @@ import os
import warnings
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
from typing import Optional
from transformers import TrainingArguments
from transformers.debug_utils import DebugOption
@@ -28,13 +28,13 @@ from transformers.trainer_utils import (
SchedulerType,
ShardedDDPOption,
)
from transformers.training_args import OptimizerNames, default_logdir, logger, trainer_log_levels
from transformers.training_args import OptimizerNames, default_logdir, logger
from transformers.utils import (
ExplicitEnum,
add_start_docstrings,
get_full_repo_name,
is_torch_available,
is_torch_bf16_available,
is_torch_bf16_cpu_available,
is_torch_bf16_gpu_available,
is_torch_tf32_available,
logging,
)
@@ -148,7 +148,6 @@ class ORTTrainingArguments(TrainingArguments):
self.half_precision_backend = self.fp16_backend
if self.bf16 or self.bf16_full_eval:
if self.no_cuda and not is_torch_bf16_cpu_available():
# cpu
raise ValueError("Your setup doesn't support bf16/cpu. You need torch>=1.10")
@@ -16,7 +16,6 @@ from dataclasses import dataclass, field
from typing import Optional
from transformers import Seq2SeqTrainingArguments
from transformers.utils import add_start_docstrings
from .training_args import ORTTrainingArguments
-2
View File
@@ -198,8 +198,6 @@ def validate_provider_availability(provider: str):
f"Asked to use {provider}, but `onnxruntime-gpu` package was not found. Make sure to install `onnxruntime-gpu` package instead of `onnxruntime`."
)
from onnxruntime.capi import _ld_preload
if provider == "CUDAExecutionProvider":
if os.environ.get("ORT_CUDA_UNAVAILABLE", "0") == "1":
raise ImportError(
+2 -2
View File
@@ -173,7 +173,7 @@ def load_bettertransformer(
use_auth_token: Optional[Union[bool, str]] = None,
revision: str = "main",
model_kwargs: Optional[Dict[str, Any]] = None,
**kwargs
**kwargs,
):
if model_kwargs is None:
model_kwargs = {}
@@ -207,7 +207,7 @@ def load_ort_pipeline(
use_auth_token: Optional[Union[bool, str]] = None,
revision: str = "main",
model_kwargs: Optional[Dict[str, Any]] = None,
**kwargs
**kwargs,
):
if model_kwargs is None:
model_kwargs = {}
-1
View File
@@ -1,5 +1,4 @@
import logging
import os
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Optional, Union
+1 -2
View File
@@ -5,13 +5,12 @@ from time import perf_counter_ns
from typing import Set
import numpy as np
import optuna
import torch
import transformers
from datasets import Dataset
from tqdm import trange
import optuna
from . import version as optimum_version
from .utils.preprocessing import (
ImageClassificationProcessing,
+1 -1
View File
@@ -66,7 +66,7 @@ def is_onnxruntime_available():
# will be set to `True` even if not installed.
mod = importlib.import_module("onnxruntime")
inspect.getsourcefile(mod)
except:
except Exception:
return False
return _onnxruntime_available
+10 -12
View File
@@ -19,14 +19,16 @@ import logging
import os
import sys
import threading
from logging import CRITICAL # NOQA
from logging import DEBUG # NOQA
from logging import ERROR # NOQA
from logging import FATAL # NOQA
from logging import INFO # NOQA
from logging import NOTSET # NOQA
from logging import WARN # NOQA
from logging import WARNING # NOQA
from logging import (
CRITICAL, # NOQA
DEBUG, # NOQA
ERROR, # NOQA
FATAL, # NOQA
INFO, # NOQA
NOTSET, # NOQA
WARN, # NOQA
WARNING, # NOQA
)
from typing import Optional
@@ -62,17 +64,14 @@ def _get_default_logging_level():
def _get_library_name() -> str:
return __name__.split(".")[0]
def _get_library_root_logger() -> logging.Logger:
return logging.getLogger(_get_library_name())
def _configure_library_root_logger() -> None:
global _default_handler
with _lock:
@@ -90,7 +89,6 @@ def _configure_library_root_logger() -> None:
def _reset_library_root_logger() -> None:
global _default_handler
with _lock:
@@ -1,13 +1,12 @@
from functools import partial
from typing import Dict, List
from typing import List
import torch
from datasets import Dataset, load_dataset
from evaluate import Metric, combine, evaluator
from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor
from transformers import FeatureExtractionMixin, ImageClassificationPipeline
from evaluate import Metric, combine, evaluator
from .base import DatasetProcessing
@@ -53,7 +52,7 @@ class ImageClassificationProcessing(DatasetProcessing):
try:
eval_dataset = eval_dataset.align_labels_with_mapping(self.config.label2id, self.ref_keys[0])
except Exception as e:
except Exception:
print(
f"\nModel label mapping: {self.config.label2id}"
f"\nDataset label features: {eval_dataset.features[self.ref_keys[0]]}"
@@ -2,9 +2,8 @@ from functools import partial
from typing import Dict, List
from datasets import Dataset, load_dataset
from transformers import PreTrainedTokenizerBase, QuestionAnsweringPipeline
from evaluate import combine, evaluator, load
from transformers import PreTrainedTokenizerBase, QuestionAnsweringPipeline
from .base import DatasetProcessing
@@ -2,10 +2,8 @@ from functools import partial
from typing import Dict, List
from datasets import Dataset, load_dataset
from transformers import PretrainedConfig, PreTrainedTokenizerBase, TextClassificationPipeline
from transformers.pipelines.text_classification import ClassificationFunction
from evaluate import combine, evaluator
from transformers import PreTrainedTokenizerBase, TextClassificationPipeline
from .base import DatasetProcessing
@@ -1,10 +1,9 @@
from functools import partial
from typing import Dict, List, Optional
from datasets import ClassLabel, Dataset, load_dataset
from transformers import PreTrainedTokenizerBase, TokenClassificationPipeline
from typing import Dict, List
from datasets import Dataset, load_dataset
from evaluate import combine, evaluator
from transformers import PreTrainedTokenizerBase, TokenClassificationPipeline
from .base import DatasetProcessing
@@ -46,7 +45,7 @@ class TokenClassificationProcessing(DatasetProcessing):
eval_dataset = eval_dataset.align_labels_with_mapping(
label2id=self.config.label2id, label_column=self.ref_keys[0]
)
except Exception as e:
except Exception:
print(
f"\nModel label mapping: {self.config.label2id}"
f"\nDataset label features: {eval_dataset.features[self.ref_keys[0]]}"
+3 -3
View File
@@ -244,8 +244,8 @@ class Run(_RunDefaults, _RunBase):
# validate `task_args`
if self.task == "text-classification":
message = "For text classification, whether the task is regression should be explicity specified in the task_args.is_regression key."
assert self.task_args != None, message
assert self.task_args["is_regression"] != None, message
assert self.task_args is not None, message
assert self.task_args["is_regression"] is not None, message
# validate `dataset`
if self.quantization_approach == "static":
@@ -260,7 +260,7 @@ class Run(_RunDefaults, _RunBase):
), "Calibration parameters should be passed for static quantization in the calibration key."
# validate `aware_training`
assert self.aware_training == False, "Quantization-Aware Training not supported."
assert self.aware_training is False, "Quantization-Aware Training not supported."
@generate_doc_dataclass
-2
View File
@@ -7,8 +7,6 @@ import sys
import unittest
from typing import Any, Callable, Dict, Iterable, Optional, Tuple
from packaging import version
from . import is_accelerate_available, is_diffusers_available
+15 -1
View File
@@ -14,4 +14,18 @@
[tool.black]
line-length = 119
target-version = ['py35']
target-version = ['py37']
[tool.ruff]
# Never enforce `E501` (line length violations).
ignore = ["E501", "E741", "W605"]
select = ["E", "F", "I", "W"]
line-length = 119
# Ignore import violations in all `__init__.py` files.
[tool.ruff.per-file-ignores]
"__init__.py" = ["E402", "F401", "F403", "F811"]
[tool.ruff.isort]
lines-after-imports = 2
known-first-party = ["optimum"]
-38
View File
@@ -4,44 +4,6 @@ ensure_newline_before_comments = True
force_grid_wrap = 0
include_trailing_comma = True
known_first_party = optimum
known_third_party =
absl
conllu
datasets
elasticsearch
fairseq
faiss-cpu
fastprogress
fire
fugashi
git
h5py
matplotlib
nltk
numpy
packaging
pandas
PIL
psutil
pytest
pytorch_lightning
rouge_score
sacrebleu
seqeval
scikit-learn
streamlit
tensorboardX
tensorflow
tensorflow_datasets
timeout_decorator
torch
torchaudio
torchtext
torchvision
torch_xla
tqdm
transformers
line_length = 119
lines_after_imports = 2
multi_line_output = 3
+1 -1
View File
@@ -25,7 +25,7 @@ REQUIRED_PKGS = [
TESTS_REQUIRE = ["pytest", "requests", "parameterized", "pytest-xdist", "Pillow", "sacremoses", "diffusers"]
QUALITY_REQUIRE = ["black~=22.0", "flake8>=3.8.3", "isort>=5.5.4"]
QUALITY_REQUIRE = ["black~=23.1", "ruff>=0.0.241"]
BENCHMARK_REQUIRE = ["optuna", "tqdm", "scikit-learn", "seqeval", "torchvision", "evaluate>=0.2.0"]
@@ -1,10 +1,10 @@
import argparse
import requests
import torch
from PIL import Image
from transformers import AutoFeatureExtractor, AutoModel
import requests
from optimum.bettertransformer import BetterTransformer
@@ -91,7 +91,6 @@ if __name__ == "__main__":
output_file = open("log_{}.csv".format(args.model_name.replace("/", "-")), "w")
output_file.write("num_batches,batch_size,is_cuda,is_half,HF_time,BT_time,Speedup\n")
for bs in BATCH_SIZES:
total_bt_time, total_hf_time = benchmark(
args.model_name,
args.num_batches,
@@ -12,18 +12,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import tempfile
import unittest
import numpy as np
import torch
from parameterized import parameterized
from testing_bettertransformer_utils import BetterTransformersTestMixin
from transformers import AutoFeatureExtractor, AutoModel, AutoProcessor
from optimum.bettertransformer import BetterTransformer
from optimum.utils.testing_utils import grid_parameters
from parameterized import parameterized
from testing_bettertransformer_utils import BetterTransformersTestMixin
ALL_AUDIO_MODELS_TO_TEST = [
@@ -19,12 +19,12 @@ import unittest
import pytest
import torch
import transformers
from parameterized import parameterized
from testing_bettertransformer_utils import BetterTransformersTestMixin
from transformers import AutoModel, AutoTokenizer
from optimum.bettertransformer import BetterTransformer, BetterTransformerManager
from optimum.utils.testing_utils import grid_parameters, require_accelerate, require_torch_gpu
from parameterized import parameterized
from testing_bettertransformer_utils import BetterTransformersTestMixin
ALL_ENCODER_MODELS_TO_TEST = [
@@ -14,15 +14,15 @@
# limitations under the License.
import unittest
import requests
import torch
from parameterized import parameterized
from PIL import Image
from testing_bettertransformer_utils import BetterTransformersTestMixin
from transformers import AutoFeatureExtractor, AutoModel, AutoProcessor
import requests
from optimum.bettertransformer import BetterTransformer
from optimum.utils.testing_utils import grid_parameters
from parameterized import parameterized
from testing_bettertransformer_utils import BetterTransformersTestMixin
ALL_VISION_MODELS_TO_TEST = [
+1 -1
View File
@@ -2,12 +2,12 @@ import os
import unittest
import torch
from parameterized import parameterized
from transformers import AutoModel
from optimum.bettertransformer import BetterTransformer
from optimum.utils import logging
from optimum.utils.testing_utils import grid_parameters
from parameterized import parameterized
logger = logging.get_logger()
+5 -5
View File
@@ -48,7 +48,7 @@ class TestCLI(unittest.TestCase):
export_commands = [
f"optimum-cli export onnx --model hf-internal-testing/tiny-random-BertModel {tempdir}/encoder",
f"optimum-cli export onnx --model hf-internal-testing/tiny-random-gpt2 {tempdir}/decoder",
f"optimum-cli export onnx --model hf-internal-testing/tiny-random-t5 {tempdir}/encoder-decoder",
f"optimum-cli export onnx --model hf-internal-testing/tiny-random-bart {tempdir}/encoder-decoder",
]
optimize_commands = [
f"optimum-cli onnxruntime optimize --onnx_model {tempdir}/encoder -O1",
@@ -60,7 +60,7 @@ class TestCLI(unittest.TestCase):
subprocess.run(export, shell=True, check=True)
subprocess.run(optimize, shell=True, check=True)
def test_optimize_commands(self):
def test_quantize_commands(self):
with tempfile.TemporaryDirectory() as tempdir:
# First export a tiny encoder, decoder only and encoder-decoder
export_commands = [
@@ -68,12 +68,12 @@ class TestCLI(unittest.TestCase):
f"optimum-cli export onnx --model hf-internal-testing/tiny-random-gpt2 {tempdir}/decoder",
f"optimum-cli export onnx --model hf-internal-testing/tiny-random-t5 {tempdir}/encoder-decoder",
]
optimize_commands = [
quantize_commands = [
f"optimum-cli onnxruntime quantize --onnx_model {tempdir}/encoder --avx2",
f"optimum-cli onnxruntime quantize --onnx_model {tempdir}/decoder --avx2",
f"optimum-cli onnxruntime quantize --onnx_model {tempdir}/encoder-decoder --avx2",
]
for export, optimize in zip(export_commands, optimize_commands):
for export, quantize in zip(export_commands, quantize_commands):
subprocess.run(export, shell=True, check=True)
subprocess.run(optimize, shell=True, check=True)
subprocess.run(quantize, shell=True, check=True)
@@ -18,11 +18,11 @@ import unittest
from tempfile import TemporaryDirectory
from typing import Dict, Optional
from parameterized import parameterized
from transformers import is_torch_available
from transformers.testing_utils import require_torch, require_vision
from optimum.onnxruntime import ONNX_DECODER_NAME, ONNX_DECODER_WITH_PAST_NAME, ONNX_ENCODER_NAME
from parameterized import parameterized
if is_torch_available():
@@ -80,7 +80,6 @@ class OnnxCLIExportTestCase(unittest.TestCase):
"""
def _onnx_export(self, test_name: str, model_name: str, task: Optional[str], monolith: bool = False):
with TemporaryDirectory() as tmpdir:
monolith = " --monolith " if monolith is True else " "
if task is not None:
@@ -113,7 +112,6 @@ class OnnxCLIExportTestCase(unittest.TestCase):
os.environ["FORCE_ONNX_EXTERNAL_DATA"] = "1" # force exporting small model with external data
with TemporaryDirectory() as tmpdirname:
task = "seq2seq-lm"
if use_cache:
task += "-with-past"
@@ -145,7 +143,7 @@ class OnnxCLIExportTestCase(unittest.TestCase):
capture_output=True,
)
self.assertTrue(out.returncode, 1)
self.assertTrue(f"requires you to execute the modeling file in that repo" in out.stderr.decode("utf-8"))
self.assertTrue("requires you to execute the modeling file in that repo" in out.stderr.decode("utf-8"))
with TemporaryDirectory() as tmpdirname:
out = subprocess.run(
@@ -17,7 +17,7 @@ import tempfile
import unittest
from pathlib import Path
import tensorflow as tf
import onnxruntime
import torch
from transformers import (
AutoModelForSeq2SeqLM,
@@ -27,13 +27,12 @@ from transformers import (
from transformers.modeling_tf_utils import TFPreTrainedModel
from transformers.modeling_utils import PreTrainedModel
import onnxruntime
from optimum.exporters import TasksManager
from optimum.exporters.onnx import OnnxConfigWithLoss, export
# OnnxConfig wrapper
from optimum.onnxruntime.utils import ONNX_DECODER_NAME, ONNX_DECODER_WITH_PAST_NAME
from optimum.utils import DummyTextInputGenerator, normalized_config
from optimum.onnxruntime.utils import ONNX_DECODER_NAME
from optimum.utils import DummyTextInputGenerator
from optimum.utils.normalized_config import NormalizedConfigManager
@@ -220,7 +219,7 @@ class TestOnnxConfigWithLoss(unittest.TestCase):
output_names = [output.name for output in ort_sess._outputs_meta]
input_feed = dict(map(lambda input_name: (input_name, inputs[input_name].cpu().numpy()), input_names))
ort_outputs = ort_sess.run(output_names, input_feed)
ort_sess.run(output_names, input_feed)
gc.collect()
+2 -3
View File
@@ -20,6 +20,7 @@ from unittest import TestCase
from unittest.mock import patch
import pytest
from parameterized import parameterized
from transformers import AutoConfig, is_tf_available, is_torch_available, set_seed
from transformers.testing_utils import require_onnx, require_tf, require_torch, require_torch_gpu, require_vision, slow
@@ -36,7 +37,6 @@ from optimum.exporters.onnx import (
)
from optimum.utils import is_diffusers_available
from optimum.utils.testing_utils import grid_parameters, require_diffusers
from parameterized import parameterized
from ..exporters_utils import (
PYTORCH_EXPORT_MODELS_TINY,
@@ -121,7 +121,6 @@ class OnnxConfigWithPastTestCase(TestCase):
"""
for name, config in OnnxConfigWithPastTestCase.SUPPORTED_WITH_PAST_CONFIGS:
with self.subTest(name):
# Without past
onnx_config_default = OnnxConfigWithPast(config())
self.assertIsNotNone(onnx_config_default.values_override, "values_override should not be None")
@@ -352,7 +351,7 @@ class OnnxExportTestCase(TestCase):
@require_tf
@require_vision
def test_tensorflow_export(self, test_name, name, model_name, task, onnx_config_class_constructor, monolith: bool):
if monolith == False:
if monolith is False:
return 0
self._onnx_export(test_name, name, model_name, task, onnx_config_class_constructor, monolith=monolith)
@@ -17,11 +17,11 @@ from tempfile import TemporaryDirectory
from typing import Dict
import pytest
from parameterized import parameterized
from transformers import is_tf_available
from transformers.testing_utils import require_tf
from optimum.utils import DEFAULT_DUMMY_SHAPES
from parameterized import parameterized
from ..exporters_utils import PYTORCH_EXPORT_MODELS_TINY
@@ -121,7 +121,7 @@ class TFLiteCLIExportTestCase(unittest.TestCase):
capture_output=True,
)
self.assertTrue(out.returncode, 1)
self.assertTrue(f"requires you to execute the modeling file in that repo" in out.stderr.decode("utf-8"))
self.assertTrue("requires you to execute the modeling file in that repo" in out.stderr.decode("utf-8"))
with TemporaryDirectory() as tmpdirname:
out = subprocess.run(
+1 -1
View File
@@ -17,12 +17,12 @@ from tempfile import NamedTemporaryFile
from typing import Dict
from unittest import TestCase
from parameterized import parameterized
from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf, require_vision, slow
from optimum.exporters.tflite import export, validate_model_outputs
from optimum.utils import DEFAULT_DUMMY_SHAPES
from parameterized import parameterized
from ..exporters_utils import PYTORCH_EXPORT_MODELS_TINY
@@ -16,6 +16,7 @@ import operator
import unittest
import torch
from parameterized import parameterized
from transformers import AutoModel, AutoModelForImageClassification, AutoTokenizer, BertModel
from transformers.models.bert.modeling_bert import BertSelfAttention
from transformers.utils.fx import symbolic_trace
@@ -31,7 +32,6 @@ from optimum.fx.optimization import (
compose,
)
from optimum.fx.utils import are_fx_features_available
from parameterized import parameterized
_MODEL_NAME = "hf-internal-testing/tiny-random-bert"
@@ -19,26 +19,25 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase
import numpy as np
import torch
from transformers import AutoModel, AutoTokenizer
import huggingface_hub
import numpy as np
import onnx
import torch
from onnx import load as onnx_load
from onnxruntime import InferenceSession
from parameterized import parameterized
from transformers import AutoModel, AutoTokenizer
from optimum.onnx.graph_transformations import (
cast_slice_nodes_inputs_to_int32,
merge_decoders,
remove_duplicate_weights,
)
from parameterized import parameterized
class WeightSharingTestCase(TestCase):
def test_weight_sharing_output_match(self):
with torch.no_grad():
for model_id in {"albert-base-v1", "albert-base-v2"}:
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModel.from_pretrained(model_id)
+74 -71
View File
@@ -24,6 +24,7 @@ from unittest.mock import Mock, patch
import nltk
import numpy as np
from datasets import load_dataset
from evaluate import load
from transformers import (
AutoModelForSeq2SeqLM,
AutoModelForSequenceClassification,
@@ -43,7 +44,6 @@ from transformers.testing_utils import (
require_sigopt,
require_torch,
require_torch_bf16_gpu,
require_torch_gpu,
require_wandb,
slow,
)
@@ -56,18 +56,17 @@ from transformers.trainer_utils import (
from transformers.training_args import OptimizerNames
from transformers.utils import is_apex_available, is_bitsandbytes_available
from evaluate import load
if is_torch_available():
import torch
import transformers.optimization
import onnxruntime
from parameterized import parameterized
from optimum.onnxruntime import ORTSeq2SeqTrainer, ORTSeq2SeqTrainingArguments, ORTTrainer, ORTTrainingArguments
from optimum.onnxruntime.training_args import ORTOptimizerNames
from optimum.utils.testing_utils import require_hf_token, require_ort_training, require_sigopt_token_and_project
from parameterized import parameterized
from optimum.utils.testing_utils import require_hf_token, require_sigopt_token_and_project
nltk.download("punkt")
@@ -120,7 +119,6 @@ def _get_models_to_test(model_list, task_list, excluded: Optional[List[str]] = N
def _get_data_collator(data_metric_config, tokenizer=None, model=None, training_args=None, label_pad_token_id=None):
if "data_collator" in data_metric_config.keys():
data_collator = data_metric_config["data_collator"]
elif "data_collator_class" in data_metric_config.keys():
@@ -151,10 +149,15 @@ def get_ort_trainer(
max_test_samples=None,
**kwargs,
):
(model, tokenizer, data_collator, train_dataset, valid_dataset, test_dataset, compute_metrics,) = load_and_prepare(
feature
)(
(
model,
tokenizer,
data_collator,
train_dataset,
valid_dataset,
test_dataset,
compute_metrics,
) = load_and_prepare(feature)(
model_name,
data_metric_config,
max_seq_length,
@@ -187,7 +190,6 @@ def load_and_prepare(feature):
def load_and_prepare_glue(model_name, data_metric_config, max_seq_length, padding="max_length", **kwargs):
# Prepare model
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -233,7 +235,6 @@ def load_and_prepare_glue(model_name, data_metric_config, max_seq_length, paddin
def load_and_prepare_ner(model_name, data_metric_config, max_seq_length, padding="max_length", **kwargs):
# Load dataset and metric
dataset = load_dataset(*data_metric_config["dataset"])
metric = load(*data_metric_config["metric"])
@@ -321,7 +322,6 @@ def load_and_prepare_ner(model_name, data_metric_config, max_seq_length, padding
def load_and_prepare_xsum(model_name, data_metric_config, padding="max_length", **kwargs):
# Prepare model
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -413,9 +413,7 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
@parameterized.expand(_get_models_to_test(_MODELS_TO_TEST, _TASKS_DATASETS_CONFIGS), skip_on_empty=True)
def test_trainer_inference_with_ort(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
output_dir=tmp_dir,
num_train_epochs=self.n_epochs,
@@ -437,19 +435,16 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate(inference_with_ort=True)
prediction = trainer.predict(test_dataset, inference_with_ort=True)
trainer.evaluate(inference_with_ort=True)
trainer.predict(test_dataset, inference_with_ort=True)
# self.assertGreaterEqual(eval_metrics["eval_accuracy"], 0.75)
gc.collect()
@parameterized.expand(_get_models_to_test(_MODELS_TO_TEST, _TASKS_DATASETS_CONFIGS), skip_on_empty=True)
def test_trainer_inference_with_pytorch(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
output_dir=tmp_dir,
num_train_epochs=self.n_epochs,
@@ -471,11 +466,10 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate()
prediction = trainer.predict(test_dataset)
trainer.evaluate()
trainer.predict(test_dataset)
# self.assertGreaterEqual(eval_metrics["eval_accuracy"], 0.75)
gc.collect()
@@ -484,9 +478,7 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
_get_models_to_test(_MODELS_TO_TEST, _TASKS_DATASETS_CONFIGS, excluded=["gpt2"]), skip_on_empty=True
)
def test_trainer_fp16(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
output_dir=tmp_dir,
num_train_epochs=self.n_epochs,
@@ -509,11 +501,10 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate()
prediction = trainer.predict(test_dataset)
trainer.evaluate()
trainer.predict(test_dataset)
# self.assertGreaterEqual(eval_metrics["eval_accuracy"], 0.75)
gc.collect()
@@ -523,7 +514,6 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
@parameterized.expand(_get_models_to_test(_MODELS_TO_TEST, _TASKS_DATASETS_CONFIGS), skip_on_empty=True)
def test_trainer_bf16(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
output_dir=tmp_dir,
num_train_epochs=self.n_epochs,
@@ -546,11 +536,10 @@ class ORTTrainerIntegrationTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate()
prediction = trainer.predict(test_dataset)
trainer.evaluate()
trainer.predict(test_dataset)
# self.assertGreaterEqual(eval_metrics["eval_accuracy"], 0.75)
gc.collect()
@@ -606,7 +595,6 @@ class ORTTrainerIntegrationDeepSpeedTest(unittest.TestCase):
)
def test_trainer_fp16_ds_stage1(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
output_dir=tmp_dir,
num_train_epochs=self.n_epochs,
@@ -630,11 +618,10 @@ class ORTTrainerIntegrationDeepSpeedTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate()
prediction = trainer.predict(test_dataset)
trainer.evaluate()
trainer.predict(test_dataset)
# self.assertGreaterEqual(eval_metrics["eval_accuracy"], 0.75)
gc.collect()
@@ -643,7 +630,6 @@ class ORTTrainerIntegrationDeepSpeedTest(unittest.TestCase):
)
def test_trainer_fp16_ds_stage2(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
output_dir=tmp_dir,
num_train_epochs=self.n_epochs,
@@ -667,11 +653,10 @@ class ORTTrainerIntegrationDeepSpeedTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate()
prediction = trainer.predict(test_dataset)
trainer.evaluate()
trainer.predict(test_dataset)
# self.assertGreaterEqual(eval_metrics["eval_accuracy"], 0.75)
gc.collect()
@@ -680,9 +665,8 @@ class ORTTrainerIntegrationDeepSpeedTest(unittest.TestCase):
@require_torch
class ORTTrainerIntegrationDDPTest(unittest.TestCase):
def test_trainer_ddp_glue(self):
subprocess.run(
f"cp examples/onnxruntime/training/text-classification/run_glue.py ./",
"cp examples/onnxruntime/training/text-classification/run_glue.py ./",
shell=True,
)
@@ -734,9 +718,15 @@ class ORTTrainerHyperParameterIntegrationTest(unittest.TestCase):
run_name="test",
)
(_, tokenizer, data_collator, train_dataset, valid_dataset, _, compute_metrics,) = load_and_prepare(
self.feature
)(
(
_,
tokenizer,
data_collator,
train_dataset,
valid_dataset,
_,
compute_metrics,
) = load_and_prepare(self.feature)(
self.model_name,
_TASKS_DATASETS_CONFIGS[self.feature],
self.max_seq_length,
@@ -776,9 +766,15 @@ class ORTTrainerHyperParameterIntegrationTest(unittest.TestCase):
run_name="test",
)
(_, tokenizer, data_collator, train_dataset, valid_dataset, _, compute_metrics,) = load_and_prepare(
self.feature
)(
(
_,
tokenizer,
data_collator,
train_dataset,
valid_dataset,
_,
compute_metrics,
) = load_and_prepare(self.feature)(
self.model_name,
_TASKS_DATASETS_CONFIGS[self.feature],
max_seq_length=self.max_seq_length,
@@ -822,9 +818,15 @@ class ORTTrainerHyperParameterIntegrationTest(unittest.TestCase):
run_name="test",
)
(_, tokenizer, data_collator, train_dataset, valid_dataset, _, compute_metrics,) = load_and_prepare(
self.feature
)(
(
_,
tokenizer,
data_collator,
train_dataset,
valid_dataset,
_,
compute_metrics,
) = load_and_prepare(self.feature)(
self.model_name,
_TASKS_DATASETS_CONFIGS[self.feature],
self.max_seq_length,
@@ -867,9 +869,15 @@ class ORTTrainerHyperParameterIntegrationTest(unittest.TestCase):
run_name="test",
)
(_, tokenizer, data_collator, train_dataset, valid_dataset, _, compute_metrics,) = load_and_prepare(
self.feature
)(
(
_,
tokenizer,
data_collator,
train_dataset,
valid_dataset,
_,
compute_metrics,
) = load_and_prepare(self.feature)(
self.model_name,
_TASKS_DATASETS_CONFIGS[self.feature],
self.max_seq_length,
@@ -997,7 +1005,6 @@ class ORTTrainerOptimizerChoiceTest(unittest.TestCase):
self.check_optim_and_kwargs(name, mandatory_kwargs, expected_cls)
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTTrainingArguments(
optim=name,
output_dir=tmp_dir,
@@ -1020,11 +1027,10 @@ class ORTTrainerOptimizerChoiceTest(unittest.TestCase):
max_test_samples=self.max_test_samples,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
eval_metrics = trainer.evaluate(inference_with_ort=True)
prediction = trainer.predict(test_dataset, inference_with_ort=True)
trainer.evaluate(inference_with_ort=True)
trainer.predict(test_dataset, inference_with_ort=True)
gc.collect()
@unittest.skip("Skip ORT Fused Adam optimizer test.") # Not merged yet.
@@ -1070,9 +1076,7 @@ class ORTSeq2SeqTrainerIntegrationTest(unittest.TestCase):
_get_models_to_test(_SEQ2SEQ_MODELS_TO_TEST, _SEQ2SEQ_TASKS_DATASETS_CONFIGS), skip_on_empty=True
)
def test_trainer_inference_with_pytorch(self, test_name, model_name, feature, data_metric_config):
with tempfile.TemporaryDirectory() as tmp_dir:
training_args = ORTSeq2SeqTrainingArguments(
output_dir=tmp_dir,
evaluation_strategy="epoch",
@@ -1114,11 +1118,10 @@ class ORTSeq2SeqTrainerIntegrationTest(unittest.TestCase):
feature=feature,
)
train_result = trainer.train()
trainer.train()
trainer.save_model()
train_metrics = train_result.metrics
ort_eval_metrics = trainer.evaluate()
ort_prediction = trainer.predict(test_dataset)
trainer.evaluate()
trainer.predict(test_dataset)
# self.assertGreaterEqual(ort_eval_metrics["eval_rouge1"], 10)
# self.assertGreaterEqual(ort_eval_metrics["eval_rouge2"], 2)
# self.assertGreaterEqual(ort_eval_metrics["eval_rougeL"], 7)
+24 -26
View File
@@ -22,8 +22,13 @@ import unittest
from typing import Dict
import numpy as np
import onnx
import onnxruntime
import pytest
import requests
import torch
from huggingface_hub.constants import default_cache_path
from parameterized import parameterized
from PIL import Image
from transformers import (
AutoConfig,
@@ -49,10 +54,6 @@ from transformers.modeling_utils import no_init_weights
from transformers.onnx.utils import get_preprocessor
from transformers.testing_utils import get_gpu_count, require_torch_gpu
import onnx
import onnxruntime
import requests
from huggingface_hub.constants import default_cache_path
from optimum.exporters import TasksManager
from optimum.onnxruntime import (
ONNX_DECODER_NAME,
@@ -73,13 +74,11 @@ from optimum.onnxruntime import (
ORTModelForTokenClassification,
ORTModelForVision2Seq,
)
from optimum.onnxruntime.base import ORTDecoder, ORTEncoder
from optimum.onnxruntime.base import ORTDecoder, ORTDecoderForSeq2Seq, ORTEncoder
from optimum.onnxruntime.modeling_ort import ORTModel
from optimum.onnxruntime.modeling_seq2seq import ORTDecoder as ORTSeq2SeqDecoder
from optimum.pipelines import pipeline
from optimum.utils import CONFIG_NAME, logging
from optimum.utils.testing_utils import grid_parameters, require_hf_token
from parameterized import parameterized
logger = logging.get_logger()
@@ -244,14 +243,14 @@ class ORTModelIntegrationTest(unittest.TestCase):
"fxmarty/tiny-mbart-subfolder", subfolder="my_folder", from_transformers=True
)
self.assertIsInstance(model.encoder, ORTEncoder)
self.assertIsInstance(model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.config, PretrainedConfig)
model = ORTModelForSeq2SeqLM.from_pretrained("fxmarty/tiny-mbart-onnx-subfolder", subfolder="my_folder")
self.assertIsInstance(model.encoder, ORTEncoder)
self.assertIsInstance(model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.config, PretrainedConfig)
def test_load_model_from_cache(self):
@@ -276,8 +275,8 @@ class ORTModelIntegrationTest(unittest.TestCase):
model = ORTModelForSeq2SeqLM.from_pretrained(self.TINY_ONNX_SEQ2SEQ_MODEL_ID, local_files_only=True)
self.assertIsInstance(model.encoder, ORTEncoder)
self.assertIsInstance(model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.config, PretrainedConfig)
def test_load_seq2seq_model_from_empty_cache(self):
@@ -310,14 +309,14 @@ class ORTModelIntegrationTest(unittest.TestCase):
def test_load_seq2seq_model_from_hub(self):
model = ORTModelForSeq2SeqLM.from_pretrained(self.ONNX_SEQ2SEQ_MODEL_ID, use_cache=True)
self.assertIsInstance(model.encoder, ORTEncoder)
self.assertIsInstance(model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(model.config, PretrainedConfig)
def test_load_seq2seq_model_without_past_from_hub(self):
model = ORTModelForSeq2SeqLM.from_pretrained(self.ONNX_SEQ2SEQ_MODEL_ID, use_cache=False)
self.assertIsInstance(model.encoder, ORTEncoder)
self.assertIsInstance(model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(model.decoder, ORTDecoderForSeq2Seq)
self.assertTrue(model.decoder_with_past is None)
self.assertIsInstance(model.config, PretrainedConfig)
@@ -362,7 +361,7 @@ class ORTModelIntegrationTest(unittest.TestCase):
def test_missing_execution_provider(self):
with self.assertRaises(ValueError) as cm:
model = ORTModel.from_pretrained(self.ONNX_MODEL_ID, provider="ThisProviderDoesNotExist")
ORTModel.from_pretrained(self.ONNX_MODEL_ID, provider="ThisProviderDoesNotExist")
self.assertTrue("but the available execution providers" in str(cm.exception))
@@ -434,7 +433,6 @@ class ORTModelIntegrationTest(unittest.TestCase):
@pytest.mark.gpu_test
def test_model_on_gpu_str(self):
model = ORTModel.from_pretrained(self.ONNX_MODEL_ID)
gpu = torch.device("cuda")
model.to("cuda")
self.assertEqual(model.device, torch.device("cuda:0"))
self.assertListEqual(model.providers, ["CUDAExecutionProvider", "CPUExecutionProvider"])
@@ -2375,9 +2373,9 @@ class ORTModelForSeq2SeqLMIntegrationTest(ORTModelTestMixin):
onnx_model = ORTModelForSeq2SeqLM.from_pretrained(self.onnx_model_dirs[test_name], use_cache=use_cache)
self.assertIsInstance(onnx_model.encoder, ORTEncoder)
self.assertIsInstance(onnx_model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(onnx_model.decoder, ORTDecoderForSeq2Seq)
if onnx_model.use_cache is True:
self.assertIsInstance(onnx_model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(onnx_model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(onnx_model.config, PretrainedConfig)
set_seed(SEED)
@@ -2676,9 +2674,9 @@ class ORTModelForSpeechSeq2SeqIntegrationTest(ORTModelTestMixin):
onnx_model = ORTModelForSpeechSeq2Seq.from_pretrained(self.onnx_model_dirs[test_name], use_cache=use_cache)
self.assertIsInstance(onnx_model.encoder, ORTEncoder)
self.assertIsInstance(onnx_model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(onnx_model.decoder, ORTDecoderForSeq2Seq)
if onnx_model.use_cache is True:
self.assertIsInstance(onnx_model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(onnx_model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(onnx_model.config, PretrainedConfig)
set_seed(SEED)
@@ -2871,12 +2869,12 @@ class ORTModelForVision2SeqIntegrationTest(ORTModelTestMixin):
SPEEDUP_CACHE = 1.2
def exclude_trocr_with_cache(params):
if params[0] == "trocr" and params[1] == True:
if params[0] == "trocr" and params[1] is True:
return None
return params
def update_trocr_with_cache(params):
if params[0] == "trocr" and params[1] == True:
if params[0] == "trocr" and params[1] is True:
params[1] = False
return params
@@ -2928,9 +2926,9 @@ class ORTModelForVision2SeqIntegrationTest(ORTModelTestMixin):
onnx_model = ORTModelForVision2Seq.from_pretrained(self.onnx_model_dirs[test_name], use_cache=use_cache)
self.assertIsInstance(onnx_model.encoder, ORTEncoder)
self.assertIsInstance(onnx_model.decoder, ORTSeq2SeqDecoder)
self.assertIsInstance(onnx_model.decoder, ORTDecoderForSeq2Seq)
if onnx_model.use_cache is True:
self.assertIsInstance(onnx_model.decoder_with_past, ORTSeq2SeqDecoder)
self.assertIsInstance(onnx_model.decoder_with_past, ORTDecoderForSeq2Seq)
self.assertIsInstance(onnx_model.config, PretrainedConfig)
set_seed(SEED)
+2 -3
View File
@@ -18,18 +18,17 @@ import tempfile
import unittest
from pathlib import Path
import onnx
import torch
from parameterized import parameterized
from transformers import AutoTokenizer
import onnx
from optimum.onnxruntime import ORTConfig, ORTModelForSequenceClassification, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig
from optimum.onnxruntime.modeling_seq2seq import ORTModelForSeq2SeqLM
from parameterized import parameterized
class ORTOptimizerTest(unittest.TestCase):
# Contribution note: Please add test models in alphabetical order. Find test models here: https://huggingface.co/hf-internal-testing.
SUPPORTED_ARCHITECTURES_WITH_MODEL_ID = (
(ORTModelForSequenceClassification, "hf-internal-testing/tiny-random-bart"),
+3 -3
View File
@@ -18,15 +18,15 @@ import unittest
from functools import partial
from pathlib import Path
from transformers import AutoTokenizer
from onnx import load as onnx_load
from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
from parameterized import parameterized
from transformers import AutoTokenizer
from optimum.onnxruntime import ORTQuantizer
from optimum.onnxruntime.configuration import AutoCalibrationConfig, ORTConfig, QuantizationConfig
from optimum.onnxruntime.modeling_ort import ORTModelForSequenceClassification
from optimum.onnxruntime.modeling_seq2seq import ORTModelForSeq2SeqLM
from parameterized import parameterized
class ORTQuantizerTest(unittest.TestCase):
+2 -2
View File
@@ -16,11 +16,11 @@ import os
import tempfile
import unittest
from huggingface_hub import HfFolder, delete_repo, set_access_token
from requests.exceptions import HTTPError
from transformers.testing_utils import TOKEN, USER, is_staging_test
from huggingface_hub import HfFolder, delete_repo, set_access_token
from optimum.configuration_utils import BaseConfig
from requests.exceptions import HTTPError
class FakeConfig(BaseConfig):
+2 -3
View File
@@ -3,10 +3,10 @@ import random
import tempfile
import unittest
import requests as r
import torch
from transformers.configuration_utils import PretrainedConfig
import requests as r
from optimum.modeling_base import OptimizedModel
from optimum.utils.testing_utils import require_hf_token
@@ -41,7 +41,6 @@ class TestOptimizedModel(unittest.TestCase):
@require_hf_token
def test_push_to_hub(self):
with tempfile.TemporaryDirectory() as tmpdirname:
model = DummyModel.from_pretrained(TEST_LOCAL_PATH)
# create remote hash to check if file was updated.
remote_hash = random.getrandbits(128)
@@ -54,6 +53,6 @@ class TestOptimizedModel(unittest.TestCase):
repository_id="unit_test_save_model",
)
# folder contains all config files and pytorch_model.bin
url = f"https://huggingface.co/philschmid/unit_test_save_model/raw/main/config.json"
url = "https://huggingface.co/philschmid/unit_test_save_model/raw/main/config.json"
response = r.get(url)
self.assertEqual(remote_hash, response.json()["from_local"])
+1 -1
View File
@@ -18,13 +18,13 @@ from typing import TYPE_CHECKING, Tuple
from unittest import TestCase
import torch
from parameterized import parameterized
from transformers import AutoConfig
from transformers.utils import is_tf_available
from optimum.utils import DummyAudioInputGenerator, DummyTextInputGenerator, DummyVisionInputGenerator
from optimum.utils.normalized_config import NormalizedConfigManager
from optimum.utils.testing_utils import grid_parameters
from parameterized import parameterized
if TYPE_CHECKING: