Skip to content

huggingface/trl

Train transformer language models with reinforcement learning.

https://skillcdn.ai/gh/huggingface/trl

Connect this address to your AI to use this skill. How to connect

  • Unverified
  • Default branchmain
  • Commit7d2b4d5
  • LicenseApache-2.0
  • Searchwords

Showing commit 7d2b4d5

The newest commit, 56f4939, is being indexed. Until it is ready, this page and every agent are served the last indexed commit.
View on GitHub

check-new-model

Check that a newly released model, or a new revision of its Hub files, works with every TRL trainer, then report the gaps and recommendations for the model provider. Use when a model is released, when its chat template or config changes, or when asked whether TRL supports a model.

Path
.agents/skills/check-new-model/SKILL.md
License
Apache-2.0

Warnings for the author

  • Not listed or searched: a skill under a hidden directory is discoverable only when the repository has no visible skill.

Check a new model against TRL

Launch support usually covers "loads, trains a step, template recognized". The checks below are what past releases got wrong anyway, often for months. Most run on the tokenizer and config alone, or on a tiny random-weight model built from the real config, so they work before the weights are public. Run them on every checkpoint of the release (sizes, base and instruct, quantized mirrors), not only one. The snippets are starting points: adapt them to the model (its template kwargs, its tool-call format).

import torch, transformers
from transformers import AutoConfig, AutoTokenizer, GenerationConfig, ProcessorMixin
from trl.chat_template_utils import (
    add_response_schema, get_training_chat_template, is_chat_template_prefix_preserving,
    is_chat_template_stop_token_trained, parse_response, supports_tool_calling,
)

tok = AutoTokenizer.from_pretrained(model_id, revision=sha)  # for a VLM, AutoProcessor, and its .tokenizer for ids
config = AutoConfig.from_pretrained(model_id, revision=sha)
gen = GenerationConfig.from_pretrained(model_id, revision=sha)
eos = gen.eos_token_id if isinstance(gen.eos_token_id, list) else [gen.eos_token_id]
text_tok = tok.tokenizer if isinstance(tok, ProcessorMixin) else tok
render = lambda msgs, **kw: tok.apply_chat_template(msgs, tokenize=False, **kw)
ids = lambda text: text_tok(text, add_special_tokens=False)["input_ids"]

user = [{"role": "user", "content": "Hi"}]
turn = user + [{"role": "assistant", "content": "Hello", "reasoning_content": "Greet."}]
tools = [{"type": "function", "function": {"name": "lookup", "description": "Look up a zip code", "parameters": {
    "type": "object", "properties": {"zip": {"type": "string"}}, "required": ["zip"]}}}]
call = {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "lookup", "arguments": {"zip": "75001"}}}]}

Chat template

A model that ships {% generation %} markers, a prefix-preserving template and a native response_template needs nothing stored in TRL. Otherwise TRL falls back to its stored copies, matched by text (the default variant of named variants, ignoring a single trailing newline), so every checkpoint's template has to match one:

from pathlib import Path
shipped = tok.chat_template["default"] if isinstance(tok.chat_template, dict) else tok.chat_template
known = {p.read_text().removesuffix("\n"): p.name for p in Path("trl/chat_templates").glob("*.jinja")}
print(known.get(shipped.removesuffix("\n"), "not stored"))

TRL trains with its stored training template when it has one, which already fixes the gaps it knows about (Qwen3 dropping reasoning from history), so the checks below run on that template. SFT asks for it without prefix-preservation, so that call raising means assistant_only_loss=True fails. GRPO, AsyncGRPO and Distillation with tools ask for it only when the shipped template isn't prefix-preserving after a tool turn, so the default call raising there means they fail:

template = get_training_chat_template(tok, require_prefix_preservation=False) or shipped
if supports_tool_calling(tok) and not is_chat_template_prefix_preserving(tok):  # TRL rejects tools otherwise
    get_training_chat_template(tok)

The generation prompt is a token prefix of the rendered turn, for every setting the template reads (enable_thinking, reasoning_effort, ...), with and without reasoning (reasoning with thinking off is a contradictory input, so skip it):

answer = [*user, {"role": "assistant", "content": "Hello"}]
for kw, msgs in (({}, turn), ({}, answer), ({"enable_thinking": False}, answer)):
    prompt = ids(render(user, add_generation_prompt=True, chat_template=template, **kw))
    assert ids(render(msgs, chat_template=template, **kw))[: len(prompt)] == prompt, kw

For the tool loop, appending a tool result leaves the earlier assistant turn unchanged (dropping reasoning from history breaks it). Probe it with token ids and a generation prompt, as TRL does:

if supports_tool_calling(tok):  # otherwise report that tools are unsupported and skip this check
    tool_call = {**call, "content": ""}
    tool_result = {"role": "tool", "name": "lookup", "content": "75001"}
    prefix = tok.apply_chat_template([*user, tool_call], chat_template=template, tokenize=True, return_dict=False)
    full = tok.apply_chat_template(
        [*user, tool_call, tool_result], chat_template=template, tokenize=True, return_dict=False,
        add_generation_prompt=True,
    )
    if isinstance(tok, ProcessorMixin):  # processors return a batch of one
        prefix, full = prefix[0], full[0]
    assert full[: len(prefix)] == prefix

The assistant mask starts right after the generation prompt (a prefilled <think> is not trained) and ends on the end-of-turn token:

out = tok.apply_chat_template(
    turn, chat_template=template, tokenize=True, return_dict=True, return_assistant_tokens_mask=True
)
token_ids, mask = out["input_ids"], out["assistant_masks"]
if isinstance(tok, ProcessorMixin):  # processors return a batch of one
    token_ids, mask = token_ids[0], mask[0]
print(text_tok.decode([t for t, m in zip(token_ids, mask) if m]))
assert is_chat_template_stop_token_trained(tok, template)

Small things that break rendering in training but not in a chat demo:

if isinstance(tok, ProcessorMixin):  # TRL sends content as parts to processors only
    assert render([{"role": "user", "content": [{"type": "text", "text": "Hi"}]}]) == render(user)
assert "SYSTEM_SENTINEL" in render([{"role": "system", "content": "SYSTEM_SENTINEL"}, *user])
if supports_tool_calling(tok):  # otherwise report that tools are unsupported and skip these checks
    assert render(user, tools=[]) == render(user)
    assert len({render([*user, {**call, "content": c}]) for c in (None, "")} | {render([*user, call])}) == 1
    calls = [{"type": "function", "function": {"name": n, "arguments": {"zip": "75001"}}} for n in ("lookup_a", "lookup_b")]
    assert all(n in render([*user, {"role": "assistant", "tool_calls": calls}]) for n in ("lookup_a", "lookup_b"))
    json_args = {"type": "function", "function": {"name": "lookup", "arguments": '{"zip": "75001"}'}}
    assert render([*user, {**call, "tool_calls": [json_args]}]) == render([*user, call])  # some callers send JSON strings
text_ids = ids(render(turn))
assert ids(text_tok.decode(text_ids)) == text_ids  # decode then encode gives the same ids

Parsing and stopping

The token that ends an assistant turn is a stop id. Compare ids: a template that ends a turn with the next role marker, or a model that samples a different token than the template renders, runs past the turn:

print(text_tok.convert_ids_to_tokens(ids(render(turn))[-3:]), text_tok.convert_ids_to_tokens(eos))

Parsing returns what the template rendered, with reasoning in reasoning_content and arguments typed by the tool schema:

if getattr(text_tok, "response_template", None) is None:  # what the trainers check before falling back to TRL's copies
    tok = add_response_schema(tok)  # sets it on the inner tokenizer; raises when TRL doesn't know the template
prompt = ids(render(user, add_generation_prompt=True))
expected = turn[-1] if "Greet." in render(turn) else answer[-1]  # a template without reasoning drops it
assert parse_response(text_tok, ids(render(turn))[len(prompt):], prefix=prompt) == expected
if supports_tool_calling(tok):  # otherwise report that tools are unsupported and skip these checks
    prompt = ids(render(user, tools=tools, add_generation_prompt=True))
    completion = ids(render([*user, call], tools=tools))[len(prompt):]
    assert parse_response(text_tok, completion, prefix=prompt, tools=tools)["tool_calls"] == call["tool_calls"]
    assert "tool_calls" not in parse_response(text_tok, completion[:-4], prefix=prompt, tools=tools)  # truncated: not executed

In GRPO with tools, look at the completion ids of an episode: the stop token must not appear twice where the tool result is appended.

Config and loading

TRL loads a model id with config.architectures[0], so it must be the class of the model_type:

assert getattr(transformers, config.architectures[0]).config_class.model_type == config.model_type

generation_config.json exists, ids agree across files, and pad is not an EOS (else padding is trained as a stop or masked as one):

text = config.get_text_config()
print(text.eos_token_id, gen.eos_token_id, text_tok.eos_token_id, text_tok.pad_token_id, text_tok.bos_token_id)

TRL's fused LM head (add_fused_lm_head in trl/trainer/utils.py) must read the logit scaling and softcap fields. Random tiny weights never reach a softcap, so compare names, not outputs:

print({k: v for k, v in text.to_dict().items() if any(s in k for s in ("softcap", "logit", "scale", "multiplier"))})

A head_dim above 256 rules out Flash Attention 3, so padding-free and AsyncGRPO. Read it here: the tiny model below shrinks text in place, and tiny models hide it anyway:

print({k: v for k, v in text.to_dict().items() if "head_dim" in k})

The Reward trainer needs AutoModelForSequenceClassification.from_config(config) to work. A quantized release must load by name dequantized (SFTTrainer(model=model_id)) and survive save_pretrained then reload with the same logits.

Architecture

Build a tiny model from the real config: small widths, one layer of each layer_types entry, a few experts and at least one MoE layer (first_k_dense_replace, mlp_only_layers), a small vision tower, no tower you don't test (audio). Shrink every field that mirrors a text size (a vision config's text_hidden_size, out_hidden_size):

mha = text.num_key_value_heads == text.num_attention_heads  # keep MHA models MHA (MLA needs it)
text.update({"hidden_size": 64, "intermediate_size": 128, "num_attention_heads": 4, "num_key_value_heads": 4 if mha else 2,
             "head_dim": 16, "layer_types": list(dict.fromkeys(text.to_dict().get("layer_types") or ["full_attention"]))})
text.num_hidden_layers = len(text.layer_types)
model = getattr(transformers, config.architectures[0])(config).to("cuda", torch.bfloat16)

MoE: TRL computes the load-balancing loss itself (add_fused_lm_head), from the router logits of the backbone and num_experts_per_tok, so the backbone must return one (tokens, experts) tensor per MoE layer. LoraConfig(task_type="CAUSAL_LM") without target_modules must work (PEFT knows the model_type), and LoRA must reach the routed experts:

x = torch.randint(0, 100, (1, 16), device="cuda")
router_logits = model.base_model(x, output_router_logits=True).router_logits
print(len(router_logits), router_logits[0].shape, text.num_experts_per_tok)
print([n for n, _ in peft_model.named_parameters() if "lora" in n and "expert" in n][:3])

Packing doesn't leak across documents, through attention, conv and recurrent layers (run with the flash attention path TRL's padding-free uses; noise stays under 1e-2, a leak is largest on the first tokens). Build the batch with TRL's padding-free collator and pass all its keys: gated-DeltaNet layers split documents on cu_seq_lens_q and seq_idx, not on position_ids alone:

from trl.trainer.sft_trainer import DataCollatorForLanguageModeling

# The kernel TRL picks (no FlashAttention-3 on Blackwell); eager and SDPA attend across documents
if torch.version.hip:
    attn = "kernels-community/aiter-flash-attn@v2"
elif torch.cuda.get_device_capability()[0] >= 10:
    attn = "kernels-community/flash-attn4"
else:
    attn = "kernels-community/flash-attn3"
(model if model._supports_flash_attn else model.get_decoder()).set_attn_implementation(attn)  # wrappers that refuse it
collator = DataCollatorForLanguageModeling(pad_token_id=tok.pad_token_id, padding_free=True)
def logps(before, doc):  # lists of token ids
    batch = collator([{"input_ids": before}, {"input_ids": doc}])
    batch = {k: v.cuda() if torch.is_tensor(v) else v for k, v in batch.items() if k != "labels"}
    return model(**batch).logits[0, len(before):].float().log_softmax(-1)
print((logps(doc_a, doc) - logps(doc_b, doc)).abs().amax(-1))

Also:

  • layer_types declares every non-attention layer: AsyncGRPO tree packing reads it.
  • The fast kernels for linear attention and conv layers are installed: the PyTorch fallback is silent and about 10x slower, so grep the logs for "falling back".
  • VLM: config.image_token_id is the processor's image token, the checkpoint also trains text-only through AutoModelForCausalLM, and every processor output key reaches the model.
  • vLLM in TRL's range serves it (trl vllm-serve --model <id> --revision <sha>), and one GRPO step with use_vllm=True and one AsyncGRPO step sync every weight.

What providers usually miss

Two things are missing in most compatibility requests:

  • a response template, without which TRL can't parse completions, so GRPO, AsyncGRPO and Distillation with tools fail at init;
  • {% generation %} markers, without which assistant_only_loss=True fails.

Recommend adding them, and send the fix: the new chat template with the markers, or the response template. Test it extensively before sending: add the model to the pytest.param lists of tests/test_chat_template_utils.py (TestAddResponseSchema, TestSupportsToolCalling, TestIsChatTemplatePrefixPreserving, TestIsChatTemplateStopTokenTrained, TestGetTrainingChatTemplate, TestParseResponse) and run them with tests/test_data_utils.py.

Report

Keep it to one screen:

  • the versions (TRL, transformers, vLLM, PEFT) and the revision checked;
  • a verdict per trainer: SFT, DPO, KTO, Reward, GRPO and RLOO with tools, Distillation, AsyncGRPO, VLM paths, LoRA;
  • each gap with its evidence and one owner: TRL, transformers, vLLM, PEFT or the provider;
  • short recommendations for the provider, naming the exact token, file or field to change.

Files of this skill

This skill has no supporting files.

Browse all supporting files