Entry point called by bench.py. Must match reference.cross_entropy_ref signature. Args: logits: [batch_size, vocab_size] raw logits (float16 or float32) targets: [batch_size] integer class indices (long) Returns: Scalar mean cross-entropy loss
(logits: torch.Tensor, targets: torch.Tensor)
| 68 | |
| 69 | |
| 70 | def kernel_fn(logits: torch.Tensor, targets: torch.Tensor) -> torch.Tensor: |
| 71 | """ |
| 72 | Entry point called by bench.py. Must match reference.cross_entropy_ref signature. |
| 73 | |
| 74 | Args: |
| 75 | logits: [batch_size, vocab_size] raw logits (float16 or float32) |
| 76 | targets: [batch_size] integer class indices (long) |
| 77 | |
| 78 | Returns: |
| 79 | Scalar mean cross-entropy loss |
| 80 | """ |
| 81 | assert logits.is_cuda and targets.is_cuda |
| 82 | |
| 83 | # Handle multi-dim: flatten to 2D |
| 84 | if logits.ndim > 2: |
| 85 | logits = logits.view(-1, logits.shape[-1]) |
| 86 | targets = targets.view(-1) |
| 87 | |
| 88 | n_rows, n_cols = logits.shape |
| 89 | assert targets.shape[0] == n_rows |
| 90 | |
| 91 | losses = torch.empty(n_rows, device=logits.device, dtype=torch.float32) |
| 92 | |
| 93 | BLOCK_SIZE = triton.next_power_of_2(n_cols) |
| 94 | |
| 95 | grid = (n_rows,) |
| 96 | cross_entropy_kernel[grid]( |
| 97 | logits, |
| 98 | targets, |
| 99 | losses, |
| 100 | n_cols, |
| 101 | logits.stride(0), |
| 102 | BLOCK_SIZE=BLOCK_SIZE, |
| 103 | ) |
| 104 | |
| 105 | return losses.mean() |
nothing calls this directly
no outgoing calls
no test coverage detected