Skip to content

Commit e703604

Browse files
K3: dtype-robust drafter boundaries + never emit the mask sentinel
* project_aux casts aux concat to fc weight dtype; draft_block casts block embeds to aux_proj dtype (fixes fp32-aux vs bf16-drafter mat dtype error on the H200 verifier path). * forbid mask_token_id in the drafter's output logits (loop + cleanup) so a draft never proposes the mask sentinel (also removes a weight-init- dependent flaky unit test). 20 tests pass. Co-authored-by: FluffyAIcode <FluffyAIcode@users.noreply.github.com>
1 parent 32f3bd0 commit e703604

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

inference_engine/v04/dflash_drafter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ def project_aux(self, aux_hidden_states: Sequence[torch.Tensor]) -> torch.Tensor
307307
f"aux concat feature dim {cat.shape[-1]} != fc_in_features "
308308
f"{self.cfg.fc_in_features}"
309309
)
310+
# The verifier hands aux hidden in its own dtype (often upcast to
311+
# fp32 for the capture); cast to the drafter's compute dtype.
312+
cat = cat.to(self.fc.weight.dtype)
310313
return self.hidden_norm(self.fc(cat))
311314

312315
# -- transformer forward ----------------------------------------------
@@ -436,11 +439,14 @@ def draft_block(
436439
masked = block[0] == cfg.mask_token_id
437440
if not bool(masked.any()):
438441
break
439-
block_embeds = embed_fn(block) # [1, block_size, hidden]
440-
h = torch.cat([aux_proj.to(block_embeds.dtype), block_embeds], dim=1)
442+
block_embeds = embed_fn(block).to(aux_proj.dtype) # [1, block_size, hidden]
443+
h = torch.cat([aux_proj, block_embeds], dim=1)
441444
h = self.backbone(h, position_ids, attn_bias) # [1, T, hidden]
442445
block_h = h[:, 1:, :] # drop the register
443446
logits = lm_head_fn(block_h) # [1, block_size, vocab]
447+
# The drafter must never propose the mask sentinel as a real
448+
# token; forbid it before argmax.
449+
logits[..., cfg.mask_token_id] = float("-inf")
444450
x0 = torch.argmax(logits, dim=-1) # [1, block_size]
445451
probs = torch.softmax(logits.float(), dim=-1)
446452
conf = probs.gather(-1, x0.unsqueeze(-1)).squeeze(-1)[0] # [block_size]
@@ -456,10 +462,11 @@ def draft_block(
456462

457463
if bool((block[0] == cfg.mask_token_id).any()):
458464
# Collapse any leftover masks to argmax rather than emit <mask>.
459-
block_embeds = embed_fn(block)
460-
h = torch.cat([aux_proj.to(block_embeds.dtype), block_embeds], dim=1)
465+
block_embeds = embed_fn(block).to(aux_proj.dtype)
466+
h = torch.cat([aux_proj, block_embeds], dim=1)
461467
h = self.backbone(h, position_ids, attn_bias)
462468
logits = lm_head_fn(h[:, 1:, :])
469+
logits[..., cfg.mask_token_id] = float("-inf")
463470
x0 = torch.argmax(logits, dim=-1)[0]
464471
leftover = block[0] == cfg.mask_token_id
465472
block[0] = torch.where(leftover, x0, block[0])

0 commit comments

Comments
 (0)