ONNX Optimizer

repository·main·Indexed 21 days ago

https://github.com/onnx/optimizer

A C++ library and Python tool for performing arbitrary optimizations and prepackaged optimization passes on ONNX models. It provides a Python API via the optimize() function and a command-line interface to apply fusion and elimination passes, facilitating model reuse across various ONNX backend implementations.

Tokens
1.8K
Snippets
8
Records
8
Agent score
74%

What's inside onnxoptimizer

  1. Install onnxoptimizer

    main

    You can install onnxoptimizer directly from PyPI using pip3. If you encounter issues, ensure your pip is upgraded first.

    To build from source, you must have protobuf installed on your system. Clone the repository recursively to ensure submodules are included.

    ```bash
    # Install from PyPI
    pip3 install onnxoptimizer
    
    # If you have trouble, upgrade pip first
    pip3 install -U pip
    
    # Build from source
    git clone --recursive https://github.com/onnx/optimizer onnxoptimizer
    cd onnxoptimizer
    pip3 install -e .
    ```埋
  2. Reference the onnxoptimizer CLI arguments

    main

    The following optional arguments are available for the onnxoptimizer command-line tool:

    ArgumentDescription
    -h, --helpShow this help message and exit
    --print_all_passesPrint all available passes
    --print_fuse_elimination_passesPrint all fuse and elimination passes
    -p [PASSES ...], --passes [PASSES ...]List of optimization passes names. If not set, fuse_and_elimination_passes will be used
    --fixed_pointPerform fixed point optimization
    usage: python -m onnxoptimizer input_model.onnx output_model.onnx 
    
    onnxoptimizer command-line api
    
    optional arguments:
      -h, --help            show this help message and exit
      --print_all_passes    print all available passes
      --print_fuse_elimination_passes
                            print all fuse and elimination passes
      -p [PASSES ...], --passes [PASSES ...]
                            list of optimization passes name, if no set, fuse_and_elimination_passes will be used
      --fixed_point         fixed point
  3. Use the onnxoptimizer Command-line API

    main

    The onnxoptimizer provides a command-line interface to perform optimizations on ONNX models without writing a Python script. The basic syntax requires an input model path and an output model path.

    By default, if no specific passes are provided, the tool uses fuse_and_elimination_passes.

    python -m onnxoptimizer input_model.onnx output_model.onnx
  4. Retrieve available optimization passes

    main

    The onnxoptimizer package provides two functions to discover which optimization passes can be applied to a model:

    1. get_available_passes(): Returns a list of all available optimization pass names.
    2. get_fuse_and_elimination_passes(): Returns a subset of passes specifically focused on operator fusion and elimination.

    These are useful for inspecting the capabilities of the optimizer before calling optimize().

    from onnxoptimizer import get_available_passes, get_fuse_and_elimination_passes
    
    all_passes = get_available_passes()
    print(f"Available passes: {all_passes}")
    
    fusion_passes = get_fuse_and_elimination_passes()
    print(f"Fusion/Elimination passes: {fusion_passes}")
  5. Optimize an ONNX model with optimize()

    main

    Use the optimize function to apply a set of optimization passes to an onnx.ModelProto.

    If no passes are provided, the function defaults to using the passes returned by get_fuse_and_elimination_passes().

    Arguments:

    • model: The onnx.ModelProto to be optimized.
    • passes: A list of strings representing the names of the optimization passes to apply. If None, fusion and elimination passes are used.
    • fixed_point: A boolean. If True, uses fixed-point optimization logic. If False (default), uses standard optimization logic.

    Returns:

    • An optimized onnx.ModelProto.
    import onnx
    from onnxoptimizer import optimize
    
    # Load your model
    model = onnx.load("model.onnx")
    
    # Optimize using default fusion and elimination passes
    optimized_model = optimize(model)
    
    # Or specify specific passes
    optimized_model = optimize(model, passes=["some_specific_pass"])
    
    # Save the result
    onnx.save(optimized_model, "optimized_model.onnx")
  6. Reference the onnxoptimizer CLI arguments

    main

    The following flags are available when running the onnxoptimizer CLI via python -m onnxoptimizer:

    --print_all_passes
        action="store_true",
        default=False,
        help="print all available passes",
    
    --print_fuse_elimination_passes
        action="store_true",
        default=False,
        help="print all fuse and elimination passes",
    
    -p, --passes
        nargs="*",
        default=None,
        help="list of optimization passes name, if no set, fuse_and_elimination_passes will be used",
    
    --fixed_point
        action="store_true", default=False, help="fixed point",
    
    --skip_infer_shapes
        action="store_true",
        default=False,
        help="Skip shape inference after optimization"
  7. Run the ONNX Optimizer via the command line

    main
    The onnxoptimizer package provides a command-line interface (CLI) to optimize ONNX models. You can invoke the optimizer directly using the python3 -m onnxoptimizer command. Note that the current implementation is considered highly EXPERIMENTAL and APIs are subject to change.
    python3 -m onnxoptimizer
  8. Use the onnxoptimizer CLI to optimize models

    main

    The onnxoptimizer command-line tool allows you to optimize ONNX models by applying a sequence of optimization passes. By default, if no specific passes are provided, it uses a predefined set of fusion and elimination passes.

    Basic Usage:

    python -m onnxoptimizer input_model.onnx output_model.onnx

    Important Note on Large Models: For models larger than 2GB, standard onnx.checker.check_model(model) may fail. The CLI handles this by using onnx.checker.check_model(input_file) as a workaround.

    Experimental Warning: The CLI and its underlying APIs are considered highly experimental and are subject to change.

    python -m onnxoptimizer input_model.onnx output_model.onnx