Skip to content

pnnx: support npy input tensors - #6700

Merged
nihui merged 2 commits into
Tencent:masterfrom
nihui:pnnx-npy
May 6, 2026
Merged

pnnx: support npy input tensors#6700
nihui merged 2 commits into
Tencent:masterfrom
nihui:pnnx-npy

Conversation

@nihui

@nihui nihui commented May 6, 2026

Copy link
Copy Markdown
Member

No description provided.

@tencent-adm

Copy link
Copy Markdown
Member

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class support for providing .npy files as example input tensors to pnnx, enabling conversions/tests to run with real tensor values (including non-native endianness and Fortran-ordered arrays) instead of synthetic/random inputs.

Changes:

  • Add a .npy loader (load_numpy_file) and plumb input= / input2= CLI args through model loading and code generation.
  • Update TorchScript/TNN loading paths to accept inferred input shapes/types from .npy and (for TorchScript) construct input tensors from the loaded payload.
  • Add new unit tests (TorchScript/ONNX/TNN) and register them in CMake.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tools/pnnx/tests/test_pnnx_input_npy.py New TorchScript test covering .npy inputs (incl. endian + Fortran order) for multi-output model.
tools/pnnx/tests/test_pnnx_input_npy_input2.py New TorchScript test covering input + input2 with two example input sets.
tools/pnnx/tests/test_pnnx_input_npy_int64.py New TorchScript test covering int64 .npy inputs (e.g., gather indices).
tools/pnnx/tests/test_pnnx_input_npy_tnn.py New TNN test covering .npy input for a minimal ReLU network.
tools/pnnx/tests/onnx/test_pnnx_input_npy.py New ONNX test covering .npy inputs (incl. endian + Fortran order).
tools/pnnx/tests/onnx/CMakeLists.txt Registers the new ONNX .npy input test.
tools/pnnx/tests/CMakeLists.txt Registers new .npy input tests (including conditional TNN test).
tools/pnnx/src/utils.h Introduces NumpyArray + load_numpy_file() declaration.
tools/pnnx/src/utils.cpp Implements .npy parsing, dtype/shape handling, endian swap, and Fortran→C reordering.
tools/pnnx/src/save_ncnn.h Extends save_ncnn() signature to accept input .npy paths.
tools/pnnx/src/save_ncnn.cpp Emits Python that can load provided .npy inputs for generated ncnn inference wrappers.
tools/pnnx/src/main.cpp Adds input/input2 CLI params, parses .npy inputs, and wires paths/contents into downstream stages.
tools/pnnx/src/load_torchscript.h Extends load_torchscript() signature to accept raw input payloads.
tools/pnnx/src/load_torchscript.cpp Creates Torch tensors from .npy payload bytes (with validation) and adds bool handling.
tools/pnnx/src/load_tnn.h Extends load_tnn() signature to accept user-provided input shapes/types.
tools/pnnx/src/load_tnn.cpp Validates/overrides TNN input shape/type based on provided .npy/inputshape.
tools/pnnx/src/ir.h Extends Graph::python() signature to accept input .npy paths.
tools/pnnx/src/ir.cpp Emits Python that can load provided .npy inputs for generated pnnx inference wrappers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tools/pnnx/src/main.cpp Outdated
Comment thread tools/pnnx/src/save_ncnn.cpp Outdated
Comment thread tools/pnnx/src/ir.cpp Outdated
@nihui
nihui force-pushed the pnnx-npy branch 2 times, most recently from 15d9d13 to 2a79f50 Compare May 6, 2026 09:07
@nihui

nihui commented May 6, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Comment thread tools/pnnx/src/utils.cpp
Comment thread tools/pnnx/src/utils.cpp
Comment thread tools/pnnx/src/load_tnn.cpp
Comment thread tools/pnnx/src/main.cpp
Co-authored-by: lovedream-ms <209071654+lovedream-ms@users.noreply.github.com>
Co-authored-by: lancerstadium <57707690+lancerstadium@users.noreply.github.com>
Co-authored-by: AtomAlpaca <66774326+AtomAlpaca@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@nihui
nihui merged commit aa01024 into Tencent:master May 6, 2026
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants