Skip to content

Commit c742ca1

Browse files
committed
DeepSeek-V4.1: left padding
The compressor pooled consecutive tokens before the attention mask applied, so left padding shifted the groups and folded pad tokens into the compressed KV. It now moves the real tokens of each row to the front before pooling, so the groups are counted from the first real token of a row. The compressed entries are left-aligned: the cache writes the new entries of each row at that row's own offset, and keeps the last `ratio - 1` real tokens of a row for its next group. An entry is rotated at the position id of its group, and the indexer hides entries by counting real tokens instead of reading `position_ids`. `forward` still defaults `position_ids` to an `arange`, like the rest of the library. The KV are rounded after RoPE, so the model is not indifferent to a shift of the positions, and a direct call on a padded batch has to pass the positions that `generate` makes. The common left padding test is no longer skipped. It hardcoded a tolerance of 1e-5, so it now takes `atol` and `rtol` like `test_batching_equivalence` does, and we pass the 1e-2 that our other comparison tests use. A new test covers padding with a cache, in chunks and token by token.
1 parent 39eb744 commit c742ca1

5 files changed

Lines changed: 347 additions & 149 deletions

File tree

‎docs/source/en/model_doc/deepseek_v41.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ print(processor.decode(outputs[0, inputs["input_ids"].shape[1] :]))
110110
- `resize_token_embeddings` is not supported, because the Engram token map and n-gram tables are fixed to the
111111
vocabulary of the checkpoint. If you need a new token, reuse one of the placeholder tokens that the tokenizer
112112
already has.
113-
- Batched generation with left padding does not match unpadded generation yet. The compressor pools consecutive
114-
tokens, so padding shifts its groups.
113+
- Batched generation with left padding matches unpadded generation. The compressor pools consecutive tokens, so it
114+
counts its groups from the first real token of each row. If you call the model directly on a left-padded batch,
115+
pass `position_ids` that count from the first real token of each row too, like [`~GenerationMixin.generate`] does.
116+
The rounding below happens after RoPE, so the model is not indifferent to a shift of the positions.
115117
- Like the original implementation, the model rounds its KV and attention output to FP8, and its index keys, index
116118
queries and compressed KV to FP4. That is how they were quantized in training, so it happens whatever dtype you load
117119
the model in. The rounded values keep that dtype, so this does not make the cache any smaller (yet!).

0 commit comments

Comments
 (0)