本文首次对文生图扩散 Transformer (DiT) 如何在图像中正确放置具有空间关系的物体进行了机械可解释性分析。 研究发现:文本编码器的选择从根本上决定了模型内部所形成的计算"电路"—— 使用随机 token 嵌入 (RTE) 时,模型发展出模块化的两阶段注意力电路; 使用预训练 T5 时,空间关系信息则融合进上下文 token 表示中。 This work presents the first mechanistic interpretability analysis of how text-to-image diffusion Transformers (DiTs) place objects in an image with the correct spatial relation. The finding: the choice of text encoder fundamentally determines the internal computational "circuit" that the model forms - with random token embeddings (RTE) the model develops a modular two-stage attention circuit; with a pretrained T5 the spatial relation information is instead fused into the contextual token representations.
文生图模型在生成单一物体属性(颜色、形状)方面已取得显著进步, 但在生成"A 在 B 的左边"这类组合空间关系时仍然频繁出错。 已有研究从两个角度归因:一是 cross-attention 机制不足,二是文本编码器的局限性。 本文提出:文本编码器的设计从根本上塑造了空间关系生成所对应的内部计算电路,从而将两种视角统一起来。 Text-to-image models have made marked progress on single-object attributes such as color and shape, yet they still fail frequently on compositional spatial relations such as "A is to the left of B". Prior work attributes this to two causes: an inadequate cross-attention mechanism, and limitations of the text encoder. This paper argues that the design of the text encoder fundamentally shapes the internal computational circuit used for spatial relation generation, thereby unifying the two views.
"We analyze how Diffusion Transformers (DiTs) generate correct spatial relations … and discover that the underlying mechanisms differ significantly based on text encoder choice."
空间关系("上方"、"左侧"等)在语义上是非交换的("A 在 B 左边" ≠ "B 在 A 左边"), 且需要将文本语义和图像空间位置绑定在一起——这对模型来说是一个根本性挑战。 以往工作只关注现象而未揭示机制,本文通过在受控数据集上从头训练 DiT, 系统地追踪了空间关系是如何在内部被计算的。 Spatial relations ("above", "left of", etc.) are semantically non-commutative ("A is left of B" ≠ "B is left of A"), and they require binding textual semantics to image-space position - a fundamental challenge for the model. Earlier work only documented the phenomenon without revealing the mechanism; here the authors train DiTs from scratch on a controlled dataset and systematically trace how spatial relations are computed internally.
研究采用"受控数据集 + 从头训练"的范式,配合提出的 Attention Synopsis 方法, 系统地在扩散时间步和空间 token 两个维度上定位关键注意力头,再通过消融和因果干预验证其功能。 The study adopts a "controlled dataset + training from scratch" paradigm, together with the proposed Attention Synopsis method, to systematically localize the key attention heads along both the diffusion timestep and the spatial token axes, and then verifies their function through ablation and causal intervention.
[color A] [shape A] [relation] [color B] [shape B],
共 8 种空间关系(left / right / above / below / upper-left / upper-right / lower-left / lower-right)。
评估集包含 96 条 prompts(8 关系 × 12 物体对)。
Figure 2: dataset design. Each image contains two colored geometric shapes (3 shapes: circle / triangle / square; 2 colors: red / blue) on a gray background, with object positions kept collision-free. Prompts follow the format [color A] [shape A] [relation] [color B] [shape B], covering 8 spatial relations (left / right / above / below / upper-left / upper-right / lower-left / lower-right). The evaluation set contains 96 prompts (8 relations × 12 object pairs).模型采用 PixArt-style 架构,由三部分组成: The model follows a PixArt-style architecture with three components:
本文提出 Attention Synopsis:对所有扩散时间步的注意力图取汇总统计, 并在所有 prompt 条件下聚合,从而识别"对特定语义变量(如 relation)有选择性响应"的注意力头。 这避免了只看单一时间步带来的偶然性,能系统筛查模型中的稀疏功能电路。 The paper proposes Attention Synopsis: summary statistics of the attention maps are taken across all diffusion timesteps and aggregated over all prompt conditions, so as to identify attention heads that respond selectively to a particular semantic variable such as the relation. This avoids the arbitrariness of inspecting a single timestep and makes it possible to systematically screen a model for sparse functional circuits.
在 T5-DiT 中,空间信息并非由独立的 attention head 处理,而是通过 T5 自注意力 将 relation 信息融合进第二个物体 token 的 contextual embedding 中。 方差分解(Variance Partitioning)显示: DiT MLP 映射前,shape2 解释约 37.5% 的方差,relation 仅占 12%; 经 DiT MLP 投影后,relation 的贡献上升至 21.3%。 In T5-DiT the spatial information is not handled by a dedicated attention head; instead T5 self-attention fuses the relation information into the contextual embedding of the second object token. Variance partitioning shows that before the DiT MLP mapping, shape2 explains about 37.5% of the variance while the relation accounts for only 12%; after the DiT MLP projection the contribution of the relation rises to 21.3%.
在受控数据集上从头训练,使用 4 项二值指标衡量生成质量: 颜色准确率、形状准确率、绑定准确率(正确的形状-颜色对应)、空间关系准确率。 评估在 96 条 prompt 上进行(8 关系 × 12 物体对)。 Models are trained from scratch on the controlled dataset and generation quality is measured with 4 binary metrics: color accuracy, shape accuracy, binding accuracy (correct shape-color correspondence), and spatial relation accuracy. Evaluation runs over 96 prompts (8 relations × 12 object pairs).
| 模型配置Model configuration | 形状 ShapeShape | 颜色 ColorColor | 绑定 BindingBinding | 空间关系 SpatialSpatial | 严格空间 StrictStrict spatial |
|---|---|---|---|---|---|
| RTE-DiT | 0.877 | 0.928 | 0.855 | 0.843 | 0.758 |
| T5-DiT | 0.857 | 0.892 | 0.820 | 0.808 | 0.749 |
| CLIP-DiT-B | 0.806 | 0.900 | 0.772 | 0.759 | — |
| RTE(无位置编码)RTE (no positional encoding) | — | — | 0.415 | 0.207 | — |
| DiT-nano(RTE)DiT-nano (RTE) | — | — | — | 0.050 | — |
注:RTE 无位置编码时空间准确率崩溃至 20.7%,证明位置信息对空间关系生成是必要条件。 DiT-nano(3 层)空间准确率仅 5%,说明模型容量达到阈值以下时电路无法形成。 Note: without positional encoding, RTE spatial accuracy collapses to 20.7%, showing that positional information is a necessary condition for spatial relation generation. DiT-nano (3 layers) reaches only 5% spatial accuracy, indicating that below a certain model-capacity threshold the circuit cannot form.
| 消融条件Ablation condition | 受影响指标Affected metric | 消融前Before | 消融后After | 下降幅度Drop |
|---|---|---|---|---|
| 移除 L2H8(空间关系头)Remove L2H8 (spatial relation head) | 空间关系准确率Spatial relation accuracy | 67% | 33% | −34pp |
| 移除 L4H3(物体生成头)Remove L4H3 (object generation head) | 形状生成准确率Shape generation accuracy | 90% | 76% | −14pp |
将电路分析工具应用于公开预训练的 PixArt-Sigma 模型: 在所测试的物体对中,约 27% 展现出非平凡的空间关系生成能力, 同样能定位到稀疏的空间关系电路,与在受控模型上发现的机制一致。 Applying the circuit analysis toolkit to the publicly released pretrained PixArt-Sigma model: about 27% of the tested object pairs exhibit non-trivial spatial relation generation, and a sparse spatial relation circuit can likewise be localized, consistent with the mechanism found in the controlled models.
实验仅使用 3 种形状、2 种颜色、8 种空间关系的极简数据集,背景为纯灰色、物体无纹理。 虽然这种受控设置便于机制发现,但其结论能否直接推广到真实场景的文生图模型(如复杂背景、多物体、连续颜色)尚不明确。 The experiments use a minimal dataset of only 3 shapes, 2 colors and 8 spatial relations, with a plain gray background and untextured objects. This controlled setting makes mechanism discovery tractable, but whether the conclusions transfer directly to real-world text-to-image models (complex backgrounds, many objects, continuous colors) remains unclear.
在预训练 PixArt-Sigma 上,仅约 27% 的物体对展示出非平凡的空间生成能力, 稀疏度更高、机制更难识别。在通用大模型中进行完整的电路逆向工程仍面临巨大挑战。 On the pretrained PixArt-Sigma, only about 27% of object pairs show non-trivial spatial generation ability; the circuits are sparser and harder to identify. Full circuit-level reverse engineering of general large models remains a formidable challenge.
T5 contextual embedding 的空间信息高度依赖 prompt 的词语组合, 仅添加填充词 "the" 就能使空间准确率从 80.8% 跌至 49.8%(下降 31pp)。 这表明基于预训练语言模型的 T5 电路存在分布偏移脆弱性, 在面向更多样化 prompt 时泛化能力受限。 The spatial information in T5 contextual embeddings depends heavily on the exact wording of the prompt: merely adding the filler word "the" drops spatial accuracy from 80.8% to 49.8% (a fall of 31pp). This indicates that the T5 circuit, built on a pretrained language model, is fragile under distribution shift and generalizes poorly to more diverse prompts.
本文聚焦于"左/右/上/下"等 8 种二维空间关系, 未涉及数量关系、属性绑定以外的组合推理(如大小比较、遮挡等)。 是否存在类似的模块化电路机制需要进一步研究。 The paper focuses on 8 two-dimensional spatial relations such as left / right / above / below, and does not address counting or compositional reasoning beyond attribute binding (such as size comparison or occlusion). Whether analogous modular circuit mechanisms exist there requires further study.
DiT-nano(3 层,3 头,192 维)的空间关系准确率仅为 5%,说明存在某个模型容量阈值。 当模型规模低于该阈值时,两阶段空间电路无法形成,空间关系学习完全失败。 具体的阈值条件尚未被系统研究。 DiT-nano (3 layers, 3 heads, 192 dimensions) reaches only 5% spatial relation accuracy, indicating the existence of a model-capacity threshold. Below that threshold the two-stage spatial circuit fails to form and spatial relation learning breaks down entirely. The exact threshold condition has not yet been studied systematically.