OnnxSlim

repository·main·Indexed 19 days ago

https://github.com/inisis/onnxslim

A toolkit designed to optimize ONNX models by reducing the number of operators while maintaining accuracy to improve inference speed. It features Common Subexpression Elimination (CSE), input shape modification, and output customization. The tool provides a CLI and a Python API via the onnxslim.slim class, supporting features such as model inspection, data type conversion (fp16/fp32), and custom fusion pattern registration.

Tokens
6K
Snippets
31
Records
36
Agent score
67%

What's inside onnxslim

  1. How Common Subexpression Elimination (CSE) works in onnxslim

    main
    Common Subexpression Elimination (CSE) is an optimization technique used by onnxslim to improve the performance of slimmed ONNX models. It identifies redundant computations—expressions that are computed multiple times with the same result—and replaces subsequent occurrences with references to the original computation result. This reduces both computational overhead and memory usage by eliminating duplicate operations.
  2. Install OnnxSlim

    main

    You can install OnnxSlim using several methods depending on your needs:

    Using Prebuilt Version

    Install the latest stable version from PyPI:

    pip install onnxslim

    From Source

    Install directly from the main branch on GitHub:

    pip install git+https://github.com/inisis/OnnxSlim@main

    From Local Repository

    Clone the repository and install it locally:

    git clone https://github.com/inisis/OnnxSlim && cd OnnxSlim/
    pip install .
  3. Modify ONNX model outputs using the CLI

    main

    You can customize the output specifications of an ONNX model by specifying which output nodes to keep using the --outputs flag. This allows you to filter the model to only include the specific outputs required for your application, reducing unnecessary data overhead.

    onnxslim yolov5m.onnx slim.onnx --outputs 591 739 443
  4. Export YOLO models with OnnxSlim simplification

    main

    You can use the Ultralytics CLI to export YOLO models to ONNX format. To apply OnnxSlim optimizations during the export, set the simplify flag to True. If set to False, the model will be exported without OnnxSlim streamlining.

    With OnnxSlim (Recommended):

    !yolo export format=onnx model=yolo11n.pt simplify=True

    Without OnnxSlim:

    !yolo export format=onnx model=yolo11n.pt simplify=False
  5. Use the onnxslim CLI to optimize ONNX models

    main

    The onnxslim CLI tool is used to slim down and optimize ONNX models. It supports model modification (inputs/outputs/shapes), optimization settings (fusion patterns, size thresholds), and model checking (verifying output consistency via ONNX Runtime).

    To use the CLI, you typically provide an input model and an optional output path. The tool will perform a series of optimizations and can optionally verify the results against the original model to ensure correctness.

    # Example CLI usage (conceptual based on argument parser structure)
    # onnxslim <input_model> <output_model> [options]
    # onnxslim input.onnx output.onnx --dtype float16 --optimize
  6. Inspect ONNX model details with --inspect

    main

    You can use the --inspect flag with the onnxslim CLI to perform a deep dive into an ONNX model's structure. This command provides detailed insights into:

    • Input and output details
    • Operator information
    • Opset version
    • Other model metadata and intricacies
    onnxslim --inspect UNetModel-fp16.onnx
  7. Run the Common Subexpression Elimination (CSE) demo

    main

    To see how CSE works in practice, you can run the provided demo. This involves two steps: first, generating an ONNX model that contains redundant subexpressions, and second, using onnxslim to optimize it.

    1. Generate the demo model: Run the demo script to create the initial ONNX model (ln_cse.onnx).

    2. Slim the model: Use the onnxslim CLI to process the model and produce an optimized version (slim.onnx).

    # 1. Generate onnx model for demo
    python cse_demo.py
    
    # 2. Apply onnxslim to eliminate common subexpressions
    onnxslim ln_cse.onnx slim.onnx
  8. Modify ONNX model input shapes with onnxslim

    main

    You can use onnxslim to adjust the dimensions of input tensors in an ONNX model. This is useful for ensuring compatibility or optimizing performance for specific neural network architectures. Use the --input_shapes flag to specify the new shapes for your input tensors using the format name:shape (e.g., name:batch,channels,height,width).

    # Syntax: onnxslim <input_model> <output_model> --input_shapes <tensor_name>:<shape>
    # Example: Changing the input shape of UNetModel-fp16.onnx
    onnxslim UNetModel-fp16.onnx slim.onnx --input_shapes cc:1,1,768
  9. Use OnnxSlim via Python API

    main

    To integrate OnnxSlim into your Python code, use the onnxslim.slim() function. This function takes an ONNX model object and returns a slimmed version of it.

    import onnx
    import onnxslim
    
    # Load the original model
    model = onnx.load("model.onnx")
    
    # Slim the model
    slimmed_model = onnxslim.slim(model)
    
    # Save the slimmed model if successful
    if slimmed_model:
        onnx.save(slimmed_model, "slimmed_model.onnx")
    import onnx
    import onnxslim
    
    model = onnx.load("model.onnx")
    slimmed_model = onnxslim.slim(model)
    
    if slimmed_model:
        onnx.save(slimmed_model, "slimmed_model.onnx")