Skip to content

Add cache to process inputs #1111

Open
Open

Description

@nanjiangwill

We currently apply apply-chat-template and process inputs in two places:

This causes duplicated computation and unnecessary overhead.

Why Two Stages Are Necessary

We cannot merge these into a single stage(right now, open to optimize):

  • Data level: required to check context length early (with tokenization).
  • Rollout level: performs tokenization and handles multimodal inputs.

Proposed Solution

Introduce caching to avoid duplicate work.

  1. Add a cache at the data level for processed chat templates.
  2. At the rollout level, reuse the cached result when available.

Activity

  1. liuhao-labs commented on Aug 23, 2026

    @liuhao-labs
    Contributor

    Hi, I rechecked this against current main. The duplicate work is still present: filter_long_prompt() tokenizes retained text prompts only to test their length, discards those IDs, and _prepare_prompt_ids() encodes the same rendered prompt again during the default rollout. #1113 improved tokenization correctness but did not retain or reuse those IDs.

    I can submit a focused CPU-tested fix that caches only retained text prompt IDs when max-length filtering is enabled and consumes that cache in the default rollout path. I would intentionally leave multimodal processor outputs uncached: retaining pixel tensors for the whole Dataset and deepcopying them for every sampled sibling would create a much larger host-memory regression.

    Would this text-only, default-rollout scope be the preferred first PR, or would you prefer a compact cache representation/API that custom rollout functions can explicitly opt into? I will keep max_length=None, custom rollout, and multimodal behavior unchanged.

  2. nanjiangwill commented on Aug 23, 2026

    @nanjiangwill
    CollaboratorAuthor

    replied in #2309 (comment)

    Hi, I rechecked this against current main. The duplicate work is still present: filter_long_prompt() tokenizes retained text prompts only to test their length, discards those IDs, and _prepare_prompt_ids() encodes the same rendered prompt again during the default rollout. #1113 improved tokenization correctness but did not retain or reuse those IDs.

    I can submit a focused CPU-tested fix that caches only retained text prompt IDs when max-length filtering is enabled and consumes that cache in the default rollout path. I would intentionally leave multimodal processor outputs uncached: retaining pixel tensors for the whole Dataset and deepcopying them for every sampled sibling would create a much larger host-memory regression.

    Would this text-only, default-rollout scope be the preferred first PR, or would you prefer a compact cache representation/API that custom rollout functions can explicitly opt into? I will keep max_length=None, custom rollout, and multimodal behavior unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions