Repository navigation
[executorch][native-aot] Serialize lifted constants in FQN order - #25584
Closed
pytorchbot wants to merge 56 commits into
Closed
pytorchbot wants to merge 56 commits into
pytorchbot wants to merge 56 commits into
Conversation
…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/)
…rope FuseRoPEPass also matches rotary tables of [R, B, T] position rows that are all equal, as transformers' Qwen3.5 M-RoPE builds them for text: trig of every row, the first row's interleaved slots overwritten with the others' (select_scatter, or the where it decomposes to), then cat(row, row). These rotate by the first row's positions; rows that may differ stay decomposed. Qwen3.5 graphs built these tables from cos, sin, where, slice_scatter and strided slices instead of one rope per projection. Authored with assistance from Claude and Codex. Differential Revision: [D132884113](https://our.internmc.facebook.com/intern/diff/D132884113/) ghstack-source-id: 480469661 Pull-Request: #25485
A model whose StaticCache has linear-attention layers (Qwen3.5's gated DeltaNet) exports a static one-token `forward`: their conv and recurrent states become non-persistent buffers that a call at position 0 resets in the graph by multiplying them by 0, and golden samples feed prompts one token per call. transformers' export wrapper registers every layer's keys and values, which these layers lack, and their chunked prefill does not export with a dynamic token count. Authored with assistance from Claude and Codex. Differential Revision: [D132169803](https://our.internmc.facebook.com/intern/diff/D132169803/) ghstack-source-id: 480469671 Pull-Request: #25486
…ables Qwen3.5 GGUF files load through transformers' own GGUF tables: its renames, norm offsets, `A_log`, convolution shape and value-head untiling. Quantized tensors keep their blocks where `get_gguf_plan` marks every conversion safe on packed rows; `out_proj`, whose tiled columns cross blocks, is dequantized. The MTP block and, with `--layers`, blocks past the last layer are skipped. transformers maintains these tables per architecture, so export reuses them instead of copying each one; unknown ops or a changed plan API fail loudly. Authored with assistance from Claude and Codex. Differential Revision: [D132169804](https://our.internmc.facebook.com/intern/diff/D132169804/) ghstack-source-id: 480469666 Pull-Request: #25487
A GGUF weight whose columns transformers' tables reorder, Qwen3.5's `linear_attn.out_proj`, now keeps its blocks as stored: `load_gguf` reorders its module's input instead, with a forward pre-hook that exports as a view and a transpose (two axes of the last dim swapped); other reorderings are refused. The reordering crosses quantization blocks, so the previous diff dequantized these weights: 1.33 GB more in a Qwen3.5-9B PTN, 5.19 GB in a 27B one. Authored with assistance from Claude and Codex. Differential Revision: [D132169807](https://our.internmc.facebook.com/intern/diff/D132169807/) ghstack-source-id: 480469665 Pull-Request: #25488
…DA and HIP On CUDA and HIP the ggml engine now stores a zero-initialized F32 mutable buffer in F16 when its only uses are row writes and flash attention keys or values that the GPU runs; the PTN still declares an F32 cache. The testing option `f32_gpu_kv_cache_for_testing` (a ggml_ptn_runner flag) keeps these in-graph caches F32; off-graph caches take CacheConfig::kv_dtype. ggml-cuda's flash attention rounds F32 keys and values to F16 on every call, as an F16 `set_rows` does, so results are unchanged at half the cache bytes. Authored with assistance from Claude and Codex. Differential Revision: [D132607617](https://our.internmc.facebook.com/intern/diff/D132607617/) ghstack-source-id: 480469663 Pull-Request: #25489
`FuseRoPEPass` also fuses a rotate-half RoPE whose cos/sin tables broadcast over dim 2, i.e. an apply on [B, T, H, D] before the head transpose, as `native.rope` between two `permute(0, 2, 1, 3)`. Gemma 4 rotates its projections in that layout, so its RoPE stayed as cos, sin, slices, negations and concatenations in every layer. Authored with assistance from Claude and Codex. Differential Revision: [D133282463](https://our.internmc.facebook.com/intern/diff/D133282463/) ghstack-source-id: 480469673 Pull-Request: #25490
Asks ggml whether the GPU runs a cache's flash attention over F16 keys and values with the mask the lowering passes: custom_sdpa always passes one, aten SDPA when it has attn_mask or is_causal. custom_sdpa without an explicit mask attends start_pos + T keys, so it is probed at 1 key as well as the cache's. ggml-cuda runs head size 512 only with a mask, a grouped query and a key count divisible by 256, so the mask-less probe kept gemma-4's global-layer caches in F32: twice their memory, and a conversion to F16 on every call. Authored with assistance from Claude and Codex. Differential Revision: [D133282464](https://our.internmc.facebook.com/intern/diff/D133282464/) ghstack-source-id: 480469670 Pull-Request: #25491
A method's lifted parameters, buffers and constants are now serialized in the order of their FQNs: their placeholder nodes, the method's constants and its mutable buffers. Nothing else in the program moves. Strict export lifts them in the order the model first reads them. Gemma 4 reads its two rotary inv_freq buffers in a set's iteration order, which follows the process's hash seed, so one export in three wrote another PTG. Authored with assistance from Claude and Codex. Differential Revision: [D133282465](https://our.internmc.facebook.com/intern/diff/D133282465/) ghstack-source-id: 480469660 Pull-Request: #25492
pytorchbot
requested review from
JacobSzwejbka and
larryliu0820
as code owners
November 23, 2026 06:39
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25584
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 2 PendingAs of commit 811e28b with merge base 8c3f75d ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
|
Duplicate merge-bot PR; this orig already landed via stack #25603. |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #25492 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/285/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/285/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/284/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/285/orig
Differential Revision: D133282465
@diff-train-skip-merge