- Move dead dsv4/ modules to dsv4/_archive/ (52 files)
- model/{dsv4,mtp,layer,layer_schedule}
- layers/{embedding,attention,ffn,norm} (kept linear,mhc,router,moe,shared_expert,grouped_linear - live)
- cache/*, kernels/cache/*, kernels/indexer/{csa_indexer,score_topk,compute_valid_lens}
- kernels/router/{nvfp4_fused_router,dense_router_decode_kernel,dense_router_prefill}
- ops/{topk,topk_select,rope,router}, loader/{hf_checkpoint,layout_convert}
- reference/{attention,compressor,csa_attention,moe_pipeline}
- kernels/compressor/{compress_tail,csa_hca}
- Restore dsv4/ops/{router,custom_ops}.py (needed by live layers)
- Fix dsv4/kernels/{indexer,compressor,attention}/__init__.py (removed broken imports)
- Remove preload_all() from loader.py (dead, referenced nonexistent .cu file)
- Fix loader.py docstring (fused_amax_quantize_nvfp4 → quantize_nvfp4_from_buffer)
- Move broken tests to tests/e2e_archive/
- test_fused_router, production_values_test, e2e/{one_layer,model_construction,csa_hca}
- vLLM has 0 imports of dsv4 (Step 0 confirmed)
83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
"""A single DSV4 transformer layer.
|
|
|
|
Structure (paper Figure 2):
|
|
X_l ─→ mHC.pre_block ─→ RMSNorm ─→ Attention ─→ mHC.post_block (using F_attn)
|
|
│
|
|
▼
|
|
mHC.pre_block ─→ RMSNorm ─→ FFN ─→ mHC.post_block (using F_ffn)
|
|
│
|
|
▼
|
|
X_{l+1}
|
|
|
|
Each layer owns:
|
|
- One LayerSpec (from build_schedule).
|
|
- Two mHC instances (one per sub-block).
|
|
- One AttentionSubBlock (type fixed by spec.attn).
|
|
- One FFNSubBlock (router mode fixed by spec.router_mode).
|
|
- Two RMSNorm weight tensors.
|
|
|
|
The layer is otherwise pure orchestration: no learned params live
|
|
directly on TransformerLayer, only on its components.
|
|
"""
|
|
from __future__ import annotations
|
|
from typing import TYPE_CHECKING
|
|
import torch
|
|
|
|
from dsv4.layers.mhc import mHCLayer
|
|
from dsv4.layers.attention import AttentionSubBlock
|
|
from dsv4.layers.ffn import FFNSubBlock
|
|
from dsv4.layers.norm import RMSNorm # PyTorch ref for now, fused later
|
|
from dsv4.model.layer_schedule import LayerSpec
|
|
|
|
if TYPE_CHECKING:
|
|
from dsv4.model.config import DSV4Config
|
|
from dsv4.cache.paged_cache import LayerCacheHandle
|
|
|
|
|
|
class TransformerLayer:
|
|
def __init__(self, config: "DSV4Config", spec: LayerSpec):
|
|
self.config = config
|
|
self.spec = spec
|
|
self.layer_idx = spec.layer_idx
|
|
|
|
# Two mHC wrappers — one per sub-block. mHCLayer holds its own
|
|
# projection weights (W_pre, W_post, W_comb) and static biases.
|
|
self.mhc_attn = mHCLayer(
|
|
hidden_dim=config.hidden_size,
|
|
n_hc=config.n_hc,
|
|
t_max_sinkhorn=config.sinkhorn_iters,
|
|
)
|
|
self.mhc_ffn = mHCLayer(
|
|
hidden_dim=config.hidden_size,
|
|
n_hc=config.n_hc,
|
|
t_max_sinkhorn=config.sinkhorn_iters,
|
|
)
|
|
|
|
# Pre-block norms (one per sub-block).
|
|
self.norm_attn = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
self.norm_ffn = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
|
|
# Sub-blocks — type-frozen at construction.
|
|
self.attn = AttentionSubBlock(config, spec)
|
|
self.ffn = FFNSubBlock(config, spec)
|
|
|
|
def forward(
|
|
self,
|
|
X: torch.Tensor, # (T, n_hc, hidden_size) BF16 — residual streams
|
|
token_ids: torch.Tensor, # (T,) int32 — for hash routing
|
|
cache: "LayerCacheHandle",
|
|
) -> torch.Tensor:
|
|
# ---- Attention sub-block ----
|
|
x_attn_in, ctx_attn = self.mhc_attn.pre_block(X)
|
|
x_attn_in = self.norm_attn(x_attn_in)
|
|
F_attn = self.attn.forward(x_attn_in, cache)
|
|
X = self.mhc_attn.post_block(X, F_attn, ctx_attn)
|
|
|
|
# ---- FFN sub-block ----
|
|
x_ffn_in, ctx_ffn = self.mhc_ffn.pre_block(X)
|
|
x_ffn_in = self.norm_ffn(x_ffn_in)
|
|
F_ffn = self.ffn.forward(x_ffn_in, token_ids)
|
|
X = self.mhc_ffn.post_block(X, F_ffn, ctx_ffn)
|
|
|
|
return X
|