카테고리 없음

구글 Gemma 사용법

24_bean 2024. 2. 25. 15:18

Gemma란?

 

DeepMind, Google이 최근 Gemma를 공개했습니다. 이는 그 이전의 모델들과는 달리 open-weight architecture를 특징으로 하는 최신 대규모 언어 모델(LLM)입니다.

 

https://ai.google.dev/gemma

 

Gemma - Google의 경량형 첨단 개방형 모델 제품군  |  Google AI for Developers

오픈소스 경량 언어 모델 제품군인 Gemma를 소개합니다. 빠른 시작 가이드, 벤치마크, Google Cloud에서 학습 및 배포를 살펴보고 커뮤니티에 참여하여 AI 연구를 발전시키세요.

ai.google.dev

 


사용법

 

Google에서 공개한 LLM 답게 JAX를 사용하여 Gemma를 사용하실 수 있습니다.

물론, Pytorch 혹은 C++등 다양한 수단을 통해서도 사용할 수 있도록 코드를 공개했습니다.

 

CPU, GPU, TPU 모두 호환이 됩니다. 

추천하는 시스템 사양은 2B 체크포인트의 경우 GPU에서 8GB 이상의 RAM을, 7B 체크포인트의 경우 GPU에서 24GB 이상의 RAM을 권장합니다.

 

아래 Gemma 접근을 위한 Github repo를 공유합니다.

 

JAX

https://github.com/google-deepmind/gemma

 

GitHub - google-deepmind/gemma: Open weights LLM from Google DeepMind.

Open weights LLM from Google DeepMind. Contribute to google-deepmind/gemma development by creating an account on GitHub.

github.com


Pytorch

https://github.com/google/gemma_pytorch?tab=readme-ov-file

 

GitHub - google/gemma_pytorch: The official PyTorch implementation of Google's Gemma models

The official PyTorch implementation of Google's Gemma models - google/gemma_pytorch

github.com

 


추가로 Kaggle에 공개한 모델 카드 또한 공유합니다

 

https://www.kaggle.com/models/google/gemma

 

Gemma

Gemma is a family of lightweight, open models built from the research and technology that Google used to create the Gemini models.

www.kaggle.com


Hugging Face - transformers

CPU

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b")

input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")

outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))

 

GPU

# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b", device_map="auto")

input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")

outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))

 

Quantization

# pip install bitsandbytes accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(load_in_8bit=True)

tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b", quantization_config=quantization_config)

input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")

outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))

 

자세한 내용은 아래를 참조해주세요

https://huggingface.co/google/gemma-7b

 

google/gemma-7b · Hugging Face

This repository is publicly accessible, but you have to accept the conditions to access its files and content. To access Gemma on Hugging Face, you’re required to review and agree to Google’s usage license. To do this, please ensure you’re logged-in

huggingface.co