मुख्य सामग्री पर जाएँ
Skillpedia
मशीन लर्निंग11k · #11783

trl-training

यह क्या करती है

यह स्किल AI को ऐसा ट्रेनिंग कोड लिखने और ठीक करने देती है जो भाषा मॉडल को पहली ट्रेनिंग के बाद बेहतर बनाता है। यह उन डेवलपर्स के लिए है जो TRL से मॉडल को fine-tune या align करना चाहते हैं।

कब इस्तेमाल करें

  • ✓जब आप भाषा मॉडल को SFT से fine-tune करना चाहते हैं या preference डेटा से align करना चाहते हैं।
  • ✓जब आप Python में या कमांड लाइन से TRL ट्रेनिंग कोड लिखना या ठीक करना चाहते हैं।
  • ✓जब आपको reward function वाली reinforcement learning चाहिए, जैसे GRPO।

Skill का लिंक

https://github.com/huggingface/skills/blob/main/skills/trl-training/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/huggingface/skills/blob/main/skills/trl-training/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“मेरे चुने और अस्वीकृत जवाबों के जोड़ों के लिए एक छोटे Qwen मॉडल पर TRL का DPO ट्रेनिंग स्क्रिप्ट लिखिए।”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

AI तरीके और डेटासेट प्रकार के हिसाब से सही trainer और मेल खाती configuration चुनता है। फिर वह मानक TRL classes और options का इस्तेमाल करके Python कोड या कमांड लिखता या ठीक करता है।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

TRL

Each method pairs a *Trainer class with a *Config dataclass. Configs extend transformers.TrainingArguments, so all of its arguments work in any trainer config.

Trainer Dataset type
SFTTrainer language modeling or prompt-completion
DPOTrainer preference (chosen/rejected pairs)
GRPOTrainer prompt-only + reward function(s)
DistillationTrainer prompt-only + a teacher model (on-policy distillation)
KTOTrainer unpaired preference (per-sample bool label)
RewardTrainer preference (chosen/rejected pairs); trains a scalar reward model, not a policy

Many more trainers (OnlineDPO, ORPO, CPO, GKD, …) live in trl.experimental with unstable APIs: https://huggingface.co/docs/trl/experimental_overview

from datasets import load_dataset
from trl import SFTConfig, SFTTrainer

trainer = SFTTrainer(
    model="Qwen/Qwen2.5-0.5B",  # model ID or a PreTrainedModel instance
    args=SFTConfig(output_dir="Qwen2.5-0.5B-SFT"),
    train_dataset=load_dataset("trl-lib/Capybara", split="train"),
)
trainer.train()

Pass model as a string and route loading kwargs through model_init_kwargs (e.g. {"dtype": "bfloat16", "attn_implementation": "kernels-community/flash-attn2"}) instead of calling from_pretrained yourself. The tokenizer/processor is inferred from the model; pass processing_class only when it differs. For LoRA, pass peft_config=LoraConfig(...).

Dataset formats

Conversational: {"messages": [{"role": ..., "content": ...}]} (language modeling) or {"prompt": [...], "completion": [...]}. The chat template is applied automatically — never apply it yourself. Extra columns are allowed; GRPO forwards them to reward functions. Reference: https://huggingface.co/docs/trl/dataset_formats

SFT: the fields that matter

SFTConfig(
    max_length=1024,        # truncation length; None disables truncation
    packing=True,           # pack sequences into max_length blocks: fewer pad tokens, higher throughput
    padding_free=True,      # flatten batch, no padding; requires FlashAttention; implied by packing
    use_liger_kernel=True,  # fused Liger kernels, reduces peak memory
    assistant_only_loss=True,  # loss only on assistant turns (conversational datasets)
)

GRPO: online RL

def reward_len(completions, **kwargs):
    return [-abs(20 - len(c[0]["content"])) for c in completions]

trainer = GRPOTrainer(
    model="Qwen/Qwen2.5-0.5B-Instruct",
    reward_funcs=reward_len,  # or a list; rewards are summed
    args=GRPOConfig(output_dir="Qwen2.5-0.5B-GRPO", max_completion_length=512),
    train_dataset=load_dataset("trl-lib/DeepMath-103K", split="train"),
)

Reward functions are called with keyword arguments prompts, completions, completion_ids, trainer_state, plus every extra dataset column — accept **kwargs for the ones you ignore. Return list[float], one reward per completion. With conversational data, completions is a list of message lists, not strings.

The generation batch is per_device_train_batch_size × num_processes × steps_per_generation (or set generation_batch_size directly) and must be divisible by num_generations (default 8). Generation is the usual bottleneck — enable vLLM with use_vllm=True: vllm_mode="colocate" shares the training GPUs (size with vllm_gpu_memory_utilization); vllm_mode="server" uses a separate trl vllm-serve --model <model_id>.

AsyncGRPOTrainer (trl.experimental.async_grpo) implements the same algorithm with generation decoupled from training: a background worker streams completions from a vLLM server while the training loop consumes them, so the two overlap instead of alternating.

CLI

Flags mirror the config fields: trl sft --model_name_or_path Qwen/Qwen2.5-0.5B --dataset_name trl-lib/Capybara. YAML via --config; distributed presets via --accelerate_config zero3 (Python scripts: accelerate launch train.py).

अक्सर पूछे जाने वाले सवाल

कौन से ट्रेनिंग तरीके कवर होते हैं?

यह SFT, DPO, GRPO, KTO, reward modeling और distillation कवर करती है, और कुछ और तरीके प्रयोगात्मक के रूप में सूचीबद्ध हैं।

डेटा का फ़ॉर्मेट क्या चाहिए?

फ़ॉर्मेट trainer पर निर्भर करता है, जैसे DPO के लिए chosen और rejected जोड़े, या GRPO के लिए reward function वाले prompts।

क्या यह मॉडल लोड करना भी संभालती है?

हाँ। यह मॉडल को नाम के रूप में देती है और लोडिंग विकल्पों का इस्तेमाल करती है, हाथ से मॉडल लोड करने की बजाय।

जानकारी

लेखक का मूल विवरण

Post-train LLMs with TRL (Transformers Reinforcement Learning) — SFT, DPO, GRPO, KTO, and reward-model training. Use when writing or debugging training code with the TRL Python API or the trl CLI.

repo: huggingface/skills

लोकप्रियता

GitHub स्टार
11k
रैंकिंग में स्थान
#11783
लाइसेंस
Apache-2.0
अपडेट की गई
30/10/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

hf-cli

11k

यह आपके एजेंट को hf कमांड लाइन टूल से Hugging Face पर मॉडल, डेटासेट, Spaces, रिपो और लोकल कैश डाउनलोड करने, अपलोड करने और संभालने में मदद करती है।

डेटामशीन लर्निंग

hf-cloud-aws-context-discovery

11k

यह आपके एजेंट को किसी भी क्लाउड काम से पहले AWS प्रोफ़ाइल, क्षेत्र और खाते का विवरण खोजने में मदद करती है, ताकि वह अंदाज़ा लगाकर खाता न चुने।

कोडिंगडिप्लॉय और क्लाउड (DevOps)

hf-cloud-python-env-setup

11k

यह आपके एजेंट को SageMaker या AWS ऑटोमेशन चलाने से पहले सही Python संस्करण और ताज़ा लाइब्रेरी वाला साफ़ Python वातावरण सेट करने में मदद करती है।

कोडिंगडिप्लॉय और क्लाउड (DevOps)

hf-cloud-sagemaker-deployment-planner

11k

यह आपके एजेंट को मॉडल को Amazon SageMaker पर डिप्लॉय करने की योजना बनाने में मदद करती है, real-time, serverless, async या batch होस्टिंग में से सही विकल्प चुनकर।

कोडिंगडिप्लॉय और क्लाउड (DevOps)

मिलती-जुलती skills