{"library":"pnnx","title":"PNNX","description":"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.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install pnnx"],"cli":{"name":"pnnx","version":""}},"imports":["import pnnx"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import torch\nimport os\n\n# 1. Define a simple PyTorch model\nclass MyModel(torch.nn.Module):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        self.conv = torch.nn.Conv2d(3, 32, 3, padding=1)\n        self.relu = torch.nn.ReLU()\n\n    def forward(self, x):\n        return self.relu(self.conv(x))\n\nmodel = MyModel()\ndummy_input = torch.rand(1, 3, 64, 64)\n\n# 2. Trace the model using torch.jit.trace\n# This creates a TorchScript module which pnnx can convert.\ntraced_model = torch.jit.trace(model, dummy_input)\n\n# 3. Save the traced model to a file\nmodel_path = \"my_model.pt\"\ntraced_model.save(model_path)\n\nprint(f\"PyTorch model saved to {model_path}\")\nprint(\"\\nNow, open your terminal and run the following command to convert the model:\")\nprint(f\"pnnx {model_path} inputshape=[{','.join(map(str, dummy_input.shape))}] outputpath=converted_model\")\nprint(\"\\nThis will generate NCNN model files (e.g., converted_model.param, converted_model.bin) in the 'converted_model' directory.\")\n\n# You can also specify ONNX output:\n# print(\"Or for ONNX output:\")\n# print(f\"pnnx {model_path} inputshape=[{','.join(map(str, dummy_input.shape))}] outputpath=converted_model --onnx\")\n","lang":"python","description":"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`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}