-
Notifications
You must be signed in to change notification settings - Fork 85
Hugging Face Model integration in Superbench #803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ed0d4f2
21a4cc8
c531a18
18df07b
12a65ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,22 +9,23 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import torch.hub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import torch.onnx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import torchvision.models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import BertConfig, GPT2Config, LlamaConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_bert import BertBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_gpt2 import GPT2BenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_lstm import LSTMBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_llama import LlamaBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_mixtral import MixtralBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import traceback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if MixtralBenchmarkModel is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import MixtralConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.common.utils import logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class torch2onnxExporter(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """PyTorch model to ONNX exporter.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Constructor.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import BertConfig, GPT2Config, LlamaConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_bert import BertBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_gpt2 import GPT2BenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_lstm import LSTMBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_llama import LlamaBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superbench.benchmarks.model_benchmarks.pytorch_mixtral import MixtralBenchmarkModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.num_classes = 100 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.lstm_input_size = 256 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.benchmark_models = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -129,6 +130,7 @@ def __init__(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Only include Mixtral models if MixtralBenchmarkModel is available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if MixtralBenchmarkModel is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import MixtralConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.benchmark_models.update( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'mixtral-8x7b': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -270,3 +272,151 @@ def export_benchmark_model(self, model_name, batch_size=1, seq_length=512): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| del dummy_input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| torch.cuda.empty_cache() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def export_huggingface_model(self, model, model_name, batch_size=1, seq_length=512, output_dir=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Export a HuggingFace model to ONNX format. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model: HuggingFace model instance to export. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_name (str): Name for the exported ONNX model file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| batch_size (int): Batch size of input. Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| seq_length (int): Sequence length of input. Defaults to 512. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_dir (str): Output directory path. If None, uses default path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| str: Exported ONNX model file path, or empty string if export fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Use custom output directory if provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_path = Path(output_dir) if output_dir else self._onnx_model_path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file_name = str(output_path / (model_name + '.onnx')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Put model in eval mode and move to CUDA if available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.eval() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Disable cache to avoid DynamicCache issues with ONNX export | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if hasattr(model.config, 'use_cache'): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.config.use_cache = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if torch.cuda.is_available(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model = model.cuda() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| device = 'cuda' if torch.cuda.is_available() else 'cpu' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Get model's dtype for inputs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_dtype = next(model.parameters()).dtype | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+294
to
+307
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Put model in eval mode and move to CUDA if available | |
| model.eval() | |
| # Disable cache to avoid DynamicCache issues with ONNX export | |
| if hasattr(model.config, 'use_cache'): | |
| model.config.use_cache = False | |
| if torch.cuda.is_available(): | |
| model = model.cuda() | |
| device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
| # Get model's dtype for inputs | |
| model_dtype = next(model.parameters()).dtype | |
| # Put model in eval mode and respect its current device placement | |
| model.eval() | |
| # Disable cache to avoid DynamicCache issues with ONNX export | |
| if hasattr(model.config, 'use_cache'): | |
| model.config.use_cache = False | |
| try: | |
| first_param = next(model.parameters()) | |
| device = first_param.device | |
| model_dtype = first_param.dtype | |
| except StopIteration: | |
| try: | |
| first_buffer = next(model.buffers()) | |
| device = first_buffer.device | |
| model_dtype = first_buffer.dtype | |
| except StopIteration: | |
| device = torch.device('cpu') | |
| model_dtype = torch.float32 | |
| # Get model's dtype for inputs |
Copilot
AI
Apr 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For many NLP models the exported output shape is sequence-dependent (e.g., logits/hidden states often have a seq_length dimension). Currently only the batch dimension is marked dynamic for output, which can lock the exported ONNX to a fixed seq_length and break dynamic-shape inference/engine building. Consider adding the sequence dimension to output’s dynamic_axes when the model output is 3D (batch, seq, hidden/vocab).
Copilot
AI
Apr 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For models >2GB, exporting without enabling external-data at export time can fail due to protobuf size limits (the subsequent convert_model_to_external_data() may never run). Pass the appropriate export-time option (e.g., use_external_data_format=use_external_data) so large-model exports succeed reliably.
| dynamic_axes=dynamic_axes, | |
| dynamic_axes=dynamic_axes, | |
| use_external_data_format=use_external_data, |
Copilot
AI
Apr 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For models larger than ~2GB, torch.onnx.export(...) may fail before the later convert_model_to_external_data(...) step due to protobuf size limits, because the export itself still attempts to serialize initializers into the main ONNX file. For large-model support to be reliable, enable PyTorch's large/external-data export mode at export time (e.g., using the appropriate large_model / use_external_data_format option supported by your PyTorch version) rather than only converting after the fact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
export_huggingface_model()introduces multiple branches (vision vs NLP, dynamic axes behavior, and external-data conversion for >2GB) but there’s no targeted unit test coverage shown for this method. Consider adding mocked/unit tests that validate: (1) correct input/output names for vision and NLP paths, and (2) external-data conversion is invoked when the size threshold is exceeded (can be done by mocking parameter sizing and ONNX helpers).