forked from huggingface/diffusers
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request huggingface#164 from AmosLewis/move
Rewrite&Move tflite examples to up tank dir
- Loading branch information
Showing
44 changed files
with
1,566 additions
and
584 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
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
105 changes: 105 additions & 0 deletions
105
tank/arbitrary-image-stylization-v1-256/arbitrary-image-stylization-v1-256_tflite_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,105 @@ | ||
import numpy as np | ||
from shark.shark_importer import SharkImporter | ||
from shark.shark_inference import SharkInference | ||
import pytest | ||
import unittest | ||
from shark.parser import shark_args | ||
from shark.tflite_utils import TFLitePreprocessor | ||
|
||
|
||
# model_path = "https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/prediction/1?lite-format=tflite" | ||
|
||
|
||
def compare_results(mlir_results, tflite_results, details): | ||
print("Compare mlir_results VS tflite_results: ") | ||
assert len(mlir_results) == len(tflite_results), "Number of results do not match" | ||
for i in range(len(details)): | ||
mlir_result = mlir_results[i] | ||
tflite_result = tflite_results[i] | ||
mlir_result = mlir_result.astype(np.single) | ||
tflite_result = tflite_result.astype(np.single) | ||
mlir_result = np.expand_dims(mlir_result, axis=0) | ||
print("mlir_result.shape", mlir_result.shape) | ||
print("tflite_result.shape", tflite_result.shape) | ||
assert mlir_result.shape == tflite_result.shape, "shape doesnot match" | ||
max_error = np.max(np.abs(mlir_result - tflite_result)) | ||
print("Max error (%d): %f", i, max_error) | ||
|
||
|
||
class ArbitraryImageStylizationV1TfliteModuleTester: | ||
def __init__( | ||
self, | ||
dynamic=False, | ||
device="cpu", | ||
save_mlir=False, | ||
save_vmfb=False, | ||
): | ||
self.dynamic = dynamic | ||
self.device = device | ||
self.save_mlir = save_mlir | ||
self.save_vmfb = save_vmfb | ||
|
||
def create_and_check_module(self): | ||
shark_args.save_mlir = self.save_mlir | ||
shark_args.save_vmfb = self.save_vmfb | ||
|
||
tflite_preprocessor = TFLitePreprocessor(model_name="arbitrary-image-stylization-v1-256") | ||
|
||
raw_model_file_path = tflite_preprocessor.get_raw_model_file() | ||
inputs = tflite_preprocessor.get_inputs() | ||
tflite_interpreter = tflite_preprocessor.get_interpreter() | ||
|
||
my_shark_importer = SharkImporter( | ||
module=tflite_interpreter, | ||
inputs=inputs, | ||
frontend="tflite", | ||
raw_model_file=raw_model_file_path, | ||
) | ||
mlir_model, func_name = my_shark_importer.import_mlir() | ||
|
||
shark_module = SharkInference( | ||
mlir_module=mlir_model, | ||
function_name=func_name, | ||
device=self.device, | ||
mlir_dialect="tflite", | ||
) | ||
# Case1: Use shark_importer default generate inputs | ||
shark_module.compile() | ||
mlir_results = shark_module.forward(inputs) | ||
## post process results for compare | ||
input_details, output_details = tflite_preprocessor.get_model_details() | ||
mlir_results = list(mlir_results) | ||
for i in range(len(output_details)): | ||
dtype = output_details[i]["dtype"] | ||
mlir_results[i] = mlir_results[i].astype(dtype) | ||
tflite_results = tflite_preprocessor.get_raw_model_output() | ||
compare_results(mlir_results, tflite_results, output_details) | ||
# print(mlir_results) | ||
|
||
|
||
class ArbitraryImageStylizationV1TfliteModuleTest(unittest.TestCase): | ||
@pytest.fixture(autouse=True) | ||
def configure(self, pytestconfig): | ||
self.save_mlir = pytestconfig.getoption("save_mlir") | ||
self.save_vmfb = pytestconfig.getoption("save_vmfb") | ||
|
||
def setUp(self): | ||
self.module_tester = ArbitraryImageStylizationV1TfliteModuleTester(self) | ||
self.module_tester.save_mlir = self.save_mlir | ||
|
||
import sys | ||
|
||
@pytest.mark.xfail(sys.platform == "darwin", reason="known macos tflite install issue") | ||
def test_module_static_cpu(self): | ||
self.module_tester.dynamic = False | ||
self.module_tester.device = "cpu" | ||
self.module_tester.create_and_check_module() | ||
|
||
|
||
if __name__ == "__main__": | ||
# module_tester = ArbitraryImageStylizationV1TfliteModuleTester() | ||
# module_tester.save_mlir = True | ||
# module_tester.save_vmfb = True | ||
# module_tester.create_and_check_module() | ||
|
||
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
Oops, something went wrong.