Hub documentation

Hugging Face에서 PEFT 사용

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Hugging Face에서 PEFT 사용

🤗 Parameter-Efficient Fine-Tuning (PEFT)는 모델의 모든 매개변수를 미세 조정하지 않고 사전 학습된 언어 모델을 다양한 다운스트림 작업에 효율적으로 적응시키기 위한 라이브러리입니다.

Hub에서 PEFT 탐색

모델 페이지 왼쪽에서 필터링하여 PEFT 모델을 찾을 수 있습니다.

설치

시작하려면 PEFT 문서의 빠른 가이드를 확인해 보세요. 설치하려면 PEFT 설치 가이드를 따르세요. 또는 다음 한 줄 명령으로 pip를 통해 설치할 수도 있습니다:

$ pip install peft

기존 모델 사용

모든 PEFT 모델은 Hub에서 로드할 수 있습니다. PEFT 모델을 사용하려면 아래에 표시된 것처럼 fine-tuning에 사용된 base model도 로드해야 합니다. 모든 fine-tuned 모델의 model card에는 base model이 명시되어 있습니다.

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

base_model = "mistralai/Mistral-7B-v0.1"
adapter_model = "dfurman/Mistral-7B-Instruct-v0.2"

model = AutoModelForCausalLM.from_pretrained(base_model)
model = PeftModel.from_pretrained(model, adapter_model)
tokenizer = AutoTokenizer.from_pretrained(base_model)

model = model.to("cuda")
model.eval()

로드가 완료되면, 입력을 토크나이저에 전달하여 준비한 후, 일반적인 transformers 방식대로 model.generate()를 호출할 수 있습니다.

inputs = tokenizer("Tell me the recipe for chocolate chip cookie", return_tensors="pt")

with torch.no_grad():
    outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=10)
    print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0])

다음과 같이 출력됩니다:

Tell me the recipe for chocolate chip cookie dough.

1. Preheat oven to 375 degrees F (190 degrees C).
2. In a large bowl, cream together 1/2 cup (1 stick) of butter or margarine, 1/2 cup granulated sugar, and 1/2 cup packed brown sugar.
3. Beat in 1 egg and 1 teaspoon vanilla extract.
4. Mix in 1 1/4 cups all-purpose flour.
5. Stir in 1/2 teaspoon baking soda and 1/2 teaspoon salt.
6. Fold in 3/4 cup semisweet chocolate chips.
7. Drop by

특정 PEFT 모델을 로드하려면 모델 카드에서 Use in PEFT를 클릭하면 작동하는 스니펫을 받을 수 있습니다!

추가 리소스

Update on GitHub