Profile 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 befor
(onnx_file: str, steps: int = 1, providers=['CUDAExecutionProvider'], provider_options=None)
| 42 | |
| 43 | |
| 44 | def Profile(onnx_file: str, steps: int = 1, providers=['CUDAExecutionProvider'], provider_options=None): |
| 45 | """ Profile with Onnxruntime |
| 46 | |
| 47 | * Quantized Model of Onnxruntime - TensorrtExecutionProvider and Onnxruntime - CUDAExecutionProvider has different format. |
| 48 | |
| 49 | * Onnx that generated by PPQ is not supportable with TensorrtExecutionProvider. |
| 50 | |
| 51 | * Set providers=CUDAExecutionProvider before benchmark this file. |
| 52 | |
| 53 | """ |
| 54 | options = ort.SessionOptions() |
| 55 | options.enable_profiling = True |
| 56 | sess = ort.InferenceSession( |
| 57 | path_or_bytes=onnx_file, providers=providers, |
| 58 | provider_options=provider_options, sess_options=options) |
| 59 | |
| 60 | feed_dict, output_names = {}, [] |
| 61 | for input_meta in sess.get_inputs(): |
| 62 | name, dtype, shape = input_meta.name, input_meta.type, input_meta.shape |
| 63 | |
| 64 | for element in shape: |
| 65 | if element is None or type(element) == str: |
| 66 | raise TypeError('Dynamic input is not supported by this function.') |
| 67 | |
| 68 | if dtype == 'tensor(float)': |
| 69 | feed_dict[name] = np.random.random(size=shape).astype(np.float32) |
| 70 | else: |
| 71 | raise Exception(f'Input {name} has unexpected data type.') |
| 72 | |
| 73 | for output_meta in sess.get_outputs(): |
| 74 | output_names.append(output_meta.name) |
| 75 | |
| 76 | for _ in tqdm(range(steps)): |
| 77 | sess.run(output_names=output_names, input_feed=feed_dict) |
| 78 | |
| 79 | prof_file = sess.end_profiling() |
| 80 | print(f'Profile file is generated at {prof_file}, open it with your web browser chrome://tracing/') |
no test coverage detected