-
Notifications
You must be signed in to change notification settings - Fork 91
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
bd0b7c6
commit 1209486
Showing
10 changed files
with
218 additions
and
38 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,21 @@ | ||
## functional_roberta Demo | ||
## How to Run | ||
|
||
Use `pytest --disable-warnings models/demos/roberta/demo/demo.py::test_demo[models.demos.bert.tt.ttnn_optimized_bert-deepset/roberta-large-squad2-models/demos/roberta/demo/input_data.json]` to run the demo. | ||
|
||
If you wish to run the demo for ttnn_optimized_functional_roberta, use `pytest --disable-warnings --input-path="models/demos/roberta/demo/input_data.json" models/demos/roberta/demo/demo.py::test_demo[models.demos.bert.tt.ttnn_optimized_bert-deepset/roberta-large-squad2-models/demos/roberta/demo/input_data.json]` to run the demo. | ||
|
||
If you wish to run the demo with a different input use `pytest --disable-warnings --input-path="<address_to_your_json_file.json>" models/demos/roberta/demo/demo.py::test_demo[models.demos.bert.tt.ttnn_bert-deepset/roberta-large-squad2]`. This file is expected to have exactly 8 inputs. | ||
|
||
Our second demo is designed to run SQuADV2 dataset, run this with `pytest --disable-warnings models/demos/roberta/demo/demo.py::test_demo_squadv2[3-models.demos.bert.tt.ttnn_bert-deepset/roberta-large-squad2]`. | ||
|
||
If you wish to run for `n_iterations` samples, use `pytest --disable-warnings models/demos/roberta/demo/demo.py::test_demo_squadv2[<n_iterations>-models.demos.bert.tt.ttnn_bert-deepset/roberta-large-squad2]` | ||
|
||
|
||
# Inputs | ||
Inputs by default are provided from `input_data.json`. If you wish you to change the inputs, provide a different path to test_demo. | ||
|
||
We do not recommend modifying `input_data.json` file. | ||
|
||
# Details | ||
The entry point to functional_roberta model is bert_for_question_answering in `models/demos/bert/tt/ttnn_bert.py` (`models/demos/bert/tt/ttnn_optimized_bert.py` for optimized version). The model picks up certain configs and weights from huggingface pretrained model. We have used `deepset/roberta-large-squad2` version from huggingface as our reference. |
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
File renamed without changes.
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, is_grayskull | ||
from models.perf.device_perf_utils import run_device_perf, check_device_perf, prep_device_perf_report | ||
|
||
|
||
@pytest.mark.models_device_performance_bare_metal | ||
@pytest.mark.parametrize( | ||
"batch_size, test, expected_perf", | ||
[ | ||
[ | ||
8, | ||
"sequence_size=384-batch_size=8-model_name=deepset/roberta-large-squad2", | ||
66.7 if is_grayskull() else 129.8, | ||
], | ||
], | ||
) | ||
def test_perf_device_bare_metal(batch_size, test, expected_perf): | ||
subdir = "ttnn_roberta_" | ||
num_iterations = 3 | ||
margin = 0.03 | ||
command = f"pytest tests/ttnn/integration_tests/roberta/test_ttnn_opitmized_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("/", "_"), | ||
) |
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 was deleted.
Oops, something went wrong.
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
116 changes: 116 additions & 0 deletions
116
tests/ttnn/integration_tests/roberta/test_ttnn_opitmized_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,116 @@ | ||
# 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 | ||
from ttnn.model_preprocessing import preprocess_model_parameters | ||
from tests.ttnn.utils_for_testing import assert_with_pcc | ||
|
||
|
||
@pytest.mark.parametrize("model_name", ["deepset/roberta-large-squad2"]) | ||
@pytest.mark.parametrize("batch_size", [8]) | ||
@pytest.mark.parametrize("sequence_size", [384]) | ||
def test_roberta(device, use_program_cache, reset_seeds, model_name, batch_size, sequence_size): | ||
config = transformers.RobertaConfig.from_pretrained(model_name) | ||
model = transformers.RobertaModel.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 = torch_output.last_hidden_state | ||
|
||
tt_model_name = f"ttnn_{model_name}_optimized" | ||
|
||
parameters = preprocess_model_parameters( | ||
model_name=tt_model_name, | ||
initialize_model=lambda: transformers.RobertaModel.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( | ||
config, | ||
*ttnn_roberta_inputs, | ||
parameters=parameters, | ||
) | ||
|
||
tt_output = ttnn.to_torch(tt_output) | ||
|
||
assert_with_pcc(torch_output, tt_output, 0.94) | ||
|
||
|
||
@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 = transformers.RobertaConfig.from_pretrained(model_name) | ||
model = transformers.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.88) | ||
assert_with_pcc(torch_output_end_logits, tt_output_end_logits, 0.89) |