vulkan: fix RotaryEmbed on 2D input when seqlen is a multiple of 4 (#7336)
- vulkan: fix RotaryEmbed pack4 for 2D input
A 2D RotaryEmbed input (embed_dim, seqlen) is packed along seqlen on Vulkan, so seqlen % 4 == 0 runs rotaryembed_pack4.comp. That shader was written for 3D input packed along num_heads, where the 4 lanes of a vec4 share one cos/sin row. For 2D input the 4 lanes are 4 consecutive positions, but all of them used the cos/sin row of the packed row index, so every position except the first one was rotated by the wrong angle.
Pass dims to the shaders and load the cos/sin rows of the 4 positions when the input is 2D. The 3D path is unchanged.
Add 2D test cases with seqlen 1, 3, 4, 8, 13 and 16 for both modes and both cos/sin widths.
- vulkan: keep the RotaryEmbed 3D pack4 path as before
Without a shape hint, dims is a push constant, and turning cos/sin into vec4 for every input made the 3D pack4 shader about 15% slower on MoltenVK (128x2048x32, 398 -> 466 us). Handle 2D input in its own block at the top of the shader and leave the 3D code as it was on master.
- vulkan: preserve RotaryEmbed packing
Bind input and cache buffers directly and keep the input packing on output. Use vector loads for 2D pack4 data and caches, with shared cos/sin addressing for either cache layout.
Cover native pack1 and pack4, check output packing before download, and exercise host pack8, pack16, and pack32 through Vulkan upload.
- vulkan: simplify RotaryEmbed cache loads
Load pack1 and pack4 caches through matching buffer views and use cache_hpack for the cache stride. Cover 1D caches with single-token inputs and fix the packed FP16 buffer_st2 macro.
Co-authored-by: nihui nihuini@tencent.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
ncnn
ncnn is a high-performance neural network inference framework optimized for mobile, embedded, and desktop deployment. It has no third-party runtime dependencies, runs across CPU and Vulkan GPU backends, and provides tools such as pnnx for converting PyTorch and ONNX models to ncnn. Developers can deploy deep learning models efficiently on phones, PCs, browsers, and edge devices. ncnn is currently being used in many Tencent applications, such as QQ, Qzone, WeChat, Pitu, and so on.
ncnn 是一个面向移动端、嵌入式和桌面端部署优化的高性能神经网络推理框架。 ncnn 无第三方运行时依赖,支持 CPU 和 Vulkan GPU 后端,并提供 pnnx 等工具将 PyTorch 和 ONNX 模型转换为 ncnn 模型。 基于 ncnn,开发者可以将深度学习模型高效部署到手机、PC、浏览器和边缘设备上。 ncnn 目前已在腾讯多款应用中使用,如:QQ,Qzone,微信,天天 P 图等。
Quick Start
The recommended beginner path is PyTorch -> pnnx -> ncnn.
Install pnnx in a PyTorch environment
Export a PyTorch model to ncnn
This generates
model.ncnn.paramandmodel.ncnn.bin.Run with ncnn C++ API
Or use Python
See pnnx, use ncnn with PyTorch or ONNX, Python API, and examples for complete workflows.
Community
637093648 (超多大佬)
答案:卷卷卷卷卷(已满)
https://t.me/ncnnyes
https://discord.gg/YRsxgmF
677104663 (超多大佬)
答案:multi-level intermediate representation
818998520 (新群!)
Download & Build status
https://github.com/Tencent/ncnn/releases/latest
how to build ncnn library on Linux / Windows / macOS / Raspberry Pi3, Pi4 / POWER / Android / NVIDIA Jetson / iOS / WebAssembly / AllWinner D1 / Loongson 2K1000
Build
Use the prebuilt packages above when possible. To build from source, see the full how to build ncnn library guide for Linux, Windows, macOS, Android, iOS, WebAssembly, HarmonyOS, Raspberry Pi, Jetson, and embedded targets.
Common Linux build:
Model Conversion
pnnx.export(model, "model.pt", (input_tensor,))orpnnx model.pt inputshape=[...]pnnx model.onnxncnnoptimize model.param model.bin new.param new.bin flagUse Netron to inspect
.param,.onnx, and.pnnx.paramgraphs.Features
.paramand.binmodels.Model and Workload Coverage
ncnn is still strong for classic and mobile CNN workloads, but current usage is broader than CNN-only deployment.
For operator-level detail, see supported PyTorch operator status, supported ONNX operator status, and operation param weight table.
Project Examples
Documentation And FAQ
License
BSD 3 Clause