# Export functions

You can export models to ONNX from PyTorch. 
There is an export function for PyTorch models [export_pytorch()](/docs/optimum/pr_101/en/onnx/package_reference/export#optimum.exporters.onnx.convert.export_pytorch), 
but the recommended way of using those is via the main export function `~optimum.exporters.main_export`, 
which will take care of using the proper exporting function according to the available framework, 
check that the exported model is valid, and provide extended options to run optimizations on the exported model.

## Main functions[[optimum.exporters.onnx.main_export]]

#### optimum.exporters.onnx.main_export[[optimum.exporters.onnx.main_export]]

[Source](https://github.com/huggingface/optimum-onnx/blob/vr_101/optimum/exporters/onnx/__main__.py#L57)

Full-suite ONNX export function, exporting **from a model ID on Hugging Face Hub or a local model repository**.

Example usage:
```python
>>> from optimum.exporters.onnx import main_export

>>> main_export("gpt2", output="gpt2_onnx/")
```

#### optimum.exporters.onnx.onnx_export_from_model[[optimum.exporters.onnx.onnx_export_from_model]]

[Source](https://github.com/huggingface/optimum-onnx/blob/vr_101/optimum/exporters/onnx/convert.py#L809)

Full-suite ONNX export function, exporting **from a pre-loaded PyTorch model**. This function is especially useful in case one needs to do modifications on the model, as overriding a forward call, before exporting to ONNX.

Example usage:
```python
>>> from transformers import AutoModelForCausalLM

>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> # At this point, we could override some submodules, forward methods, weights, etc. from the model.

>>> onnx_export_from_model(model, output="gpt2_onnx/")
```

#### optimum.exporters.onnx.export[[optimum.exporters.onnx.export]]

[Source](https://github.com/huggingface/optimum-onnx/blob/vr_101/optimum/exporters/onnx/convert.py#L706)

Exports a Pytorch model to an ONNX Intermediate Representation.

**Parameters:**

model (`PreTrainedModel` or `ModelMixin`) : The model to export.

config ([OnnxConfig](/docs/optimum/pr_101/en/onnx/package_reference/configuration#optimum.exporters.onnx.OnnxConfig)) : The ONNX configuration associated with the exported model.

output (`Path`) : Directory to store the exported ONNX model.

opset (`Optional[int]`, defaults to `None`) : The version of the ONNX operator set to use.

device (`Optional[str]`, defaults to `"cpu"`) : The device on which the ONNX model will be exported. Either `cpu` or `cuda`. Only PyTorch is supported for export on CUDA devices.

input_shapes (`Optional[Dict]`, defaults to `None`) : If specified, allows to use specific shapes for the example input provided to the ONNX exporter.

disable_dynamic_axes_fix (`Optional[bool]`, defaults to `False`) : Whether to disable the default dynamic axes fixing.

dtype (`Optional[str]`, defaults to `None`) : Data type to remap the model inputs to. PyTorch-only. Only `fp16` is supported.

no_dynamic_axes (bool, defaults to `False`) : If True, disables the use of dynamic axes during ONNX export.

do_constant_folding (bool, defaults to `True`) : PyTorch-specific argument. If `True`, the PyTorch ONNX export will fold constants into adjacent nodes, if possible.

model_kwargs (`Optional[Dict[str, Any]]`, defaults to `None`) : Experimental usage: keyword arguments to pass to the model during the export. This argument should be used along the `custom_onnx_config` argument in case, for example, the model inputs/outputs are changed (for example, if `model_kwargs={"output_attentions": True}` is passed).

**Returns:**

``Tuple[List[str], List[str]]``

A tuple with an ordered list of the model's inputs, and the named outputs from
the ONNX configuration.

#### optimum.exporters.onnx.convert.export_pytorch[[optimum.exporters.onnx.convert.export_pytorch]]

[Source](https://github.com/huggingface/optimum-onnx/blob/vr_101/optimum/exporters/onnx/convert.py#L463)

Exports a PyTorch model to an ONNX Intermediate Representation.

**Parameters:**

model (`PreTrainedModel`) : The model to export.

config ([OnnxConfig](/docs/optimum/pr_101/en/onnx/package_reference/configuration#optimum.exporters.onnx.OnnxConfig)) : The ONNX configuration associated with the exported model.

opset (`int`) : The version of the ONNX operator set to use.

output (`Path`) : Path to save the exported ONNX file to.

device (`str`, defaults to `"cpu"`) : The device on which the ONNX model will be exported. Either `cpu` or `cuda`. Only PyTorch is supported for export on CUDA devices.

input_shapes (`Optional[Dict]`, defaults to `None`) : If specified, allows to use specific shapes for the example input provided to the ONNX exporter.

no_dynamic_axes (bool, defaults to `False`) : If True, disables the use of dynamic axes during ONNX export.

do_constant_folding (bool, defaults to `True`) : PyTorch-specific argument. If `True`, the PyTorch ONNX export will fold constants into adjacent nodes, if possible.

model_kwargs (`Optional[Dict[str, Any]]`, defaults to `None`) : Experimental usage: keyword arguments to pass to the model during the export. This argument should be used along the `custom_onnx_config` argument in case, for example, the model inputs/outputs are changed (for example, if `model_kwargs={"output_attentions": True}` is passed).

**Returns:**

``Tuple[List[str], List[str]]``

A tuple with an ordered list of the model's inputs, and the named outputs from
the ONNX configuration.

## Utility functions[[optimum.exporters.utils.check_dummy_inputs_are_allowed]]

#### optimum.exporters.utils.check_dummy_inputs_are_allowed[[optimum.exporters.utils.check_dummy_inputs_are_allowed]]

[Source](https://github.com/huggingface/optimum-onnx/blob/vr_101/optimum/exporters/utils.py#L621)

Checks that the dummy inputs from the ONNX config is a subset of the allowed inputs for `model`.

**Parameters:**

model (`PreTrainedModel` or `ModelMixin`) : The model instance.

model_inputs (`Iterable[str]`) : The model input names.

#### optimum.exporters.onnx.validate_model_outputs[[optimum.exporters.onnx.validate_model_outputs]]

[Source](https://github.com/huggingface/optimum-onnx/blob/vr_101/optimum/exporters/onnx/convert.py#L167)

Validates the export by checking that the outputs from both the reference and the exported model match.

**Parameters:**

config (`~OnnxConfig`) : The configuration used to export the model.

reference_model (`Union["PreTrainedModel", "ModelMixin"]`) : The model used for the export.

onnx_model (`Path`) : The path to the exported model.

onnx_named_outputs (`List[str]`) : The names of the outputs to check.

atol (`Optional[float]`, defaults to `None`) : The absolute tolerance in terms of outputs difference between the reference and the exported model.

input_shapes (`Optional[Dict]`, defaults to `None`) : If specified, allows to use specific shapes to validate the ONNX model on.

device (`str`, defaults to `"cpu"`) : The device on which the ONNX model will be validated. Either `cpu` or `cuda`. Validation on a CUDA device is supported only for PyTorch.

use_subprocess (`Optional[bool]`, defaults to `True`) : Launch validation of each exported model in a subprocess.

model_kwargs (`Optional[Dict[str, Any]]`, defaults to `None`) : Experimental usage: keyword arguments to pass to the model during the export and validation.