draft_tau.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Draft proposal temperature. Gated on DSV41_DRAFT_TAU (unset or 1 = off).
  2. Multiplies the per-request temperature that the folded DSpark draft sampler uses (sampled requests
  3. only; greedy rows stay argmax). The same tensor feeds the in-graph sampling of the draft tokens and
  4. the draft probabilities of the rejection sampler (`dspark_draft` takes `draft_sampler.temperatures`
  5. into the draft block, `accept_draft_tokens` softmaxes the corrected logits with it), so proposal and
  6. acceptance always see the same q. Speculative sampling is exact for any q: the output distribution
  7. does not change, only acceptance does.
  8. Measured offline on held-out captured traffic at the model card's T=1 / top_p=0.95, with a draft
  9. forward whose per-position acceptance matches the engine within 0.016: 0.8 raises accepted tokens
  10. per step 3.242 -> 3.280 (+1.2 %); 0.7-0.8 is the optimum, 0.5 already gives some back.
  11. Requires SGLANG_DSPARK_FOLDED_SAMPLING=2 (the folded sampler), which production sets.
  12. """
  13. import os
  14. TAU = float(os.environ.get("DSV41_DRAFT_TAU", "1") or 1)
  15. def install(module):
  16. """sglang.srt.speculative.dspark_components.dspark_draft_sampler"""
  17. if TAU == 1.0:
  18. return
  19. cls = getattr(module, "DsparkDraftSampler", None)
  20. if cls is None or not hasattr(cls, "stage_sampling_params"):
  21. raise RuntimeError("DSV41_DRAFT_TAU: DsparkDraftSampler.stage_sampling_params is gone; engine drifted")
  22. if getattr(cls, "_dsv41_draft_tau", False):
  23. return
  24. cls._dsv41_draft_tau = True
  25. orig = cls.stage_sampling_params
  26. def stage_sampling_params(self, *, bs, sampling_info):
  27. orig(self, bs=bs, sampling_info=sampling_info)
  28. if self.folded_sampling and self.temperatures is not None and sampling_info is not None:
  29. self.temperatures[:bs].mul_(TAU)
  30. cls.stage_sampling_params = stage_sampling_params
  31. print(f"[draft_tau] draft temperature x{TAU}", flush=True)