# Exporting 🤗 Transformers models to ONNX

🤗 Transformers は `transformers.onnx` パッケージを提供します。
設定オブジェクトを利用することで、モデルのチェックポイントをONNXグラフに変換することができます。

詳細は[ガイド](../serialization) を参照してください。
を参照してください。

## ONNX Configurations

以下の3つの抽象クラスを提供しています。
エクスポートしたいモデルアーキテクチャのタイプに応じて、継承すべき3つの抽象クラスを提供します：

* エンコーダーベースのモデルは [OnnxConfig](/docs/transformers/v4.57.1/ja/main_classes/onnx#transformers.onnx.OnnxConfig) を継承します。
* デコーダーベースのモデルは [OnnxConfigWithPast](/docs/transformers/v4.57.1/ja/main_classes/onnx#transformers.onnx.OnnxConfigWithPast) を継承します。
* エンコーダー・デコーダーモデルは [OnnxSeq2SeqConfigWithPast](/docs/transformers/v4.57.1/ja/main_classes/onnx#transformers.onnx.OnnxSeq2SeqConfigWithPast) を継承しています。

### OnnxConfig[[transformers.onnx.OnnxConfig]]

#### transformers.onnx.OnnxConfig[[transformers.onnx.OnnxConfig]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L69)

Base class for ONNX exportable model describing metadata on how to export the model through the ONNX format.

flatten_output_collection_propertytransformers.onnx.OnnxConfig.flatten_output_collection_propertyhttps://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L427[{"name": "name", "val": ": str"}, {"name": "field", "val": ": Iterable"}]- **name** -- The name of the nested structure
- **field** -- The structure to, potentially, be flattened0(dict[str, Any])Outputs with flattened structure and key mapping this new structure.

Flatten any potential nested structure expanding the name of the field with the index of the element within the
structure.

**Parameters:**

name : The name of the nested structure

field : The structure to, potentially, be flattened

**Returns:**

`(dict[str, Any])`

Outputs with flattened structure and key mapping this new structure.
#### from_model_config[[transformers.onnx.OnnxConfig.from_model_config]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L130)

Instantiate a OnnxConfig for a specific model

**Parameters:**

config : The model's configuration to use when exporting to ONNX

**Returns:**

OnnxConfig for this model
#### generate_dummy_inputs[[transformers.onnx.OnnxConfig.generate_dummy_inputs]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L283)

Generate inputs to provide to the ONNX exporter for the specific framework

**Parameters:**

preprocessor : ([PreTrainedTokenizerBase](/docs/transformers/v4.57.1/ja/internal/tokenization_utils#transformers.PreTrainedTokenizerBase), [FeatureExtractionMixin](/docs/transformers/v4.57.1/ja/main_classes/feature_extractor#transformers.FeatureExtractionMixin), or [ImageProcessingMixin](/docs/transformers/v4.57.1/ja/main_classes/image_processor#transformers.ImageProcessingMixin)): The preprocessor associated with this model configuration.

batch_size (`int`, *optional*, defaults to -1) : The batch size to export the model for (-1 means dynamic axis).

num_choices (`int`, *optional*, defaults to -1) : The number of candidate answers provided for multiple choice task (-1 means dynamic axis).

seq_length (`int`, *optional*, defaults to -1) : The sequence length to export the model for (-1 means dynamic axis).

is_pair (`bool`, *optional*, defaults to `False`) : Indicate if the input is a pair (sentence 1, sentence 2)

framework (`TensorType`, *optional*, defaults to `None`) : The framework (PyTorch or TensorFlow) that the tokenizer will generate tensors for.

num_channels (`int`, *optional*, defaults to 3) : The number of channels of the generated images.

image_width (`int`, *optional*, defaults to 40) : The width of the generated images.

image_height (`int`, *optional*, defaults to 40) : The height of the generated images.

sampling_rate (`int`, *optional* defaults to 22050) : The sampling rate for audio data generation.

time_duration (`float`, *optional* defaults to 5.0) : Total seconds of sampling for audio data generation.

frequency (`int`, *optional* defaults to 220) : The desired natural frequency of generated audio.

**Returns:**

Mapping[str, Tensor] holding the kwargs to provide to the model's forward function
#### generate_dummy_inputs_onnxruntime[[transformers.onnx.OnnxConfig.generate_dummy_inputs_onnxruntime]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L403)

Generate inputs for ONNX Runtime using the reference model inputs. Override this to run inference with seq2seq
models which have the encoder and decoder exported as separate ONNX files.

**Parameters:**

reference_model_inputs ([`Mapping[str, Tensor]`) : Reference inputs for the model.

**Returns:**

``Mapping[str, Tensor]``

The mapping holding the kwargs to provide to the model's forward function
#### use_external_data_format[[transformers.onnx.OnnxConfig.use_external_data_format]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L244)

Flag indicating if the model requires using external data format

**Parameters:**

num_parameters : Number of parameter on the model

**Returns:**

True if model.num_parameters() * size_of(float32) >= 2Gb False otherwise

### OnnxConfigWithPast[[transformers.onnx.OnnxConfigWithPast]]

#### transformers.onnx.OnnxConfigWithPast[[transformers.onnx.OnnxConfigWithPast]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L446)

fill_with_past_key_values_transformers.onnx.OnnxConfigWithPast.fill_with_past_key_values_https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L553[{"name": "inputs_or_outputs", "val": ": Mapping"}, {"name": "direction", "val": ": str"}, {"name": "inverted_values_shape", "val": ": bool = False"}]- **inputs_or_outputs** -- The mapping to fill.
- **direction** -- either "inputs" or "outputs", it specifies whether input_or_outputs is the input mapping or the
  output mapping, this is important for axes naming.
- **inverted_values_shape** --
  If `True`, store values on dynamic axis 1, else on axis 2.0

Fill the input_or_outputs mapping with past_key_values dynamic axes considering.

**Parameters:**

inputs_or_outputs : The mapping to fill.

direction : either "inputs" or "outputs", it specifies whether input_or_outputs is the input mapping or the output mapping, this is important for axes naming.

inverted_values_shape : If `True`, store values on dynamic axis 1, else on axis 2.
#### with_past[[transformers.onnx.OnnxConfigWithPast.with_past]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L457)

Instantiate a OnnxConfig with `use_past` attribute set to True

**Parameters:**

config : The underlying model's config to use when exporting to ONNX

**Returns:**

OnnxConfig with `.use_past = True`

### OnnxSeq2SeqConfigWithPast[[transformers.onnx.OnnxSeq2SeqConfigWithPast]]

#### transformers.onnx.OnnxSeq2SeqConfigWithPast[[transformers.onnx.OnnxSeq2SeqConfigWithPast]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/config.py#L593)

## ONNX Features

各 ONNX 構成は、次のことを可能にする一連の _機能_ に関連付けられています。
さまざまなタイプのトポロジまたはタスクのモデルをエクスポートします。

### FeaturesManager[[transformers.onnx.FeaturesManager]]

#### transformers.onnx.FeaturesManager[[transformers.onnx.FeaturesManager]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L85)

check_supported_model_or_raisetransformers.onnx.FeaturesManager.check_supported_model_or_raisehttps://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L711[{"name": "model", "val": ": typing.Union[ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')]"}, {"name": "feature", "val": ": str = 'default'"}]- **model** -- The model to export.
- **feature** -- The name of the feature to check if it is available.0(str) The type of the model (OnnxConfig) The OnnxConfig instance holding the model export properties.

Check whether or not the model has the requested features.

**Parameters:**

model : The model to export.

feature : The name of the feature to check if it is available.

**Returns:**

(str) The type of the model (OnnxConfig) The OnnxConfig instance holding the model export properties.
#### determine_framework[[transformers.onnx.FeaturesManager.determine_framework]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L628)

Determines the framework to use for the export.

The priority is in the following order:
1. User input via `framework`.
2. If local checkpoint is provided, use the same framework as the checkpoint.
3. Available framework in environment, with priority given to PyTorch

**Parameters:**

model (`str`) : The name of the model to export.

framework (`str`, *optional*, defaults to `None`) : The framework to use for the export. See above for priority if none provided.

**Returns:**

The framework to use for the export.
#### get_config[[transformers.onnx.FeaturesManager.get_config]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L736)

Gets the OnnxConfig for a model_type and feature combination.

**Parameters:**

model_type (`str`) : The model type to retrieve the config for.

feature (`str`) : The feature to retrieve the config for.

**Returns:**

``OnnxConfig``

config for the combination
#### get_model_class_for_feature[[transformers.onnx.FeaturesManager.get_model_class_for_feature]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L601)

Attempts to retrieve an AutoModel class from a feature name.

**Parameters:**

feature (`str`) : The feature required.

framework (`str`, *optional*, defaults to `"pt"`) : The framework to use for the export.

**Returns:**

The AutoModel class corresponding to the feature.
#### get_model_from_feature[[transformers.onnx.FeaturesManager.get_model_from_feature]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L678)

Attempts to retrieve a model from a model's name and the feature to be enabled.

**Parameters:**

feature (`str`) : The feature required.

model (`str`) : The name of the model to export.

framework (`str`, *optional*, defaults to `None`) : The framework to use for the export. See `FeaturesManager.determine_framework` for the priority should none be provided.

**Returns:**

The instance of the model.
#### get_supported_features_for_model_type[[transformers.onnx.FeaturesManager.get_supported_features_for_model_type]]

[Source](https://github.com/huggingface/transformers/blob/v4.57.1/src/transformers/onnx/features.py#L556)

Tries to retrieve the feature -> OnnxConfig constructor map from the model type.

**Parameters:**

model_type (`str`) : The model type to retrieve the supported features for.

model_name (`str`, *optional*) : The name attribute of the model object, only used for the exception message.

**Returns:**

The dictionary mapping each feature to a corresponding OnnxConfig constructor.

