Commit ed07a9d
authored
[MLX] Fix HF sliding-window KV cache sizing and enable ring buffers (#21635)
**Summary**
export_llm_hf sized every layer's KV cache to the model's
sliding_window, which truncates the full-attention layers of a hybrid
model. Sliding attention was also never actually enabled on this path:
replace_hf_cache_with_mlx gave all layers plain linear caches and plain
causal SDPA.
Hybrid models now install ring buffers for sliding layers and
full-length linear caches for full-attention layers.
replace_hf_cache_with_mlx_ring_buffer takes max_cache_len separately
from window_size, because it previously applied one size to both. The
dynamic sequence dimension is bounded by the window rather than the
cache length, since a ring cannot absorb a single step longer than its
window.
**Files**
* source_transformation.py — replace_hf_cache_with_mlx_ring_buffer gains
a max_cache_len parameter so full-attention layers are sized to the
whole context while sliding layers stay bounded by the window, instead
of applying window_size to both.
* export_llm_hf.py — stops capping the cache length to the window,
routes models with a sliding_window to the ring-buffer cache and
sliding-window SDPA, and bounds the dynamic sequence dimension by the
window rather than the cache length.
**Test**
Exported:
python -m executorch.backends.mlx.examples.llm.export_llm_hf \
--model-id unsloth/gemma-3-1b-it --output gemma3_1b.pte \
--use-custom-sdpa --use-custom-kv-cache --max-seq-len 1024 --dtype fp32
Ran the .pte step by step through pybindings and compared each step's
top-1 token against HuggingFace eager, teacher-forced on the same token
sequence, over a full 1024-token context split at the sliding-window
boundary. Exported and run in fp32, the two agree on every step.
Repeating in bf16 gives a small number of disagreements on both sides of
the boundary (2.8% below, 1.4% above).1 parent d863a27 commit ed07a9d
2 files changed
Lines changed: 69 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
187 | 191 | | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
194 | 196 | | |
195 | 197 | | |
196 | 198 | | |
| |||
228 | 230 | | |
229 | 231 | | |
230 | 232 | | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
236 | 238 | | |
237 | | - | |
238 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
239 | 249 | | |
240 | 250 | | |
241 | | - | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
242 | 254 | | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
251 | 279 | | |
252 | 280 | | |
253 | 281 | | |
| |||
266 | 294 | | |
267 | 295 | | |
268 | 296 | | |
269 | | - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
270 | 304 | | |
271 | 305 | | |
272 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
166 | 167 | | |
167 | 168 | | |
168 | 169 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
179 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
180 | 184 | | |
181 | 185 | | |
182 | 186 | | |
183 | 187 | | |
184 | 188 | | |
185 | 189 | | |
186 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
187 | 195 | | |
188 | 196 | | |
189 | 197 | | |
190 | 198 | | |
191 | | - | |
| 199 | + | |
192 | 200 | | |
193 | 201 | | |
194 | 202 | | |
| |||
242 | 250 | | |
243 | 251 | | |
244 | 252 | | |
245 | | - | |
246 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
247 | 256 | | |
248 | 257 | | |
249 | 258 | | |
| |||
0 commit comments