-
Notifications
You must be signed in to change notification settings - Fork 90
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
d306891
commit 72b75e3
Showing
5 changed files
with
134 additions
and
9 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
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
39 changes: 39 additions & 0 deletions
39
models/experimental/functional_roberta/tests/test_perf_device_roberta.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,39 @@ | ||
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
from models.utility_functions import skip_for_wormhole_b0 | ||
from models.perf.device_perf_utils import run_device_perf, check_device_perf, prep_device_perf_report | ||
|
||
|
||
@skip_for_wormhole_b0() | ||
@pytest.mark.models_device_performance_bare_metal | ||
@pytest.mark.models_performance_bare_metal | ||
@pytest.mark.parametrize( | ||
"batch_size, test, expected_perf", | ||
[ | ||
# [8, "3-models.demos.bert.tt.ttnn_optimized_bert-deepset/roberta-large-squad2", 252], | ||
[8, "384-8-deepset/roberta-large-squad2", 252], | ||
], | ||
) | ||
def test_perf_device_bare_metal(batch_size, test, expected_perf): | ||
subdir = "ttnn_roberta" | ||
num_iterations = 3 | ||
margin = 0.03 | ||
# command = f"pytest models/experimental/functional_roberta/demo/demo.py::test_demo_squadv2[{test}]" | ||
command = f"pytest models/experimental/functional_roberta/tests/test_ttnn_optimized_roberta.py::test_roberta_for_question_answering[{test}]" | ||
cols = ["DEVICE FW", "DEVICE KERNEL", "DEVICE BRISC KERNEL"] | ||
|
||
inference_time_key = "AVG DEVICE KERNEL SAMPLES/S" | ||
expected_perf_cols = {inference_time_key: expected_perf} | ||
|
||
post_processed_results = run_device_perf(command, subdir, num_iterations, cols, batch_size) | ||
expected_results = check_device_perf(post_processed_results, margin, expected_perf_cols) | ||
prep_device_perf_report( | ||
model_name=f"ttnn_roberta_{batch_size}", | ||
batch_size=batch_size, | ||
post_processed_results=post_processed_results, | ||
expected_results=expected_results, | ||
comments=test.replace("/", "_"), | ||
) |
74 changes: 74 additions & 0 deletions
74
models/experimental/functional_roberta/tests/test_ttnn_optimized_roberta.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,74 @@ | ||
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import ttnn | ||
import torch | ||
import pytest | ||
import tt_lib | ||
import transformers | ||
|
||
from models.demos.bert.tt import ttnn_optimized_bert, ttnn_bert | ||
from ttnn.model_preprocessing import preprocess_model_parameters | ||
from tests.ttnn.utils_for_testing import assert_with_pcc | ||
from models.utility_functions import skip_for_wormhole_b0 | ||
|
||
from transformers import RobertaForQuestionAnswering, RobertaConfig | ||
|
||
|
||
@skip_for_wormhole_b0() | ||
@pytest.mark.models_device_performance_bare_metal | ||
@pytest.mark.models_performance_bare_metal | ||
@pytest.mark.parametrize("model_name", ["deepset/roberta-large-squad2"]) | ||
@pytest.mark.parametrize("batch_size", [8]) | ||
@pytest.mark.parametrize("sequence_size", [384]) | ||
def test_roberta_for_question_answering(device, use_program_cache, reset_seeds, model_name, batch_size, sequence_size): | ||
config = RobertaConfig.from_pretrained(model_name) | ||
model = RobertaForQuestionAnswering.from_pretrained(model_name) | ||
|
||
input_ids = torch.randint(0, config.vocab_size, (batch_size, sequence_size)).to(torch.int32) | ||
torch_token_type_ids = torch.zeros((batch_size, sequence_size), dtype=torch.int32) | ||
torch_position_ids = torch.zeros((batch_size, sequence_size), dtype=torch.int32) | ||
torch_attention_mask = torch.ones(batch_size, sequence_size) | ||
|
||
torch_output = model( | ||
input_ids=input_ids, | ||
attention_mask=torch_attention_mask, | ||
token_type_ids=torch_token_type_ids, | ||
position_ids=torch_position_ids, | ||
) | ||
torch_output_start_logits = torch_output.start_logits | ||
torch_output_end_logits = torch_output.end_logits | ||
|
||
tt_model_name = f"ttnn_{model_name}_optimized" | ||
|
||
parameters = preprocess_model_parameters( | ||
model_name=tt_model_name, | ||
initialize_model=lambda: transformers.RobertaForQuestionAnswering.from_pretrained( | ||
model_name, torchscript=False | ||
).eval(), | ||
custom_preprocessor=ttnn_optimized_bert.custom_preprocessor, | ||
device=device, | ||
) | ||
|
||
ttnn_roberta_inputs = ttnn_optimized_bert.preprocess_inputs( | ||
input_ids, | ||
torch_token_type_ids, | ||
torch_position_ids, | ||
torch_attention_mask, | ||
device=device, | ||
) | ||
|
||
tt_output = ttnn_optimized_bert.bert_for_question_answering( | ||
config, | ||
*ttnn_roberta_inputs, | ||
parameters=parameters, | ||
name="roberta", | ||
) | ||
tt_output = ttnn.to_torch(tt_output) | ||
|
||
tt_output_start_logits = tt_output[..., :, 0] | ||
tt_output_end_logits = tt_output[..., :, 1] | ||
|
||
assert_with_pcc(torch_output_start_logits, tt_output_start_logits, 0.4505) | ||
assert_with_pcc(torch_output_end_logits, tt_output_end_logits, 0.4590) |