Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Hiyouga LLaMA Factory Attention Config

From Leeroopedia
Revision as of 15:05, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Hiyouga_LLaMA_Factory_Attention_Config.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Attention Mechanisms, Model Configuration
Last Updated 2026-02-06 19:00 GMT

Overview

Configures and reports the attention implementation strategy for pretrained models, mapping user preferences to HuggingFace's internal attention backend settings.

Description

This module provides two functions for managing attention implementations. configure_attn_implementation translates user-specified attention preferences (auto, disabled/eager, SDPA, FlashAttention-2, FlashAttention-3) into the appropriate HuggingFace config attributes. It includes model-specific handling for architectures with special requirements: Gemma 2 requires FlashAttention-2 for soft-capping support, GPT-OSS uses FlashAttention-3 with hub kernels, InternLM2 uses a non-standard config attribute, and Kimi-VL and Youtu-VL require per-submodel configuration. print_attn_implementation inspects the loaded model configuration to report which attention backend is actually in use.

Usage

Use configure_attn_implementation during model config patching (called automatically by the model loader) to set the desired attention backend. Use print_attn_implementation after model loading to verify which attention implementation was selected.

Code Reference

Source Location

Signature

def configure_attn_implementation(
    config: "PretrainedConfig",
    model_args: "ModelArguments",
) -> None:
    ...

def print_attn_implementation(
    config: "PretrainedConfig",
) -> None:
    ...

Import

from llamafactory.model.model_utils.attention import configure_attn_implementation, print_attn_implementation

I/O Contract

Inputs

Name Type Required Description
config PretrainedConfig Yes Model configuration object to modify with the attention implementation setting
model_args ModelArguments Yes (for configure) Model arguments containing flash_attn setting (AttentionFunction enum: AUTO, DISABLED, SDPA, FA2, FA3)

Outputs

Name Type Description
(side effect) None Modifies config._attn_implementation (or model-specific equivalent) in-place
(side effect) None May modify model_args.flash_attn in-place for model-specific overrides (e.g., Gemma 2)

Usage Examples

from llamafactory.model.model_utils.attention import configure_attn_implementation, print_attn_implementation

# Configure attention for FlashAttention-2
configure_attn_implementation(config, model_args)

# After model loading, verify the attention backend
print_attn_implementation(config)
# Output: "Using FlashAttention-2 for faster training and inference."
# or:    "Using torch SDPA for faster training and inference."
# or:    "Using vanilla attention implementation."

Related Pages

Page Connections

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