Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Huggingface Optimum TasksManager Get Exporter Config

From Leeroopedia
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:

  1. If library_name is None, defaults to "transformers" and merges all library-specific supported model type dictionaries
  2. If model_type is None, extracts it from model.config.model_type (or model.config.export_model_type for custom libraries)
  3. Calls get_supported_tasks_for_model_type to validate the task is supported
  4. Handles task synonym resolution (e.g., "default" may be a synonym for "feature-extraction")
  5. Looks up the constructor from the nested registry: supported_model_type[model_type][exporter][task]
  6. If exporter_config_kwargs is provided, wraps the constructor with functools.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"}}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment