티스토리 뷰

728x90
반응형

 

 

chatGPT 데이터 학습 및 사용 방법은 아래 링크를 눌러주세요!

[chatGPT] GPT 3버전 fine-tuning으로 데이터 학습 및 사용 방법

 

[chatGPT] GPT 3버전 fine-tuning으로 데이터 학습 및 사용 방법

이전 글을 보시려면 아래 링크를 클릭해주세요. [chatGPT] 파이썬으로 chatGPT API 호출하기 [chatGPT] 파이썬으로 chatGPT API 호출하기 [chatGPT] 파이썬으로 chatGPT API 호출하기 1. 인증키 발급 chatGPT를 API로

domdom.tistory.com

 

 

 

이번 편에서는 한국어버전 GPT인 beomi/KoAlpaca-Polyglot을 사용해보려고 합니다.

https://github.com/Beomi/KoAlpaca

 

GitHub - Beomi/KoAlpaca: KoAlpaca: Korean Alpaca Model based on Stanford Alpaca (feat. LLAMA and Polyglot-ko)

KoAlpaca: Korean Alpaca Model based on Stanford Alpaca (feat. LLAMA and Polyglot-ko) - GitHub - Beomi/KoAlpaca: KoAlpaca: Korean Alpaca Model based on Stanford Alpaca (feat. LLAMA and Polyglot-ko)

github.com

 

 

beomi/KoAlpaca-Polyglot은 EleutherAI/polyglot-ko 모델을 백본으로 사용하여

네이버 지식인 게시물 등 다량의 한국의 데이터가 파인튜닝된 모델이라고 합니다.

다양한 버전의 모델이 존재하고, 모델명에서  b앞에 붙어있는 숫자가 커질수록 성능이 좋은 모델입니다.

 

 

Polyglot-ko 12.8B 기반 [Full Finetune v1.1] -> 🤗 beomi/KoAlpaca-Polyglot-12.8B
Polyglot-ko 5.8B 기반 [Full Finetune] -> 🤗 beomi/KoAlpaca-Polyglot
MetaLLAMA 7B 기반 [Full Finetune] -> 🤗 beomi/KoAlpaca
Meta LLAMA 13B 기반 [LoRA] -> 🤗 KoAlpaca-13B-LoRA
Meta LLAMA 30B 기반 [LoRA] -> 🤗 KoAlpaca-30B-LoRA
Meta LLAMA 65B 기반 [LoRA] -> 🤗 KoAlpaca-65B-LoRA

 

 

beomi/KoAlpaca-Polyglot-12.8B 모델 기반으로 만든 채팅페이지도 있다고 해서 들어가보았습니다.

https://chat.koalpaca.com/

 

HuggingChat

Model: beomi/KoAlpaca-Polyglot-12.8B · Generated content may be inaccurate or false.

chat.koalpaca.com

 

 

 

오늘 서울 날씨 어떠냐고 물어보니 이모티콘까지 써가며 자세히 답변해주고..!!

성능이 아주 좋은 것 같더라고요.

 

 

HuggingChat

Model: beomi/KoAlpaca-Polyglot-12.8B · Generated content may be inaccurate or false.

chat.koalpaca.com

 

 

KoAlpaca-Polyglot를 사용하는 방법은 아주 쉽습니다!

Beomi/KoAlpaca 깃헙에 나와있는 모델 중, 사용하고자하는 모델을 클릭하면  huggingface 사이트로 이동됩니다.

 

 

 

 

우측 상단에 있는 Use in Transformers 버튼을 클릭합니다.

 

 

 

 

 

 

그러면 아래와 같이 모델을 불러오는 소스코드를 복사할 수 있습니다~

 

 

 

모델을 사용하기 전, 필요한 라이브러리들을 모두 설치해줍니다.

제가 설치했던 라이브러리들입니다.

 

pip install git+https://github.com/huggingface/transformers
pip install sentencepiece
pip install bitsandbytes
pip install datasets
pip install loralib
반응형

 

이제 모델을 불러와봅니다.

멀티 GPU를 사용하고 있다면, 모델을 불러올 때  device_map="auto" 옵션을 추가해주면 됩니다.

 

 

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("beomi/KoAlpaca-Polyglot-12.8B")
model = AutoModelForCausalLM.from_pretrained("beomi/KoAlpaca-Polyglot-12.8B").to("cuda")

 

 

모델을 불러온 후 GPU 상태를 모니터링해보니... GPU 메모리를 어마어마하게 사용합니다.

모델을 불러와 사용하는 것까지는 가능하지만, 파인튜닝은 웬만한 일반 컴퓨터에서는 GPU메모리 에러로 불가능하지 않을까.. 싶습니다. 

 

불러온 모델을 사용해봅니다.

모델을 사용하는 방법은 Beomi/KoAlpaca github에서 Inference Test.ipynb 파일을 참고! 

https://github.com/Beomi/KoAlpaca/blob/main/Inference%20Test.ipynb

 

GitHub - Beomi/KoAlpaca: KoAlpaca: Korean Alpaca Model based on Stanford Alpaca (feat. LLAMA and Polyglot-ko)

KoAlpaca: Korean Alpaca Model based on Stanford Alpaca (feat. LLAMA and Polyglot-ko) - GitHub - Beomi/KoAlpaca: KoAlpaca: Korean Alpaca Model based on Stanford Alpaca (feat. LLAMA and Polyglot-ko)

github.com

PROMPT_DICT = {
    "prompt_input": (
        "Below is an instruction that describes a task, paired with an input that provides further context.\n"
        "아래는 작업을 설명하는 명령어와 추가적 맥락을 제공하는 입력이 짝을 이루는 예제입니다.\n\n"
        "Write a response that appropriately completes the request.\n요청을 적절히 완료하는 응답을 작성하세요.\n\n"
        "### Instruction(명령어):\n{instruction}\n\n### Input(입력):\n{input}\n\n### Response(응답):"
    ),
    "prompt_no_input": (
        "Below is an instruction that describes a task.\n"
        "아래는 작업을 설명하는 명령어입니다.\n\n"
        "Write a response that appropriately completes the request.\n명령어에 따른 요청을 적절히 완료하는 응답을 작성하세요.\n\n"
        "### Instruction(명령어):\n{instruction}\n\n### Response(응답):"
    ),
}

def gen(prompt, user_input=None, max_new_tokens=128, temperature=0.5):
    if user_input:
        x = PROMPT_DICT['prompt_input'].format(instruction=prompt, input=user_input)
    else:
        x = PROMPT_DICT['prompt_no_input'].format(instruction=prompt)
    
    input_ids = tokenizer.encode(x, return_tensors="pt").to('cuda:0')
    gen_tokens = model.generate(
        input_ids, 
        max_new_tokens=max_new_tokens, 
        num_return_sequences=1, 
        temperature=temperature,
        no_repeat_ngram_size=6,
        do_sample=True,
    )
    gen_text = tokenizer.decode(gen_tokens[0], skip_special_tokens=True)
    
    return gen_text.replace(x, '')
    
# Chat
prompt = "오늘 점심 메뉴를 추천해주세요."
generated_text = gen(prompt)
print(generated_text)

 

 

 

최근에는 Koalpaca polyglot 12.8b 모델과 Koalpaca polyglot 5.8b에 8bit 양자화모델이 추가되었더라고요. 

Beomi님께서 일반 사용자용으로 8bit quantize한 모델도 함께 올려 많은 사람들이 로컬(혹은 코랩 등)에서 서빙해볼 수 있도록 만들어올리셨다고 하니, 참고하시면 될 것 같습니다~!

 

Koalpaca polyglot 12.8b - 8bit

https://huggingface.co/beomi/KoAlpaca-Polyglot-12.8B/tree/8bit

 

beomi/KoAlpaca-Polyglot-12.8B at 8bit

Detected Pickle imports (6) "torch._utils._rebuild_tensor_v2", "torch.CharStorage", "torch.BoolStorage", "collections.OrderedDict", "torch.FloatStorage", "torch.HalfStorage" What is a pickle import?

huggingface.co

 

Koalpaca polyglot 5.8b 8bit

https://huggingface.co/beomi/KoAlpaca-Polyglot-5.8B/tree/8bit

 

beomi/KoAlpaca-Polyglot-5.8B at 8bit

 

huggingface.co

 

 

 

 

 참고로,,  다양한 모델을 설치하여 사용하다보면 금방 메모리가 찬답니다..

사용하지 않는 모델은 삭제합시다. 모델이 저장되는 디렉토리는 ~/.chace/huggingface/hub 입니다.!!

 

끝!

 

728x90
반응형
댓글