Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set device for local PyTorchGemmaLanguageModel #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions concordia/language_model/pytorch_gemma_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
*,
measurements: measurements_lib.Measurements | None = None,
channel: str = language_model.DEFAULT_STATS_CHANNEL,
device: str = 'cpu'
) -> None:
"""Initializes the instance.

Expand All @@ -46,11 +47,12 @@ def __init__(
"""
self._model_name = model_name
self._tokenizer_name = model_name
self._device = device

os.environ['TOKENIZERS_PARALLELISM'] = 'false'

self._model = transformers.GemmaForCausalLM.from_pretrained(
self._model_name)
self._model_name).to(self._device)
self._tokenizer = transformers.AutoTokenizer.from_pretrained(
self._tokenizer_name)

Expand Down Expand Up @@ -80,14 +82,14 @@ def sample_text(
inputs = self._tokenizer(prompt_with_system_message, return_tensors='pt')

generated_tokens = self._model.generate(
inputs.input_ids,
inputs.input_ids.to(self._device),
max_new_tokens=max_tokens,
return_dict_in_generate=True,
output_scores=True,
)

response = self._tokenizer.decode(
np.int64(generated_tokens.sequences[0]),
np.int64(generated_tokens.sequences[0].cpu()),
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
Expand Down Expand Up @@ -116,13 +118,13 @@ def sample_choice(

inputs = self._tokenizer(prompt, return_tensors='pt')
generated_tokens = self._model.generate(
inputs.input_ids,
inputs.input_ids.to(self._device),
max_new_tokens=1,
return_dict_in_generate=True,
output_scores=True,
)
sample = self._tokenizer.batch_decode(
[np.argmax(generated_tokens.scores[0][0])],
[np.argmax(generated_tokens.scores[0][0].cpu())],
skip_special_tokens=True,
clean_up_tokenization_spaces=False)[0]
answer = sampling.extract_choice_response(sample)
Expand Down
3 changes: 2 additions & 1 deletion concordia/language_model/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def language_model_setup(
model_name: str,
api_key: str | None = None,
disable_language_model: bool = False,
device: str = 'cpu'
) -> language_model.LanguageModel:
"""Get the wrapped language model.

Expand Down Expand Up @@ -69,7 +70,7 @@ def language_model_setup(
elif api_type == 'openai':
return gpt_model.GptLanguageModel(model_name, api_key=api_key)
elif api_type == 'pytorch_gemma':
return pytorch_gemma_model.PyTorchGemmaLanguageModel(model_name)
return pytorch_gemma_model.PyTorchGemmaLanguageModel(model_name, device=device)
elif api_type == 'together_ai':
return together_ai.Gemma2(model_name, api_key=api_key)
else:
Expand Down