[Model Runner V2] Cache draft logits in model's LM head dtype (#50910)
Signed-off-by: Giancarlo Delfin <gdelfin@inferact.ai>
This commit is contained in:
@@ -91,9 +91,9 @@ def gumbel_block_argmax(
|
||||
temp_ptr,
|
||||
seeds_ptr,
|
||||
pos_ptr,
|
||||
processed_logits_ptr,
|
||||
processed_logits_stride,
|
||||
processed_logits_col_ptr,
|
||||
logits_cache_ptr,
|
||||
logits_cache_stride,
|
||||
logits_cache_col_ptr,
|
||||
vocab_size,
|
||||
APPLY_TEMPERATURE: tl.constexpr,
|
||||
USE_FP64: tl.constexpr,
|
||||
@@ -104,30 +104,30 @@ def gumbel_block_argmax(
|
||||
temp = tl.load(temp_ptr + req_state_idx, mask=is_valid_req, other=0.0).to(
|
||||
tl.float32
|
||||
)
|
||||
if temp != 0.0 and APPLY_TEMPERATURE:
|
||||
# Apply temperature.
|
||||
# NOTE(woosuk): Match the behavior of _temperature_kernel.
|
||||
# E.g., if the kernel uses tl.div_rn, we should use tl.div_rn here too.
|
||||
logits = logits / temp
|
||||
|
||||
if processed_logits_ptr is not None:
|
||||
# Store the temperature-applied logits.
|
||||
if processed_logits_col_ptr is not None:
|
||||
if PER_TOKEN_COL:
|
||||
col = tl.load(processed_logits_col_ptr + token_idx)
|
||||
else:
|
||||
col = tl.load(processed_logits_col_ptr)
|
||||
if logits_cache_ptr is not None:
|
||||
# Store the logits *before* temperature. Dividing first would produce a
|
||||
# value that is generally not representable in the cache's dtype, forcing
|
||||
# it to be fp32. Consumers (the rejection sampler) divide by the same
|
||||
# temperature on load, which reproduces the value used below bitwise.
|
||||
if PER_TOKEN_COL:
|
||||
col = tl.load(logits_cache_col_ptr + token_idx)
|
||||
else:
|
||||
col = 0
|
||||
col = tl.load(logits_cache_col_ptr)
|
||||
tl.store(
|
||||
processed_logits_ptr
|
||||
+ req_state_idx * processed_logits_stride
|
||||
logits_cache_ptr
|
||||
+ req_state_idx * logits_cache_stride
|
||||
+ col * vocab_size
|
||||
+ block,
|
||||
logits,
|
||||
mask=mask & is_valid_req,
|
||||
)
|
||||
|
||||
if temp != 0.0 and APPLY_TEMPERATURE:
|
||||
# Apply temperature.
|
||||
# NOTE(woosuk): Match the behavior of _temperature_kernel.
|
||||
# E.g., if the kernel uses tl.div_rn, we should use tl.div_rn here too.
|
||||
logits = logits / temp
|
||||
|
||||
# fp32 is the default reduction dtype; fp64 is ~1/32–1/64x the throughput
|
||||
# on H100/Ada/Blackwell and empirically indistinguishable for Gumbel-max.
|
||||
if USE_FP64:
|
||||
@@ -164,9 +164,9 @@ def _gumbel_sample_kernel(
|
||||
local_argmax_stride,
|
||||
local_max_ptr,
|
||||
local_max_stride,
|
||||
processed_logits_ptr,
|
||||
processed_logits_stride,
|
||||
processed_logits_col_ptr,
|
||||
logits_cache_ptr,
|
||||
logits_cache_stride,
|
||||
logits_cache_col_ptr,
|
||||
logits_ptr,
|
||||
logits_stride,
|
||||
expanded_idx_mapping_ptr,
|
||||
@@ -199,9 +199,9 @@ def _gumbel_sample_kernel(
|
||||
temp_ptr,
|
||||
seeds_ptr,
|
||||
pos_ptr,
|
||||
processed_logits_ptr,
|
||||
processed_logits_stride,
|
||||
processed_logits_col_ptr,
|
||||
logits_cache_ptr,
|
||||
logits_cache_stride,
|
||||
logits_cache_col_ptr,
|
||||
vocab_size,
|
||||
APPLY_TEMPERATURE=APPLY_TEMPERATURE,
|
||||
USE_FP64=USE_FP64,
|
||||
@@ -219,33 +219,30 @@ def gumbel_sample(
|
||||
seed: torch.Tensor, # [max_num_reqs]
|
||||
pos: torch.Tensor, # [num_tokens]
|
||||
apply_temperature: bool,
|
||||
output_processed_logits: torch.Tensor | None = None,
|
||||
output_processed_logits_col: torch.Tensor | None = None,
|
||||
logits_cache: torch.Tensor | None = None, # [max_num_reqs, num_cols, vocab_size]
|
||||
logits_cache_col: torch.Tensor | None = None, # scalar or [num_tokens]
|
||||
use_fp64: bool = False,
|
||||
) -> torch.Tensor:
|
||||
# Enforce contiguity on non-strided input tensors
|
||||
expanded_idx_mapping = expanded_idx_mapping.contiguous()
|
||||
pos = pos.contiguous()
|
||||
if output_processed_logits_col is not None:
|
||||
output_processed_logits_col = output_processed_logits_col.contiguous()
|
||||
if logits_cache_col is not None:
|
||||
logits_cache_col = logits_cache_col.contiguous()
|
||||
num_tokens, vocab_size = logits.shape
|
||||
BLOCK_SIZE = 1024
|
||||
num_blocks = triton.cdiv(vocab_size, BLOCK_SIZE)
|
||||
local_argmax = logits.new_empty(num_tokens, num_blocks, dtype=torch.int64)
|
||||
local_max_dtype = torch.float64 if use_fp64 else torch.float32
|
||||
local_max = logits.new_empty(num_tokens, num_blocks, dtype=local_max_dtype)
|
||||
per_token_col = (
|
||||
output_processed_logits_col is not None
|
||||
and output_processed_logits_col.dim() > 0
|
||||
)
|
||||
per_token_col = logits_cache_col is not None and logits_cache_col.dim() > 0
|
||||
_gumbel_sample_kernel[(num_tokens, num_blocks)](
|
||||
local_argmax,
|
||||
local_argmax.stride(0),
|
||||
local_max,
|
||||
local_max.stride(0),
|
||||
output_processed_logits,
|
||||
output_processed_logits.stride(0) if output_processed_logits is not None else 0,
|
||||
output_processed_logits_col,
|
||||
logits_cache,
|
||||
logits_cache.stride(0) if logits_cache is not None else 0,
|
||||
logits_cache_col,
|
||||
logits,
|
||||
logits.stride(0),
|
||||
expanded_idx_mapping,
|
||||
|
||||
Reference in New Issue
Block a user