Reading weights
×A weight between 0 and 1 for each stream.
- in
- [B batch, T positions, 4 mix numbers]
- out
- [B batch, T positions, 4 streams]
ϵ = 0.000001. 4 offsets and one scale.
Parameters5
5 parameters owned by this component
| Component | Parameters |
|---|---|
| learned vector | 4 |
| learned vector | 1 |
| Per-instance total | 5 |
Sources
In codetorch.sigmoid(mixes * hc_scale + hc_base) + self.hc_eps · 733
Direct evidence
The head reads the streams once more with weights σ(m · scale + base) + ε from its own projection, normalises, and projects the last position to the vocabulary in float32.
model.py 703–735 ↗ · deepseek-ai/DeepSeek-V4-Flash @ 60d8d70770c6
Excerpt
class ParallelHead(nn.Module):
def __init__(self, vocab_size: int, dim: int, norm_eps: float = 1e-6, hc_eps: float = 1e-6):
super().__init__()
self.vocab_size = vocab_size
self.dim = dim
self.norm_eps = norm_eps
self.hc_eps = hc_eps
self.part_vocab_size = (vocab_size // world_size)
# lm_head in the checkpoint is stored in bf16, while the parameter here is stored in fp32 for easier computation of logits later.
self.weight = nn.Parameter(torch.empty(self.part_vocab_size, self.dim, dtype=torch.float32))
def get_logits(self, x):
return F.linear(x[:, -1].float(), self.weight)
def forward(self, x: torch.Tensor, hc_fn: torch.Tensor, hc_scale: torch.Tensor, hc_base: torch.Tensor, norm: RMSNorm):
# x: [b,s,hc,d]
x = self.hc_head(x, hc_fn, hc_scale, hc_base)
logits = self.get_logits(norm(x))
if world_size > 1:
all_logits = [torch.empty_like(logits) for _ in range(world_size)]
dist.all_gather(all_logits, logits)
logits = torch.cat(all_logits, dim=-1)
return logits
def hc_head(self, x: torch.Tensor, hc_fn: torch.Tensor, hc_scale: torch.Tensor, hc_base: torch.Tensor):
shape, dtype = x.size(), x.dtype
x = x.flatten(2).float()
rsqrt = torch.rsqrt(x.square().mean(-1, keepdim=True) + self.norm_eps)
mixes = F.linear(x, hc_fn) * rsqrt
pre = torch.sigmoid(mixes * hc_scale + hc_base) + self.hc_eps
y = torch.sum(pre.unsqueeze(-1) * x.view(shape), dim=2)
return y.to(dtype)