$ show inisis/OnnxSlim
OnnxSlim is a Python library and CLI tool for optimizing ONNX models through graph rewriting. It applies elimination of redundant operations (e.g., no-op reshapes, slices, unsqueezes) and fusion of common patterns (Conv+BN, Conv+Add, GELU, etc.), combined with dead node elimination and shape inference. The architecture is modular: a core optimization pipeline orchestrates pattern matchers, a shape inference engine, and a vendored onnx_graphsurgeon for graph manipulation. The tool is used in production by NVIDIA TensorRT-Model-Optimizer, HuggingFace Optimum, and Ultralytics, confirming its practical value. The codebase demonstrates strong engineering with comprehensive tests, a clean CLI, and a well-organized package structure.
The LLM identified 2 files worth highlighting for reuse:
Implements a shape inference context that propagates ValueInfo through an ONNX graph, updating output shapes and dtypes. It integrates with a registry of per-op shape functions covering standard ONNX ops, contrib ops (attention, normalization, etc.), and uses symbolic computation via sympy. This component is self-contained and could be extracted as a standalone shape inference library for ONNX graphs, demonstrating deep knowledge of ONNX semantics and symbolic reasoning.
Provides a pattern matching framework and registry for ONNX graph transformations. The registry allows registering and applying elimination and fusion patterns (e.g., Concat, Reshape, Conv+BN, GELU). Each pattern matcher is a self-contained class that can be tested in isolation. This modular design enables easy extension with custom patterns and is reusable in other ONNX optimization pipelines.