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 Processor Utils

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


Knowledge Sources
Domains Data Processing, Algorithms
Last Updated 2026-02-06 19:00 GMT

Overview

Provides the abstract DatasetProcessor base class and utility functions for sequence length inference and greedy knapsack packing used by all concrete dataset processors.

Description

This module defines the foundational DatasetProcessor abstract base class, which holds references to the template, tokenizer, processor, and data arguments, and declares the preprocess_dataset and print_data_example abstract methods that all concrete processors must implement. It also provides three critical utility functions: infer_seqlen intelligently distributes a cutoff length budget between source (prompt) and target (response) sequences, prioritizing the shorter component; search_for_fit performs binary search to find the largest number fitting a given capacity; and greedy_knapsack implements an efficient bin-packing algorithm that groups variable-length sequences into fixed-capacity bins for sequence packing during training.

Usage

Use DatasetProcessor as the base class when implementing a new dataset processor. Use infer_seqlen whenever you need to truncate prompt-response pairs to fit within a maximum sequence length. Use greedy_knapsack when implementing packed training to efficiently group multiple shorter examples into fixed-length batches.

Code Reference

Source Location

Signature

@dataclass
class DatasetProcessor(ABC):
    template: "Template"
    tokenizer: "PreTrainedTokenizer"
    processor: Optional["ProcessorMixin"]
    data_args: "DataArguments"

    @abstractmethod
    def preprocess_dataset(self, examples: dict[str, list[Any]]) -> dict[str, list[Any]]: ...

    @abstractmethod
    def print_data_example(self, example: dict[str, list[int]]) -> None: ...

def search_for_fit(numbers: list[int], capacity: int) -> int

def greedy_knapsack(numbers: list[int], capacity: int) -> list[list[int]]

def infer_seqlen(source_len: int, target_len: int, cutoff_len: int) -> tuple[int, int]

Import

from llamafactory.data.processor.processor_utils import DatasetProcessor, infer_seqlen, greedy_knapsack, search_for_fit

I/O Contract

Inputs

Name Type Required Description
source_len int Yes Length of the source (prompt) token sequence (for infer_seqlen)
target_len int Yes Length of the target (response) token sequence (for infer_seqlen)
cutoff_len int Yes Maximum allowed total sequence length (for infer_seqlen)
numbers list[int] Yes List of sequence lengths to pack (for greedy_knapsack)
capacity int Yes Maximum bin capacity (for greedy_knapsack)

Outputs

Name Type Description
(new_source_len, new_target_len) tuple[int, int] Truncated source and target lengths that fit within cutoff_len (from infer_seqlen)
knapsacks list[list[int]] Groups of sequence lengths packed into bins (from greedy_knapsack)
index int Index of the largest fitting number, or -1 if none fits (from search_for_fit)

Usage Examples

from llamafactory.data.processor.processor_utils import infer_seqlen, greedy_knapsack

# Distribute a cutoff budget of 2048 between prompt and response
source_len, target_len = infer_seqlen(source_len=500, target_len=2000, cutoff_len=2048)
# Result: source is truncated minimally, target gets most of the budget

# Pack sequences of varying lengths into bins of capacity 4096
lengths = [1024, 2048, 512, 3000, 1500, 800]
bins = greedy_knapsack(lengths, capacity=4096)
# Each bin in bins is a list of lengths that sum to <= 4096

Related Pages

Page Connections

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