Implementation:Huggingface Optimum OptimizedModel From Pretrained
Overview
Implementation of the OptimizedModel abstract base class providing a unified from_pretrained interface for loading models into backend-specific optimized formats, with optional on-the-fly export.
Source
File: optimum/modeling_base.py
Repository: optimum
APIs
from_pretrained (L308-419)
@classmethod
@add_start_docstrings(FROM_PRETRAINED_START_DOCSTRING)
def from_pretrained(
cls,
model_id: Union[str, Path],
config: Optional[PretrainedConfig] = None,
export: bool = False,
# hub options
subfolder: str = "",
revision: str = "main",
force_download: bool = False,
local_files_only: bool = False,
trust_remote_code: bool = False,
cache_dir: str = HUGGINGFACE_HUB_CACHE,
token: Optional[Union[bool, str]] = None,
**kwargs,
) -> "OptimizedModel":
Description: Main entry point for loading optimized models. Resolves configuration, determines loading method, and delegates to the appropriate backend-specific loading function.
__call__ (L108-109)
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
Description: Makes the model callable. Delegates directly to forward().
forward (L111-116)
@abstractmethod
def forward(self, *args, **kwargs):
"""
Forward pass of the model, needs to be overwritten.
"""
raise NotImplementedError
Description: Abstract method that must be implemented by all backend-specific subclasses. Defines the actual inference execution path.
_from_pretrained (L270-284)
@classmethod
def _from_pretrained(
cls,
model_id: Union[str, Path],
subfolder: str = "",
revision: str = "main",
force_download: bool = False,
local_files_only: bool = False,
trust_remote_code: bool = False,
cache_dir: str = HUGGINGFACE_HUB_CACHE,
token: Optional[Union[bool, str]] = None,
**kwargs,
) -> "OptimizedModel":
Description: Abstract method for loading pre-exported optimized models. Subclasses must override this.
_export (L287-304)
@classmethod
def _export(
cls,
model_id: Union[str, Path],
config: PretrainedConfig,
subfolder: str = "",
revision: str = "main",
force_download: bool = False,
local_files_only: bool = False,
trust_remote_code: bool = False,
cache_dir: str = HUGGINGFACE_HUB_CACHE,
token: Optional[Union[bool, str]] = None,
**kwargs,
) -> "OptimizedModel":
Description: Abstract method for on-the-fly export from a vanilla HuggingFace model. Subclasses must override this.
Import
from optimum.modeling_base import OptimizedModel
I/O
| Direction | Type | Description |
|---|---|---|
| Input: model_id | Union[str, Path]
|
Model identifier (Hub repo ID) or local path to a model directory |
| Input: config | Optional[PretrainedConfig]
|
Model configuration. Auto-loaded from the model repository if None.
|
| Input: export | bool
|
When True, exports a vanilla HuggingFace model to the optimized format on-the-fly
|
| Output | OptimizedModel
|
The loaded (or exported) optimized model instance |
Internal Logic of from_pretrained
The from_pretrained method at L308-419 follows this sequence:
- Path normalization (L328-329): Converts
Pathobjects to POSIX strings - Revision parsing (L331-339): Handles deprecated
model_id@revisionsyntax - File discovery (L341-347): Uses
TasksManager.get_model_files()to list available model files - Config folder resolution (L349-354): Falls back to top-level directory if config is not found in subfolder
- Library detection (L356-358): Infers the source library (transformers, timm, etc.)
- Config loading (L365-392): Resolves the model configuration from multiple possible sources
- Export decision (L394-405): Selects the loading method based on the
exportflag:
if export:
if hasattr(cls, "_from_transformers"):
# legacy support for models that implement `_from_transformers`
from_pretrained_method = cls._from_transformers
elif hasattr(cls, "_export"):
from_pretrained_method = cls._export
else:
raise ValueError(
"The `export` argument is set to `True`, but the class does not implement `_export` methods."
)
else:
from_pretrained_method = cls._from_pretrained
- Delegation (L407-419): Calls the selected method with all parameters forwarded
Constructor (L98-106)
def __init__(
self,
model: "PreTrainedModel",
config: "PretrainedConfig",
preprocessors: Optional[List["PreprocessorT"]] = None,
):
self.model = model
self.config = config
self.preprocessors = preprocessors or []
The constructor stores the backend-specific model object, the HuggingFace configuration, and an optional list of preprocessors (tokenizers, feature extractors, image processors).