Pad D to 16-byte alignment for TMA on SM 90+ (H100, B200, etc.). TMA requires the innermost tensor dimension to be 16-byte aligned. For bf16 (2 bytes), D must be a multiple of 8. After shift_logits_negative adds a bias column (D = hidden_size + 1), D=11 is not aligned. Zero-padding
(
weights: torch.Tensor, hidden_states: torch.Tensor
)
| 73 | |
| 74 | |
| 75 | def pad_to_tma_alignment( |
| 76 | weights: torch.Tensor, hidden_states: torch.Tensor |
| 77 | ) -> tuple[torch.Tensor, torch.Tensor]: |
| 78 | """Pad D to 16-byte alignment for TMA on SM 90+ (H100, B200, etc.). |
| 79 | |
| 80 | TMA requires the innermost tensor dimension to be 16-byte aligned. |
| 81 | For bf16 (2 bytes), D must be a multiple of 8. After shift_logits_negative |
| 82 | adds a bias column (D = hidden_size + 1), D=11 is not aligned. Zero-padding |
| 83 | extra columns preserves logits (they contribute nothing to the matmul). |
| 84 | """ |
| 85 | d = weights.shape[1] |
| 86 | aligned_d = (d + 7) & ~7 # next multiple of 8 bf16 elements = 16 bytes |
| 87 | if aligned_d > d: |
| 88 | pad = aligned_d - d |
| 89 | weights = torch.nn.functional.pad(weights, (0, pad)) |
| 90 | hidden_states = torch.nn.functional.pad(hidden_states, (0, pad)) |
| 91 | return weights, hidden_states |
| 92 | |
| 93 | |
| 94 | def shard_weights(weights: torch.Tensor, tp: TPInfo) -> torch.Tensor: |
no outgoing calls
no test coverage detected