OnnxSlim is an open-source toolkit developed by Microsoft for optimizing ONNX (Open Neural Network Exchange) models. It helps reduce model size and improve inference speed through various techniques like node elimination, constant folding, and shape inference. As of version 0.1.91, it is under active development with frequent updates, aiming to provide a robust solution for model deployment.
pip install onnxslimVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load an ONNX model (or create a dummy one for the example) and then use `onnxslim.slim.slim()` to optimize it. The `slim` function returns an ONNX model protocol buffer object, which can then be saved to a new file using `onnx.save()`.
Run inference on the slimmed model with sample data and compare outputs (e.g., using ONNX Runtime) against the original model. Evaluate performance metrics like latency and throughput in your target environment.
After `slim()` and `onnx.save()`, manually copy or manage the external data files. If `onnxslim` changes paths or removes nodes that referenced external data, you might need to re-export the model with external data or verify paths carefully.
Use the `input_shapes` parameter in the `slim` function to provide explicit fixed or symbolic shapes for dynamic inputs, e.g., `slim(model, input_shapes={'input_name': [1, 3, 224, 224]})` or carefully inspect the slimmed model's input shapes using `onnx.checker`.