Fused matrix-multiply & sampling using the CUDA C++ kernel.
(
weights: torch.Tensor, # [V, D] bfloat16
hidden_states: torch.Tensor, # [H, D] bfloat16
num_samples: int,
temperature: torch.Tensor, # scalar (0-d)
seed: int = 0,
**kwargs,
)
| 42 | |
| 43 | |
| 44 | def fused_mm_sample_cuda( |
| 45 | weights: torch.Tensor, # [V, D] bfloat16 |
| 46 | hidden_states: torch.Tensor, # [H, D] bfloat16 |
| 47 | num_samples: int, |
| 48 | temperature: torch.Tensor, # scalar (0-d) |
| 49 | seed: int = 0, |
| 50 | **kwargs, |
| 51 | ) -> torch.Tensor: |
| 52 | """Fused matrix-multiply & sampling using the CUDA C++ kernel.""" |
| 53 | V, D = weights.shape # noqa: N806 |
| 54 | H = hidden_states.shape[0] # noqa: N806 |
| 55 | assert hidden_states.shape[1] == D |
| 56 | |
| 57 | n_tiles_v = (V + TILE_V - 1) // TILE_V |
| 58 | |
| 59 | # Temperature must be float32 on GPU |
| 60 | if temperature.dtype != torch.float32: |
| 61 | temperature = temperature.float() |
| 62 | |
| 63 | maxs = torch.empty((n_tiles_v, H, num_samples), dtype=torch.float32, device=weights.device) |
| 64 | maxs_idx = torch.empty((n_tiles_v, H, num_samples), dtype=torch.long, device=weights.device) |
| 65 | |
| 66 | mod = _get_module() |
| 67 | mod.fmms_stage1(weights, hidden_states, maxs, maxs_idx, temperature, seed) |
| 68 | |
| 69 | # Stage 2: reduce across V-tiles (identical to Triton wrapper) |
| 70 | idxs = maxs.max(dim=0).indices # [H, num_samples] |
| 71 | samples = maxs_idx.gather(dim=0, index=idxs.unsqueeze(0)).squeeze(0) |
| 72 | return samples # [H, num_samples] |
nothing calls this directly
no test coverage detected