908 Commits

Author SHA1 Message Date
LI f4e86ad88f fix lrn even across-channel window size (#7070) 2026-10-12 14:08:14 +08:00
風的彷徨 ab3790faca fix MHA singleton mask broadcast across backends (#6869)
Keep persistent KV caches on the blob allocator, broadcast single-channel attention masks across heads on CPU and Vulkan paths, and add long multi-token cache regressions.
2026-10-10 19:47:13 +08:00
nihui 1efcd3ac4a fix interp scale factor sampling and pnnx recomputed sizes (#7075)
* fix interp scale factor sampling and pnnx recomputed sizes

Use the original scale factor for linear and cubic interpolation instead
of deriving coordinates from the truncated output size. Preserve explicit
sizes, reference sizes, and size expressions across CPU and Vulkan paths.
Keep same-axis linear behavior, resample non-unit same-size bicubic input,
and clamp interpolation taps for short axes without out-of-bounds reads.

Lower recompute_scale_factor to runtime size expressions, retain the flag
when importing nn.Upsample, and avoid deleting non-unit same-size resizes.

Add independent ncnn reference checks and pnnx static/dynamic round-trip
tests covering fractional up/downscales, recomputed sizes, short axes,
align_corners, and nn.Upsample.

Validated CPU interpolation tests, ASan/UBSan, Vulkan llvmpipe, and pnnx
TorchScript interpolation tests against PyTorch 2.10.0 CPU. The original
bilinear/bicubic reproductions fail on the base and pass with this fix.

Refs: Tencent/ncnn#6836, Tencent/ncnn#6837, Tencent/ncnn#6844

---------

Co-authored-by: Acture <acturea@gmail.com>
Co-authored-by: xy200303 <3483421977@qq.com>
Co-authored-by: nihui <171016+nihui@users.noreply.github.com>
2026-10-10 00:00:45 +08:00
Layla b9af93c74b Add percentile PTQ method in ncnn2table (#6854) 2026-10-09 10:33:34 +08:00
風的彷徨 c4393a13b0 pnnx: preserve batch semantics across reshape and flatten (#6934)
Co-authored-by: nihui <nihuini@tencent.com>
2026-10-08 19:08:41 +08:00
acture c798db5525 RotaryEmbed: support full-width cos/sin cache for 2D / vision RoPE (#6834)
* RotaryEmbed: support full-width cos/sin cache for 2D / vision RoPE

The non-interleaved path read only the first embed_dim/2 values of the
cos/sin cache and reused them for both halves of each head vector. That is
correct for standard 1-D RoPE (cos = concat(freqs, freqs), identical halves),
but the pnnx fuse_convert_rotaryembed pass lowers the general expression
x*cos + rotate_half(x)*sin — with a full-width cos — into this op without
checking that the halves are equal. For 2-D / vision RoPE where the halves
differ (cos = concat(cos_h, cos_w), e.g. the Penguin-VL vision encoder) the
second half was rotated with the wrong frequencies, silently producing
wrong output (max|diff| ~3.4 vs PyTorch on the Penguin-VL vision encoder).

Read the second half's cos/sin for the second output half. Backward
compatible via a cache-width guard: when the cache is full width (== embed_dim)
the two halves are independent; when it is embed_dim/2 (the standard-rope form,
as the unit test supplies) the second half reuses the first, so existing
half-width callers stay bit-identical. Applied to every backend
(naive / arm / x86 / loongarch / mips / vulkan) and its bf16/fp16 paths.

Adds a full-width differing-halves test case; the previous test only exercised
half-width caches, which are trivially identical-halves.

After the fix the Penguin-VL vision encoder converts in-graph at
max|diff| = 6.2e-4 vs PyTorch (fp32).

Fixes #6833

* test: add a pnnx test that can actually fail for the full-width cache fix

tests/test_rotaryembed.cpp cannot detect this bug. ncnn's test_layer() compares
the optimized backends against the naive layer, and this PR changes the naive
layer too, so the test stays green whether the semantics are right or wrong —
rebuilding the PR's test against master's src/layer still exits 0.

tools/pnnx/tests/ncnn/ is the only harness in-tree that pins semantics to an
external reference (torch.allclose against PyTorch), so the check belongs there.

Every input is drawn with torch.rand in forward() parameter order, because the
generated test_ncnn_fuse_rotaryembed_ncnn.py takes no arguments: it reseeds with
manual_seed(0) and regenerates each input the same way. An input built any other
way would leave the two sides comparing different tensors, and the test would
fail unconditionally while appearing to test something.

Random caches are also the stronger choice here: the defect is that the layer
reads one cache row for both halves, so it only shows when the halves differ,
which random values guarantee and a hand-built cat(f, f) cache would hide. A
"standard RoPE stays correct" case is deliberately absent — the cache must stay a
pnnx.Input for the fusion pattern to match, and the generator always randomizes
inputs, so that case cannot be expressed in this harness.

Also parameterizes the interleaved path in tests/test_rotaryembed.cpp. On a
CPU-only build that is code-path coverage rather than a correctness check, for
the reason above; its value is on the Vulkan backend.

Not run here: building pnnx needs libtorch. Worth confirming under ctest that the
generated .param really contains RotaryEmbed layers — if the fusion does not
fire, the test passes without exercising anything.

* fix pnnx rotaryembed regression test layout and fusion checks

---------

Co-authored-by: nihui <nihuini@tencent.com>
2026-10-08 18:56:24 +08:00
風的彷徨 123a97c4c5 fix stft detect to support torchaudio.transforms.MelSpectrogram (#6548)
* pnnx: preserve leading dimensions in spectrogram lowering
* pnnx: fix linear leading dimensions and spectrogram tests
* pnnx: fix padding on native batch axes

---------

Co-authored-by: nihui <nihuini@tencent.com>
2026-10-07 16:05:39 +08:00
nihui dae7741b49 update pnnx ci torch 2.14 (#7013)
* do not dce graph input, update transformer tests
2026-09-18 13:07:28 +08:00
禾可 64d633f454 pnnx: handle float16 scalar constant when importing onnx (#6852)
Signed-off-by: 林晨 (Leo Cheng) <leo-cheng@vip.qq.com>
Co-authored-by: nihui <nihuini@tencent.com>
2026-09-17 16:11:57 +08:00
nihui 56e83f813a pnnx do not lower C++20 to C++17 when enabling protobuf (#7010) 2026-09-17 11:21:48 +08:00
primorLee babb819dfa fix: stabilize logaddexp expression evaluation (#6924)
Co-authored-by: nihui <nihuini@tencent.com>
2026-09-17 10:41:00 +08:00
nihui 61ea75123d pnnx requires c++20 for pytorch 2.13+ (#7009) 2026-09-17 10:31:50 +08:00
nihui 8bc95a1e7f pnnx: store bool MemoryData attributes as raw int8 (#7006)
* pnnx: store bool MemoryData attributes as raw int8

Keep bool constants as one byte per element and select MemoryData
load_type=3. Implement the reserved raw int8 ModelBin loading mode and
pad exported bool data to four bytes so subsequent weights stay aligned.
This preserves the byte representation used by the proposed logical ops.

Add regression coverage for scalar and padded bool exports in both fp16
modes, multidimensional MemoryData, raw int8 loading through reference
and copy readers, trailing fp32 weights, and missing padding. Document
the format.

Based on the bool attribute serialization fixes proposed in:
https://github.com/Tencent/ncnn/pull/6956
https://github.com/Tencent/ncnn/pull/6866

Fixes #6855

* tools: preserve byte MemoryData when optimizing models

Save MemoryData load_type so raw int8 constants retain their byte
representation after ModelWriter saves the model. Skip fusion paths that
interpret MemoryData as float32 when its elements are not four bytes.

This change does not add bool arithmetic support or automatic type
promotion. Byte bool storage is intended for future logical, comparison
and selection operators.

Validated ncnnoptimize save and reload for scalar and one-to-five-element
bool constants in fp32 and fp16 storage modes, preserving byte values
and element size in all 12 cases.

---------

Co-authored-by: ktz03 <2484593937@qq.com>
Co-authored-by: Giulio Corradi <53930027+giuliocorradi@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 10:09:00 +08:00
nihui c0abf4830d harden paramdict parsing and typed access (#6983)
Co-authored-by: Nathan Keys <nathan.keys@pm.me>
Co-authored-by: beilzx <2069360659@qq.com>
2026-09-10 17:23:55 +08:00
nihui 34a748349c fp8/fp16 conversion round to nearest ties away from zero (#6958) 2026-09-04 16:23:00 +08:00
nihui 7f162381ab kvcache allocator (#6901)
* decode benchmark
* reject kvcache batch
* enable mha vulkan gemm out_elempack 1
2026-08-17 15:44:30 +08:00
nihui c189d88a8d modelbin bf16 storage type (#6900)
* fix modelwriter on big-endian
2026-08-13 17:19:50 +08:00
nihui 92b73d6665 x86/arm/mips/loongarch/riscv gemm block quantization optimization (#6831)
* benchncnn_llm int8 models
2026-08-04 14:34:22 +08:00
nihui f74639deeb gemm block quantization infrastructure for llm decoder style (#6814)
* another mha oob read
2026-07-13 10:13:39 +08:00
nihui bd5842174f pnnx reshape improve compatibility with old ncnn (#6806)
* onnx flatten to reshape
2026-07-06 19:23:00 +08:00
nihui 81e8cbffd0 pnnx ci update torch 2.12 (#6796) 2026-07-03 23:46:56 +08:00
nihui b10a8327fb pnnx ncnn legalize global pooling layout (#6795) 2026-07-03 16:21:59 +08:00
nihui 127b414614 reshape support batch to/from dim (#6784) 2026-07-02 19:29:34 +08:00
噜小噜芋团 cc16734913 fix modelwriter optional weight serialization (#6726)
Co-authored-by: nihui <nihuini@tencent.com>
Co-authored-by: nihui <shuizhuyuanluo@126.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-05-22 18:36:55 +08:00
nihui 706c4d1964 support 4d mat in more operators (#6737)
Add 4d Mat handling across generic and optimized backends, extend operator tests, and document supported input and output Mat dimensions.
2026-05-22 15:31:01 +08:00
Lune bedf9c6568 unaryop: extend sign, hyperbolic, expm1 and log1p support (#6675)
Add backend support for new UnaryOp operations across scalar, SIMD and Vulkan paths, including sign, sinh, cosh, asinh, acosh, atanh, expm1 and log1p.

Wire the new ops through onnx2ncnn and pnnx expression conversion, update generated Python emission, and add regression coverage for the new conversion paths.

Fix portability issues for expm1/log1p math declarations, NCNN_SIMPLEMATH builds, ONNX-enabled pnnx linking, and stabilize unary op tests across fp16, bf16 and torch exporter/version differences.

Co-authored-by: nihui <nihuini@tencent.com>
2026-05-21 16:19:58 +08:00
Yeuvoir 9db7174bc6 pnnx: fix Conv2d padding tuple normalization (#6694)
Normalize 4-element aten pad values from (left, right, top, bottom) to nn.Conv2d padding order (height, width) when fusing Conv2d modules.

Also add regression coverage for asymmetric ZeroPad2d + depthwise Conv2d to keep the output shape and ncnn pad params correct.
2026-05-21 10:17:58 +08:00
Layla 3724d10fc8 tools:Move Embed/MHA/RNN/LSTM/GRU weight scale generation to ncnn2table (#6688)
Co-authored-by: nihui <nihuini@tencent.com>
2026-05-18 19:38:07 +08:00
e8g(w7n) 485d17327b feat(pnnx): convert prelu[num_parameters=1] to leakyrelu, so that it can be fused with conv (#6344)
Co-authored-by: nihui <nihuini@tencent.com>
2026-05-07 19:46:19 +08:00
nihui 476901a510 pnnx print flops and memops (#5836)
---------

Co-authored-by: luxincn <2391719912@qq.com>
Co-authored-by: SZUwishion <2559916473@qq.com>
Co-authored-by: luxincn <luxincn@users.noreply.github.com>
Co-authored-by: Wu YuXuan <148472521+SZUwishion@users.noreply.github.com>
Co-authored-by: nihui <nihui@users.noreply.github.com>
Co-authored-by: luxincn <118981961+luxincn@users.noreply.github.com>
Co-authored-by: nihui <171016+nihui@users.noreply.github.com>
2026-05-07 19:28:58 +08:00
nihui aa01024557 pnnx: support npy input tensors (#6700)
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>
2026-05-06 20:52:04 +08:00
nihui 0d29a8d048 update pnnx torch-2.11 (#6701) 2026-05-06 20:06:51 +08:00
Lune 82352c9158 [bugfix] pnnx: emit Erf layer for expression erf (#6677) 2026-04-14 19:01:54 +08:00
Molly Sophia c0034d2f51 fix fuse_padding_convolution asymmetric padding params being skipped (#6661)
Signed-off-by: mollyji <mollyji@tencent.com>
2026-04-09 15:26:20 +08:00
nihui 3e97a6e61c update pnnx ci torch 2.10.0 (#6592)
* Downgrade actions/cache from v5 to v4
2026-03-10 11:49:14 +08:00
風的彷徨 f698111525 fmod logaddexp floor_divide remainder support for binaryop (#6549) 2026-03-08 00:05:11 +08:00
nihui b1a08ae9ed packed shape hint for vulkan layers (#6553) 2026-02-26 14:33:43 +08:00
Sense_wang 22ef29fe0a fix: replace 1 bare except clauses with except Exception (#6555) 2026-02-26 11:50:20 +08:00
Evgeny Proydakov 4699b26933 Using snprintf instead of sprintf for a tools (#6554)
Signed-off-by: Evgeny Proydakov <e.proydakov@gmail.com>
2026-02-26 10:29:28 +08:00
nihui ec19da2b61 fix modelwriter padding crash on empty per_channel_pad_data (#6533) 2026-02-04 16:55:39 +08:00
Yiming 4063d7c3df pnnx: add TNN Flatten operator support in pass_level2 (#6513) 2026-01-15 14:09:09 +08:00
nihui 6ebcb644c5 skip opening foldable constant zip without any foldables (#6492) 2026-01-06 16:25:45 +08:00
nihui bfcdd0633e ncnn2mem support new style array and string type (#6440) 2026-01-05 14:12:10 +08:00
nihui 99eccaff80 pnnx ncnn more interleaved rope pattern (#6468) 2025-12-26 17:19:43 +08:00
nihui 7d357031db pnnx fuse adjacent permute, eliminate noop permute (#6467) 2025-12-26 14:16:34 +08:00
nihui c74787622e pnnx ncnn torch flatten handle multiple dynamic dimension (#6466) 2025-12-25 20:01:54 +08:00
nihui a7c1fc2499 pnnx fix ncnn torch stack shape out-of-range crash with negative axis (#6461) 2025-12-24 10:17:33 +08:00
nihui 232c7ab244 pnnx ncnn convert torch flatten, add test (#6462)
* Update upload-artifact action version to v5
2025-12-23 18:36:47 +08:00
nihui bee91ae026 modelwriter tile (#6426) 2025-11-26 19:54:54 +08:00
nihui 430b3124a7 fix ncnn2int8 top_blob int8 scales saving for dequant only style (#6425) 2025-11-26 17:31:57 +08:00