콘텐츠로 이동

Llama-3-8B (챗봇)

Overview

이 튜토리얼은 여러 개의 RBLN NPU를 사용하여 HuggingFace의 Llama 3 모델을 컴파일하고 배포하는 방법을 설명합니다. 이 가이드에서는 meta-llama/Meta-Llama-3-8B-Instruct 모델을 사용합니다.

Note

리벨리온 확장형 설계(Rebellions Scalable Design, RSD)는 ATOM™+ (RBLN-CA12 and RBLN-CA22)와 ATOM™-Max (RBLN-CA25)에서 사용 가능합니다. 현재 사용 중인 RBLN NPU 종류는 rbln-stat 명령어로 확인할 수 있습니다.

Note

Llama 3는 LLAMA 커뮤니티 라이선스에 따라 라이선스가 부여되며, 저작권은 Meta Platforms, Inc.가 소유하며 모든 권리를 보유합니다.

Setup & Installation

시작하기 전에 시스템 환경이 올바르게 구성되어 있으며, 필요한 모든 필수 패키지가 설치되어 있는지 확인하십시오. 다음 항목이 포함됩니다:

Note

rebel-compiler를 사용하려면 RBLN 포털 계정이 필요하니 참고하십시오.

Note

HuggingFace의 meta-llama/Meta-Llama-3-8B-Instruct 모델은 접근이 제한되어 있습니다. 접근 권한을 부여받은 후, 아래와 같이 huggingface-cli 명령어를 사용하여 로그인할 수 있습니다:

$ huggingface-cli login

    _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
    _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
    _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
    _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
    _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

    To login, `huggingface_hub` requires a token generated from [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) .
Token: *****

Optimum RBLN 라이브러리 사용하기

다중 NPU를 위한 모델 컴파일

먼저, optimum-rbln에서 RBLNLlamaForCausalLM 클래스를 가져옵니다. 이 클래스의 from_pretrained() 메서드는 HuggingFace 허브에서 Llama 3 모델을 다운로드하고 RBLN SDK를 사용하여 컴파일합니다. 모델을 익스포트할 때 다음 매개변수를 지정하십시오:

  • export: 모델을 컴파일하려면 True로 설정해야 합니다.
  • rbln_batch_size: 컴파일을 위한 배치 크기를 정의합니다.
  • rbln_max_seq_len: 최대 시퀀스 길이를 정의합니다.
  • rbln_tensor_parallel_size: 추론에 사용할 NPU의 수를 정의합니다.

컴파일 후에는 save_pretrained() 메서드를 사용하여 모델 아티팩트를 디스크에 저장합니다. 이 과정은 컴파일된 모델을 포함하는 디렉터리(예: rbln-Llama-3-8B-Instruct)를 생성합니다.

from optimum.rbln import RBLNLlamaForCausalLM

# Define the HuggingFace model ID
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"

# Compile the model for 4 RBLN NPUs
compiled_model = RBLNLlamaForCausalLM.from_pretrained(
    model_id=model_id,
    export=True,
    rbln_batch_size=1,
    rbln_max_seq_len=8192,
    rbln_tensor_parallel_size=4,
)

compiled_model.save_pretrained("rbln-Llama-3-8B-Instruct")

컴파일된 RBLN 모델 불러오기

RBLNLlamaForCausalLM.from_pretrained()를 사용하여 컴파일된 RBLN 모델을 불러옵니다. 저장된 디렉터리 경로를 model_id로 전달하고 export 매개변수를 False로 설정하십시오.

1
2
3
4
5
6
7
from optimum.rbln import RBLNLlamaForCausalLM

# Load the compiled RBLN model from the specified directory
compiled_model = RBLNLlamaForCausalLM.from_pretrained(
    model_id="rbln-Llama-3-8B-Instruct",
    export=False
)

입력 준비하기

transformers 라이브러리의 AutoTokenizer를 사용하여 입력 시퀀스를 토큰화합니다. Llama 3와 같은 지시-튜닝(instruction-tuned) 모델의 경우 채팅 템플릿을 적용하는 것이 중요합니다.

1
2
3
4
5
6
7
8
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
tokenizer.pad_token = tokenizer.eos_token

conversation = [{"role": "user", "content": "Hey, are you conscious? Can you talk to me?"}]
text = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
inputs = tokenizer(text, return_tensors="pt", padding=True)

모델 추론

이제 generate() 메서드를 사용하여 응답을 생성할 수 있습니다.

1
2
3
4
5
6
7
8
9
output_sequence = compiled_model.generate(
    inputs.input_ids,
    attention_mask=inputs.attention_mask,
    do_sample=False,
    max_length=8192,
)

out = tokenizer.batch_decode(output_sequence, skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(out[0])

결과는 다음과 같이 표시됩니다:

1
2
3
4
5
Hello! I'm an AI, which means I'm a computer program designed to simulate conversation and answer questions to the best of my ability. I don't have consciousness in the way that humans do, but I'm designed to be very responsive and interactive.

I can understand and respond to language, and I can even learn and improve over time based on the conversations I have with users like you. So, in a sense, I'm "awake" and ready to chat with you!

What would you like to talk about? Do you have a specific question or topic in mind, or do you just want to chat about something random? I'm here to listen and help if I can!

Summary and References

이 튜토리얼은 4개의 RBLN NPU에서 실행되도록 meta-llama/Meta-Llama-3-8B-Instruct 모델을 컴파일하는 방법을 보여주었습니다. 컴파일된 모델은 챗봇을 위해 RBLN NPU에서 효율적으로 추론될 수 있습니다.

References: