TRL documentation

학습 후 모델 사용

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.17.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

학습 후 모델 사용

SFTTrainer 또는 DPOTrainer를 사용하여 모델을 학습하면 텍스트 생성에 사용할 수 있는 파인튜닝된 모델이 생성됩니다. 이 섹션에서는 파인튜닝된 모델을 로드하고 텍스트를 생성하는 과정을 안내합니다. 학습된 모델로 추론 서버를 실행해야 하는 경우, text-generation-inference와 같은 라이브러리를 탐색해 볼 수 있습니다.

로드 및 생성

PEFT를 사용하지 않고 모델을 완전히 파인튜닝한 경우, transformers의 다른 언어 모델과 마찬가지로 간단히 로드할 수 있습니다:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_name_or_path = "Qwen/Qwen3-0.6B" #path/to/your/model/or/name/on/hub
device = torch.accelerator.current_accelerator() if torch.accelerator.is_available() else "cpu"

model = AutoModelForCausalLM.from_pretrained(model_name_or_path).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)

inputs = tokenizer.encode("This movie was really", return_tensors="pt").to(device)
outputs = model.generate(inputs)
print(tokenizer.decode(outputs[0]))

대신 파이프라인을 사용할 수도 있습니다:

from transformers import pipeline

model_name_or_path = "Qwen/Qwen3-0.6B" #path/to/your/model/or/name/on/hub
pipe = pipeline("text-generation", model=model_name_or_path)
print(pipe("This movie was really")[0]["generated_text"])

Adapters PEFT 사용

from peft import PeftConfig, PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer

base_model_name = "Qwen/Qwen3-0.6B" #path/to/your/model/or/name/on/hub
adapter_model_name = "path/to/my/adapter"

model = AutoModelForCausalLM.from_pretrained(base_model_name)
model = PeftModel.from_pretrained(model, adapter_model_name)

tokenizer = AutoTokenizer.from_pretrained(base_model_name)

어댑터를 베이스 모델에 병합하여 일반적인 transformers 모델처럼 사용할 수도 있지만, 이 경우 체크포인트 크기가 상당히 커집니다:

model = AutoModelForCausalLM.from_pretrained(base_model_name)
model = PeftModel.from_pretrained(model, adapter_model_name)

model = model.merge_and_unload()
model.save_pretrained("merged_adapters")

model을 로드한 후 어댑터를 병합했거나 별도로 유지한 상태라면, 위에서 설명한 일반 모델과 동일한 방식으로 생성을 실행할 수 있습니다.

Update on GitHub