Files
nvfp4-megamoe-kernel/dsv4/model/layer.py
biondizzle 5a63604d6a Layer dispatch: config, schedule, attention/FFN sub-blocks, TransformerLayer
DSV4Config: frozen dataclass with .flash() / .pro() classmethods.
All architectural constants (dims, heads, MoE params, mHC) in one place.

LayerSchedule: pure-data per-layer-index -> (attn_type, ffn_type, router_mode).
  Flash: SWA, SWA, CSA, HCA, CSA, HCA, ... (43 layers)
  Pro:   HCA, HCA, CSA, HCA, CSA, HCA, ... (61 layers)
  Both:  first 3 MoE layers = hash routing, rest = dense
  validate_schedule() enforces correctness at construction.

AttentionSubBlock: CSA / HCA / SWA variants.
  - Low-rank Q projection (q_down -> q_up)
  - KV down-projection (varies by attn type: 4h/2h/1h)
  - CSA: indexer_q_up + indexer_head_weights
  - Grouped output projection (wo_a + wo_b)
  - Kernel calls are imports (NotImplementedError until kernel lands)
  - No PyTorch fallback paths

FFNSubBlock: MoE + shared expert.
  - Router (hash/dense) mode from LayerSpec
  - Nvfp4MoE + Nvfp4SharedExpert

TransformerLayer: composition of mHC + norm + attention + FFN.
  - Two mHC wrappers (attn + ffn sub-blocks)
  - Two RMSNorm (one per sub-block)
  - Pure orchestration, no learned params on the layer itself

Tests: schedule construction + validation for both variants.
No forward tests yet (depends on FMHA kernel + KV cache).
2026-05-21 23:11:09 +00:00

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_res, W_post) and static biases.
self.mhc_attn = mHCLayer(
hidden_size=config.hidden_size,
n_hc=config.n_hc,
sinkhorn_iters=config.sinkhorn_iters,
)
self.mhc_ffn = mHCLayer(
hidden_size=config.hidden_size,
n_hc=config.n_hc,
sinkhorn_iters=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