Agent skill
hf-generate-internals
HF Transformers generate() internals — scores vs logits, LogitsProcessors, KV cache, StoppingCriteria, chat templates. Use when code calls model.generate(), output_scores, output_logits, return_dict_in_generate, GenerateDecoderOnlyOutput, LogitsProcessor, StoppingCriteria, past_key_values, DynamicCache, apply_chat_template, do_sample, or num_beams.
Install this agent skill to your Project
npx add-skill https://github.com/JoaquinCampo/Skills/tree/main/hf-generate-internals
SKILL.md
HuggingFace Transformers Generation Internals
Reference for HF Transformers generation pipeline internals, verified against
transformers 5.3.0 source code. Covers the full path from model.generate()
to output tensors.
Extension Files
| File | Content |
|---|---|
references/generate-loop.md |
generate() entry point, mode dispatch, _sample() loop structure, token selection, prefill |
references/scores-and-logits.md |
output_scores vs output_logits, tensor shapes, float32 conversion, GenerateDecoderOnlyOutput, memory, compute_transition_scores() |
references/logits-processors.md |
LogitsProcessorList, processing order, always-active vs sampling-only processors, key implementations |
references/kv-cache.md |
Cache class hierarchy, DynamicCache, attention mask / position_id updates, kvpress interaction |
references/stopping-criteria.md |
StoppingCriteria interface, built-in criteria, custom stopping, when checked in the loop |
references/chat-templates.md |
apply_chat_template(), Jinja2 rendering, add_generation_prompt, continue_final_message, tokenization patterns |
Quick Decision Tree
What do you need from generation?
│
├─ Per-token logit features → output_scores=True (see scores-and-logits.md)
│ └─ Need truly raw logits? → output_logits=True (bypasses LogitsProcessors)
│
├─ Custom stopping logic → StoppingCriteria subclass (see stopping-criteria.md)
│
├─ Modify token probabilities → LogitsProcessor subclass (see logits-processors.md)
│
├─ KV cache compression/debug → past_key_values / DynamicCache (see kv-cache.md)
│
├─ Chat-formatted prompts → tokenizer.apply_chat_template() (see chat-templates.md)
│
└─ Understand the loop itself → _sample() internals (see generate-loop.md)
Critical Facts
-
scores != logits:
.scoresare post-LogitsProcessor,.logitsare raw model output. Both are pre-softmax. Withdo_sample=Falseand no custom processors, they're identical. -
Greedy and sampling share
_sample(): In v5.3.0, there is no separate_greedy_search(). Thedo_sampleflag controls behavior inside the same method. -
Always float32: Logits are converted to float32 before processing (line 2754), regardless of model precision (float16/bfloat16).
-
Score shape: Each element of
outputs.scoresis(batch_size, vocab_size)— one tensor per generated token. -
Memory cost: For Qwen2.5-7B (vocab=152064), 512 tokens of scores costs ~296 MB per batch element in float32.
-
Sampling processors are gated: Temperature, top-k, top-p warpers are only added when
do_sample=True. With greedy decoding, scores pass through the pipeline essentially unmodified. -
Left-pad required: Decoder-only models require left-padding for batched generation. HF warns if right-padding is detected.
Review Checklist
□ Using return_dict_in_generate=True with output_scores=True?
□ Indexing scores correctly? scores[t] is (batch, vocab), not (vocab,)
□ Know whether scores are pre- or post-processor for your use case?
□ Not holding all score tensors on GPU unnecessarily? (move to CPU or process inline)
□ Padding direction correct for batched generation? (left-pad for decoder-only)
□ StoppingCriteria returns BoolTensor of shape (batch,)?
□ Custom LogitsProcessor signature: (input_ids: LongTensor, scores: FloatTensor) -> FloatTensor?
□ Chat template rendered with add_generation_prompt=True for generation?
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
sparse-retrieval-eval
Evaluate sparse retrieval models on standard IR benchmarks (BEIR, MIRACL, mMARCO). Covers all IR metrics (nDCG@k, Recall@k, MAP, MRR), dataset loading, sparse corpus encoding to CSR matrices, IDF-weighted retrieval, caching, and result interpretation. Triggers on: evaluate retrieval, BEIR benchmark, nDCG, recall@k, sparse retrieval evaluation, MIRACL evaluation, information retrieval metrics, IR evaluation, search quality metrics.
wandb-plot
Download and generate plots from Weights & Biases runs. Use when you need to: - List projects you have access to - List runs in a W&B project - Inspect available metrics for a run - Download existing plot images from a run - Generate line plots from metric history (loss, accuracy, etc.)
go
Go engineering best practices and idioms. This skill should be used when writing, reviewing, or refactoring Go code. Triggers on any .go file work, Go module operations, go test, go build, go run, go vet, go generate, or when the user mentions Go, golang, goroutines, channels, context, Go interfaces, Go error handling, Go testing, or Go concurrency. ALWAYS use this skill when Go code is involved, even for simple functions.
fastapi
FastAPI best practices and conventions. Use when working with FastAPI APIs and Pydantic models for them. Keeps FastAPI code clean and up to date with the latest features and patterns, updated with new versions. Write new code or refactor and update old code.
kvpress
kvpress (NVIDIA) KV-cache compression for HuggingFace LLMs. Use when: kvpress imports, compression_ratio, press(model) context managers, StreamingLLMPress, SnapKVPress, ExpectedAttentionPress, TOVAPress, KnormPress, KV-cache eviction, token pruning during generation, or attention sink methods.
plantuml
Create, edit, and render PlantUML diagrams. Triggers on: architecture diagrams, flowcharts, sequence diagrams, data models, state machines, visual documentation.
Didn't find tool you were looking for?