This page provides an overview of advanced capabilities in the Transformers library for model optimization and deployment. These features enable loading and running models efficiently in production environments through techniques like quantization, weight conversion, adapter integration, model compilation, and conversational handling.
For basic model loading and inference, see Core Architecture. For training-specific features, see Training System. For text generation capabilities, see Generation System.
The Advanced Features subsystem encompasses six primary capability areas:
HfQuantizer system.WeightConverter and ConversionOps.PeftAdapterMixin.torch.compile and static caching for production.apply_chat_template.Agent and Tool classes.The following diagram illustrates how advanced features hook into the core model loading and execution flow.
Sources: src/transformers/quantizers/base.py73-84 src/transformers/conversion_mapping.py42-141 src/transformers/integrations/peft.py57-93 src/transformers/core_model_loading.py86-105
The quantization system reduces model memory footprint by representing weights and activations with lower precision. The library supports a wide array of methods through the HfQuantizer base class src/transformers/quantizers/base.py73-77 and specialized implementations.
The library integrates with multiple backends to provide various quantization flavors:
Bnb4BitHfQuantizer and Bnb8BitHfQuantizer tests/quantization/bnb/test_4bit.py128-133Mxfp4Quantize, Mxfp4Dequantize, and Mxfp4Deserialize operations src/transformers/integrations/mxfp4.py49-173requires_calibration = True in the quantizer src/transformers/quantizers/base.py86For detailed configuration and calibration details, see Quantization System.
The weight conversion system provides a structured way to translate weights between different architectural implementations using WeightConverter src/transformers/core_model_loading.py31
The system uses ConversionOps to define transformations src/transformers/core_model_loading.py86-93 Core operations include:
WeightRenaming or PrefixChange src/transformers/conversion_mapping.py149-158For implementation details and reverse conversion for saving, see Weight Conversion System.
The library natively integrates with parameter-efficient fine-tuning (PEFT) through the PeftAdapterMixin src/transformers/integrations/peft.py57-74
load_adapter from local paths or the Hub src/transformers/integrations/peft.py80-93torch.compile recompilation src/transformers/integrations/peft.py121-127enable_adapters(), disable_adapters(), and set_adapter() to control active weights at runtime tests/peft_integration/test_peft_integration.py165-171build_peft_weight_mapping handles complex weight mappings for Mixture-of-Expert layers.For details on adapter state management, see PEFT and Adapter Integration.
For high-performance production deployment, the library provides tools to optimize the execution graph.
For optimization strategies, see Model Export and Compilation.
The library handles the complexity of different chat prompt formats through the chat template system, typically defined in tokenizer_config.json.
chat_parsing_utils.py.For template syntax and tool-calling details, see Chat Templates and Conversation Handling.
The transformers.agents system allows models to interact with external environments by executing code or calling tools.
transformers serve command to expose agentic capabilities via an OpenAI-compatible API.For building agentic workflows, see Agents and Tools System.
Sources: src/transformers/core_model_loading.py86-181 src/transformers/conversion_mapping.py42-158 src/transformers/integrations/peft.py57-145 src/transformers/quantizers/base.py73-98 src/transformers/integrations/mxfp4.py49-176 tests/peft_integration/test_peft_integration.py90-175
Refresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.