Datasets:

Modalities:
Image
Size:
< 1K
Libraries:
Datasets
License:
ArthurZ HF Staff commited on
Commit
276ae7d
·
verified ·
1 Parent(s): 16f1674

Upload folder using huggingface_hub

Browse files
deepseek_v4/deepseek_v4_mask_layer0_sliding_attention.svg CHANGED
deepseek_v4/deepseek_v4_mask_layer1_compressed_sparse_attention.svg CHANGED
deepseek_v4/deepseek_v4_mask_layer2_heavily_compressed_attention.svg CHANGED
deepseek_v4/visualize_attention_masks.py CHANGED
@@ -184,9 +184,9 @@ def _render(
184
  SVG_CELL = 24
185
  SVG_GAP = 2
186
  SVG_PAD_LEFT = 120 # roomy enough for the longest row-label token ("morning") at 11pt
187
- SVG_PAD_TOP = 180 # title/shape/meta (60px) + C_w + pos + rotated tokens (~60px) + a touch.
188
  SVG_PAD_RIGHT = 48
189
- SVG_PAD_BOTTOM = 64
190
  SVG_COLORS = {
191
  # Dark-mode palette: black background, light text.
192
  "background": "#0b1220", # near-black with a hint of blue
@@ -225,11 +225,12 @@ def _render_svg(mask_2d: torch.Tensor, title: str, sliding_kv: int, meta: dict |
225
  n_compr = n_kv - sliding_kv
226
  m = (meta or {}).get("compress_rate") or 1
227
  stride = SVG_CELL + SVG_GAP
228
- compr_block_w = m * SVG_CELL + (m - 1) * SVG_GAP
229
 
230
- # Per-column x positions and widths. Sliding cells: 1×cell. Compressor entries:
231
- # `m`×cell wide each, with a small gap between sections.
232
- section_gap = SVG_GAP * 4
 
 
233
  col_x: list[float] = []
234
  col_w: list[float] = []
235
  cursor = float(SVG_PAD_LEFT)
@@ -241,12 +242,21 @@ def _render_svg(mask_2d: torch.Tensor, title: str, sliding_kv: int, meta: dict |
241
  cursor += section_gap
242
  for _w in range(n_compr):
243
  col_x.append(cursor)
244
- col_w.append(compr_block_w)
245
- cursor += compr_block_w + stride
246
  grid_w = cursor - SVG_PAD_LEFT - stride
247
  grid_h = n_q * stride - SVG_GAP
 
 
 
 
 
 
 
 
 
248
  w = SVG_PAD_LEFT + grid_w + SVG_PAD_RIGHT
249
- h = SVG_PAD_TOP + grid_h + SVG_PAD_BOTTOM
250
 
251
  subtitle = _meta_subtitle(meta)
252
  # Explicit `width` / `height` (matching viewBox) so renderers don't fall back to the
@@ -291,40 +301,42 @@ def _render_svg(mask_2d: torch.Tensor, title: str, sliding_kv: int, meta: dict |
291
  f'transform="rotate(-90 {cx:.1f} {token_baseline_y})" '
292
  f'fill="{SVG_COLORS["text"]}" font-size="12" font-style="italic">{tok}</text>'
293
  )
 
 
 
 
294
  for w_idx in range(n_compr):
295
  j = sliding_kv + w_idx
296
  cx = col_x[j] + col_w[j] / 2
297
- block_x = col_x[j]
298
- a, b = w_idx * m, (w_idx + 1) * m - 1
299
- # `C_w` + `pos a–b` sit on the same row as the sliding-section indices, hugging
300
- # the top of the rotated-token area — same vertical band as the column ticks.
301
  elems.append(
302
- f' <text x="{cx:.1f}" y="{idx_y - 14}" text-anchor="middle" '
303
- f'fill="{SVG_COLORS["visible"]}" font-size="13" font-weight="700">C{w_idx}</text>'
304
  )
 
 
 
 
 
305
  elems.append(
306
- f' <text x="{cx:.1f}" y="{idx_y}" text-anchor="middle" '
307
- f'fill="{SVG_COLORS["muted"]}" font-size="10">pos {a}–{b}</text>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
  )
309
- # One rotated label per source position, dashed vertical separators between
310
- # sub-cells inside the block (only as tall as the rotated-token area, not the
311
- # whole gutter — they're a hint that the wide green block bundles m positions).
312
- for sub_i in range(m):
313
- pos = a + sub_i
314
- sub_cx = block_x + sub_i * stride + SVG_CELL / 2
315
- tok = SVG_TOKENS[pos] if pos < len(SVG_TOKENS) else f"t{pos}"
316
- elems.append(
317
- f' <text x="{sub_cx:.1f}" y="{token_baseline_y}" text-anchor="start" '
318
- f'transform="rotate(-90 {sub_cx:.1f} {token_baseline_y})" '
319
- f'fill="{SVG_COLORS["text"]}" font-size="12" font-style="italic">{tok}</text>'
320
- )
321
- if sub_i > 0:
322
- sep_x = block_x + sub_i * stride - SVG_GAP / 2
323
- elems.append(
324
- f' <line x1="{sep_x:.1f}" y1="{token_top_y}" '
325
- f'x2="{sep_x:.1f}" y2="{SVG_PAD_TOP}" '
326
- f'stroke="{SVG_COLORS["separator"]}" stroke-width="0.7" stroke-dasharray="3 3" />'
327
- )
328
 
329
  # Row labels: query index (small, muted) + token (italic, dark) on the left.
330
  # `SVG_PAD_LEFT` is sized to fit the longest token in the static word list.
@@ -396,6 +408,22 @@ def _render_svg(mask_2d: torch.Tensor, title: str, sliding_kv: int, meta: dict |
396
  )
397
  x_cursor += 20 + len(label) * 6.5 + 24
398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  elems.append("</svg>")
400
  return "\n".join(elems)
401
 
 
184
  SVG_CELL = 24
185
  SVG_GAP = 2
186
  SVG_PAD_LEFT = 120 # roomy enough for the longest row-label token ("morning") at 11pt
187
+ SVG_PAD_TOP = 200 # title (20) + shape (40) + meta (60) + section header (~80) + col idx + rotated tokens
188
  SVG_PAD_RIGHT = 48
189
+ SVG_PAD_BOTTOM = 56
190
  SVG_COLORS = {
191
  # Dark-mode palette: black background, light text.
192
  "background": "#0b1220", # near-black with a hint of blue
 
225
  n_compr = n_kv - sliding_kv
226
  m = (meta or {}).get("compress_rate") or 1
227
  stride = SVG_CELL + SVG_GAP
 
228
 
229
+ # All cells (sliding + compressor) are the same `SVG_CELL` width — a single KV slot
230
+ # is a single KV slot regardless of which cache produced it. The compressor section
231
+ # is shifted right by `section_gap` so the `cat([sliding_mask, block_bias])` boundary
232
+ # reads clearly.
233
+ section_gap = stride * 2
234
  col_x: list[float] = []
235
  col_w: list[float] = []
236
  cursor = float(SVG_PAD_LEFT)
 
242
  cursor += section_gap
243
  for _w in range(n_compr):
244
  col_x.append(cursor)
245
+ col_w.append(SVG_CELL)
246
+ cursor += stride
247
  grid_w = cursor - SVG_PAD_LEFT - stride
248
  grid_h = n_q * stride - SVG_GAP
249
+
250
+ # Footer legend: one line per compressed entry mapping it to its source tokens.
251
+ legend_lines = []
252
+ for w_idx in range(n_compr):
253
+ a, b = w_idx * m, (w_idx + 1) * m - 1
254
+ toks = [SVG_TOKENS[p] if p < len(SVG_TOKENS) else f"t{p}" for p in range(a, b + 1)]
255
+ legend_lines.append(f"C{w_idx} = compress( {', '.join(toks)} )")
256
+ footer_h = (len(legend_lines) * 18 + 32) if legend_lines else 0
257
+
258
  w = SVG_PAD_LEFT + grid_w + SVG_PAD_RIGHT
259
+ h = SVG_PAD_TOP + grid_h + SVG_PAD_BOTTOM + footer_h
260
 
261
  subtitle = _meta_subtitle(meta)
262
  # Explicit `width` / `height` (matching viewBox) so renderers don't fall back to the
 
301
  f'transform="rotate(-90 {cx:.1f} {token_baseline_y})" '
302
  f'fill="{SVG_COLORS["text"]}" font-size="12" font-style="italic">{tok}</text>'
303
  )
304
+ # `C_w` label sits on the same row as the sliding-section indices — the compressor
305
+ # cell looks like any other KV slot, with its `C_w` identifier centered above it
306
+ # in the column-index band. The actual source-tokens-that-got-compressed mapping
307
+ # lives in the footer legend so the body of the diagram stays clean.
308
  for w_idx in range(n_compr):
309
  j = sliding_kv + w_idx
310
  cx = col_x[j] + col_w[j] / 2
 
 
 
 
311
  elems.append(
312
+ f' <text x="{cx:.1f}" y="{idx_y}" text-anchor="middle" '
313
+ f'fill="{SVG_COLORS["visible"]}" font-size="11" font-weight="700">C{w_idx}</text>'
314
  )
315
+
316
+ # Section headers + `cat` indicator above the column-index band. The dashed
317
+ # separator line marks the `cat([sliding_mask, block_bias], dim=-1)` join.
318
+ if sliding_kv > 0:
319
+ sliding_mid_x = (col_x[0] + col_x[sliding_kv - 1] + col_w[sliding_kv - 1]) / 2
320
  elems.append(
321
+ f' <text x="{sliding_mid_x:.1f}" y="{idx_y - 32}" text-anchor="middle" '
322
+ f'fill="{SVG_COLORS["text"]}" font-size="12" font-weight="600">Sliding K=V cache</text>'
323
+ )
324
+ if n_compr > 0:
325
+ compr_mid_x = (col_x[sliding_kv] + col_x[-1] + col_w[-1]) / 2
326
+ elems.append(
327
+ f' <text x="{compr_mid_x:.1f}" y="{idx_y - 32}" text-anchor="middle" '
328
+ f'fill="{SVG_COLORS["text"]}" font-size="12" font-weight="600">Compressed cache</text>'
329
+ )
330
+ # `cat` operator centered in the section gap between the two sections.
331
+ cat_x = (col_x[sliding_kv - 1] + col_w[sliding_kv - 1] + col_x[sliding_kv]) / 2
332
+ elems.append(
333
+ f' <text x="{cat_x:.1f}" y="{idx_y - 32}" text-anchor="middle" '
334
+ f'fill="{SVG_COLORS["muted"]}" font-size="14" font-weight="700">⊕</text>'
335
+ )
336
+ elems.append(
337
+ f' <text x="{cat_x:.1f}" y="{idx_y - 18}" text-anchor="middle" '
338
+ f'fill="{SVG_COLORS["muted"]}" font-size="9">cat dim=-1</text>'
339
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
 
341
  # Row labels: query index (small, muted) + token (italic, dark) on the left.
342
  # `SVG_PAD_LEFT` is sized to fit the longest token in the static word list.
 
408
  )
409
  x_cursor += 20 + len(label) * 6.5 + 24
410
 
411
+ # Footer: `C_w = compress(token, token, …)` lines, one per compressed entry. Says
412
+ # explicitly which source tokens each compressed-cache slot summarises — replaces
413
+ # the per-cell rotated-token annotations we used to render above each C_w block
414
+ # when the compressor section had wide cells.
415
+ if legend_lines:
416
+ footer_y = legend_y + 24
417
+ elems.append(
418
+ f' <text x="{SVG_PAD_LEFT}" y="{footer_y}" '
419
+ f'fill="{SVG_COLORS["muted"]}" font-size="11" font-weight="600">Compressed cache contents</text>'
420
+ )
421
+ for line_idx, line in enumerate(legend_lines):
422
+ elems.append(
423
+ f' <text x="{SVG_PAD_LEFT}" y="{footer_y + 18 + line_idx * 18}" '
424
+ f'fill="{SVG_COLORS["text"]}" font-size="11">{line}</text>'
425
+ )
426
+
427
  elems.append("</svg>")
428
  return "\n".join(elems)
429