forked from nod-ai/SHARK-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more torch hg model tests (nod-ai#238)
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
tank/bert-base-cased_torch/bert-base-cased_torch_test.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,109 @@ | ||
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.shark_downloader import download_torch_model | ||
|
||
import torch | ||
import unittest | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
class BertBaseUncasedModuleTester: | ||
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( | ||
"bert-base-cased", dynamic | ||
) | ||
|
||
# from shark.shark_importer import SharkImporter | ||
# mlir_importer = SharkImporter( | ||
# model, | ||
# (input,), | ||
# frontend="torch", | ||
# ) | ||
# minilm_mlir, func_name = mlir_importer.import_mlir( | ||
# is_dynamic=dynamic, tracing_required=True | ||
# ) | ||
|
||
shark_module = SharkInference( | ||
model_mlir, | ||
func_name, | ||
device=device, | ||
mlir_dialect="linalg", | ||
is_benchmark=self.benchmark, | ||
) | ||
shark_module.compile() | ||
results = shark_module.forward(input) | ||
assert True == compare_tensors(act_out, results) | ||
|
||
if self.benchmark == True: | ||
shark_module.shark_runner.benchmark_all_csv( | ||
(input), | ||
"bert-base-cased", | ||
dynamic, | ||
device, | ||
"torch", | ||
) | ||
|
||
|
||
class BertBaseUncasedModuleTest(unittest.TestCase): | ||
@pytest.fixture(autouse=True) | ||
def configure(self, pytestconfig): | ||
self.module_tester = BertBaseUncasedModuleTester(self) | ||
self.module_tester.benchmark = pytestconfig.getoption("benchmark") | ||
|
||
def test_module_static_cpu(self): | ||
dynamic = False | ||
device = "cpu" | ||
self.module_tester.create_and_check_module(dynamic, device) | ||
|
||
def test_module_dynamic_cpu(self): | ||
dynamic = True | ||
device = "cpu" | ||
self.module_tester.create_and_check_module(dynamic, device) | ||
|
||
@pytest.mark.skipif( | ||
check_device_drivers("gpu"), reason=device_driver_info("gpu") | ||
) | ||
def test_module_static_gpu(self): | ||
dynamic = False | ||
device = "gpu" | ||
self.module_tester.create_and_check_module(dynamic, device) | ||
|
||
@pytest.mark.skipif( | ||
check_device_drivers("gpu"), reason=device_driver_info("gpu") | ||
) | ||
def test_module_dynamic_gpu(self): | ||
dynamic = True | ||
device = "gpu" | ||
self.module_tester.create_and_check_module(dynamic, device) | ||
|
||
@pytest.mark.skipif( | ||
check_device_drivers("vulkan"), reason=device_driver_info("vulkan") | ||
) | ||
def test_module_static_vulkan(self): | ||
dynamic = False | ||
device = "vulkan" | ||
self.module_tester.create_and_check_module(dynamic, device) | ||
|
||
@pytest.mark.skipif( | ||
check_device_drivers("vulkan"), reason=device_driver_info("vulkan") | ||
) | ||
def test_module_dynamic_vulkan(self): | ||
dynamic = True | ||
device = "vulkan" | ||
self.module_tester.create_and_check_module(dynamic, device) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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