Skip to content
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

#0: Added a naive batching mechanism for some models in the tests folder #740

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
bd6ec3c
#0: Added a naive batching mechanism for some models in the tests folder
jbedichekTT Feb 4, 2025
1b79c98
Added workflow for testing max batch sizes
jbedichekTT Feb 6, 2025
485a032
Empty workflow placeholder
jbedichekTT Feb 6, 2025
48e330e
Reduce verbosity of tests
jbedichekTT Feb 6, 2025
d616377
Merge branch 'main' into jb/adding-batching-to-tests
jbedichekTT Feb 7, 2025
c4a6a5a
First max batch size search (unoptimzed)
jbedichekTT Feb 19, 2025
253f534
Fixed issue with Github triggering
jbedichekTT Feb 19, 2025
9a7438d
Workflow troubleshooting
jbedichekTT Feb 19, 2025
1f2b60f
Fixed PYTHONPATH issue
jbedichekTT Feb 19, 2025
2bb52a2
Updated workflow enviornment var
jbedichekTT Feb 19, 2025
28a6369
Fixing workflow config
jbedichekTT Feb 19, 2025
4748595
Fixing variable scope
jbedichekTT Feb 20, 2025
f8f7112
Adjust bounds of batch search
jbedichekTT Feb 20, 2025
de0e23d
added adaptive search bounds
jbedichekTT Feb 20, 2025
47e2ea0
converting end-to-end test
jbedichekTT Feb 20, 2025
c8c0af6
hand_landmark fix
jbedichekTT Feb 21, 2025
e11510d
syntax fix for search script
jbedichekTT Feb 21, 2025
bf0027d
syntax fix for search script
jbedichekTT Feb 21, 2025
4e31900
Update dependencies to 0.56.0-rc9 (#745)
ayerofieiev-tt Feb 7, 2025
ea160c9
Update requirements.txt
ayerofieiev-tt Feb 8, 2025
b406a8c
Update requirements.txt
ayerofieiev-tt Feb 8, 2025
250cddc
Update requirements.txt
ayerofieiev-tt Feb 8, 2025
fe6bb36
Update update-ttnn-wheel.yaml
ayerofieiev-tt Feb 9, 2025
a757c30
Update requirements.txt
ayerofieiev-tt Feb 21, 2025
f6c0ded
deleted unnecessary files
jbedichekTT Feb 21, 2025
caa813e
reformatting and adding default batch size
jbedichekTT Feb 24, 2025
13964e8
run additional tests
jbedichekTT Feb 24, 2025
036e966
refactoring
jbedichekTT Feb 24, 2025
907751f
removing extraneous imports
jbedichekTT Feb 24, 2025
74dd352
removing extreneous imports
jbedichekTT Feb 24, 2025
bf2cd30
conftest argument fix
jbedichekTT Feb 24, 2025
3ba6ef6
utils typo fix
jbedichekTT Feb 24, 2025
d92db1f
reconfigure test
jbedichekTT Feb 25, 2025
68fbb73
refactoring tests and only searching even batches
jbedichekTT Feb 25, 2025
8ffaeda
search script fix
jbedichekTT Feb 25, 2025
5d710ec
search script grouping fix
jbedichekTT Feb 25, 2025
894b00f
reduced verbosity
jbedichekTT Feb 26, 2025
153b274
further reducing verbsoity
jbedichekTT Feb 26, 2025
e3a5db9
modified search to exit on highest even value
jbedichekTT Feb 26, 2025
bce3f43
reduced num iterations
jbedichekTT Feb 26, 2025
1f6a8c2
exclude tests
jbedichekTT Feb 27, 2025
1204887
pruning tests for next iteration
jbedichekTT Feb 27, 2025
8403376
next batch of tests
jbedichekTT Feb 28, 2025
95f3367
bounds reconfig
jbedichekTT Feb 28, 2025
15c386e
arithmetic fix
jbedichekTT Feb 28, 2025
6f70217
reset tests
jbedichekTT Feb 28, 2025
b2493e3
isolating BERT test
jbedichekTT Feb 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def pytest_addoption(parser):
help="Run up to the specified iteration count and report metrics based on this iteration.",
)
parser.addoption("--gen_op_accuracy_tests", action="store_true")

parser.addoption("--batch_size", action="store", default=None, help="Batch size for testing")

@pytest.fixture(scope="session")
def input_var_only_native(request):
Expand Down Expand Up @@ -69,6 +69,9 @@ def device():
ttnn.synchronize_device(device)
ttnn.close_device(device)

@pytest.fixture(scope="session")
def get_batch_size(request):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returned value is a value, not a function, so the name starting with "get" is a bit weird here.
Wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just "batch_size"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just "batch_size"

I agree, looks cleaner

return request.config.getoption("--batch_size")

def get_dispatch_core_type():
# Instead of conditionally returning WORKER or ETH, here we always return ETH
Expand Down
13 changes: 10 additions & 3 deletions tests/models/MobileNetV2/test_MobileNetV2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from PIL import Image

import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs



class ThisTester(ModelTester):
Expand All @@ -32,12 +33,18 @@ def _load_inputs(self):
["eval"],
)
@pytest.mark.converted_end_to_end
def test_MobileNetV2(record_property, mode):
def test_MobileNetV2(record_property, mode, get_batch_size):
model_name = "MobileNetV2"
record_property("model_name", model_name)
record_property("mode", mode)
batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size)
if mode == "eval":
# Print the top 5 predictions
_, indices = torch.topk(results, 5)
Expand Down
18 changes: 14 additions & 4 deletions tests/models/albert/test_albert_masked_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from transformers import AutoTokenizer, AlbertForMaskedLM
import torch
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits


class ThisTester(ModelTester):
Expand Down Expand Up @@ -40,15 +40,25 @@ def append_fake_loss_function(self, outputs):
"albert/albert-xxlarge-v2",
],
)
def test_albert_masked_lm(record_property, model_name, mode):


def test_albert_masked_lm(record_property, model_name, mode, get_batch_size):
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()

results = tester.test_model(batch_size=batch_size)
if mode == "eval":
# retrieve index of [MASK]

results.logits = process_batched_logits(results.logits, batch_size)
#print(results.logits.shape)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

logits = results.logits
mask_token_index = (tester.inputs.input_ids == tester.tokenizer.mask_token_id)[0].nonzero(as_tuple=True)[0]
predicted_token_id = logits[0, mask_token_index].argmax(axis=-1)
Expand Down
17 changes: 11 additions & 6 deletions tests/models/albert/test_albert_question_answering.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from transformers import AutoTokenizer, AlbertForQuestionAnswering
import torch
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs


class ThisTester(ModelTester):
Expand All @@ -24,17 +24,22 @@ def _load_inputs(self):
)
@pytest.mark.converted_end_to_end
@pytest.mark.parametrize("model_name", ["twmkn9/albert-base-v2-squad2"])
def test_albert_question_answering(record_property, model_name, mode):
def test_albert_question_answering(record_property, model_name, mode, get_batch_size):
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
answer_start_index = results.start_logits.argmax()
answer_end_index = results.end_logits.argmax()

answer_start_index = process_batched_logits(results.start_logits,batch_size).argmax()
answer_end_index = process_batched_logits(results.end_logits,batch_size).argmax()
predict_answer_tokens = tester.inputs.input_ids[0, answer_start_index : answer_end_index + 1]
answer = tester.tokenizer.decode(predict_answer_tokens, skip_special_tokens=True)

Expand Down
16 changes: 11 additions & 5 deletions tests/models/albert/test_albert_sequence_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from transformers import AlbertTokenizer, AlbertForSequenceClassification
import torch
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs


class ThisTester(ModelTester):
Expand All @@ -23,15 +23,21 @@ def _load_inputs(self):
)
@pytest.mark.converted_end_to_end
@pytest.mark.parametrize("model_name", ["textattack/albert-base-v2-imdb"])
def test_albert_sequence_classification(record_property, model_name, mode):
def test_albert_sequence_classification(record_property, model_name, mode, get_batch_size):
record_property("model_name", model_name)
record_property("mode", mode)

tester = ThisTester(model_name, mode)
results = tester.test_model()
batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
logits = results.logits
logits = process_batched_logits(results.logits, batch_size)
predicted_class_id = logits.argmax().item()
predicted_label = tester.model.config.id2label[predicted_class_id]

Expand Down
20 changes: 16 additions & 4 deletions tests/models/albert/test_albert_token_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from transformers import AutoTokenizer, AlbertForTokenClassification
import torch
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs


class ThisTester(ModelTester):
Expand All @@ -27,15 +27,27 @@ def _load_inputs(self):
pytest.param("albert/albert-base-v2", marks=pytest.mark.converted_end_to_end),
],
)
def test_albert_token_classification(record_property, model_name, mode):
def test_albert_token_classification(record_property, model_name, mode, get_batch_size):
record_property("model_name", f"{model_name}-classification")
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
logits = results.logits
if batch_size is not None:
results.logits = results.logits.squeeze(0) # Temporary fix, not the neatest solution

logits = process_batched_logits(results.logits, batch_size).unsqueeze(0)
if batch_size is None:
logits = logits.squeeze(0) # Adjust dimensions to account for batch reshaping ^

predicted_token_class_ids = logits.argmax(-1)

# Note that tokens are classified rather then input words which means that
Expand Down
13 changes: 10 additions & 3 deletions tests/models/autoencoder_conv/test_autoencoder_conv_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import torchvision.transforms as transforms
from datasets import load_dataset
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs



class ConvAE(torch.nn.Module):
Expand Down Expand Up @@ -71,13 +72,19 @@ def _load_inputs(self):
"mode",
["train", "eval"],
)
def test_autoencoder_conv_v2(record_property, mode):
def test_autoencoder_conv_v2(record_property, mode, get_batch_size):
model_name = f"Autoencoder (conv)"
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
print("Output: ", results)
Expand Down
12 changes: 9 additions & 3 deletions tests/models/autoencoder_linear/test_autoencoder_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import torchvision.transforms as transforms
from datasets import load_dataset
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs



class LinearAE(torch.nn.Module):
Expand Down Expand Up @@ -80,13 +81,18 @@ def _load_inputs(self):
"mode",
["train", pytest.param("eval", marks=pytest.mark.converted_end_to_end)],
)
def test_autoencoder_linear(record_property, mode):
def test_autoencoder_linear(record_property, mode, get_batch_size):
model_name = "Autoencoder (linear)"
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size)

if mode == "eval":
print("Output: ", results)
Expand Down
12 changes: 9 additions & 3 deletions tests/models/beit/test_beit_image_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import pytest
import torch
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs


class ThisTester(ModelTester):
Expand Down Expand Up @@ -33,12 +33,18 @@ def get_results_train(self, model, inputs, outputs):

@pytest.mark.parametrize("mode", ["train", "eval"])
@pytest.mark.parametrize("model_name", ["microsoft/beit-base-patch16-224", "microsoft/beit-large-patch16-224"])
def test_beit_image_classification(record_property, model_name, mode):
def test_beit_image_classification(record_property, model_name, mode, get_batch_size):
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
logits = results.logits
Expand Down
12 changes: 9 additions & 3 deletions tests/models/bert/test_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Load model directly
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs


class ThisTester(ModelTester):
Expand Down Expand Up @@ -35,13 +35,19 @@ def _load_inputs(self):
["eval"],
)
@pytest.mark.converted_end_to_end
def test_bert(record_property, mode):
def test_bert(record_property, mode, get_batch_size):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead this being an option of the run here, I think it might be better to simply pass it to the ModelTester in conftest like this

outputs_after = model_tester.test_model(as_ttnn=True, option=option, batch_size=batch_size)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead this being an option of the run here, I think it might be better to simply pass it to the ModelTester in conftest like this

outputs_after = model_tester.test_model(as_ttnn=True, option=option, batch_size=batch_size)

Will do

model_name = "BERT"
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
# Helper function to decode output to human-readable text
Expand Down
13 changes: 10 additions & 3 deletions tests/models/bloom/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs



class ThisTester(ModelTester):
Expand Down Expand Up @@ -33,13 +34,19 @@ def _load_inputs(self):
["eval"],
)
@pytest.mark.converted_end_to_end
def test_bloom(record_property, mode):
def test_bloom(record_property, mode, get_batch_size):
model_name = "Bloom"
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
# Helper function to decode output to human-readable text
Expand Down
13 changes: 10 additions & 3 deletions tests/models/distilbert/test_distilbert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from transformers import DistilBertTokenizer, DistilBertModel
import torch
import pytest
from tests.utils import ModelTester
from tests.utils import ModelTester, validate_batch_size, process_batched_logits, batch_object_inputs



class ThisTester(ModelTester):
Expand All @@ -22,12 +23,18 @@ def _load_inputs(self):
)
@pytest.mark.converted_end_to_end
@pytest.mark.parametrize("model_name", ["distilbert-base-uncased"])
def test_distilbert(record_property, model_name, mode):
def test_distilbert(record_property, model_name, mode, get_batch_size):
record_property("model_name", model_name)
record_property("mode", mode)

batch_size = get_batch_size
if batch_size is not None:
batch_size = int(batch_size)
validate_batch_size(batch_size)

tester = ThisTester(model_name, mode)
results = tester.test_model()
results = tester.test_model(batch_size=batch_size)
batch_object_inputs(tester, batch_size) # This is necessary to avoid shape mismatch errors in tester processing

if mode == "eval":
print(f"Model: {model_name} | Input: {tester.text} | Output: {results}")
Expand Down
Loading
Loading