-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ddilbaz/mamba
- Loading branch information
Showing
4 changed files
with
124 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Reference: https://huggingface.co/Qwen/Qwen2.5-1.5B | ||
|
||
from transformers import AutoTokenizer, Qwen2ForCausalLM, GenerationConfig | ||
import torch | ||
import pytest | ||
from tests.utils import ModelTester | ||
|
||
|
||
class ThisTester(ModelTester): | ||
def _load_model(self): | ||
model = Qwen2ForCausalLM.from_pretrained( | ||
self.model_name, torch_dtype=torch.bfloat16 | ||
) | ||
self.tokenizer = AutoTokenizer.from_pretrained( | ||
self.model_name, torch_dtype=torch.bfloat16 | ||
) | ||
return model.generate | ||
|
||
def _load_inputs(self): | ||
|
||
self.text = "Hey, are you conscious? Can you talk to me?" | ||
input_ids = self.tokenizer(self.text, return_tensors="pt").input_ids | ||
generation_config = GenerationConfig(max_length=30) | ||
arguments = {"input_ids": input_ids, "generation_config": generation_config} | ||
return arguments | ||
|
||
def set_model_eval(self, model): | ||
return model | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"mode", | ||
["eval", "train"], | ||
) | ||
@pytest.mark.parametrize( | ||
"model_name", | ||
[ | ||
"Qwen/Qwen2.5-1.5B", | ||
], | ||
) | ||
def test_qwen2_casual_lm(record_property, model_name, mode): | ||
if mode == "train": | ||
pytest.skip() | ||
record_property("model_name", model_name) | ||
record_property("mode", mode) | ||
|
||
tester = ThisTester(model_name, mode) | ||
results = tester.test_model() | ||
|
||
if mode == "eval": | ||
gen_text = tester.tokenizer.batch_decode( | ||
results, | ||
skip_special_tokens=True, | ||
clean_up_tokenization_spaces=False, | ||
)[0] | ||
|
||
print( | ||
f"Model: {model_name} | Input: {tester.text} | Generated Text: {gen_text}" | ||
) | ||
|
||
record_property("torch_ttnn", (tester, results)) |
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,59 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from transformers import AutoTokenizer, Qwen2ForTokenClassification | ||
import torch | ||
import pytest | ||
from tests.utils import ModelTester | ||
|
||
|
||
class ThisTester(ModelTester): | ||
def _load_model(self): | ||
return Qwen2ForTokenClassification.from_pretrained( | ||
self.model_name, torch_dtype=torch.bfloat16 | ||
) | ||
|
||
def _load_inputs(self): | ||
self.tokenizer = AutoTokenizer.from_pretrained( | ||
self.model_name, torch_dtype=torch.bfloat16 | ||
) | ||
self.text = "HuggingFace is a company based in Paris and New York." | ||
self.inputs = self.tokenizer( | ||
self.text, add_special_tokens=False, return_tensors="pt" | ||
) | ||
return self.inputs | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"mode", | ||
["eval", "train"], | ||
) | ||
@pytest.mark.parametrize( | ||
"model_name", | ||
[ | ||
"Qwen/Qwen2-7B", | ||
], | ||
) | ||
def test_qwen2_token_classification(record_property, model_name, mode): | ||
if mode == "train": | ||
pytest.skip() | ||
record_property("model_name", model_name) | ||
record_property("mode", mode) | ||
|
||
tester = ThisTester(model_name, mode) | ||
with torch.no_grad(): | ||
results = tester.test_model() | ||
|
||
if mode == "eval": | ||
logits = results.logits | ||
predicted_token_class_ids = logits.argmax(-1) | ||
predicted_tokens_classes = [ | ||
tester.model.config.id2label[t.item()] for t in predicted_token_class_ids[0] | ||
] | ||
input_ids = tester.inputs["input_ids"] | ||
tokens = tester.tokenizer.convert_ids_to_tokens(input_ids[0]) | ||
print( | ||
f"Model: {model_name} | Tokens: {tokens} | Predictions: {predicted_tokens_classes}" | ||
) | ||
|
||
record_property("torch_ttnn", (tester, results)) |
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