Fix GPU benchmarks on PyTorch model tests. (#218)

* Add CUDA_BENCHMARKS option to setup.venv to enable PyTorch benchmarks on CUDA.

* Fix PyTorch GPU benchmarks for tank models.
This commit is contained in:
Ean Garvey
2022-07-29 17:17:55 -05:00
committed by GitHub
parent 4c0deb9899
commit 0ee515a7be
18 changed files with 95 additions and 299 deletions

View File

@@ -75,7 +75,7 @@ jobs:
if: matrix.suite == 'cpu'
run: |
cd $GITHUB_WORKSPACE
PYTHON=python${{ matrix.python-version }} IMPORTER=1 ./setup_venv.sh
PYTHON=python${{ matrix.python-version }} ./setup_venv.sh
source shark.venv/bin/activate
pytest -k 'cpu' --ignore=shark/tests/test_shark_importer.py --ignore=benchmarks/tests/test_hf_benchmark.py --ignore=benchmarks/tests/test_benchmark.py
@@ -83,7 +83,7 @@ jobs:
if: matrix.suite == 'gpu'
run: |
cd $GITHUB_WORKSPACE
PYTHON=python${{ matrix.python-version }} IMPORTER=1 ./setup_venv.sh
PYTHON=python${{ matrix.python-version }} ./setup_venv.sh
source shark.venv/bin/activate
pytest -k "gpu" --ignore=shark/tests/test_shark_importer.py --ignore=benchmarks/tests/test_hf_benchmark.py --ignore=benchmarks/tests/test_benchmark.py
@@ -91,6 +91,6 @@ jobs:
if: matrix.suite == 'vulkan'
run: |
cd $GITHUB_WORKSPACE
PYTHON=python${{ matrix.python-version }} IMPORTER=1 ./setup_venv.sh
PYTHON=python${{ matrix.python-version }} ./setup_venv.sh
source shark.venv/bin/activate
pytest -k 'vulkan' --ignore=shark/tests/test_shark_importer.py --ignore=benchmarks/tests/test_hf_benchmark.py --ignore=benchmarks/tests/test_benchmark.py

View File

@@ -10,3 +10,4 @@ gsutil
# Testing
pytest
pytest-xdist
Pillow

View File

@@ -98,7 +98,7 @@ if [[ ! -z "${IMPORTER}" ]]; then
echo "${Yellow}Installing importer tools.."
if [[ $(uname -s) = 'Linux' ]]; then
echo "${Yellow}Linux detected.. installing Linux importer tools"
$PYTHON -m pip install --upgrade -r "$TD/requirements-importer.txt" -f https://github.com/${RUNTIME}/releases --extra-index-url https://test.pypi.org/simple/ --extra-index-url https://download.pytorch.org/whl/nightly/cpu
$PYTHON -m pip install --upgrade -r "$TD/requirements-importer.txt" -f https://github.com/${RUNTIME}/releases --extra-index-url https://test.pypi.org/simple/ --extra-index-url https://download.pytorch.org/whl/nightly/cu116
elif [[ $(uname -s) = 'Darwin' ]]; then
echo "${Yellow}macOS detected.. installing macOS importer tools"
#Conda seems to have some problems installing these packages and hope they get resolved upstream.
@@ -108,6 +108,16 @@ fi
$PYTHON -m pip install -e . --extra-index-url https://download.pytorch.org/whl/nightly/cpu -f https://github.com/llvm/torch-mlir/releases -f https://github.com/${RUNTIME}/releases
if [[ $(uname -s) = 'Linux' ]]; then
$PYTHON -m pip uninstall -y torch torchvision
$PYTHON -m pip install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cu116
if [ $? -eq 0 ];then
echo "Successfully Installed torch + cu116."
else
echo "Could not install torch + cu116." >&2
fi
fi
if [[ -z "${CONDA_PREFIX}" ]]; then
echo "${Green}Before running examples activate venv with:"
echo " ${Green}source $VENV_DIR/bin/activate"

View File

@@ -19,6 +19,7 @@ from shark.iree_utils.benchmark_utils import (
run_benchmark_module,
)
from shark.parser import shark_args
from tank.model_utils import get_torch_model
from datetime import datetime
import time
import csv
@@ -59,20 +60,33 @@ class SharkBenchmarkRunner(SharkRunner):
mlir_dialect=self.mlir_dialect,
)
def benchmark_frontend(self, inputs):
def benchmark_frontend(self, inputs, modelname):
if self.frontend in ["pytorch", "torch"]:
return self.benchmark_torch(inputs)
return self.benchmark_torch(modelname)
elif self.frontend in ["tensorflow", "tf"]:
return self.benchmark_tf(inputs)
return self.benchmark_tf(inputs, modelname)
def benchmark_torch(self, modelname):
import torch
if self.device == "gpu":
torch.set_default_tensor_type(torch.cuda.FloatTensor)
else:
torch.set_default_tensor_type(torch.FloatTensor)
torch_device = torch.device(
"cuda:0" if self.device == "gpu" else "cpu"
)
HFmodel, input, act_out = get_torch_model(modelname)
frontend_model = HFmodel.model
frontend_model.to(torch_device)
input.to(torch_device)
def benchmark_torch(self, input_tuple):
inputs = input_tuple[0]
for i in range(shark_args.num_warmup_iterations):
self.frontend_model.forward(inputs)
frontend_model.forward(input)
begin = time.time()
for i in range(shark_args.num_iterations):
out = self.frontend_model.forward(inputs)
out = frontend_model.forward(input)
if i == shark_args.num_iterations - 1:
end = time.time()
break
@@ -84,13 +98,13 @@ class SharkBenchmarkRunner(SharkRunner):
f"{((end-begin)/shark_args.num_iterations)*1000}",
]
def benchmark_tf(self, inputs):
def benchmark_tf(self, frontend_model, inputs):
for i in range(shark_args.num_warmup_iterations):
self.frontend_model.forward(*inputs)
frontend_model.forward(*inputs)
begin = time.time()
for i in range(shark_args.num_iterations):
out = self.frontend_model.forward(*inputs)
out = frontend_model.forward(*inputs)
if i == shark_args.num_iterations - 1:
end = time.time()
break
@@ -162,12 +176,12 @@ class SharkBenchmarkRunner(SharkRunner):
for p in platforms:
if p == "frontend":
bench_result["platform"] = frontend
bench_result["iter/sec"] = self.benchmark_frontend(inputs)[
0
]
bench_result["ms/iter"] = self.benchmark_frontend(inputs)[
1
]
bench_result["iter/sec"] = self.benchmark_frontend(
inputs, modelname
)[0]
bench_result["ms/iter"] = self.benchmark_frontend(
inputs, modelname
)[1]
elif p == "shark_python":
bench_result["platform"] = "shark_python"
bench_result["iter/sec"] = self.benchmark_python(inputs)[0]

View File

@@ -2,9 +2,27 @@ from shark.shark_inference import SharkInference
import torch
import numpy as np
import sys
torch.manual_seed(0)
vision_models = [
"alexnet",
"resnet101",
"resnet18",
"resnet50",
"squeezenet1_0",
"wide_resnet50_2",
]
def get_torch_model(modelname):
if modelname in vision_models:
return get_vision_model(modelname)
else:
return get_hf_model(modelname)
##################### Hugging Face LM Models ###################################
@@ -12,7 +30,10 @@ class HuggingFaceLanguage(torch.nn.Module):
def __init__(self, hf_model_name):
super().__init__()
from transformers import AutoModelForSequenceClassification
import transformers as trf
transformers_path = trf.__path__[0]
hf_model_path = f"{transformers_path}/models/{hf_model_name}"
self.model = AutoModelForSequenceClassification.from_pretrained(
hf_model_name, # The pretrained model.
num_labels=2, # The number of output labels--2 for binary classification.

View File

@@ -1,10 +1,8 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import compare_tensors
from shark.parser import shark_args
from shark.shark_downloader import download_torch_model
import torch
import unittest
import numpy as np
import pytest
@@ -13,20 +11,14 @@ import pytest
class MiniLMModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"microsoft/MiniLM-L12-H384-uncased", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -50,18 +42,9 @@ class MiniLMModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
from tank.model_utils import get_hf_model
torch.manual_seed(0)
model, input, act_out = get_hf_model(
"microsoft/MiniLM-L12-H384-uncased"
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
"MiniLM-L12-H384-uncased",
(input),
"microsoft/MiniLM-L12-H384-uncased",
dynamic,
device,
"torch",
@@ -72,8 +55,6 @@ class MiniLMModuleTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def configure(self, pytestconfig):
self.module_tester = MiniLMModuleTester(self)
self.module_tester.save_mlir = pytestconfig.getoption("save_mlir")
self.module_tester.save_vmfb = pytestconfig.getoption("save_vmfb")
self.module_tester.benchmark = pytestconfig.getoption("benchmark")
def test_module_static_cpu(self):

View File

@@ -1,10 +1,8 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_hf_model, compare_tensors
from shark.parser import shark_args
from tank.model_utils import compare_tensors
from shark.shark_downloader import download_torch_model
import torch
import unittest
import numpy as np
import pytest
@@ -13,20 +11,14 @@ import pytest
class AlbertModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"albert-base-v2", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -50,14 +42,8 @@ class AlbertModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
torch.manual_seed(0)
model, input, act_out = get_hf_model("albert-base-v2")
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"albert-base-v2",
dynamic,
device,
@@ -69,8 +55,6 @@ class AlbertModuleTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def configure(self, pytestconfig):
self.module_tester = AlbertModuleTester(self)
self.module_tester.save_mlir = pytestconfig.getoption("save_mlir")
self.module_tester.save_vmfb = pytestconfig.getoption("save_vmfb")
self.module_tester.benchmark = pytestconfig.getoption("benchmark")
def test_module_static_cpu(self):

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_vision_model, compare_tensors
from shark.parser import shark_args
from tank.model_utils import compare_tensors
from shark.shark_downloader import download_torch_model
import unittest
@@ -12,20 +11,14 @@ import pytest
class AlexnetModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"alexnet", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -49,16 +42,8 @@ class AlexnetModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
import torchvision.models as models
torch.manual_seed(0)
model, input, act_out = get_vision_model(
models.alexnet(pretrained=True)
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"alexnet",
dynamic,
device,
@@ -70,8 +55,6 @@ class AlexnetModuleTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def configure(self, pytestconfig):
self.module_tester = AlexnetModuleTester(self)
self.module_tester.save_mlir = pytestconfig.getoption("save_mlir")
self.module_tester.save_vmfb = pytestconfig.getoption("save_vmfb")
self.module_tester.benchmark = pytestconfig.getoption("benchmark")
def test_module_static_cpu(self):

View File

@@ -0,0 +1 @@
platform,model,dynamic,device,iter/sec,ms/iter,datetime
1 platform model dynamic device iter/sec ms/iter datetime

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_hf_model, compare_tensors
from shark.parser import shark_args
from tank.model_utils import compare_tensors
from shark.shark_downloader import download_torch_model
import torch
@@ -25,8 +24,6 @@ class BertBaseUncasedModuleTester:
model_mlir, func_name, input, act_out = download_torch_model(
"bert-base-uncased", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -50,14 +47,8 @@ class BertBaseUncasedModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
torch.manual_seed(0)
model, input, act_out = get_hf_model("bert-base-uncased")
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"bert-base-uncased",
dynamic,
device,
@@ -69,8 +60,6 @@ class BertBaseUncasedModuleTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def configure(self, pytestconfig):
self.module_tester = BertBaseUncasedModuleTester(self)
self.module_tester.save_mlir = pytestconfig.getoption("save_mlir")
self.module_tester.save_vmfb = pytestconfig.getoption("save_vmfb")
self.module_tester.benchmark = pytestconfig.getoption("benchmark")
def test_module_static_cpu(self):

View File

@@ -1,10 +1,9 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_hf_model, compare_tensors
from tank.model_utils import compare_tensors
from shark.parser import shark_args
from shark.shark_downloader import download_torch_model
import torch
import unittest
import numpy as np
import pytest
@@ -13,20 +12,14 @@ import pytest
class DistilBertModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"distilbert-base-uncased", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -50,14 +43,8 @@ class DistilBertModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
torch.manual_seed(0)
model, input, act_out = get_hf_model("distilbert-base-uncased")
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"distilbert-base-uncased",
dynamic,
device,
@@ -73,19 +60,25 @@ class DistilBertModuleTest(unittest.TestCase):
self.module_tester.save_vmfb = pytestconfig.getoption("save_vmfb")
self.module_tester.benchmark = pytestconfig.getoption("benchmark")
@pytest.mark.skip(reason="DistilBert needs to be uploaded to cloud.")
@pytest.mark.skip(
reason="Fails to lower in torch-mlir. See https://github.com/nod-ai/SHARK/issues/222"
)
def test_module_static_cpu(self):
dynamic = False
device = "cpu"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="DistilBert needs to be uploaded to cloud.")
@pytest.mark.skip(
reason="Fails to lower in torch-mlir. See https://github.com/nod-ai/SHARK/issues/222"
)
def test_module_dynamic_cpu(self):
dynamic = True
device = "cpu"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="DistilBert needs to be uploaded to cloud.")
@pytest.mark.skip(
reason="Fails to lower in torch-mlir. See https://github.com/nod-ai/SHARK/issues/222"
)
@pytest.mark.skipif(
check_device_drivers("gpu"), reason=device_driver_info("gpu")
)

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_hf_model, compare_tensors
from shark.parser import shark_args
from tank.model_utils import compare_tensors
from shark.shark_downloader import download_torch_model
import torch
@@ -13,20 +12,14 @@ import pytest
class MobileBertModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"google/mobilebert-uncased", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -50,15 +43,9 @@ class MobileBertModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
torch.manual_seed(0)
model, input, act_out = get_hf_model("google/mobilebert-uncased")
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
"mobilebert-uncased",
(input),
"google/mobilebert-uncased",
dynamic,
device,
"torch",
@@ -69,8 +56,6 @@ class MobileBertModuleTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def configure(self, pytestconfig):
self.module_tester = MobileBertModuleTester(self)
self.module_tester.save_mlir = pytestconfig.getoption("save_mlir")
self.module_tester.save_vmfb = pytestconfig.getoption("save_vmfb")
self.module_tester.benchmark = pytestconfig.getoption("benchmark")
def test_module_static_cpu(self):

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_vision_model, compare_tensors
from shark.parser import shark_args
from tank.model_utils import compare_tensors
from shark.shark_downloader import download_torch_model
import unittest
@@ -12,20 +11,14 @@ import pytest
class Resnet101ModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"resnet101", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -49,16 +42,8 @@ class Resnet101ModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
import torchvision.models as models
torch.manual_seed(0)
model, input, act_out = get_vision_model(
models.resnet101(pretrained=True)
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"resnet101",
dynamic,
device,

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_vision_model, compare_tensors
from shark.parser import shark_args
from shark.shark_downloader import download_torch_model
import unittest
@@ -12,22 +11,15 @@ import pytest
class Resnet18ModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"resnet18", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
# model,
# (input,),
@@ -49,16 +41,8 @@ class Resnet18ModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
import torchvision.models as models
torch.manual_seed(0)
model, input, act_out = get_vision_model(
models.resnet18(pretrained=True)
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"resnet18",
dynamic,
device,

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_vision_model, compare_tensors
from shark.parser import shark_args
from shark.shark_downloader import download_torch_model
import unittest
@@ -12,20 +11,14 @@ import pytest
class Resnet50ModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"resnet50", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -49,16 +42,8 @@ class Resnet50ModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
import torchvision.models as models
torch.manual_seed(0)
model, input, act_out = get_vision_model(
models.resnet50(pretrained=True)
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"resnet50",
dynamic,
device,

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_vision_model, compare_tensors
from shark.parser import shark_args
from shark.shark_downloader import download_torch_model
import unittest
@@ -12,20 +11,14 @@ import pytest
class SqueezenetModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"squeezenet1_0", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -49,16 +42,8 @@ class SqueezenetModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
import torchvision.models as models
torch.manual_seed(0)
model, input, act_out = get_vision_model(
models.squeezenet1_0(pretrained=True)
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"squeezenet1_0",
dynamic,
device,

View File

@@ -1,7 +1,6 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers, device_driver_info
from tank.model_utils import get_vision_model, compare_tensors
from shark.parser import shark_args
from shark.shark_downloader import download_torch_model
import unittest
@@ -12,20 +11,14 @@ import pytest
class WideResnet50ModuleTester:
def __init__(
self,
save_mlir=False,
save_vmfb=False,
benchmark=False,
):
self.save_mlir = save_mlir
self.save_vmfb = save_vmfb
self.benchmark = benchmark
def create_and_check_module(self, dynamic, device):
model_mlir, func_name, input, act_out = download_torch_model(
"wide_resnet50_2", dynamic
)
shark_args.save_mlir = self.save_mlir
shark_args.save_vmfb = self.save_vmfb
# from shark.shark_importer import SharkImporter
# mlir_importer = SharkImporter(
@@ -49,16 +42,8 @@ class WideResnet50ModuleTester:
assert True == compare_tensors(act_out, results)
if self.benchmark == True:
import torch
import torchvision.models as models
torch.manual_seed(0)
model, input, act_out = get_vision_model(
models.wide_resnet50_2(pretrained=True)
)
shark_module.shark_runner.frontend_model = model
shark_module.shark_runner.benchmark_all_csv(
(input,),
(input),
"wide_resnet50_2",
dynamic,
device,

View File

@@ -1,90 +0,0 @@
from shark.shark_inference import SharkInference
from shark.iree_utils._common import check_device_drivers
from tank.model_utils_tf import get_TFhf_model, compare_tensors_tf
import tensorflow as tf
import unittest
import numpy as np
import pytest
class MiniLMTFModuleTester:
def create_and_check_module(self, dynamic, device):
model, input, act_out = get_TFhf_model(
"microsoft/MiniLM-L12-H384-uncased"
)
shark_module = SharkInference(
model, (input,), device=device, dynamic=dynamic, jit_trace=True
)
shark_module.set_frontend("tensorflow")
shark_module.compile()
results = shark_module.forward((input))
assert True == compare_tensors_tf(act_out, results)
class MiniLMTFModuleTest(unittest.TestCase):
def setUp(self):
self.module_tester = MiniLMTFModuleTester()
@pytest.mark.skip(reason="TF testing temporarily unavailable.")
def test_module_static_cpu(self):
dynamic = False
device = "cpu"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="TF testing temporarily unavailable.")
@pytest.mark.xfail(
reason="Language models currently failing for dynamic case"
)
def test_module_dynamic_cpu(self):
dynamic = True
device = "cpu"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="TF testing temporarily unavailable.")
@pytest.mark.skipif(
check_device_drivers("gpu"), reason="nvidia-smi not found"
)
def test_module_static_gpu(self):
dynamic = False
device = "gpu"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="TF testing temporarily unavailable.")
@pytest.mark.xfail(
reason="Language models currently failing for dynamic case"
)
@pytest.mark.skipif(
check_device_drivers("gpu"), reason="nvidia-smi not found"
)
def test_module_dynamic_gpu(self):
dynamic = True
device = "gpu"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="TF testing temporarily unavailable.")
@pytest.mark.skipif(
check_device_drivers("vulkan"),
reason="vulkaninfo not found, install from https://github.com/KhronosGroup/MoltenVK/releases",
)
def test_module_static_vulkan(self):
dynamic = False
device = "vulkan"
self.module_tester.create_and_check_module(dynamic, device)
@pytest.mark.skip(reason="TF testing temporarily unavailable.")
@pytest.mark.xfail(
reason="Language models currently failing for dynamic case"
)
@pytest.mark.skipif(
check_device_drivers("vulkan"),
reason="vulkaninfo not found, install from https://github.com/KhronosGroup/MoltenVK/releases",
)
def test_module_dynamic_vulkan(self):
dynamic = True
device = "vulkan"
self.module_tester.create_and_check_module(dynamic, device)
if __name__ == "__main__":
unittest.main()