Trace and export a model to onnx format. Args: model (nn.Module): inputs (tuple[args]): the model will be called by `model(*inputs)` Returns: an onnx model
(model, inputs)
| 31 | |
| 32 | |
| 33 | def export_onnx_model(model, inputs): |
| 34 | """ |
| 35 | Trace and export a model to onnx format. |
| 36 | |
| 37 | Args: |
| 38 | model (nn.Module): |
| 39 | inputs (tuple[args]): the model will be called by `model(*inputs)` |
| 40 | |
| 41 | Returns: |
| 42 | an onnx model |
| 43 | """ |
| 44 | assert isinstance(model, torch.nn.Module) |
| 45 | |
| 46 | # make sure all modules are in eval mode, onnx may change the training state |
| 47 | # of the module if the states are not consistent |
| 48 | def _check_eval(module): |
| 49 | assert not module.training |
| 50 | |
| 51 | model.apply(_check_eval) |
| 52 | |
| 53 | # Export the model to ONNX |
| 54 | with torch.no_grad(): |
| 55 | with io.BytesIO() as f: |
| 56 | torch.onnx.export( |
| 57 | model, |
| 58 | inputs, |
| 59 | f, |
| 60 | operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK, |
| 61 | # verbose=True, # NOTE: uncomment this for debugging |
| 62 | # export_params=True, |
| 63 | ) |
| 64 | onnx_model = onnx.load_from_string(f.getvalue()) |
| 65 | |
| 66 | # Apply ONNX's Optimization |
| 67 | all_passes = onnx.optimizer.get_available_passes() |
| 68 | passes = ["fuse_bn_into_conv"] |
| 69 | assert all(p in all_passes for p in passes) |
| 70 | onnx_model = onnx.optimizer.optimize(onnx_model, passes) |
| 71 | return onnx_model |
| 72 | |
| 73 | |
| 74 | def _op_stats(net_def): |
no outgoing calls
no test coverage detected