-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
487d25d
commit b893bec
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
forge/test/models/pytorch/text/phi3/test_phi3_medium.py
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,137 @@ | ||
# SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import pytest | ||
from transformers import ( | ||
AutoTokenizer, | ||
Phi3Config, | ||
Phi3ForCausalLM, | ||
Phi3ForSequenceClassification, | ||
Phi3ForTokenClassification, | ||
) | ||
|
||
import forge | ||
|
||
from test.models.utils import Framework, Source, Task, build_module_name | ||
|
||
variants = ["microsoft/Phi-3-medium-128k-instruct", "microsoft/Phi-3-mini-128k-instruct"] | ||
|
||
|
||
@pytest.mark.nightly | ||
@pytest.mark.parametrize("variant", variants) | ||
def test_phi3_causal_lm(record_forge_property, variant): | ||
|
||
# Build Module Name | ||
module_name = build_module_name(variant, Source.HUGGINGFACE, Framework.PYTORCH, Task.CAUSAL_LM) | ||
|
||
# Record Forge Property | ||
record_forge_property("model_name", module_name) | ||
|
||
# Phi3Config from pretrained variant, disable return_dict and caching. | ||
config = Phi3Config.from_pretrained(variant) | ||
config_dict = config.to_dict() | ||
config_dict["return_dict"] = False | ||
config_dict["use_cache"] = False | ||
config = Phi3Config(**config_dict) | ||
|
||
# Load tokenizer and model from HuggingFace | ||
tokenizer = AutoTokenizer.from_pretrained(variant) | ||
framework_model = Phi3ForCausalLM.from_pretrained(variant, config=config).to("cpu") | ||
framework_model.eval() | ||
|
||
# input_prompt | ||
input_prompt = "Africa is an emerging economy because" | ||
|
||
# Tokenize input | ||
inputs = tokenizer(input_prompt, return_tensors="pt").to("cpu") | ||
|
||
sample_inputs = [inputs["input_ids"], inputs["attention_mask"]] | ||
|
||
# Forge compile framework model | ||
compiled_model = forge.compile(framework_model, sample_inputs, module_name) | ||
|
||
|
||
@pytest.mark.nightly | ||
@pytest.mark.parametrize("variant", variants) | ||
def test_phi3_token_classification(record_forge_property, variant): | ||
|
||
# Build Module Name | ||
module_name = build_module_name( | ||
framework=Framework.PYTORCH, | ||
model="phi3", | ||
variant=variant, | ||
task=Task.TOKEN_CLASSIFICATION, | ||
source=Source.HUGGINGFACE, | ||
) | ||
|
||
# Record Forge Property | ||
record_forge_property("model_name", module_name) | ||
|
||
# Phi3Config from pretrained variant, disable return_dict and caching. | ||
config = Phi3Config.from_pretrained(variant) | ||
config_dict = config.to_dict() | ||
config_dict["return_dict"] = False | ||
config_dict["use_cache"] = False | ||
config = Phi3Config(**config_dict) | ||
|
||
# Tokenize input | ||
tokenizer = AutoTokenizer.from_pretrained(variant, trust_remote_code=True) | ||
framework_model = Phi3ForTokenClassification.from_pretrained(variant, trust_remote_code=True, config=config) | ||
framework_model.eval() | ||
|
||
# input_prompt | ||
input_prompt = "HuggingFace is a company based in Paris and New York" | ||
|
||
# Tokenize input | ||
inputs = tokenizer(input_prompt, return_tensors="pt") | ||
inputs = [inputs["input_ids"]] | ||
|
||
# Forge compile framework model | ||
compiled_model = forge.compile(framework_model, inputs, module_name) | ||
|
||
|
||
@pytest.mark.nightly | ||
@pytest.mark.parametrize("variant", variants) | ||
def test_phi3_sequence_classification(record_forge_property, variant): | ||
|
||
# Build Module Name | ||
module_name = build_module_name( | ||
framework=Framework.PYTORCH, | ||
model="phi3", | ||
variant=variant, | ||
task=Task.SEQUENCE_CLASSIFICATION, | ||
source=Source.HUGGINGFACE, | ||
) | ||
|
||
# Record Forge Property | ||
record_forge_property("model_name", module_name) | ||
|
||
# Phi3Config from pretrained variant, disable return_dict and caching. | ||
config = Phi3Config.from_pretrained(variant) | ||
config_dict = config.to_dict() | ||
config_dict["return_dict"] = False | ||
config_dict["use_cache"] = False | ||
config_dict["pad_token_id"] = None | ||
config = Phi3Config(**config_dict) | ||
|
||
# Load tokenizer and model from HuggingFace | ||
tokenizer = AutoTokenizer.from_pretrained(variant, return_tensors="pt", trust_remote_code=True) | ||
framework_model = Phi3ForSequenceClassification.from_pretrained(variant, trust_remote_code=True, config=config) | ||
framework_model.eval() | ||
|
||
# input_prompt | ||
input_prompt = "the movie was great!" | ||
|
||
# Tokenize input | ||
inputs = tokenizer( | ||
input_prompt, | ||
return_tensors="pt", | ||
max_length=256, | ||
pad_to_max_length=True, | ||
truncation=True, | ||
) | ||
|
||
inputs = [inputs["input_ids"]] | ||
|
||
# Forge compile framework model | ||
compiled_model = forge.compile(framework_model, inputs, module_name) |