-
Notifications
You must be signed in to change notification settings - Fork 19
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
Llama 3.2 1B Instruct on TPU v4, bumping transformers to 4.45.2 #109
Open
artus-LYTiQ
wants to merge
8
commits into
huggingface:main
Choose a base branch
from
artus-LYTiQ:llama3-tpu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−48
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3caeae8
Installation guide for TPU v4.
artus-LYTiQ 446bb2c
Forgot pyproject.toml
artus-LYTiQ 1ea17d0
Llama 3.2 readiness, current transformer version
artus-LYTiQ 7210745
Fix - again forgot a file due to rename
artus-LYTiQ caab98c
Changed logging back to debug
artus-LYTiQ d7ea97a
removed restore of hf hub from gs bucket
artus-LYTiQ f0237d1
change default model for generation, updated logging
artus-LYTiQ 1588182
Changed default models
artus-LYTiQ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import torch | ||
from transformers import AutoTokenizer | ||
from optimum.tpu.distributed_model import DistributedModel | ||
from loguru import logger | ||
import sys | ||
|
||
# Remove default handler | ||
logger.remove() | ||
|
||
# Add a handler to write to file | ||
logger.add("distributed_model.log", rotation="100 MB", level="DEBUG") | ||
|
||
# Add a handler to write to stderr | ||
logger.add(sys.stderr, level="INFO") | ||
|
||
def sample_greedy(logits): | ||
next_logits = logits[:, -1] | ||
next_token_id = torch.argmax(next_logits, dim=-1)[:, None].int() | ||
return next_token_id | ||
|
||
def _test_distributed_model_generation(model_id, max_new_tokens=20): | ||
print(f"Beginning test with model: {model_id}") | ||
os.environ["TOKENIZERS_PARALLELISM"] = "false" | ||
tokenizer = AutoTokenizer.from_pretrained(model_id) | ||
text = ["Running something in parallel means"] | ||
inputs = tokenizer(text, return_tensors="pt") | ||
input_ids = inputs["input_ids"] | ||
attention_mask = inputs["attention_mask"] | ||
tokens = input_ids.clone() | ||
|
||
print("Initializing DistributedModel...") | ||
model = DistributedModel(model_id, sample_greedy) | ||
|
||
print("Generating tokens...") | ||
for _ in range(max_new_tokens): | ||
pos_ids = torch.arange(tokens.shape[1], device=tokens.device).unsqueeze(0) | ||
next_token = model.prefill(input_ids=tokens, attention_mask=attention_mask, position_ids=pos_ids) | ||
tokens = torch.cat([tokens, next_token], dim=-1) | ||
attention_mask = torch.cat([attention_mask, torch.ones_like(next_token)], dim=-1) | ||
|
||
# Optional: Break if EOS token is generated | ||
if next_token.item() == tokenizer.eos_token_id: | ||
break | ||
|
||
decoded_text = tokenizer.batch_decode(tokens, skip_special_tokens=True) | ||
print("\n------------------------------------------") | ||
print("Generated text:") | ||
print(decoded_text[0]) | ||
print("------------------------------------------") | ||
|
||
if __name__ == "__main__": | ||
print("Script started") | ||
try: | ||
_test_distributed_model_generation("meta-llama/Meta-Llama-3.1-8B", max_new_tokens=200) | ||
except Exception as e: | ||
print(f"An error occurred: {str(e)}") | ||
import traceback | ||
traceback.print_exc() | ||
print("Script completed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import torch | ||
import torch_xla.core.xla_model as xm | ||
|
||
devices = xm.get_xla_supported_devices() | ||
print(f'PyTorch can access {len(devices)} TPU cores') | ||
|
||
# Example tensor operations on TPU | ||
dev = xm.xla_device() | ||
print(f"PyTorich device: {dev}") | ||
t1 = torch.randn(3,3,device=dev) | ||
t2 = torch.randn(3,3,device=dev) | ||
print(t1 + t2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for tests, please create one test similar to
tests/test_distributed_model.py
(or modify the existing one). To launch it, you can use pytest:python -m pytest -sv /path/to/test_mytest.py::test_my_test_function
.