# 🤗 Transformers 모델을 ONNX로 내보내기[[exporting--transformers-models-to-onnx]]

🤗 트랜스포머는 `transformers.onnx` 패키지를 제공하며, 이 패키지는 설정 객체를 활용하여 모델 체크포인트를 ONNX 그래프로 변환할 수 있게 합니다.

🤗 Transformers에 대한 자세한 내용은 [이 가이드](../serialization)를 참조하세요.

## ONNX 설정[[onnx-configurations]]

내보내려는(export) 모델 아키텍처의 유형에 따라 상속받아야 할 세 가지 추상 클래스를 제공합니다:

* 인코더 기반 모델은 [OnnxConfig](/docs/transformers/v4.57.1/ko/main_classes/onnx#transformers.onnx.OnnxConfig)을 상속받습니다.
* 디코더 기반 모델은 [OnnxConfigWithPast](/docs/transformers/v4.57.1/ko/main_classes/onnx#transformers.onnx.OnnxConfigWithPast)을 상속받습니다.
* 인코더-디코더 기반 모델은 [OnnxSeq2SeqConfigWithPast](/docs/transformers/v4.57.1/ko/main_classes/onnx#transformers.onnx.OnnxSeq2SeqConfigWithPast)을 상속받습니다.

### OnnxConfig[[transformers.onnx.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/ko/internal/tokenization_utils#transformers.PreTrainedTokenizerBase), [FeatureExtractionMixin](/docs/transformers/v4.57.1/ko/main_classes/feature_extractor#transformers.FeatureExtractionMixin), or [ImageProcessingMixin](/docs/transformers/v4.57.1/ko/internal/image_processing_utils#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[[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[[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 특징[[onnx-features]]

각 ONNX 설정은 다양한 유형의 토폴로지나 작업에 대해 모델을 내보낼 수 있게(exporting) 해주는 _features_ 세트와 연관되어 있습니다.

### FeaturesManager[[transformers.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.

