PNNX (PyTorch to NCNN eXporter) is a Python command-line tool designed for PyTorch model interoperability, primarily converting PyTorch models into the NCNN deep learning inference framework format, or ONNX. It is maintained by Tencent and is part of the larger NCNN project. The current PyPI version is 20260409, with frequent updates often tied to the NCNN project's release cycle.
pip install pnnxVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple PyTorch model, trace it using `torch.jit.trace`, save it, and then use the `pnnx` command-line tool to convert it to the NCNN format. Ensure you have `torch` installed alongside `pnnx`.
Refer to the GitHub README or `pnnx --help` for correct command-line usage. Programmatic use of internal modules is advanced and less documented.
Ensure your PyTorch model is TorchScript-compatible. Use `torch.jit.script` for models with control flow, or refactor models to use static shapes and operations where possible. Thoroughly test the traced model before conversion.
Manually install PyTorch in your environment via `pip install torch` (or `pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118` for CUDA-enabled versions) before attempting to use pnnx.
Always check the GitHub repository's release notes for the exact date version you are using or plan to use to understand changes and new features. Be aware that updates are frequent.
Install PyTorch: `pip install torch` (or the appropriate command for your system/CUDA version).
Ensure `pip install pnnx` completed successfully. Verify that your environment's Python scripts directory (e.g., `~/.local/bin` or `venv/bin`) is included in your system's PATH.
Consult `pnnx --help` for the correct syntax and available options. Ensure `inputshape` is correctly formatted, e.g., `inputshape=[1,3,224,224]`.
Simplify your PyTorch model, remove dynamic elements, or ensure all operations are TorchScript-compatible. Consider using `torch.jit.script` for models with control flow, or debugging the tracing process in PyTorch directly.