Implementation:Huggingface Optimum TasksManager Get Exporter Config
| Field | Value |
|---|---|
| Page Type | Implementation |
| Source Repository | https://github.com/huggingface/optimum |
| Source File | optimum/exporters/tasks.py
|
| Domains | NLP, Computer_Vision, Export |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
This implementation provides the concrete APIs for constructing backend-specific export configurations. It includes the TasksManager.get_exporter_config_constructor class method for registry lookup and the make_backend_config_constructor_for_task module-level helper for creating partially-applied configuration constructors.
API Reference
TasksManager.get_exporter_config_constructor
Source location: optimum/exporters/tasks.py, lines 1227-1312
Purpose: Retrieves the ExportConfigConstructor for a given model (or model type) and task combination, scoped to a specific export backend.
Signature:
@staticmethod
def get_exporter_config_constructor(
exporter: str,
model: Optional["PreTrainedModel"] = None,
task: str = "feature-extraction",
model_type: Optional[str] = None,
model_name: Optional[str] = None,
exporter_config_kwargs: Optional[Dict[str, Any]] = None,
library_name: Optional[str] = None,
) -> "ExportConfigConstructor":
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
exporter |
str |
required | The exporter backend to use (e.g., "onnx", "tflite").
|
model |
Optional[PreTrainedModel] |
None |
The model instance. Used to infer model_type if not provided.
|
task |
str |
"feature-extraction" |
The task to retrieve the config for. |
model_type |
Optional[str] |
None |
The model type string (e.g., "bert", "gpt2"). Inferred from model if not provided.
|
model_name |
Optional[str] |
None |
The name of the model, used only for exception messages. |
exporter_config_kwargs |
Optional[Dict[str, Any]] |
None |
Additional arguments to pass to the exporter config class constructor. |
library_name |
Optional[str] |
None |
The library name ("transformers", "timm", "diffusers", "sentence_transformers").
|
Returns: ExportConfigConstructor -- A callable that takes a PretrainedConfig and returns an ExporterConfig.
Internal logic:
- If
library_nameisNone, defaults to"transformers"and merges all library-specific supported model type dictionaries - If
model_typeisNone, extracts it frommodel.config.model_type(ormodel.config.export_model_typefor custom libraries) - Calls
get_supported_tasks_for_model_typeto validate the task is supported - Handles task synonym resolution (e.g.,
"default"may be a synonym for"feature-extraction") - Looks up the constructor from the nested registry:
supported_model_type[model_type][exporter][task] - If
exporter_config_kwargsis provided, wraps the constructor withfunctools.partial
make_backend_config_constructor_for_task
Source location: optimum/exporters/tasks.py, lines 52-59
Purpose: Creates a partially-applied ExportConfigConstructor from a config class and task string. Handles the special -with-past task suffix by setting use_past=True.
Signature:
def make_backend_config_constructor_for_task(
config_cls: Type,
task: str,
) -> "ExportConfigConstructor":
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
config_cls |
Type |
required | The ExporterConfig subclass to create a constructor for.
|
task |
str |
required | The task string (e.g., "text-classification", "text-generation-with-past").
|
Returns: ExportConfigConstructor -- A functools.partial object that, when called with a PretrainedConfig, returns an ExporterConfig instance.
Internal logic:
def make_backend_config_constructor_for_task(config_cls: Type, task: str) -> "ExportConfigConstructor":
if "-with-past" in task:
if not getattr(config_cls, "SUPPORTS_PAST", False):
raise ValueError(f"{config_cls} does not support tasks with past.")
constructor = partial(config_cls, use_past=True, task=task.replace("-with-past", ""))
else:
constructor = partial(config_cls, task=task)
return constructor
Import
from optimum.exporters import TasksManager
from optimum.exporters.tasks import make_backend_config_constructor_for_task
Input/Output Summary
| API | Input | Output |
|---|---|---|
get_exporter_config_constructor |
Exporter name, model/model_type, task | ExportConfigConstructor callable
|
make_backend_config_constructor_for_task |
Config class, task string | ExportConfigConstructor (partial)
|
Usage Example
from optimum.exporters import TasksManager
from transformers import AutoConfig
# Get the ONNX export config constructor for a BERT model
config_constructor = TasksManager.get_exporter_config_constructor(
exporter="onnx",
model_type="bert",
task="text-classification",
library_name="transformers",
)
# Build the export configuration
model_config = AutoConfig.from_pretrained("bert-base-uncased")
export_config = config_constructor(model_config)
# The export_config now contains input/output specs, dynamic axes, etc.
print(export_config.inputs) # e.g., {"input_ids": {0: "batch", 1: "sequence"}, ...}
print(export_config.outputs) # e.g., {"logits": {0: "batch"}}