Skip to content

Add DeepSeek V4 architecture adapter#1513

Draft
abhinav-bellapu wants to merge 2 commits into
TransformerLensOrg:devfrom
abhinav-bellapu:codex/add-deepseek-v4-adapter
Draft

Add DeepSeek V4 architecture adapter#1513
abhinav-bellapu wants to merge 2 commits into
TransformerLensOrg:devfrom
abhinav-bellapu:codex/add-deepseek-v4-adapter

Conversation

@abhinav-bellapu

Copy link
Copy Markdown
Contributor

Description

Adds a TransformerBridge architecture adapter for DeepseekV4ForCausalLM, addressing #1466.

DeepSeek V4 does not have a conventional single residual stream or V2/V3-style MLA. It keeps hc_mult residual streams alive between blocks through manifold-constrained Hyper-Connections (mHC), and interleaves sliding attention with Compressed Sparse Attention (CSA) and Heavily Compressed Attention (HCA). This adapter delegates those model-specific calculations to Transformers for parity, while exposing the internal surfaces needed for interpretability:

  • mHC stream inputs, collapse weights, stream-mix matrices, and collapsed attention/MLP inputs
  • Q/KV and grouped output projections
  • CSA/HCA compressed KV states and causal block bias
  • Lightning Indexer selections and projections on CSA layers
  • hash and learned top-k MoE routers, routed experts, and shared experts
  • final hyper-head collapse, RMSNorm, embeddings, and unembedding

The block bridge intentionally does not register conventional residual aliases: block-boundary hooks carry [batch, pos, hc_mult, d_model], so presenting them as an ordinary three-dimensional residual would make patching code silently target the wrong tensor. Collapsed sublayer inputs remain directly patchable at blocks.{i}.attn_hc.hook_out and blocks.{i}.mlp_hc.hook_out.

The architecture is registered in the factory and all model-registry sites, and V4-specific compression, mHC, indexer, and router metadata is preserved on bridge.cfg for analysis tools.

Validation

The integration suite builds a download-free ~40K-parameter DeepseekV4ForCausalLM spanning all three attention layer types and both router types.

  • Exact bridge-vs-Hugging Face FP32 logit parity (atol=0, rtol=0)
  • mHC stream/collapse hook shapes and intervention patchability
  • CSA/HCA compressed KV and block-bias hooks
  • CSA Lightning Indexer output and HCA/sliding optional-component boundaries
  • Hash-MoE and learned top-k router/expert hooks
  • Focused adapter + registry suite: 15 passed
  • DeepSeek V2/V3/V4 integration suite: 34 passed
  • Docstring suite: 18 passed, 25 skipped
  • mypy .: success across 309 source files
  • pycln --check --all, isort --check-only, black --check, and git diff --check: clean

A broad local unit sweep reached 3,215 passed, 34 skipped, 42 deselected, and 9 expected failures. Its remaining 18 setup errors required unavailable Hugging Face downloads, and one unrelated dataset test was blocked by the local sandbox's shared-memory restriction. CI should provide the clean-environment result.

As scoped in #1466, this PR does not claim full Flash/Pro checkpoint verification: the smallest real checkpoint is 284B total parameters and requires cluster hardware. The tiny model exercises the architecture and numerical parity without that hardware dependency.

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have commented non-obvious behavior
  • I have made corresponding registry/documentation changes
  • My changes generate no new warnings
  • I have added tests that prove the adapter and hooks work
  • New focused and family tests pass locally
  • I have not rewritten tests for existing interfaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant