mirror of
https://github.com/nod-ai/SHARK-Studio.git
synced 2026-01-10 06:17:55 -05:00
Remove tf package dep for SharkDownloader tflite tests (#212)
This commit is contained in:
@@ -115,6 +115,7 @@ def save_tflite_model(tflite_model_list):
|
||||
with open(tflite_model_list) as csvfile:
|
||||
tflite_reader = csv.reader(csvfile, delimiter=",")
|
||||
for row in tflite_reader:
|
||||
print("\n")
|
||||
tflite_model_name = row[0]
|
||||
tflite_model_link = row[1]
|
||||
print("tflite_model_name", tflite_model_name)
|
||||
@@ -125,13 +126,6 @@ def save_tflite_model(tflite_model_list):
|
||||
os.makedirs(tflite_model_name_dir, exist_ok=True)
|
||||
print(f"TMP_TFLITE_MODELNAME_DIR = {tflite_model_name_dir}")
|
||||
|
||||
tflite_tosa_file = "/".join(
|
||||
[
|
||||
tflite_model_name_dir,
|
||||
str(tflite_model_name) + "_tflite.mlir",
|
||||
]
|
||||
)
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(str(tflite_model_name))
|
||||
raw_model_file_path = tflite_preprocessor.get_raw_model_file()
|
||||
@@ -145,15 +139,11 @@ def save_tflite_model(tflite_model_list):
|
||||
frontend="tflite",
|
||||
raw_model_file=raw_model_file_path,
|
||||
)
|
||||
mlir_model, func_name = my_shark_importer.import_mlir()
|
||||
|
||||
if os.path.exists(tflite_tosa_file):
|
||||
print("Exists", tflite_tosa_file)
|
||||
else:
|
||||
mlir_str = mlir_model.decode("utf-8")
|
||||
with open(tflite_tosa_file, "w") as f:
|
||||
f.write(mlir_str)
|
||||
print(f"Saved mlir in {tflite_tosa_file}")
|
||||
my_shark_importer.import_debug(
|
||||
dir=tflite_model_name_dir,
|
||||
model_name=tflite_model_name,
|
||||
func_name="main",
|
||||
)
|
||||
|
||||
|
||||
# Validates whether the file is present or not.
|
||||
@@ -170,7 +160,7 @@ if __name__ == "__main__":
|
||||
"--torch_model_csv",
|
||||
type=lambda x: is_valid_file(x),
|
||||
default="./tank/pytorch/torch_model_list.csv",
|
||||
help="""Contains the file with torch_model name and args.
|
||||
help="""Contains the file with torch_model name and args.
|
||||
Please see: https://github.com/nod-ai/SHARK/blob/main/tank/pytorch/torch_model_list.csv""",
|
||||
)
|
||||
parser.add_argument(
|
||||
|
||||
@@ -27,7 +27,7 @@ input_type_to_np_dtype = {
|
||||
"int8": np.int8,
|
||||
}
|
||||
|
||||
WORKDIR = os.path.join(os.path.dirname(__file__), "gen_shark_tank")
|
||||
WORKDIR = os.path.join(os.path.dirname(__file__), "./../gen_shark_tank")
|
||||
|
||||
# Checks whether the directory and files exists.
|
||||
def check_dir_exists(model_name, frontend="torch", dynamic=""):
|
||||
@@ -83,6 +83,36 @@ def download_torch_model(model_name, dynamic=False):
|
||||
return mlir_file, function_name, inputs_tuple, golden_out_tuple
|
||||
|
||||
|
||||
# Downloads the tflite model from gs://shark_tank dir.
|
||||
def download_tflite_model(model_name, dynamic=False):
|
||||
dyn_str = "_dynamic" if dynamic else ""
|
||||
os.makedirs(WORKDIR, exist_ok=True)
|
||||
if not check_dir_exists(model_name, dyn_str):
|
||||
gs_command = (
|
||||
'gsutil -o "GSUtil:parallel_process_count=1" cp -r gs://shark_tank'
|
||||
+ "/"
|
||||
+ model_name
|
||||
+ " "
|
||||
+ WORKDIR
|
||||
)
|
||||
if os.system(gs_command) != 0:
|
||||
raise Exception("model not present in the tank. Contact Nod Admin")
|
||||
|
||||
model_dir = os.path.join(WORKDIR, model_name)
|
||||
with open(
|
||||
os.path.join(model_dir, model_name + dyn_str + "_tflite.mlir")
|
||||
) as f:
|
||||
mlir_file = f.read()
|
||||
|
||||
function_name = str(np.load(os.path.join(model_dir, "function_name.npy")))
|
||||
inputs = np.load(os.path.join(model_dir, "inputs.npz"))
|
||||
golden_out = np.load(os.path.join(model_dir, "golden_out.npz"))
|
||||
|
||||
inputs_tuple = tuple([inputs[key] for key in inputs])
|
||||
golden_out_tuple = tuple([golden_out[key] for key in golden_out])
|
||||
return mlir_file, function_name, inputs_tuple, golden_out_tuple
|
||||
|
||||
|
||||
def download_tf_model(model_name):
|
||||
model_name = model_name.replace("/", "_")
|
||||
os.makedirs(WORKDIR, exist_ok=True)
|
||||
@@ -109,149 +139,3 @@ def download_tf_model(model_name):
|
||||
inputs_tuple = tuple([inputs[key] for key in inputs])
|
||||
golden_out_tuple = tuple([golden_out[key] for key in golden_out])
|
||||
return mlir_file, function_name, inputs_tuple, golden_out_tuple
|
||||
|
||||
|
||||
class SharkDownloader:
|
||||
def __init__(
|
||||
self,
|
||||
model_name: str,
|
||||
tank_url: str = "https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir: str = "./../gen_shark_tank/tflite",
|
||||
model_type: str = "tflite",
|
||||
input_json: str = "input.json",
|
||||
input_type: str = "int32",
|
||||
):
|
||||
self.model_name = model_name
|
||||
self.local_tank_dir = local_tank_dir
|
||||
self.tank_url = tank_url
|
||||
self.model_type = model_type
|
||||
self.input_json = input_json # optional if you don't have input
|
||||
self.input_type = input_type_to_np_dtype[
|
||||
input_type
|
||||
] # optional if you don't have input
|
||||
self.mlir_file = None # .mlir file local address.
|
||||
self.mlir_url = None
|
||||
self.inputs = None # Input has to be (list of np.array) for sharkInference.forward use
|
||||
self.mlir_model = []
|
||||
|
||||
# create tmp model file directory
|
||||
if self.tank_url is None and self.model_name is None:
|
||||
print("Error. No tank_url, No model name,Please input either one.")
|
||||
return
|
||||
|
||||
self.workdir = os.path.join(
|
||||
os.path.dirname(__file__), self.local_tank_dir
|
||||
)
|
||||
os.makedirs(self.workdir, exist_ok=True)
|
||||
print(f"TMP_MODEL_DIR = {self.workdir}")
|
||||
# use model name get dir.
|
||||
self.model_name_dir = os.path.join(self.workdir, str(self.model_name))
|
||||
if not os.path.exists(self.model_name_dir):
|
||||
print(
|
||||
"Model has not been download."
|
||||
"shark_downloader will automatically download by "
|
||||
"tank_url if provided. You can also manually to "
|
||||
"download the model from shark_tank by yourself."
|
||||
)
|
||||
os.makedirs(self.model_name_dir, exist_ok=True)
|
||||
print(f"TMP_MODELNAME_DIR = {self.model_name_dir}")
|
||||
|
||||
# read inputs from json file
|
||||
self.load_json_input()
|
||||
# get milr model file
|
||||
self.load_mlir_model()
|
||||
|
||||
def get_mlir_file(self):
|
||||
return self.mlir_model
|
||||
|
||||
def get_inputs(self):
|
||||
return self.inputs
|
||||
|
||||
def load_json_input(self):
|
||||
print("load json inputs")
|
||||
if self.model_type in ["tflite"]:
|
||||
input_url = (
|
||||
self.tank_url + "/" + str(self.model_name) + "/" + "input.json"
|
||||
)
|
||||
input_file = "/".join([self.model_name_dir, str(self.input_json)])
|
||||
if os.path.exists(input_file):
|
||||
print("Input has been downloaded before.", input_file)
|
||||
else:
|
||||
print("Download input", input_url)
|
||||
urllib.request.urlretrieve(input_url, input_file)
|
||||
|
||||
args = []
|
||||
with open(input_file, "r") as f:
|
||||
args = json.load(f)
|
||||
self.inputs = [
|
||||
np.asarray(arg, dtype=self.input_type) for arg in args
|
||||
]
|
||||
else:
|
||||
print(
|
||||
"No json input required for current model type. "
|
||||
"You could call setup_inputs(YOU_INPUTS)."
|
||||
)
|
||||
return self.inputs
|
||||
|
||||
def load_mlir_model(self):
|
||||
if self.model_type in ["tflite"]:
|
||||
self.mlir_url = (
|
||||
self.tank_url
|
||||
+ "/"
|
||||
+ str(self.model_name)
|
||||
+ "/"
|
||||
+ str(self.model_name)
|
||||
+ "_tflite.mlir"
|
||||
)
|
||||
self.mlir_file = "/".join(
|
||||
[self.model_name_dir, str(self.model_name) + "_tfite.mlir"]
|
||||
)
|
||||
elif self.model_type in ["tensorflow"]:
|
||||
self.mlir_url = (
|
||||
self.tank_url
|
||||
+ "/"
|
||||
+ str(self.model_name)
|
||||
+ "/"
|
||||
+ str(self.model_name)
|
||||
+ "_tf.mlir"
|
||||
)
|
||||
self.mlir_file = "/".join(
|
||||
[self.model_name_dir, str(self.model_name) + "_tf.mlir"]
|
||||
)
|
||||
elif self.model_type in ["torch", "jax", "mhlo", "tosa"]:
|
||||
self.mlir_url = (
|
||||
self.tank_url
|
||||
+ "/"
|
||||
+ str(self.model_name)
|
||||
+ "/"
|
||||
+ str(self.model_name)
|
||||
+ "_"
|
||||
+ str(self.model_type)
|
||||
+ ".mlir"
|
||||
)
|
||||
self.mlir_file = "/".join(
|
||||
[
|
||||
self.model_name_dir,
|
||||
str(self.model_name)
|
||||
+ "_"
|
||||
+ str(self.model_type)
|
||||
+ ".mlir",
|
||||
]
|
||||
)
|
||||
else:
|
||||
print("Unsupported mlir model")
|
||||
|
||||
if os.path.exists(self.mlir_file):
|
||||
print("Model has been downloaded before.", self.mlir_file)
|
||||
else:
|
||||
print("Download mlir model", self.mlir_url)
|
||||
urllib.request.urlretrieve(self.mlir_url, self.mlir_file)
|
||||
|
||||
print("Get .mlir model return")
|
||||
with open(self.mlir_file) as f:
|
||||
self.mlir_model = f.read()
|
||||
return self.mlir_model
|
||||
|
||||
def setup_inputs(self, inputs):
|
||||
print("Setting up inputs. Input has to be (list of np.array)")
|
||||
self.inputs = inputs
|
||||
|
||||
@@ -129,7 +129,7 @@ class SharkImporter:
|
||||
inputs_name = "inputs.npz"
|
||||
outputs_name = "golden_out.npz"
|
||||
func_file_name = "function_name"
|
||||
model_name_mlir = model_name + ".mlir"
|
||||
model_name_mlir = model_name + "_" + self.frontend + ".mlir"
|
||||
np.savez(os.path.join(dir, inputs_name), *inputs)
|
||||
np.savez(os.path.join(dir, outputs_name), *outputs)
|
||||
np.save(os.path.join(dir, func_file_name), np.array(func_name))
|
||||
@@ -139,6 +139,8 @@ class SharkImporter:
|
||||
mlir_str = mlir_data.operation.get_asm()
|
||||
elif self.frontend == "tf":
|
||||
mlir_str = mlir_data.decode("utf-8")
|
||||
elif self.frontend == "tflite":
|
||||
mlir_str = mlir_data.decode("utf-8")
|
||||
with open(os.path.join(dir, model_name_mlir), "w") as mlir_file:
|
||||
mlir_file.write(mlir_str)
|
||||
|
||||
@@ -214,6 +216,14 @@ class SharkImporter:
|
||||
if self.frontend in ["tflite", "tf-lite"]:
|
||||
# TODO(Chi): Validate it for tflite models.
|
||||
golden_out = self.module.invoke_tflite(self.inputs)
|
||||
self.save_data(
|
||||
dir,
|
||||
model_name,
|
||||
imported_mlir[0],
|
||||
imported_mlir[1],
|
||||
self.inputs,
|
||||
golden_out,
|
||||
)
|
||||
return (
|
||||
imported_mlir,
|
||||
self.inputs,
|
||||
|
||||
@@ -98,7 +98,7 @@ class AlbertTfliteModuleTester:
|
||||
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()
|
||||
tflite_results = tflite_preprocessor.get_golden_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
@@ -114,7 +114,7 @@ class AlbertTfliteModuleTester:
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
tflite_results = tflite_preprocessor.get_golden_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import numpy as np
|
||||
import os
|
||||
import csv
|
||||
import urllib.request
|
||||
import json
|
||||
|
||||
|
||||
class TFLiteModelUtil:
|
||||
@@ -48,6 +47,8 @@ class TFLiteModelUtil:
|
||||
)
|
||||
|
||||
for i in range(len(self.output_details)):
|
||||
# print("output_details ", i, "shape", self.output_details[i]["shape"].__name__,
|
||||
# ", dtype: ", self.output_details[i]["dtype"].__name__)
|
||||
out_dtype = self.output_details[i]["dtype"]
|
||||
tflite_results[i] = tflite_results[i].astype(out_dtype)
|
||||
return tflite_results
|
||||
@@ -84,6 +85,7 @@ class TFLitePreprocessor:
|
||||
None # could be tflite/tf/torch_interpreter in utils
|
||||
)
|
||||
self.input_file = None
|
||||
self.output_file = None
|
||||
|
||||
# create tmp model file directory
|
||||
if self.model_path is None and self.model_name is None:
|
||||
@@ -127,7 +129,9 @@ class TFLitePreprocessor:
|
||||
self.mlir_file = "/".join(
|
||||
[tflite_model_name_dir, str(self.model_name) + "_tflite.mlir"]
|
||||
)
|
||||
self.input_file = "/".join([tflite_model_name_dir, "input.json"])
|
||||
self.input_file = "/".join([tflite_model_name_dir, "inputs"])
|
||||
self.output_file = "/".join([tflite_model_name_dir, "golden_out"])
|
||||
# np.save("/".join([tflite_model_name_dir, "function_name"]), np.array("main"))
|
||||
|
||||
if os.path.exists(self.raw_model_file):
|
||||
print(
|
||||
@@ -165,21 +169,15 @@ class TFLitePreprocessor:
|
||||
def generate_inputs(self, input_details):
|
||||
self.inputs = []
|
||||
for tmp_input in input_details:
|
||||
# print(str(tmp_input["shape"]), tmp_input["dtype"].__name__)
|
||||
print(
|
||||
"input_details shape:",
|
||||
str(tmp_input["shape"]),
|
||||
" type:",
|
||||
tmp_input["dtype"].__name__,
|
||||
)
|
||||
self.inputs.append(
|
||||
np.ones(shape=tmp_input["shape"], dtype=tmp_input["dtype"])
|
||||
)
|
||||
# save inputs into json file
|
||||
tmp_json = []
|
||||
for tmp_input in input_details:
|
||||
# print(str(tmp_input["shape"]), tmp_input["dtype"].__name__)
|
||||
tmp_json.append(
|
||||
np.ones(
|
||||
shape=tmp_input["shape"], dtype=tmp_input["dtype"]
|
||||
).tolist()
|
||||
)
|
||||
with open(self.input_file, "w") as f:
|
||||
json.dump(tmp_json, f)
|
||||
return self.inputs
|
||||
|
||||
def setup_inputs(self, inputs):
|
||||
@@ -195,8 +193,9 @@ class TFLitePreprocessor:
|
||||
def get_inputs(self):
|
||||
return self.inputs
|
||||
|
||||
def get_raw_model_output(self):
|
||||
def get_golden_output(self):
|
||||
self.output_tensor = self.interpreter.invoke_tflite(self.inputs)
|
||||
np.savez(self.output_file, *self.output_tensor)
|
||||
return self.output_tensor
|
||||
|
||||
def get_model_details(self):
|
||||
|
||||
@@ -1,154 +0,0 @@
|
||||
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/tensorflow/lite-model/albert_lite_base/squadv1/1?lite-format=tflite"
|
||||
# model_path = model_path
|
||||
|
||||
# Inputs modified to be useful albert inputs.
|
||||
def generate_inputs(input_details):
|
||||
for input in input_details:
|
||||
print(str(input["shape"]), input["dtype"].__name__)
|
||||
|
||||
args = []
|
||||
args.append(
|
||||
np.random.randint(
|
||||
low=0,
|
||||
high=256,
|
||||
size=input_details[0]["shape"],
|
||||
dtype=input_details[0]["dtype"],
|
||||
)
|
||||
)
|
||||
args.append(
|
||||
np.ones(
|
||||
shape=input_details[1]["shape"], dtype=input_details[1]["dtype"]
|
||||
)
|
||||
)
|
||||
args.append(
|
||||
np.zeros(
|
||||
shape=input_details[2]["shape"], dtype=input_details[2]["dtype"]
|
||||
)
|
||||
)
|
||||
return args
|
||||
|
||||
|
||||
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)
|
||||
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 AlbertTfliteModuleTester:
|
||||
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
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="albert_lite_base")
|
||||
raw_model_file_path = tflite_preprocessor.get_raw_model_file()
|
||||
inputs = tflite_preprocessor.get_inputs()
|
||||
tflite_interpreter = tflite_preprocessor.get_interpreter()
|
||||
|
||||
# Use SharkImporter to get SharkInference input args
|
||||
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()
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
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)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
function_name=func_name,
|
||||
device=self.device,
|
||||
mlir_dialect="tflite",
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
class AlbertTfliteModuleTest(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 = AlbertTfliteModuleTester(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 = AlbertTfliteModuleTester()
|
||||
# module_tester.save_mlir = True
|
||||
# module_tester.save_vmfb = True
|
||||
# module_tester.create_and_check_module()
|
||||
|
||||
unittest.main()
|
||||
177
tank/albert_lite_base/albert_lite_base_tflite_sharkimporter.txt
Normal file
177
tank/albert_lite_base/albert_lite_base_tflite_sharkimporter.txt
Normal file
@@ -0,0 +1,177 @@
|
||||
# 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/tensorflow/lite-model/albert_lite_base/squadv1/1?lite-format=tflite"
|
||||
# # model_path = model_path
|
||||
#
|
||||
# # Inputs modified to be useful albert inputs.
|
||||
# def generate_inputs(input_details):
|
||||
# for input in input_details:
|
||||
# print(str(input["shape"]), input["dtype"].__name__)
|
||||
# # [ 1 384] int32
|
||||
# # [ 1 384] int32
|
||||
# # [ 1 384] int32
|
||||
#
|
||||
# args = []
|
||||
# args.append(
|
||||
# np.random.randint(
|
||||
# low=0,
|
||||
# high=256,
|
||||
# size=input_details[0]["shape"],
|
||||
# dtype=input_details[0]["dtype"],
|
||||
# )
|
||||
# )
|
||||
# args.append(
|
||||
# np.ones(
|
||||
# shape=input_details[1]["shape"], dtype=input_details[1]["dtype"]
|
||||
# )
|
||||
# )
|
||||
# args.append(
|
||||
# np.zeros(
|
||||
# shape=input_details[2]["shape"], dtype=input_details[2]["dtype"]
|
||||
# )
|
||||
# )
|
||||
# return args
|
||||
#
|
||||
#
|
||||
# def compare_results(mlir_results, tflite_results):
|
||||
# print("Compare mlir_results VS tflite_results: ")
|
||||
# assert len(mlir_results) == len(
|
||||
# tflite_results
|
||||
# ), "Number of results do not match"
|
||||
# rtol = 1e-02
|
||||
# atol = 1e-03
|
||||
# print(
|
||||
# "numpy.allclose: ",
|
||||
# np.allclose(mlir_results, tflite_results, rtol, atol),
|
||||
# )
|
||||
# for i in range(len(mlir_results)):
|
||||
# mlir_result = mlir_results[i]
|
||||
# tflite_result = tflite_results[i]
|
||||
# mlir_result = mlir_result.astype(np.single)
|
||||
# tflite_result = tflite_result.astype(np.single)
|
||||
# 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 AlbertTfliteModuleTester:
|
||||
# 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
|
||||
#
|
||||
# # Preprocess to get SharkImporter input args
|
||||
# tflite_preprocessor = TFLitePreprocessor(model_name="albert_lite_base")
|
||||
# raw_model_file_path = tflite_preprocessor.get_raw_model_file()
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
# tflite_interpreter = tflite_preprocessor.get_interpreter()
|
||||
#
|
||||
# # Use SharkImporter to get SharkInference input args
|
||||
# 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()
|
||||
#
|
||||
# # Use SharkInference to get inference result
|
||||
# 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_golden_output()
|
||||
# compare_results(mlir_results, tflite_results)
|
||||
# # import pdb
|
||||
# # pdb.set_trace()
|
||||
#
|
||||
# # Case2: Use manually set inputs
|
||||
# # input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
# input_details = [
|
||||
# {
|
||||
# "shape": [1, 384],
|
||||
# "dtype": np.int32,
|
||||
# },
|
||||
# {
|
||||
# "shape": [1, 384],
|
||||
# "dtype": np.int32,
|
||||
# },
|
||||
# {
|
||||
# "shape": [1, 384],
|
||||
# "dtype": np.int32,
|
||||
# },
|
||||
# ]
|
||||
# inputs = generate_inputs(input_details) # new inputs
|
||||
#
|
||||
# shark_module = SharkInference(
|
||||
# mlir_module=mlir_model,
|
||||
# function_name=func_name,
|
||||
# device=self.device,
|
||||
# mlir_dialect="tflite",
|
||||
# )
|
||||
# shark_module.compile()
|
||||
# mlir_results = shark_module.forward(inputs)
|
||||
# ## post process results for compare
|
||||
# tflite_results = tflite_preprocessor.get_golden_output()
|
||||
# compare_results(mlir_results, tflite_results)
|
||||
# # print(mlir_results)
|
||||
#
|
||||
#
|
||||
# class AlbertTfliteModuleTest(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 = AlbertTfliteModuleTester(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 = AlbertTfliteModuleTester()
|
||||
# module_tester.save_mlir = True
|
||||
# module_tester.save_vmfb = True
|
||||
# module_tester.create_and_check_module()
|
||||
|
||||
# unittest.main()
|
||||
@@ -1,10 +1,9 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/tensorflow/lite-model/albert_lite_base/squadv1/1?lite-format=tflite"
|
||||
@@ -14,6 +13,9 @@ from shark.tflite_utils import TFLitePreprocessor
|
||||
def generate_inputs(input_details):
|
||||
for input in input_details:
|
||||
print(str(input["shape"]), input["dtype"].__name__)
|
||||
# [ 1 384] int32
|
||||
# [ 1 384] int32
|
||||
# [ 1 384] int32
|
||||
|
||||
args = []
|
||||
args.append(
|
||||
@@ -37,18 +39,22 @@ def generate_inputs(input_details):
|
||||
return args
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
rtol = 1e-02
|
||||
atol = 1e-03
|
||||
print(
|
||||
"numpy.allclose: ",
|
||||
np.allclose(mlir_results, tflite_results, rtol, atol),
|
||||
)
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
tflite_result = tflite_result.astype(np.single)
|
||||
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)
|
||||
@@ -70,26 +76,24 @@ class AlbertTfliteModuleTester:
|
||||
def create_and_check_module(self):
|
||||
shark_args.save_mlir = self.save_mlir
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="albert_lite_base",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
)
|
||||
tflite_tosa_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
(
|
||||
mlir_model,
|
||||
function_name,
|
||||
inputs,
|
||||
tflite_results,
|
||||
) = download_tflite_model(model_name="albert_lite_base")
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=tflite_tosa_model,
|
||||
mlir_module=mlir_model,
|
||||
function_name="main",
|
||||
device=self.device,
|
||||
mlir_dialect="tflite",
|
||||
)
|
||||
shark_module.compile()
|
||||
shark_module.forward(inputs)
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
# print(shark_results)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class AlbertTfliteModuleTest(unittest.TestCase):
|
||||
@@ -102,11 +106,6 @@ class AlbertTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = AlbertTfliteModuleTester(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"
|
||||
@@ -117,21 +116,3 @@ if __name__ == "__main__":
|
||||
unittest.main()
|
||||
# module_tester = AlbertTfliteModuleTester()
|
||||
# module_tester.create_and_check_module()
|
||||
|
||||
# TEST RESULT:
|
||||
# (shark.venv) nod% python albert_lite_base_tflite_mlir_test.py
|
||||
# load json inputs
|
||||
# TMP_MODEL_DIR = shark/SHARK/shark/./../gen_shark_tank/tflite
|
||||
# Model has not been download.shark_downloader will automatically download by tank_url if provided. You can also manually to download the model from shark_tank by yourself.
|
||||
# TMP_MODELNAME_DIR = shark/SHARK/shark/./../gen_shark_tank/tflite/albert_lite_base
|
||||
# Download mlir model https://storage.googleapis.com/shark_tank/tflite/albert_lite_base/albert_lite_base_tosa.mlir
|
||||
# Get tosa.mlir model return
|
||||
# Target triple found:x86_64-linux-gnu
|
||||
# (shark.venv) nod% python albert_lite_base_tflite_mlir_test.py
|
||||
# load json inputs
|
||||
# TMP_MODEL_DIR = shark/SHARK/shark/./../gen_shark_tank/tflite
|
||||
# TMP_MODELNAME_DIR = shark/SHARK/shark/./../gen_shark_tank/tflite/albert_lite_base
|
||||
# Model has been downloaded before. shark/SHARK/shark/./../gen_shark_tank/tflite/albert_lite_base/albert_lite_base_tosa.mlir
|
||||
# Get tosa.mlir model return
|
||||
# Target triple found:x86_64-linux-gnu
|
||||
#
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -45,22 +44,14 @@ class ArbitraryImageStylizationV1TfliteModuleTester:
|
||||
shark_args.save_mlir = self.save_mlir
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
(
|
||||
mlir_model,
|
||||
function_name,
|
||||
inputs,
|
||||
tflite_results,
|
||||
) = download_tflite_model(
|
||||
model_name="arbitrary-image-stylization-v1-256"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="arbitrary-image-stylization-v1-256",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
function_name="main",
|
||||
@@ -70,15 +61,8 @@ class ArbitraryImageStylizationV1TfliteModuleTester:
|
||||
# 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)
|
||||
# print(shark_results)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class ArbitraryImageStylizationV1TfliteModuleTest(unittest.TestCase):
|
||||
@@ -93,9 +77,7 @@ class ArbitraryImageStylizationV1TfliteModuleTest(unittest.TestCase):
|
||||
)
|
||||
self.module_tester.save_mlir = self.save_mlir
|
||||
|
||||
import sys
|
||||
|
||||
@pytest.mark.xfail(
|
||||
@pytest.mark.skip(
|
||||
reason="known macos tflite install issue & "
|
||||
"'tosa.conv2d' op attribute 'quantization_info' failed "
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -8,13 +8,12 @@ import os
|
||||
import sys
|
||||
import urllib.request
|
||||
from PIL import Image
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://tfhub.dev/google/lite-model/aiy/vision/classifier/birds_V1/3?lite-format=tflite"
|
||||
|
||||
|
||||
def generate_inputs(input_details):
|
||||
# input_details shape: [ 1 224 224 3] type: uint8
|
||||
exe_basename = os.path.basename(sys.argv[0])
|
||||
workdir = os.path.join(os.path.dirname(__file__), "../tmp", exe_basename)
|
||||
os.makedirs(workdir, exist_ok=True)
|
||||
@@ -29,12 +28,12 @@ def generate_inputs(input_details):
|
||||
return args
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -64,20 +63,12 @@ class BirdsV1TfliteModuleTester:
|
||||
shark_args.save_mlir = self.save_mlir
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="birds_V1")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="birds_V1",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
(
|
||||
mlir_model,
|
||||
function_name,
|
||||
inputs,
|
||||
tflite_results,
|
||||
) = download_tflite_model(model_name="birds_V1")
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
function_name="main",
|
||||
@@ -88,17 +79,15 @@ class BirdsV1TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # device_inputs
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
@@ -108,8 +97,7 @@ class BirdsV1TfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -123,9 +111,7 @@ class BirdsV1TfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = BirdsV1TfliteModuleTester(self)
|
||||
self.module_tester.save_mlir = self.save_mlir
|
||||
|
||||
import sys
|
||||
|
||||
@pytest.mark.xfail(
|
||||
@pytest.mark.skip(
|
||||
reason="known macos tflite install issue & "
|
||||
"'tosa.conv2d' op attribute 'quantization_info' failed "
|
||||
)
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/sayakpaul/lite-model/cartoongan/dr/1?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -45,25 +44,12 @@ class CartoonganTfliteModuleTester:
|
||||
shark_args.save_mlir = self.save_mlir
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="cartoongan")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
# input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
# print(input_details[0]["dtype"])
|
||||
# import pdb
|
||||
# pdb.set_trace()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="cartoongan",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
(
|
||||
mlir_model,
|
||||
function_name,
|
||||
inputs,
|
||||
tflite_results,
|
||||
) = download_tflite_model(model_name="cartoongan")
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
function_name="main",
|
||||
@@ -74,14 +60,7 @@ class CartoonganTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class CartoonganTfliteModuleTest(unittest.TestCase):
|
||||
@@ -94,11 +73,6 @@ class CartoonganTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = CartoonganTfliteModuleTester(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"
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/aiy/vision/classifier/birds_V1/3?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,20 +45,12 @@ class DeepLabV3TfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="deeplabv3")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="deeplabv3",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
(
|
||||
mlir_model,
|
||||
function_name,
|
||||
inputs,
|
||||
tflite_results,
|
||||
) = download_tflite_model(model_name="deeplabv3")
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
@@ -71,14 +62,7 @@ class DeepLabV3TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class DeepLabV3TfliteModuleTest(unittest.TestCase):
|
||||
@@ -91,11 +75,6 @@ class DeepLabV3TfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = DeepLabV3TfliteModuleTester(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"
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/tensorflow/lite-model/densenet/1/metadata/1?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,19 +45,12 @@ class DensenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="densenet")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="densenet",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
(
|
||||
mlir_model,
|
||||
function_name,
|
||||
inputs,
|
||||
tflite_results,
|
||||
) = download_tflite_model(model_name="densenet")
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
@@ -70,14 +62,7 @@ class DensenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class DensenetTfliteModuleTest(unittest.TestCase):
|
||||
@@ -90,11 +75,6 @@ class DensenetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = DensenetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# # Source https://tfhub.dev/sayannath/lite-model/image-scene/1
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -60,22 +59,9 @@ class Efficientnet_224_fp32TfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="efficientnet_224_fp32"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="efficientnet_224_fp32",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -88,17 +74,15 @@ class Efficientnet_224_fp32TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -109,9 +93,7 @@ class Efficientnet_224_fp32TfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -125,11 +107,6 @@ class Efficientnet_224_fp32TfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = Efficientnet_224_fp32TfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# # Source https://tfhub.dev/tensorflow/lite-model/efficientnet/lite0/fp32/2
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -60,21 +59,9 @@ class Efficientnet_lite0_fp32_2TfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="efficientnet_lite0_fp32_2"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="efficientnet_lite0_fp32_2",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
@@ -86,17 +73,15 @@ class Efficientnet_lite0_fp32_2TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -107,9 +92,7 @@ class Efficientnet_lite0_fp32_2TfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -123,12 +106,6 @@ class Efficientnet_lite0_fp32_2TfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = Efficientnet_lite0_fp32_2TfliteModuleTester(self)
|
||||
self.module_tester.save_mlir = self.save_mlir
|
||||
|
||||
import sys
|
||||
|
||||
@pytest.mark.xfail(
|
||||
reason="known macos tflite install issue & "
|
||||
"'tosa.conv2d' op attribute 'quantization_info' failed "
|
||||
)
|
||||
def test_module_static_cpu(self):
|
||||
self.module_tester.dynamic = False
|
||||
self.module_tester.device = "cpu"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# # Source https://tfhub.dev/tensorflow/lite-model/efficientnet/lite0/int8/2
|
||||
@@ -22,12 +21,12 @@ def generate_inputs(input_details):
|
||||
return [imagenet_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -58,21 +57,9 @@ class Efficientnet_lite0_int8_2TfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="efficientnet_lite0_int8_2"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="efficientnet_lite0_int8_2",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
@@ -84,17 +71,15 @@ class Efficientnet_lite0_int8_2TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -105,9 +90,7 @@ class Efficientnet_lite0_int8_2TfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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://s3.amazonaws.com/models.huggingface.co/bert/gpt2-64.tflite"
|
||||
@@ -23,12 +22,12 @@ def generate_inputs(input_details):
|
||||
return args
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -59,20 +58,9 @@ class GptTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="gpt2-64")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="gpt2-64",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="gpt2-64"
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
function_name="main",
|
||||
@@ -83,17 +71,15 @@ class GptTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 64],
|
||||
"dtype": np.int32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -104,9 +90,7 @@ class GptTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# # Source https://tfhub.dev/tensorflow/lite-model/inception_v4/1/default/1
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -60,22 +59,9 @@ class Inception_v4_299_fp32TfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="inception_v4_299_fp32"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="inception_v4_299_fp32",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
shark_module = SharkInference(
|
||||
mlir_module=mlir_model,
|
||||
@@ -87,17 +73,15 @@ class Inception_v4_299_fp32TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 299, 299, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -108,9 +92,7 @@ class Inception_v4_299_fp32TfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -124,11 +106,6 @@ class Inception_v4_299_fp32TfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = Inception_v4_299_fp32TfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# Source https://tfhub.dev/tensorflow/lite-model/inception_v4_quant/1/default/1
|
||||
@@ -21,12 +20,12 @@ def generate_inputs(input_details):
|
||||
return [imagenet_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -57,25 +56,9 @@ class Inception_v4_299_uint8TfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="inception_v4_299_uint8"
|
||||
)
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
model_name="inception_v4_299_uint8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="inception_v4_299_uint8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -88,17 +71,15 @@ class Inception_v4_299_uint8TfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 299, 299, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -109,9 +90,7 @@ class Inception_v4_299_uint8TfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/intel/lite-model/midas/v2_1_small/1/lite/1?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,20 +45,9 @@ class MidasTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="midas")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="midas",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="midas"
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -72,14 +60,7 @@ class MidasTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class MidasTfliteModuleTest(unittest.TestCase):
|
||||
@@ -92,11 +73,6 @@ class MidasTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MidasTfliteModuleTester(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"
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/tensorflow/lite-model/mnasnet_1.0_224/1/metadata/1?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,20 +45,9 @@ class MnasnetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="mnasnet_1.0_224")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mnasnet_1.0_224",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mnasnet_1.0_224"
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -72,14 +60,7 @@ class MnasnetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class MnasnetTfliteModuleTest(unittest.TestCase):
|
||||
@@ -92,11 +73,6 @@ class MnasnetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MnasnetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
from shark.parser import shark_args
|
||||
from tank.tflite import squad_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilebert-baseline-tf2-float.tflite"
|
||||
@@ -31,12 +30,12 @@ def generate_inputs(input_details):
|
||||
]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -64,22 +63,9 @@ class MobilebertTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilebert-baseline-tf2-float"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilebert-baseline-tf2-float",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -92,17 +78,23 @@ class MobilebertTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -113,9 +105,7 @@ class MobilebertTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -129,11 +119,6 @@ class MobilebertTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilebertTfliteModuleTester(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"
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
from shark.parser import shark_args
|
||||
from tank.tflite import squad_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilebert-baseline-tf2-quant.tflite"
|
||||
@@ -31,12 +30,12 @@ def generate_inputs(input_details):
|
||||
]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -64,25 +63,9 @@ class MobilebertTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilebert-baseline-tf2-quant"
|
||||
)
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
model_name="mobilebert-baseline-tf2-quant"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilebert-baseline-tf2-quant",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -95,17 +78,23 @@ class MobilebertTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -116,9 +105,7 @@ class MobilebertTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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://storage.googleapis.com/iree-model-artifacts/mobilebert-edgetpu-s-float.tflite"
|
||||
@@ -36,12 +35,12 @@ def generate_inputs(input_details):
|
||||
return args
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -71,22 +70,9 @@ class MobilebertTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilebert-edgetpu-s-float"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilebert-edgetpu-s-float",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -99,17 +85,23 @@ class MobilebertTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -120,9 +112,7 @@ class MobilebertTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -136,11 +126,6 @@ class MobilebertTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilebertTfliteModuleTester(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"
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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://storage.googleapis.com/iree-model-artifacts/mobilebert-edgetpu-s-quant.tflite"
|
||||
@@ -36,12 +35,12 @@ def generate_inputs(input_details):
|
||||
return
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -71,22 +70,9 @@ class MobilebertTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilebert-edgetpu-s-quant"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilebert-edgetpu-s-quant",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -99,14 +85,7 @@ class MobilebertTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class MobilebertTfliteModuleTest(unittest.TestCase):
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
from shark.parser import shark_args
|
||||
from tank.tflite import squad_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
# model_path = "https://tfhub.dev/tensorflow/lite-model/mobilebert/1/metadata/1?lite-format=tflite"
|
||||
|
||||
@@ -30,12 +29,12 @@ def generate_inputs(input_details):
|
||||
]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -63,20 +62,9 @@ class MobilebertTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="mobilebert")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilebert",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilebert"
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -89,17 +77,23 @@ class MobilebertTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
{
|
||||
"shape": [1, 384],
|
||||
"dtype": np.int32,
|
||||
},
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -110,9 +104,7 @@ class MobilebertTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -126,11 +118,6 @@ class MobilebertTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilebertTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilenet_v1_224_1.0_float.tflite"
|
||||
|
||||
@@ -23,12 +22,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -59,22 +58,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v1_224_1.0_float"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v1_224_1.0_float",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -87,17 +73,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -108,9 +92,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -124,11 +106,6 @@ class MobilenetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilenetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilenet_v1_224_1.0_uint8.tflite"
|
||||
@@ -21,12 +20,12 @@ def generate_inputs(input_details):
|
||||
return [imagenet_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -57,22 +56,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v1_224_1.0_uint8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v1_224_1.0_uint8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -85,17 +71,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -106,9 +90,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
# model_path = "https://storage.googleapis.com/tf_model_garden/vision/mobilenet/v2_1.0_int8/mobilenet_v2_1.00_224_int8.tflite"
|
||||
|
||||
@@ -23,12 +22,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -59,22 +58,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v2_1.00_224_int8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v2_1.00_224_int8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -87,17 +73,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -108,9 +92,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilenet_v2_1.0_224.tflite"
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -60,22 +59,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v2_1.0_224"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v2_1.0_224",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -88,17 +74,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -109,9 +93,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -125,11 +107,6 @@ class MobilenetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilenetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilenet_v2_224_1.0_uint8.tflite"
|
||||
@@ -21,12 +20,12 @@ def generate_inputs(input_details):
|
||||
return [imagenet_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -57,22 +56,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v2_224_1.0_uint8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v2_224_1.0_uint8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="int32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -85,17 +71,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -106,9 +90,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilenet_v3-large_224_1.0_float.tflite"
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -60,22 +59,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v3-large_224_1.0_float"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v3-large_224_1.0_float",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -88,17 +74,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -109,9 +93,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -125,11 +107,6 @@ class MobilenetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilenetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/mobilenet_v3-large_224_1.0_uint8.tflite"
|
||||
@@ -21,12 +20,12 @@ def generate_inputs(input_details):
|
||||
return [imagenet_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -57,22 +56,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v3-large_224_1.0_uint8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v3-large_224_1.0_uint8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -85,17 +71,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -106,9 +90,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import imagenet_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/tf_model_garden/vision/mobilenet/v3.5multiavg_1.0_int8/mobilenet_v3.5multiavg_1.00_224_int8.tflite"
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -60,22 +59,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v3.5multiavg_1.00_224_int8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="mobilenet_v3.5multiavg_1.00_224_int8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -89,16 +75,15 @@ class MobilenetTfliteModuleTester:
|
||||
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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 224, 224, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -109,9 +94,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/multi_person_mobilenet_v1_075_float.tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
mlir_result = np.expand_dims(mlir_result, axis=0)
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
tflite_result = tflite_result.astype(np.single)
|
||||
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)
|
||||
@@ -43,22 +45,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
model_name="multi_person_mobilenet_v1_075_float"
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="mobilenet_v2_1.0_224"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="multi_person_mobilenet_v1_075_float",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -71,14 +60,7 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class MobilenetTfliteModuleTest(unittest.TestCase):
|
||||
@@ -91,11 +73,6 @@ class MobilenetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilenetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/tensorflow/lite-model/nasnet/large/1/default/1?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,20 +45,9 @@ class NasnetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="nasnet")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="nasnet",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="nasnet"
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -72,14 +60,7 @@ class NasnetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class NasnetTfliteModuleTest(unittest.TestCase):
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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://storage.googleapis.com/tf_model_garden/vision/resnet50_imagenet/resnet_50_224_int8.tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,22 +45,9 @@ class ResnetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="resnet_50_224_int8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="resnet_50_224_int8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -74,14 +60,7 @@ class ResnetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class ResnetTfliteModuleTest(unittest.TestCase):
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
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/tensorflow/lite-model/squeezenet/1/default/1?lite-format=tflite"
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -46,20 +45,9 @@ class SequeezeNetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(model_name="squeezenet")
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="squeezenet",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="squeezenet"
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -72,14 +60,7 @@ class SequeezeNetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
|
||||
class SequeezeNetTfliteModuleTest(unittest.TestCase):
|
||||
@@ -92,12 +73,6 @@ class SequeezeNetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = SequeezeNetTfliteModuleTester(self)
|
||||
self.module_tester.save_mlir = self.save_mlir
|
||||
|
||||
import sys
|
||||
|
||||
@pytest.mark.xfail(
|
||||
reason="known macos tflite install issue & "
|
||||
"'tosa.conv2d' op attribute 'quantization_info' failed "
|
||||
)
|
||||
def test_module_static_cpu(self):
|
||||
self.module_tester.dynamic = False
|
||||
self.module_tester.device = "cpu"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import coco_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/ssd_mobilenet_v1_320_1.0_float.tflite"
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -57,22 +56,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="ssd_mobilenet_v1_320_1.0_float"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="ssd_mobilenet_v1_320_1.0_float",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -85,17 +71,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 320, 320, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -106,9 +90,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -122,11 +104,6 @@ class MobilenetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = MobilenetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import coco_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/ssd_mobilenet_v1_320_1.0_uint8.tflite"
|
||||
@@ -21,12 +20,12 @@ def generate_inputs(input_details):
|
||||
return [coco_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -54,22 +53,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="ssd_mobilenet_v1_320_1.0_uint8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="ssd_mobilenet_v1_320_1.0_uint8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -82,17 +68,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 320, 320, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -103,9 +87,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -8,7 +8,6 @@ import os
|
||||
import sys
|
||||
import urllib.request
|
||||
from PIL import Image
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# Model from https://github.com/google-coral/test_data/raw/master/ssd_mobilenet_v2_face_quant_postprocess.tflite
|
||||
@@ -33,12 +32,12 @@ def generate_inputs(input_details):
|
||||
return args
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -66,22 +65,9 @@ class MobilenetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="ssd_mobilenet_v2_face_quant"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="ssd_mobilenet_v2_face_quant",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -94,17 +80,15 @@ class MobilenetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 320, 320, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -115,9 +99,7 @@ class MobilenetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import coco_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/ssd_spaghettinet_edgetpu_large.tflite"
|
||||
@@ -24,12 +23,12 @@ def generate_inputs(input_details):
|
||||
return [inputs]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -56,22 +55,9 @@ class SpaghettinetTfliteModuleTester:
|
||||
shark_args.save_mlir = self.save_mlir
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="ssd_spaghettinet_edgetpu_large"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="ssd_spaghettinet_edgetpu_large",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="float32",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -85,16 +71,15 @@ class SpaghettinetTfliteModuleTester:
|
||||
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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 320, 320, 3],
|
||||
"dtype": np.float32,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -105,9 +90,7 @@ class SpaghettinetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
@@ -121,11 +104,6 @@ class SpaghettinetTfliteModuleTest(unittest.TestCase):
|
||||
self.module_tester = SpaghettinetTfliteModuleTester(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"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
from shark.shark_downloader import SharkDownloader
|
||||
from shark.shark_downloader import download_tflite_model
|
||||
from shark.shark_inference import SharkInference
|
||||
import pytest
|
||||
import unittest
|
||||
@@ -7,7 +7,6 @@ from shark.parser import shark_args
|
||||
import os
|
||||
import sys
|
||||
from tank.tflite import coco_data
|
||||
from shark.tflite_utils import TFLitePreprocessor
|
||||
|
||||
|
||||
# model_path = "https://storage.googleapis.com/iree-model-artifacts/ssd_spaghettinet_edgetpu_large_uint8.tflite"
|
||||
@@ -21,12 +20,12 @@ def generate_inputs(input_details):
|
||||
return [coco_data.generate_input(workdir, input_details)]
|
||||
|
||||
|
||||
def compare_results(mlir_results, tflite_results, details):
|
||||
def compare_results(mlir_results, tflite_results):
|
||||
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)):
|
||||
for i in range(len(mlir_results)):
|
||||
mlir_result = mlir_results[i]
|
||||
tflite_result = tflite_results[i]
|
||||
mlir_result = mlir_result.astype(np.single)
|
||||
@@ -54,22 +53,9 @@ class SpaghettinetTfliteModuleTester:
|
||||
shark_args.save_vmfb = self.save_vmfb
|
||||
|
||||
# Preprocess to get SharkImporter input args
|
||||
tflite_preprocessor = TFLitePreprocessor(
|
||||
mlir_model, func_name, inputs, tflite_results = download_tflite_model(
|
||||
model_name="ssd_spaghettinet_edgetpu_large_uint8"
|
||||
)
|
||||
# inputs = tflite_preprocessor.get_inputs()
|
||||
|
||||
shark_downloader = SharkDownloader(
|
||||
model_name="ssd_spaghettinet_edgetpu_large_uint8",
|
||||
tank_url="https://storage.googleapis.com/shark_tank",
|
||||
local_tank_dir="./../gen_shark_tank",
|
||||
model_type="tflite",
|
||||
input_json="input.json",
|
||||
input_type="uint8",
|
||||
)
|
||||
mlir_model = shark_downloader.get_mlir_file()
|
||||
inputs = shark_downloader.get_inputs()
|
||||
func_name = "main"
|
||||
|
||||
# Use SharkInference to get inference result
|
||||
shark_module = SharkInference(
|
||||
@@ -82,17 +68,15 @@ class SpaghettinetTfliteModuleTester:
|
||||
# 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)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
|
||||
# Case2: Use manually set inputs
|
||||
input_details, output_details = tflite_preprocessor.get_model_details()
|
||||
input_details = [
|
||||
{
|
||||
"shape": [1, 320, 320, 3],
|
||||
"dtype": np.uint8,
|
||||
}
|
||||
]
|
||||
inputs = generate_inputs(input_details) # new inputs
|
||||
|
||||
shark_module = SharkInference(
|
||||
@@ -103,9 +87,7 @@ class SpaghettinetTfliteModuleTester:
|
||||
)
|
||||
shark_module.compile()
|
||||
mlir_results = shark_module.forward(inputs)
|
||||
## post process results for compare
|
||||
tflite_results = tflite_preprocessor.get_raw_model_output()
|
||||
compare_results(mlir_results, tflite_results, output_details)
|
||||
compare_results(mlir_results, tflite_results)
|
||||
# print(mlir_results)
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ densenet, https://tfhub.dev/tensorflow/lite-model/densenet/1/metadata/1?lite-for
|
||||
efficientnet_lite0_int8_2, https://storage.googleapis.com/iree-model-artifacts/efficientnet_lite0_int8_2.tflite
|
||||
efficientnet_lite0_fp32_2, https://storage.googleapis.com/iree-model-artifacts/efficientnet_lite0_fp32_2.tflite
|
||||
efficientnet_224_fp32, https://storage.googleapis.com/iree-model-artifacts/efficientnet_224_fp32.tflite
|
||||
gpt2-64, https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-64.tflite
|
||||
inception_v4_299_fp32, https://storage.googleapis.com/iree-model-artifacts/inception_v4_299_fp32.tflite
|
||||
inception_v4_299_uint8, https://storage.googleapis.com/iree-model-artifacts/inception_v4_299_uint8.tflite
|
||||
arbitrary-image-stylization-v1-256, https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/prediction/1?lite-format=tflite
|
||||
@@ -17,7 +16,6 @@ mobilebert-edgetpu-s-quant, https://storage.googleapis.com/iree-model-artifacts/
|
||||
mobilebert, https://tfhub.dev/tensorflow/lite-model/mobilebert/1/metadata/1?lite-format=tflite
|
||||
mobilebert-baseline-tf2-float, https://storage.googleapis.com/iree-model-artifacts/mobilebert-baseline-tf2-float.tflite
|
||||
mobilebert-baseline-tf2-quant, https://storage.googleapis.com/iree-model-artifacts/mobilebert-baseline-tf2-quant.tflite
|
||||
ssd_mobilenet_v2_face_quant, https://storage.googleapis.com/iree-shared-files/models/ssd_mobilenet_v2_face_quant.tflite
|
||||
mobilenet_v1_224_1.0_float, https://storage.googleapis.com/iree-model-artifacts/mobilenet_v1_224_1.0_float.tflite
|
||||
mobilenet_v1_224_1.0_uint8, https://storage.googleapis.com/iree-model-artifacts/mobilenet_v1_224_1.0_uint8.tflite
|
||||
mobilenet_v2_1.00_224_int8, https://storage.googleapis.com/tf_model_garden/vision/mobilenet/v2_1.0_int8/mobilenet_v2_1.00_224_int8.tflite
|
||||
@@ -27,9 +25,11 @@ mobilenet_v3-large_224_1.0_float, https://storage.googleapis.com/iree-model-arti
|
||||
mobilenet_v3-large_224_1.0_uint8, https://storage.googleapis.com/iree-model-artifacts/mobilenet_v3-large_224_1.0_uint8.tflite
|
||||
mobilenet_v3.5multiavg_1.00_224_int8, https://storage.googleapis.com/tf_model_garden/vision/mobilenet/v3.5multiavg_1.0_int8/mobilenet_v3.5multiavg_1.00_224_int8.tflite
|
||||
nasnet, https://tfhub.dev/tensorflow/lite-model/nasnet/large/1/default/1?lite-format=tflite
|
||||
multi_person_mobilenet_v1_075_float, https://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/multi_person_mobilenet_v1_075_float.tflite
|
||||
resnet_50_224_int8, https://storage.googleapis.com/tf_model_garden/vision/resnet50_imagenet/resnet_50_224_int8.tflite
|
||||
squeezenet, https://tfhub.dev/tensorflow/lite-model/squeezenet/1/default/1?lite-format=tflite
|
||||
gpt2-64, https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-64.tflite
|
||||
ssd_mobilenet_v2_face_quant, https://storage.googleapis.com/iree-shared-files/models/ssd_mobilenet_v2_face_quant.tflite
|
||||
multi_person_mobilenet_v1_075_float, https://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/multi_person_mobilenet_v1_075_float.tflite
|
||||
ssd_mobilenet_v1_320_1.0_float, https://storage.googleapis.com/iree-model-artifacts/ssd_mobilenet_v1_320_1.0_float.tflite
|
||||
ssd_mobilenet_v1_320_1.0_uint8, https://storage.googleapis.com/iree-model-artifacts/ssd_mobilenet_v1_320_1.0_uint8.tflite
|
||||
ssd_spaghettinet_edgetpu_large, https://storage.googleapis.com/iree-model-artifacts/ssd_spaghettinet_edgetpu_large.tflite
|
||||
|
||||
|
Reference in New Issue
Block a user