hf-generate-internals

Analyze HuggingFace Transformers generate() internals from prompt to output.

3|Updated Dec 22, 2025
One-click install
npx skills add https://github.com/JoaquinCampo/Skills --skill hf-generate-internals
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: hf-generate-internals
Source: https://github.com/JoaquinCampo/Skills/tree/main/hf-generate-internals
Command: npx skills add https://github.com/JoaquinCampo/Skills --skill hf-generate-internals

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps engineers understand the internal flow of HuggingFace Transformers generate(), including the distinction between post-processor scores and raw logits, how LogitsProcessors shape outputs, how the KV cache is managed, and how StoppingCriteria and chat templates interact, enabling deeper debugging and safer customization.

Core Features & Use Cases

  • Reference for generate() internals across the prefill and decode stages, aligned with transformers 5.3.0.
  • Clarifies how scores, logits, and processor pipelines relate and how KV caches evolve token by token.
  • Guides debugging and optimization scenarios, including memory considerations and custom stopping logic.

Quick Start

Review the Generate internals guide to instrument your own generation workflow and observe scores, logits, and cache updates during generation.

Frequently Asked Questions about hf-generate-internals

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What is the difference between raw logits and post-processor scores in HuggingFace generate()?

Raw logits are direct model outputs before any modification, while scores are logits after passing through LogitsProcessors. Understanding this distinction is crucial for debugging HuggingFace generate() pipelines and safely customizing the logits processor ordering.

How does the KV cache update token by token during the HuggingFace generate() loop?

The KV cache evolves through prefill and decoding stages, managed via DynamicCache and past_key_values. It stores previous token states to avoid recomputation, making memory management critical for long generation sequences and performance optimization.

How do I debug HuggingFace Transformers generate() output_scores and output_logits?

Debug generate() by enabling output_scores, output_logits, and return_dict_in_generate to capture GenerateDecoderOnlyOutput. This exposes raw logits, processed scores, and cache interactions at each decoding step for detailed pipeline analysis and optimization.

Can I use custom StoppingCriteria and chat templates with HuggingFace generate()?

Yes, StoppingCriteria and chat templates interact within the generate() loop to control termination and format inputs. Custom stopping logic can be safely implemented by understanding how these components evaluate conditions during the decoding phase.

What are the memory limitations of DynamicCache during long text generation?

DynamicCache memory grows linearly with generated sequence length during decoding. Limitations arise in long generation runs where accumulated past_key_values consume significant VRAM, requiring careful memory management and processor pipeline optimization.