def test_compile_with_configs(self): with open(test_data('MNISTnet_uint8_quant_without_softmax.tflite'), 'rb') as file: compile_task = self.client.submit_compile( source=file, compiler_config={'keep_unsignedness': True}, target_npu_spec={}) compile_task.wait_for_complete() self.assertEqual(compile_task.phase(), 'Succeeded')
def test_compile(self): with open(test_data('MNISTnet_uint8_quant_without_softmax.tflite'), 'rb') as file: compile_task = self.client.submit_compile(source=file) compile_task.wait_for_complete() self.assertEqual(compile_task.phase(), 'Succeeded') self.assertIsNotNone(compile_task.list_artifacts()) self.assertIsNotNone(compile_task.get_ir()) self.assertIsNotNone(compile_task.get_logs()) self.assertIsNotNone(compile_task.get_compiler_report()) self.assertIsNotNone(compile_task.get_dot_graph()) self.assertIsNotNone(compile_task.get_memory_alloc_report())
def test_error(self): request_id = uuid.uuid4().__str__() compiler_config = {'keep_unsignedness': 1} with open(test_data('MNISTnet_uint8_quant_without_softmax.tflite'), 'rb') as file: response = self.compiler_api.create_task( x_request_id=request_id, source=file, compiler_config=compiler_config) self.assertTrue(response.task_id is not None) print(response.task_id) phase = response.phase while phase == 'Running' or phase == 'Pending': time.sleep(1) response = self.compiler_api.get_task(task_id=response.task_id) phase = response.phase self.assertEqual(phase, "Failed") self.assertTrue(response.error_message is not None) print(response.error_message)
def test_compile(self): request_id = uuid.uuid4().__str__() with open(test_data('MNISTnet_uint8_quant_without_softmax.tflite'), 'rb') as file: response = self.compiler_api.create_task(x_request_id=request_id, source=file) self.assertTrue(response.task_id is not None) phase = response.phase while phase == 'Running' or phase == 'Pending': time.sleep(1) response = self.compiler_api.get_task(task_id=response.task_id) phase = response.phase self.assertEqual(phase, "Succeeded") artifacts = self.compiler_api.list_artifacts( task_id=response.task_id) for artifact in artifacts: if artifact.name == 'output.enf': self.compiler_api.get_artifact(task_id=response.task_id, name=artifact.name, _preload_content=False) else: self.compiler_api.get_artifact(task_id=response.task_id, name=artifact.name)
import os import random import numpy as np import tensorflow as tf from furiosa.runtime import session from tests import test_data def model_path(name: str) -> str: return os.path.dirname(__file__) + "/../npu-models/" + name MNIST_MOBINENET_V2 = test_data("MNISTnet_uint8_quant_without_softmax.tflite") #COCO_SSD_MOBILENET_V1 = model_path("tflite/mscoco_300x300_ssd_mobilenet_v1_uint8_quantization-aware-trained.tflite") def assert_tensors_equal(expected, result): assert np.array_equal(expected, result), "{} was expected, but the result was {}".format(expected, result) class SessionTester: def __init__(self, model_path): self.session = session.create(model=model_path) class AsyncSessionTester: def __init__(self, model_path): (self.session, self.queue) = session.create_async(model=model_path)
class CommandTests(unittest.TestCase): mnist_model = test_data('MNISTnet_uint8_quant_without_softmax.tflite') test_onnx_model = test_data('test.onnx') test_dynamic_ranges = test_data('test_dynamic_ranges.json') compiler_config = test_data('compiler_config.yml') invalid_compiler_config = test_data('invalid_compiler_config.yml') def setUp(self): self.parser = argparser.create_argparser() def assert_file_created(self, path, keep: bool = False): self.assertTrue(os.path.isfile(path)) if not keep: os.remove(path) def test_no_command(self): result = subprocess.run(['furiosa'], capture_output=True) self.assertIn('ERROR: Need command', str(result.stderr)) def test_compile(self): result = subprocess.run([ 'furiosa', '-d', '-v', 'compile', self.mnist_model, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('output.enf has been generated', str(result.stdout)) def test_compile_only_target_npu_spec(self): result = subprocess.run([ 'furiosa', '-d', '-v', 'compile', self.mnist_model, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('output.enf has been generated', str(result.stdout)) def test_compile_with_compiler_config(self): result = subprocess.run([ 'furiosa', '-d', '-v', 'compile', self.mnist_model, '--config', self.compiler_config ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('output.enf has been generated', str(result.stdout)) def test_compile_with_target_ir(self): result = subprocess.run([ 'furiosa', '-d', '-v', 'compile', self.mnist_model, '--config', self.compiler_config, '--target-ir', 'lir' ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('output.lir has been generated', str(result.stdout)) def test_compile_with_specific_output(self): output_path = '/tmp/{}.lir'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', '-d', '-v', 'compile', self.mnist_model, '--config', self.compiler_config, '--target-ir', 'lir', '-o', output_path, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('{} has been generated'.format(output_path), str(result.stdout)) self.assert_file_created(output_path) def test_compile_with_reports(self): import uuid output_path = '/tmp/{}.lir'.format(uuid.uuid4()) compiler_report_file = '/tmp/{}.txt'.format(uuid.uuid4()) mem_alloc_report_file = '/tmp/{}.html'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', '-d', '-v', 'compile', self.mnist_model, '--config', self.compiler_config, '--compiler-report', compiler_report_file, '--mem-alloc-report', mem_alloc_report_file, '-o', output_path ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('{} has been generated'.format(output_path), str(result.stdout)) self.assert_file_created(output_path) self.assert_file_created(compiler_report_file) self.assert_file_created(mem_alloc_report_file) def test_compile_with_invalid_config(self): result = subprocess.run([ 'furiosa', 'compile', self.mnist_model, '--config', self.invalid_compiler_config, ], capture_output=True) self.assertTrue(result.returncode != 0) self.assertIn('ERROR: fail to compile', str(result.stderr)) self.assertIn( "thread \'main\' panicked at \'cannot load compiler config from a file.", str(result.stderr)) def test_perfeye(self): output_path = '/tmp/{}.html'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', '-v', 'perfeye', self.mnist_model, '--config', self.compiler_config, '-o', output_path, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('{} has been generated'.format(output_path), str(result.stdout)) self.assert_file_created(output_path) def test_perfeye_with_compiler_config(self): output_path = '/tmp/{}.html'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', '-v', 'perfeye', self.mnist_model, '--config', self.compiler_config, '-o', output_path, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('{} has been generated'.format(output_path), str(result.stdout)) self.assert_file_created(output_path) def test_perfeye_with_target_npu_spec(self): output_path = '/tmp/{}.html'.format(uuid.uuid4()) result = subprocess.run( [ 'furiosa', '-v', 'perfeye', self.mnist_model, # '--target-npu-spec', self.target_npu_spec, '--config', self.compiler_config, '-o', output_path, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('{} has been generated'.format(output_path), str(result.stdout)) self.assert_file_created(output_path) def test_optimize_and_quantize(self): optimized_model_path = '/tmp/{}.onnx'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', '-v', 'optimize', self.test_onnx_model, '-o', optimized_model_path, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) quantized_model_path = '/tmp/{}.onnx'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', 'quantize', optimized_model_path, '-o', quantized_model_path, '--dynamic-ranges', self.test_dynamic_ranges, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assert_file_created(optimized_model_path) self.assert_file_created(quantized_model_path) def test_build_calibration_model(self): output_path = '/tmp/{}.onnx'.format(uuid.uuid4()) result = subprocess.run([ 'furiosa', '-v', 'build_calibration_model', self.test_onnx_model, '-o', output_path, ], capture_output=True) self.assertEqual(0, result.returncode, result.stderr) self.assert_file_created(output_path) def test_toolchain_list(self): result = subprocess.run(['furiosa', '-v', 'toolchain', 'list'], capture_output=True) self.assertEqual(0, result.returncode, result.stdout)