Benckmark with Onnxruntime * Quantized Model of Onnxruntime - TensorrtExecutionProvider and Onnxruntime - CUDAExecutionProvider has different format. * Onnx that generated by PPQ is not supportable with TensorrtExecutionProvider. * Set providers=CUDAExecutionProvider bef
(onnx_file: str, steps: int = 10000, providers=['CUDAExecutionProvider'], provider_options=None)
| 5 | from time import time |
| 6 | |
| 7 | def Benchmark(onnx_file: str, steps: int = 10000, providers=['CUDAExecutionProvider'], provider_options=None) -> float: |
| 8 | """ Benckmark with Onnxruntime |
| 9 | |
| 10 | * Quantized Model of Onnxruntime - TensorrtExecutionProvider and Onnxruntime - CUDAExecutionProvider has different format. |
| 11 | |
| 12 | * Onnx that generated by PPQ is not supportable with TensorrtExecutionProvider. |
| 13 | |
| 14 | * Set providers=CUDAExecutionProvider before benchmark this file. |
| 15 | |
| 16 | """ |
| 17 | sess = ort.InferenceSession(path_or_bytes=onnx_file, providers=providers, provider_options=provider_options) |
| 18 | |
| 19 | feed_dict, output_names = {}, [] |
| 20 | for input_meta in sess.get_inputs(): |
| 21 | name, dtype, shape = input_meta.name, input_meta.type, input_meta.shape |
| 22 | |
| 23 | for element in shape: |
| 24 | if element is None or type(element) == str: |
| 25 | raise TypeError('Dynamic input is not supported by this function.') |
| 26 | |
| 27 | if dtype == 'tensor(float)': |
| 28 | feed_dict[name] = np.random.random(size=shape).astype(np.float32) |
| 29 | else: |
| 30 | raise Exception(f'Input {name} has unexpected data type.') |
| 31 | |
| 32 | for output_meta in sess.get_outputs(): |
| 33 | output_names.append(output_meta.name) |
| 34 | |
| 35 | tick = time() |
| 36 | for _ in tqdm(range(steps)): |
| 37 | sess.run(output_names=output_names, input_feed=feed_dict) |
| 38 | tok = time() |
| 39 | |
| 40 | print(f'Time span: {tok - tick : .4f} sec') |
| 41 | return tick - tok |
| 42 | |
| 43 | |
| 44 | def Profile(onnx_file: str, steps: int = 1, providers=['CUDAExecutionProvider'], provider_options=None): |
no test coverage detected