pnnx input npy files (libnpy-based) #6295
lancerstadium
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
✨ pnnx Support for
.npyInput Files (libnpy-based)🎯 Motivation
During graph export and verification, it is often useful to inject real input tensors into the pnnx graph:
.npyas a standard and portable format widely supported in Python/Numpy/PyTorch.The chosen solution was to integrate npy.hpp (header-only, based on libnpy), ensuring zero link-time dependency and minimal intrusion.
🛠️ Key Changes
New dependency: Added
npy.hppdirectly intopnnx/src.Graph binding API:
.npyfiles and attaches the tensor to a specific or auto-matched operand.operand.params["__data__"](keeping compatibility with existing passes).Command-line parsing:
Added
input=a.npy[,b.npy,...]option, reusingparse_string_list.Execution order:
Binding occurs before
pass_level2so later passes can consume actual tensor data.🔑 Binding Semantics
Index ≥ 0 → Force binding to
graph.operands[index].Index < 0 → Non-forced mode:
basename(path)vs.operand->name).Return codes:
0success,-1failure (file error, shape mismatch, etc.).Payload: Includes raw buffer, shape, dtype, and Fortran-order flag—kept intact for later processing.
⚡ Design Considerations & Challenges
Minimal Intrusion
dims,type), input data is side-attached as a parameter (__data__).Name vs. Index Matching
x.1) that don’t align with user file names (a.npy).Type & Layout Handling
.npysupports multiple dtypes (float32,int64, etc.) and memory layouts (C/Fortran).Error Handling
📌 Current Limitations & Future Directions
float64 → float32).input[0]=a.npy,input[name]=b.npy..npy: extend parsing to.binor.npzcontainers.💡 Open Questions for the Community
__data__be standardized into a dedicated Attribute field inOperand?🙏 Acknowledgments
Thanks to the maintainers and community for the clear structure of
pnnx, which made it possible to implement this feature with minimal and modular changes. I hope this contribution helps pave the way for more data-aware graph passes in the future. Feedback and suggestions are warmly welcome!Beta Was this translation helpful? Give feedback.
All reactions