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:Huggingface Open r1 Get Model and Tokenizer

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


Knowledge Sources
Domains NLP, Model_Architecture
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for loading pretrained causal language models and tokenizers with quantization and attention backend configuration provided by Open-R1.

Description

Two factory functions get_tokenizer and get_model that wrap HuggingFace Transformers' AutoTokenizer and AutoModelForCausalLM with Open-R1-specific defaults. get_tokenizer loads the tokenizer and optionally overrides the chat template. get_model handles dtype resolution, quantization config, attention implementation, and KV cache settings.

  • get_tokenizer loads an AutoTokenizer from the specified model path and revision, respects trust_remote_code, and optionally overrides the chat template if one is set in the training arguments.
  • get_model resolves the torch_dtype (handling the "auto" string or mapping to a torch dtype), obtains a quantization config via TRL's get_quantization_config, builds a keyword dictionary that includes attention implementation, caching behavior (disabled when gradient checkpointing is on), and device map (set only when quantization is active), then loads the model with AutoModelForCausalLM.from_pretrained.

Usage

Import these functions when setting up a model for SFT or GRPO training in the Open-R1 pipeline. They are the standard model and tokenizer constructors used across all training scripts in the project.

Code Reference

Property Value
Repository open-r1
File src/open_r1/utils/model_utils.py
Lines L9-42
Import from open_r1.utils import get_model, get_tokenizer

Function Signatures

def get_tokenizer(model_args: ModelConfig, training_args: SFTConfig | GRPOConfig) -> PreTrainedTokenizer:
    """Get the tokenizer for the model."""
def get_model(model_args: ModelConfig, training_args: SFTConfig | GRPOConfig) -> AutoModelForCausalLM:
    """Get the model"""

I/O Contract

Inputs

Parameter Type Required Description
model_args ModelConfig Yes Model configuration containing:
  • model_name_or_path -- HuggingFace Hub model ID or local path
  • model_revision -- specific model revision/commit to load
  • trust_remote_code -- whether to allow custom model code
  • torch_dtype -- numerical precision (bfloat16, float16, auto)
  • attn_implementation -- attention backend (flash_attention_2, sdpa, eager)
training_args GRPOConfig Yes Training configuration containing:
  • chat_template -- optional chat template override for the tokenizer
  • gradient_checkpointing -- controls KV cache disabling during training

Outputs

Function Return Type Description
get_tokenizer PreTrainedTokenizer Configured tokenizer with optional chat template override applied
get_model AutoModelForCausalLM Configured causal language model with dtype, quantization, attention backend, and KV cache settings applied

Usage Examples

Loading a Model with bfloat16 and Flash Attention

from open_r1.utils import get_model, get_tokenizer
from open_r1.configs import ModelConfig
from trl import SFTConfig

# Configure model arguments
model_args = ModelConfig(
    model_name_or_path="Qwen/Qwen2.5-7B",
    torch_dtype="bfloat16",
    attn_implementation="flash_attention_2",
    trust_remote_code=True,
)

# Configure training arguments
training_args = SFTConfig(
    output_dir="./output",
    gradient_checkpointing=True,
)

# Load tokenizer and model
tokenizer = get_tokenizer(model_args, training_args)
model = get_model(model_args, training_args)

Related Pages

Page Connections

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