"""Visualise the per-layer-type attention mask DeepSeek-V4 actually feeds to
`eager_attention_forward`, for the three attention layer types (sliding, CSA, HCA).
Generates the SVGs embedded in `docs/source/en/model_doc/deepseek_v4.md`.
We build a tiny V4 model (small enough that 16 source tokens × all compressed
slots fits on screen), forward a dummy batch through it, wrap each attention
layer's forward with a thin shim that replays the mask-extension logic
(`cat([sliding_mask, block_bias])`) and captures the exact post-cat mask, then
render each layer's mask in either:
* the `■` / `⬚` ANSI-coloured style of `transformers.utils.attention_visualizer`
(default; suppressed when stdout isn't a TTY or `NO_COLOR=1` is set), or
* an SVG grid suitable for embedding in markdown (with `--svg
`).
For CSA layers the visualizer remaps the literal `[S, S·k]` flat-slot `block_bias`
back to the more readable `[S, T_entries]` entry-visibility view (each compressor
column is then a compressed *entry* rather than a gather slot), and shades cells
red when an entry is causally available but the indexer's top-k did not pick it.
Run from the repository root::
python docs/source/en/imgs/deepseek_v4/visualize_attention_masks.py
python docs/source/en/imgs/deepseek_v4/visualize_attention_masks.py \\
--svg docs/source/en/imgs/deepseek_v4
"""
from __future__ import annotations
import argparse
import os
from pathlib import Path
import torch
import torch.nn.functional as F
from transformers import DeepseekV4Config, DeepseekV4ForCausalLM
from transformers.models.deepseek_v4.modeling_deepseek_v4 import (
DeepseekV4CSACompressor,
DeepseekV4HCACompressor,
DeepseekV4Indexer,
DeepseekV4PreTrainedModel,
)
from transformers.utils.output_capturing import OutputRecorder
# Tiny config so the visualised matrices fit in a terminal. Compress rates are dialled
# down (m_csa=4, m_hca=8 — vs the real 4/128) so HCA actually emits an entry at S=16.
CFG = DeepseekV4Config(
vocab_size=64,
hidden_size=64,
moe_intermediate_size=64,
num_hidden_layers=3,
num_attention_heads=4,
num_key_value_heads=1,
head_dim=32,
partial_rotary_factor=8 / 32,
q_lora_rank=32,
o_groups=2,
o_lora_rank=16,
num_experts_per_tok=2,
n_routed_experts=4,
n_shared_experts=1,
mlp_layer_types=["moe", "moe", "moe"],
layer_types=["sliding_attention", "compressed_sparse_attention", "heavily_compressed_attention"],
compress_rates={"compressed_sparse_attention": 4, "heavily_compressed_attention": 8},
sliding_window=8,
hc_mult=2,
hc_sinkhorn_iters=3,
hc_eps=1.0e-6,
index_n_heads=2,
index_head_dim=16,
index_topk=2,
num_nextn_predict_layers=0,
swiglu_limit=10.0,
rope_theta=10000.0,
compress_rope_theta=160000.0,
max_position_embeddings=64,
)
# ANSI colours, matching `transformers.utils.attention_visualizer`.
GREEN = "\033[92m"
YELLOW = "\033[93m"
DIM = "\033[90m"
RESET = "\033[0m"
BLACK_SQUARE = "■"
WHITE_SQUARE = "⬚"
def _meta_subtitle(meta: dict | None) -> str:
"""One-liner subtitle describing the layer's compressor config."""
if meta is None:
return ""
parts: list[str] = []
if meta.get("compress_rate") is not None:
parts.append(f"m={meta['compress_rate']}")
if meta.get("index_topk") is not None:
parts.append(f"k={meta['index_topk']}")
if meta.get("T_entries"):
parts.append(f"{meta['T_entries']} entries")
return " · ".join(parts)
def _render(
mask_2d: torch.Tensor,
query_labels: list[str],
kv_labels: list[str],
title: str,
*,
color: bool = True,
meta: dict | None = None,
) -> str:
"""Render a 2D additive mask (0 = visible, -inf = masked) as a ■ / ⬚ grid.
`mask_2d` is `[S_q, S_kv]`. Columns past `len(query_labels)` are the
compressor / indexer entries the caller cat'd into the mask via
`cat([sliding_causal_mask, block_bias], dim=-1)`. When `color=False`,
diagonal / compressor slots are marked with the distinguishing glyphs
`◆` and `▲` instead of ANSI colours so the output renders in markdown.
"""
visible = (mask_2d > -1e30).bool()
n_q, n_kv = visible.shape
sliding_kv = len(query_labels) # by construction kv labels begin with the sliding tokens
if color:
g, y, d, r = GREEN, YELLOW, DIM, RESET
visible_glyph = f"{g}{BLACK_SQUARE}{r}"
compr_glyph = f"{y}{BLACK_SQUARE}{r}"
else:
g = y = d = r = ""
visible_glyph = "▣"
compr_glyph = "▲"
max_q_label = max(len(q) for q in query_labels)
out: list[str] = []
out.append(title)
out.append("=" * len(title))
out.append(
f" shape=[{n_q}, {n_kv}] "
f"{visible_glyph} visible (sliding KV) {compr_glyph} visible (compressor entry) {WHITE_SQUARE} masked"
)
subtitle = _meta_subtitle(meta)
if subtitle:
out.append(f" {subtitle}")
if meta and meta.get("topk_picks") is not None:
# Append per-query indexer picks so warm-up sentinels and entry choices are auditable.
picks_lines = [f" indexer topk picks (entry id, -1 = warm-up sentinel):"]
for i, picks in enumerate(meta["topk_picks"][0]): # B=1
picks_lines.append(f" q{i:>2}: {picks}")
out.append("\n".join(picks_lines))
# Column headers (kv indices, stacked vertically across `width` lines).
width = max(2, len(str(n_kv - 1)))
for line in range(width):
header_chars = []
for j in range(n_kv):
label = str(j).rjust(width)
ch = label[line] if line < len(label) else " "
# Tint compressor-section header so it visually separates from the sliding section.
header_chars.append(f"{d}{ch}{r}" if j >= sliding_kv else ch)
prefix = " " * (max_q_label + 6)
out.append(prefix + " ".join(header_chars))
# Body rows.
for i in range(n_q):
row_cells = []
for j in range(n_kv):
if not visible[i, j]:
row_cells.append(WHITE_SQUARE)
elif j >= sliding_kv:
row_cells.append(compr_glyph)
else:
row_cells.append(visible_glyph)
out.append(f"{query_labels[i].rjust(max_q_label)} : {str(i).rjust(2)} " + " ".join(row_cells))
out.append(f" {compr_glyph} columns past index {sliding_kv - 1} are compressor / indexer slots (block_bias).")
return "\n".join(out)
# SVG palette + geometry. Bigger cells so we can fit token-string labels comfortably,
# and compressor columns are drawn `m` cells wide so the visual encodes the fact that
# one compressed entry summarizes m source tokens (rather than a 1:1 KV slot like the
# sliding section).
SVG_CELL = 24
SVG_GAP = 2
SVG_PAD_LEFT = 120 # roomy enough for the longest row-label token ("morning") at 11pt
SVG_PAD_TOP = 200 # title (20) + shape (40) + meta (60) + section header (~80) + col idx + rotated tokens
SVG_PAD_RIGHT = 48
SVG_PAD_BOTTOM = 56
SVG_COLORS = {
# Dark-mode palette: black background, light text.
"background": "#0b1220", # near-black with a hint of blue
"visible": "#22c55e", # green-500, pops against black
"masked": "#1e293b", # slate-800 — dim cells that are visible against bg but recede
"indexer_masked": "#ef4444",# red-500, also pops
"compressor": "#22c55e", # same green as visible (single-color attended-to)
"separator": "#475569", # slate-600
"text": "#f8fafc", # slate-50 — main text on black
"muted": "#94a3b8", # slate-400 — secondary text
}
# Static word list used as token labels on the SVG axes. Real token semantics don't
# affect the mask (which depends only on positions), so these labels are decorative.
SVG_TOKENS = [
"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy",
"dog", "ate", "a", "small", "red", "fish", "this", "morning",
]
def _render_svg(mask_2d: torch.Tensor, title: str, sliding_kv: int, meta: dict | None = None) -> str:
"""Render the mask as a standalone SVG document.
Rows = queries (top → bottom). The sliding K=V section is one cell per
source position (key index). The compressor section is `T_entries`
blocks each `m·cell` wide — the wide rectangle is the visual hint that
one compressed entry summarizes `m` source tokens (rather than 1:1).
Cells are coloured: green = visible (the query attends to this slot),
light gray = masked. Compressor entries are tinted amber when visible
to a given query and pale amber when masked, so the compressor section
reads as a distinct "compression band" even at a glance.
"""
visible = (mask_2d > -1e30).bool()
n_q, n_kv = visible.shape
n_compr = n_kv - sliding_kv
m = (meta or {}).get("compress_rate") or 1
stride = SVG_CELL + SVG_GAP
# All cells (sliding + compressor) are the same `SVG_CELL` width — a single KV slot
# is a single KV slot regardless of which cache produced it. The compressor section
# is shifted right by `section_gap` so the `cat([sliding_mask, block_bias])` boundary
# reads clearly.
section_gap = stride * 2
col_x: list[float] = []
col_w: list[float] = []
cursor = float(SVG_PAD_LEFT)
for j in range(sliding_kv):
col_x.append(cursor)
col_w.append(SVG_CELL)
cursor += stride
if n_compr > 0:
cursor += section_gap
for _w in range(n_compr):
col_x.append(cursor)
col_w.append(SVG_CELL)
cursor += stride
grid_w = cursor - SVG_PAD_LEFT - stride
grid_h = n_q * stride - SVG_GAP
# Footer legend: one line per compressed entry mapping it to its source tokens.
legend_lines = []
for w_idx in range(n_compr):
a, b = w_idx * m, (w_idx + 1) * m - 1
toks = [SVG_TOKENS[p] if p < len(SVG_TOKENS) else f"t{p}" for p in range(a, b + 1)]
legend_lines.append(f"C{w_idx} = compress( {', '.join(toks)} )")
footer_h = (len(legend_lines) * 18 + 32) if legend_lines else 0
w = SVG_PAD_LEFT + grid_w + SVG_PAD_RIGHT
h = SVG_PAD_TOP + grid_h + SVG_PAD_BOTTOM + footer_h
subtitle = _meta_subtitle(meta)
# Explicit `width` / `height` (matching viewBox) so renderers don't fall back to the
# 150×150 SVG default when the file is embedded without sizing CSS. `preserveAspectRatio`
# keeps it sharp when scaled.
elems: list[str] = [
f'")
return "\n".join(elems)
def _sliding_causal_mask(seq_len: int, sliding_window: int) -> torch.Tensor:
"""Standard `[1, 1, S, S]` sliding-window-causal additive mask (0 visible, -inf masked).
Identical to what `create_sliding_window_causal_mask` builds for the sliding section
of every V4 attention layer — we synthesise it here so the visualiser stays decoupled
from the model's internal mask-building."""
q = torch.arange(seq_len)
k = torch.arange(seq_len)
diff = q.view(-1, 1) - k.view(1, -1)
visible = (diff >= 0) & (diff < sliding_window)
return torch.where(visible, torch.zeros((), dtype=torch.float32), torch.full((), float("-inf"))).view(1, 1, seq_len, seq_len)
def main() -> None:
torch.manual_seed(0)
# ────────────────────────────────────────────────────────────────────────────────
# Extend `DeepseekV4PreTrainedModel._can_record_outputs` with the compressor /
# indexer outputs we need to reconstruct the visualised mask. The base class
# already registers `router_logits` / `hidden_states` / `attentions`; we add three
# more keys, each routed by `OutputRecorder(target_class=…, index=…)`:
#
# * `csa_block_bias` ← `DeepseekV4CSACompressor.forward()` returns `(gathered, block_bias)`,
# we grab index 1 (the per-query bias `[B, 1, S, S*k]`).
# * `hca_block_bias` ← `DeepseekV4HCACompressor.forward()` likewise (already entry-resolved).
# * `indexer_topk` ← `DeepseekV4Indexer.forward()` returns a single tensor `[B, S, k]`.
#
# Setting this *before* instantiating the model is what `PreTrainedModel.__init__`
# picks up to populate `_CAN_RECORD_REGISTRY`. The `@capture_outputs` decorator on
# `DeepseekV4Model.forward` then installs the corresponding `register_forward_hook`
# calls lazily on first request and stashes the captures on the returned
# `ModelOutput`. We just flip `config.output_=True` to switch them on.
# ────────────────────────────────────────────────────────────────────────────────
DeepseekV4PreTrainedModel._can_record_outputs = {
**(DeepseekV4PreTrainedModel._can_record_outputs or {}),
"csa_block_bias": OutputRecorder(DeepseekV4CSACompressor, index=1),
"hca_block_bias": OutputRecorder(DeepseekV4HCACompressor, index=1),
"indexer_topk": OutputRecorder(DeepseekV4Indexer, index=0),
}
cfg = CFG
for key in ("csa_block_bias", "hca_block_bias", "indexer_topk"):
setattr(cfg, f"output_{key}", True)
full_model = DeepseekV4ForCausalLM(cfg).eval()
# `@capture_outputs` decorates `DeepseekV4Model.forward` (the inner model). The outer
# `DeepseekV4ForCausalLM.forward` builds a `CausalLMOutput` that only forwards logits
# and last_hidden_state, so our extra capture keys disappear if we go via the outer.
# Call the inner directly — we don't need logits to visualise masks.
inner_model = full_model.model
seq_len = 16
input_ids = torch.arange(seq_len).unsqueeze(0) % cfg.vocab_size # [1, S]
with torch.no_grad():
out = inner_model(input_ids=input_ids)
# Tuples (one entry per layer of the matching module class). With our 3-layer
# config: 1 CSA layer + 1 HCA layer + 1 sliding-only layer.
csa_block_biases = getattr(out, "csa_block_bias", ()) or ()
hca_block_biases = getattr(out, "hca_block_bias", ()) or ()
indexer_topks = getattr(out, "indexer_topk", ()) or ()
# ────────────────────────────────────────────────────────────────────────────────
# Build the display mask + metadata per layer from the captured outputs.
# ────────────────────────────────────────────────────────────────────────────────
sliding_mask = _sliding_causal_mask(seq_len, cfg.sliding_window) # [1, 1, S, S]
captured: dict[int, tuple[str, torch.Tensor, dict]] = {}
csa_iter = iter(csa_block_biases)
hca_iter = iter(hca_block_biases)
indexer_iter = iter(indexer_topks)
for layer_idx, layer in enumerate(inner_model.layers):
attn = layer.self_attn
layer_type = attn.layer_type
meta = {
"compress_rate": (attn.compressor.compress_rate if attn.compressor is not None else None),
"index_topk": (
attn.compressor.indexer.index_topk
if attn.compressor is not None and getattr(attn.compressor, "indexer", None) is not None
else None
),
"T_entries": seq_len // attn.compressor.compress_rate if attn.compressor is not None else 0,
"topk_picks": None,
"indexer_rejected": None,
}
if attn.compressor is None:
captured[layer_idx] = (layer_type, sliding_mask, meta)
continue
if layer_type == "heavily_compressed_attention":
block_bias = next(hca_iter).detach().cpu()
display_mask = torch.cat([sliding_mask, block_bias.to(sliding_mask.dtype)], dim=-1)
elif layer_type == "compressed_sparse_attention":
_ = next(csa_iter) # consume the (literal) `[B, 1, S, S*k]` flat-slot bias
topk = next(indexer_iter).detach().cpu() # [B, S, k]
meta["topk_picks"] = topk.tolist()
# Remap the flat-slot view to the more readable entry-visibility view via
# the indexer's per-query top-k picks (one-hot + OR-reduce sidesteps the
# duplicate-index clobber that `scatter_` would hit on warm-up sentinels).
T_entries = meta["T_entries"]
valid = topk >= 0
safe = topk.clamp(min=0)
oh = F.one_hot(safe, num_classes=T_entries).bool() & valid.unsqueeze(-1)
em = oh.any(dim=-2) # [B, S, T_entries]
entry_bias = torch.where(em, torch.zeros((), dtype=torch.float32), torch.full((), float("-inf")))
display_mask = torch.cat([sliding_mask, entry_bias.unsqueeze(1)], dim=-1)
# Red-highlight cells: causally available entry that the indexer rejected.
causal_threshold = torch.arange(seq_len).add(1).floor_divide(attn.compressor.compress_rate)
entry_idx = torch.arange(T_entries)
causally_available = entry_idx.view(1, -1) < causal_threshold.view(-1, 1)
rejected = causally_available & ~em[0]
meta["indexer_rejected"] = torch.cat(
[torch.zeros((seq_len, seq_len), dtype=torch.bool), rejected], dim=-1
)
else:
display_mask = sliding_mask
captured[layer_idx] = (layer_type, display_mask, meta)
color = os.environ.get("NO_COLOR") is None and os.isatty(1)
svg_dir = main.svg_dir if hasattr(main, "svg_dir") else None
if svg_dir is not None:
svg_dir.mkdir(parents=True, exist_ok=True)
print()
for layer_idx in sorted(captured):
layer_type, mask, meta = captured[layer_idx]
if mask is None:
continue
if mask.ndim == 4:
mask_2d = mask[0, 0]
elif mask.ndim == 3:
mask_2d = mask[0]
else:
mask_2d = mask
q_labels = [f"q{i}" for i in range(mask_2d.shape[0])]
kv_labels_sliding = [f"k{i}" for i in range(seq_len)]
n_compressor = mask_2d.shape[1] - seq_len
kv_labels_compressor = [f"c{i}" for i in range(n_compressor)]
kv_labels = kv_labels_sliding + kv_labels_compressor
title = f"Layer {layer_idx}: {layer_type}"
print(_render(mask_2d, q_labels, kv_labels, title, color=color, meta=meta))
print()
if svg_dir is not None:
svg_path = svg_dir / f"deepseek_v4_mask_layer{layer_idx}_{layer_type}.svg"
svg_path.write_text(_render_svg(mask_2d, title, sliding_kv=seq_len, meta=meta))
print(f" wrote {svg_path}")
print()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--svg",
type=Path,
default=None,
help="Directory to write one SVG per layer (in addition to the ANSI / plain-text terminal output).",
)
args = parser.parse_args()
main.svg_dir = args.svg # type: ignore[attr-defined]
main()