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 Value Head

From Leeroopedia


Knowledge Sources
Domains Reinforcement Learning, Reward Modeling
Last Updated 2026-02-06 19:00 GMT

Overview

Loads and prepares value head parameters for RLHF reward model training pipelines.

Description

The valuehead module provides two functions for value head management in RLHF workflows. load_valuehead_params loads value head weights (v_head.summary.weight and v_head.summary.bias) from either safetensors or PyTorch binary checkpoint files, supporting both local paths and HuggingFace Hub repository IDs. It first attempts safetensors loading and falls back to the standard PyTorch format. prepare_valuehead_model patches model-specific LM head references for non-standard architectures (LLaVA, ChatGLM, InternLM2) so that TRL's AutoModelForCausalLMWithValueHead wrapper can correctly locate the output embeddings layer.

Usage

Use load_valuehead_params when resuming training of a reward model or value head model to restore the value head weights from a checkpoint. Use prepare_valuehead_model when initializing a model for PPO or reward model training to ensure the LM head is correctly exposed for the value head wrapper.

Code Reference

Source Location

Signature

def load_valuehead_params(
    path_or_repo_id: str,
    model_args: "ModelArguments",
) -> dict[str, torch.Tensor]

def prepare_valuehead_model(
    model: "PreTrainedModel",
) -> None

Import

from llamafactory.model.model_utils.valuehead import load_valuehead_params, prepare_valuehead_model

I/O Contract

Inputs

Name Type Required Description
path_or_repo_id str Yes Local path or HuggingFace Hub repository ID containing value head weights
model_args ModelArguments Yes Contains cache_dir and hf_hub_token for downloading from HuggingFace Hub
model PreTrainedModel Yes The pretrained model to prepare for value head attachment (for prepare_valuehead_model)

Outputs

Name Type Description
params dict[str, torch.Tensor] or None Dictionary with keys v_head.summary.weight and v_head.summary.bias, or None if no value head weights are found
None None prepare_valuehead_model modifies the model in-place, setting lm_head and _keys_to_ignore_on_save

Usage Examples

# Loading value head parameters from a checkpoint
from llamafactory.model.model_utils.valuehead import load_valuehead_params

vhead_params = load_valuehead_params("path/to/reward_model", model_args)
if vhead_params is not None:
    model.load_state_dict(vhead_params, strict=False)

Related Pages

Page Connections

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