Skip to content

[executorch][native-test] Record Gated DeltaNet op samples - #25574

Closed
pytorchbot wants to merge 48 commits into
mainfrom
gh/digantdesai/277/orig
Closed

pytorchbot wants to merge 48 commits into
mainfrom
gh/digantdesai/277/orig

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #25484 by @digantdesai
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/277/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/277/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/276/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/277/orig
Differential Revision: D132169802
@diff-train-skip-merge

…h built graph

Each method's graph capacity is at least graph_size and at least the metadata
room its lowerings need, 80 tensors per node and 4 per tensor-list entry, so no
graph outgrows it; the backend scheduler is sized from each built graph's
nodes and leafs; the graph builder knows whether its primary backend is a GPU.

A fixed graph_size aborted methods whose lowerings expand nodes into many
tensors, such as a cat of a long list, and sized every scheduler for the worst
case.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676076](https://our.internmc.facebook.com/intern/diff/D131676076/)


ghstack-source-id: 480469568
Pull-Request: #25438
le and lt now round each operand to its dtype, then to the F16 or BF16 dtype
eager compares in, and give a scalar a tensor's dtype within its category.
Integer operands they can trace (graph inputs and aranges, through casts,
views and repeats) must be ones F32 holds exactly, within +-2^24; graph inputs
are rechecked on every run.

Later rows reuse the exact-integer checks. Split out of "Lower gt", which
moves to the parked branch with v6.3's parked ops.

Authored with assistance from Claude and Codex.

Differential Revision: [D133282461](https://our.internmc.facebook.com/intern/diff/D133282461/)


ghstack-source-id: 480469573
Pull-Request: #25439
Each piece of `split` and `split_with_sizes` (and their copy forms) is a view of
its slice, copied only when a consumer needs it contiguous; the piece count and
sizes are checked against the declared outputs, without overflow.

The ggml engine had no lowering for them, and `chunk` and QKV splits export
them.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676083](https://our.internmc.facebook.com/intern/diff/D131676083/)


ghstack-source-id: 480469585
Pull-Request: #25440
topk takes the first k of the last dim with `ggml_argsort_top_k`, as llama.cpp
does, and gathers their values; ties may come out in another order than
PyTorch's, and integer inputs must be exact in F32. A row holding NaN comes out
unordered, as in llama.cpp; the testing option `exact_topk_for_testing` ranks
NaN above every value as eager does, at a NaN key map and a second argsort.

MoE routers pick experts with `topk`, which the engine did not lower.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676087](https://our.internmc.facebook.com/intern/diff/D131676087/)


ghstack-source-id: 480469588
Pull-Request: #25441
Reductions permute the reduced dims to the end and reduce them as one ggml row,
and softmax permutes its dim to the end and back as a view; softmax over an
empty dim is refused. amax returns NaN for a row holding one, as eager does, and
on GPUs fixes rows where argmax finds no index (only -inf or -FLT_MAX).

`sum`, `mean` and `amax` reduced trailing dims only, and `softmax` normalized
only the last dim, so reductions over a leading or middle dim were refused.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676089](https://our.internmc.facebook.com/intern/diff/D131676089/)


ghstack-source-id: 480469586
Pull-Request: #25442
With the testing option `exact_gelu_for_testing`, the CPU builds tanh GELU from
exact ops, x * (1 + tanh(c * (x + 0.044715 x^3))) / 2, matching eager to F32
precision. By default the engine keeps ggml's GELU, as llama.cpp does: ggml-cpu
reads it from an F16 table, a few 1e-4 off PyTorch's F32 result, and GPUs
evaluate the formula in F32 in one kernel. The erf form is unchanged.

Exact tanh GELU is for tests; the default follows llama.cpp's numerics.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676096](https://our.internmc.facebook.com/intern/diff/D131676096/)


ghstack-source-id: 480469589
Pull-Request: #25443
Lower `aten.ne` (Tensor and Scalar) through `lower_compare` as
`step(step(a - b) + step(b - a) + n)`, with `n` 1 where an operand is NaN,
since equal infinities also differ by NaN. Integer operands must be ones F32
holds exactly: graph inputs, rechecked on every run, or aranges, through
integer casts, views or repeats; anything else is refused. New `ne` op samples.

Gated DeltaNet models compare positions with `ne` to reset state.

Authored with assistance from Claude and Codex.

Differential Revision: [D132169792](https://our.internmc.facebook.com/intern/diff/D132169792/)


ghstack-source-id: 480469590
Pull-Request: #25444
…on the host

Every comparison now accepts integer operands that adds and subtracts compute
from graph inputs, constants and aranges, such as attention-mask positions.
The host refuses at compile one no run can bound (`x * 3`, a mutable buffer,
a constant past 2^24). It needs no per-run check when constants, shapes and
row indices bound it within +-2^24: an input written as KV cache rows lies in
[0, rows), which every run rejects otherwise as a hard error before computing,
and only a cache of at most 2^24 rows counts. Otherwise, e.g. a position a
graph input offsets, each run bounds it; only without off-graph KV.

Authored with assistance from Claude and Codex.

Differential Revision: [D133480461](https://our.internmc.facebook.com/intern/diff/D133480461/)


ghstack-source-id: 480469584
Pull-Request: #25445
A tensor above rank 4 whose extra leading dims are all 1 now runs at rank 4
(`fold_leading_ones`) in new tensors, reshapes, views and permutes; a permute
folds only when its result keeps those leading 1s in front, and slice, select,
split and cat map dims through the folded rank. Other tensors
above rank 4, and a cat along a folded 1, are refused with a message.

ggml tensors are at most rank 4, and Qwen3.5's `repeat_interleave` and
head-axis permutes build rank-5 tensors with a leading 1.

Authored with assistance from Claude and Codex.

Differential Revision: [D132169800](https://our.internmc.facebook.com/intern/diff/D132169800/)


ghstack-source-id: 480469598
Pull-Request: #25446
…cal inputs

`write_ptn` and `write_samples` fix every member's zip metadata (earliest zip
timestamp, owner read/write, Unix host); the package layout and the PTN schema
are unchanged.

Two exports of the same model gave .ptn files that differed only in zip member
timestamps, so a file hash could not show that an export is deterministic.

Authored with assistance from Claude and Codex.

Differential Revision: [D131566108](https://our.internmc.facebook.com/intern/diff/D131566108/)


ghstack-source-id: 480469601
Pull-Request: #25447
`Package::load` now refuses a big-endian host before it reads anything,
using `check_little_endian_host`, and the README states that a `.ptn` is
little-endian throughout.

On a big-endian host the safetensors header and payloads and the packed
codec bytes would be misread, since they are read as stored, little-endian;
only FlatBuffers byte-swaps on read.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791805](https://our.internmc.facebook.com/intern/diff/D131791805/)


ghstack-source-id: 480469605
Pull-Request: #25448
…ted types

The exporter takes a GGUF type's block geometry from gguf-py and
dequantizes it with `gguf.dequantize`, accepting only validated types
(`_VALIDATED_QUANT_TYPES`, here Q4_K, Q5_K and Q6_K). It refuses big-endian
GGUF files, and a test checks gguf-py's geometry against ggml's block structs.

A new type then needs no exporter code, and the exporter only passes through
bytes that ggml reads the same way.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791806](https://our.internmc.facebook.com/intern/diff/D131791806/)


ghstack-source-id: 480469604
Pull-Request: #25449
The ggml engine maps a `gguf:<type>` codec to the ggml type of that name
and sizes its blocks with `ggml_type_size` and `ggml_blck_size`, accepting
only validated types (`kValidatedGgufTypes`). A test checks ggml's sizes
against a table summed from ggml's block structs.

This replaces a per-type lookup, so a new type needs one allowlist entry, and
a ggml update that changes a block layout fails the test.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791807](https://our.internmc.facebook.com/intern/diff/D131791807/)


ghstack-source-id: 480469607
Pull-Request: #25450
native-vk keys each decoded GGUF constant by its package owner, codec and
shape instead of by owner alone, and records each owner's source bytes once.

The package keeps one copy of byte-identical constants, and different codecs
decode it differently: byte-identical Q4_K and Q5_K weights failed to load.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791809](https://our.internmc.facebook.com/intern/diff/D131791809/)


ghstack-source-id: 480469603
Pull-Request: #25451
Each codec of native-vk's reference GGUF decoder carries its own block
size and byte count, `ReferenceGgufKQuant` becomes `ReferenceGgufQuant`, and
a packed constant the decoder cannot decode is refused by name.

The decoder assumed 256-weight super-blocks, which ruled out 32-weight types
such as Q8_0.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791810](https://our.internmc.facebook.com/intern/diff/D131791810/)


ghstack-source-id: 480469609
Pull-Request: #25452
…ears

test_ggml_ops adds a Q5_K embedding test, and a Q5_K linear test that bounds
the error by ggml's activation rounding, with helpers that build random blocks
of any GGUF type.

The existing Q5_K linear test allows an absolute error of 0.35 on random
activations, and Q5_K embeddings had no test. Later types reuse the helpers.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791811](https://our.internmc.facebook.com/intern/diff/D131791811/)


ghstack-source-id: 480469602
Pull-Request: #25453
`test_gguf_hf` loads a GGUF file that mixes Q4_K, Q5_K and Q6_K tensors and
checks each one's HF parameter against a table written out in the test, and
`test_cross_engine` exports one and runs the same package end to end on ggml
and native-vk. A table gives each tensor's type by llama.cpp role, so a newly
supported type takes one entry.

GGUF files labeled Q4_K_M mix these types.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791812](https://our.internmc.facebook.com/intern/diff/D131791812/)


ghstack-source-id: 480469606
Pull-Request: #25454
…ings

A linear and an embedding of random Q4_K, Q5_K and Q6_K weights are each
exported once with a dynamic token count and run at 9, 1 and 33 tokens: on
ggml in `test_ggml_ops`, and from one package on ggml and native-vk in
`test_cross_engine`.

Exported LLMs run prefill and decode on one executable, and ggml's GPU
backends switch matmul kernels above 8 rows.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791813](https://our.internmc.facebook.com/intern/diff/D131791813/)


ghstack-source-id: 480469608
Pull-Request: #25455
The README's "Packed constants" section defines `gguf:<type>`, a GGUF tensor's
blocks exactly as the file stores them in ggml's little-endian layout, and which
component refuses what. The exporter and ggml engine accept GGUF Q8_0,
native-vk's reference decoder decodes it as ggml does, and a `gguf_q8_0`
op-sample scheme records its samples with gguf-py's block size.

Exporters and engines need one written contract for these bytes, and GGUF Q8_0
and Q4_K_M files store Q8_0 tensors, the first 32-weight type.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791815](https://our.internmc.facebook.com/intern/diff/D131791815/)


ghstack-source-id: 480469621
Pull-Request: #25456
The exporter and the ggml engine accept GGUF Q3_K, native-vk's reference
decoder decodes it as ggml does, and a `gguf_q3_k` op-sample scheme records
its linear and embedding samples. Random-weight, dynamic-length and
mixed-file tests cover it on ggml and native-vk.

GGUF files labeled Q3_K_S, Q3_K_M and Q3_K_L store Q3_K tensors.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791816](https://our.internmc.facebook.com/intern/diff/D131791816/)


ghstack-source-id: 480469617
Pull-Request: #25457
The exporter and the ggml engine accept GGUF Q2_K, native-vk's reference
decoder decodes it as ggml does, and a `gguf_q2_k` op-sample scheme records
its linear and embedding samples. Random-weight, dynamic-length and
mixed-file tests cover it on ggml and native-vk.

GGUF files labeled Q2_K store Q2_K tensors.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791818](https://our.internmc.facebook.com/intern/diff/D131791818/)


ghstack-source-id: 480469623
Pull-Request: #25458
The exporter and the ggml engine accept GGUF Q4_0, native-vk's reference
decoder decodes it as ggml does, and a `gguf_q4_0` op-sample scheme records
its linear and embedding samples. Random-weight, dynamic-length and
mixed-file tests cover it on ggml and native-vk.

GGUF files labeled Q4_0 store Q4_0 tensors, which load as stored, unlike
torchao int4 weights, which the ggml engine converts to Q4_0 at load.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791819](https://our.internmc.facebook.com/intern/diff/D131791819/)


ghstack-source-id: 480469620
Pull-Request: #25459
The exporter and the ggml engine accept GGUF Q5_0, native-vk's reference
decoder decodes it as ggml does, and a `gguf_q5_0` op-sample scheme records
its linear and embedding samples. Random-weight, dynamic-length and
mixed-file tests cover it on ggml and native-vk.

GGUF files labeled Q5_0 store Q5_0 tensors.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791820](https://our.internmc.facebook.com/intern/diff/D131791820/)


ghstack-source-id: 480469618
Pull-Request: #25460
The exporter and the ggml engine accept GGUF Q5_1, native-vk's reference
decoder decodes it as ggml does, and a `gguf_q5_1` op-sample scheme records
its linear and embedding samples. Random-weight, dynamic-length and
mixed-file tests cover it on ggml and native-vk.

GGUF files labeled Q5_1 store Q5_1 tensors.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791822](https://our.internmc.facebook.com/intern/diff/D131791822/)


ghstack-source-id: 480469622
Pull-Request: #25461
The exporter and the ggml engine accept GGUF Q4_1, native-vk's reference
decoder decodes it as ggml does, and a `gguf_q4_1` op-sample scheme records
its linear and embedding samples. Random-weight, dynamic-length and
mixed-file tests cover it on ggml and native-vk.

GGUF files labeled Q4_1 store Q4_1 tensors.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791823](https://our.internmc.facebook.com/intern/diff/D131791823/)


ghstack-source-id: 480469627
Pull-Request: #25462
… product

FuseRoPEPass tests now also build the rotary tables from the broadcast
product `position_ids[..., None].float() * inv_freq`: fusion across dtypes,
rotary widths and attention scales, every non-RoPE variant left unfused, and
a broadcast product off the FP32 contract left unfused.

transformers forms the phase this way from 5.19. The pass's matcher for it
now comes with the off-graph KV stack (#24945); these tests keep it covered.

Authored with assistance from Claude and Codex.

Differential Revision: [D131724778](https://our.internmc.facebook.com/intern/diff/D131724778/)


ghstack-source-id: 480469619
Pull-Request: #25463
TinyLlama in the ggml op tests now wraps its model with the HF exporter's
LastTokenLM instead of calling the transformers cache wrapper directly.

transformers 5.5+ reads positions from the cache length, so the direct call
left `cache_position` unused and to_native rejected the method; LastTokenLM
writes the cache at the input positions on every transformers version.

Authored with assistance from Claude and Codex.

Differential Revision: [D131724779](https://our.internmc.facebook.com/intern/diff/D131724779/)


ghstack-source-id: 480469626
Pull-Request: #25464
…tents

The ggml engine's host evaluator now computes `aten.slice` of small integer
tensors with unit step and bounds it can read (ints or input sizes), returns
an empty input or slice at once, and still refuses any other slice.

transformers 5 offsets mask query positions by `cache_position[:1]`, so
without this the attended key prefix falls back to the whole cache and every
decode attends all of it.

Authored with assistance from Claude and Codex.

Differential Revision: [D131724781](https://our.internmc.facebook.com/intern/diff/D131724781/)


ghstack-source-id: 480469625
Pull-Request: #25465
…arange

LowerHFAttention also accepts integer `arange(T) + cache_position[0:1]` as
the mask's query positions and RoPE's position_ids: start 0, step 1, alpha 1,
T the SymInt sizing the token axis of the q, k or v view (or a literal equal
to the static lengths), the offset sliced from the same cache_position input.

transformers 5 builds positions this way; they equal cache_position under the
existing contiguity check. Otherwise the block stays unlowered.

Authored with assistance from Claude and Codex.

Differential Revision: [D131724782](https://our.internmc.facebook.com/intern/diff/D131724782/)


ghstack-source-id: 480469633
Pull-Request: #25466
Select the transformers 5.20.0 stack (tokenizers 0.23.1, huggingface-hub
1.31.0, safetensors 0.8.0) for every target under backends/native with a
version modifier in its PACKAGE file. Other users keep the 4.57.1 default.

New model architectures ship only in transformers 5, and the HF exporter
follows its model code.

Authored with assistance from Claude and Codex.

Differential Revision: [D131724783](https://our.internmc.facebook.com/intern/diff/D131724783/)
A `flatten_linear_batches` pass runs every linear of a static rank-4 input as
a rank-3 one on the merged leading dims and views the result back; at batch 1
the two views are redundant but harmless. A dynamic rank-4 input, which
native-vk could not resize before either, and a rank-4 input whose output is
not rank 4 are refused at load.

ET-VK's linear ignores batch dims before -3, so a linear with a leading batch
above one returned wrong values with no error.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676068](https://our.internmc.facebook.com/intern/diff/D131676068/)


ghstack-source-id: 480469649
Pull-Request: #25477
native-vk resizes a blitted `_to_copy`'s output to its input's sizes before each
copy.

ET-VK runs a same-dtype or float/half `_to_copy` as a blit, which has no resize
hook, so with a dynamic input the output kept the sizes it was built with and
later calls returned the wrong shape.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676069](https://our.internmc.facebook.com/intern/diff/D131676069/)


ghstack-source-id: 480469652
Pull-Request: #25478
native-vk computes log_softmax over a dim of size one as `x - x`, reading x in
the output's storage and layout as softmax does.

That result is exactly 0 (NaN where x is not finite), but ET-VK's log(sum(exp(x
- max))) came out off for large inputs.

Authored with assistance from Claude and Codex.

Differential Revision: [D131676070](https://our.internmc.facebook.com/intern/diff/D131676070/)


ghstack-source-id: 480469645
Pull-Request: #25479
F16 matrix multiplies keep ggml's accumulation by default, as llama.cpp does.
For testing, the engine option `cuda_f32_accumulation_for_testing` (a runner
flag too) makes linear and matmul with an F16 operand request F32
accumulation on CUDA, at 2-3x prefill and 4x decode GEMM time plus a
transient F32 weight copy; HIP and the CPU never ask.

cuBLAS multiplies F16 operands in F16 unless asked otherwise, so long F16 sums
drift up to a few 1e-2 from eager; the option lets a test check F32 numerics.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791824](https://our.internmc.facebook.com/intern/diff/D131791824/)


ghstack-source-id: 480469646
Pull-Request: #25480
…ger tests

The README lists six differences from eager that ggml's GPU kernels cause,
each with a test that fails on its backend and is skipped there: on CUDA, TF32
F32 GEMMs, F16 GEMMs accumulated in F16 (a testing option requests F32), and
fast-math tanh and GELU; on HIP, BF16 GEMMs
rounded before the bias; on both, TF32 or XF32 MMF inputs for 4-16 rows and
fp16 Q4_1/Q5_1 block-scale products, which the GPU op-sample check allows.

The engine keeps ggml's defaults, as llama.cpp does.

Authored with assistance from Claude and Codex.

Differential Revision: [D131791825](https://our.internmc.facebook.com/intern/diff/D131791825/)


ghstack-source-id: 480469637
Pull-Request: #25481
…eferences

`record` runs the precise and int8-activation references of each call on their
own deep copies of the method, and restores its buffers to their initial
values after the golden calls.

A chain whose calls mutate buffers shared them across the golden, precise and
int8-activation passes, so each pass advanced the others' state. And since
`ExportedProgram.state_dict` aliases the module's buffers, the state left
after the golden calls was saved as the package's initial state.

Authored with assistance from Claude and Codex.

Differential Revision: [D132169801](https://our.internmc.facebook.com/intern/diff/D132169801/)


ghstack-source-id: 480469642
Pull-Request: #25482
The partitioner keeps aten.softplus instead of decomposing it into exp,
log1p, gt and where. ggml lowers it to ggml_softplus at beta 1 and threshold
20, and otherwise scales around it and picks x past the threshold by row
gather. et-cpu's Reference provider computes it when no linked kernel library
has it, and a CPU method that no provider accepts now names the operator.

Gated DeltaNet's decay is a softplus, one ggml kernel. PTNs exported before
hold it through log1p, which v6.3 parks, so they need re-export to run on ggml.

Authored with assistance from Claude and Codex.

Differential Revision: [D133282462](https://our.internmc.facebook.com/intern/diff/D133282462/)


ghstack-source-id: 480469639
Pull-Request: #25483
New `gdn` op samples whose oracle is transformers' Qwen3.5 code, in f32: the
recurrent gated delta rule at a small and at Qwen3.5's shape, and the
`Qwen3_5GatedDeltaNet` layer chained over one-token decode calls, its conv and
recurrent states buffers that a call at position 0 resets in the graph. GGUF
schemes get a fifth scale family for the layer's five linears.

Engines had no samples for Gated DeltaNet decode, the linear-attention layer of
Qwen3.5. f16 and bf16 are left out: ggml's depthwise convolution is F32-only.

Authored with assistance from Claude and Codex.

Differential Revision: [D132169802](https://our.internmc.facebook.com/intern/diff/D132169802/)


ghstack-source-id: 480469650
Pull-Request: #25484
@pytorch-bot

pytorch-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25574

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 7c51882 with merge base 8c3f75d (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 23, 2026
Base automatically changed from gh/digantdesai/276/orig to main November 23, 2026 16:16
@digantdesai

Copy link
Copy Markdown
Contributor

Duplicate merge-bot PR; this orig already landed via stack #25603.

This branch was previously deployed

1 inactive deployment
cadence — 7c51882a Deployed Nov 23, 2026 by pytorchbot via hifi-op-test / hifi4 #40273
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants