Skip to content

Commit 4c4b33b

Browse files
authored
[pallas-tpu] Switch float16 to HALF_DTYPE in examples for TPU compatibility (#1648)
1 parent 519c312 commit 4c4b33b

6 files changed

Lines changed: 16 additions & 15 deletions

File tree

examples/attention.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def ref_attention(
166166
def main() -> None:
167167
"""
168168
Main entry point that runs the attention kernel test with specific parameters.
169-
Tests with batch size 2, 32 heads, 1024 sequence length, and 64-dimensional heads using float16.
169+
Tests with batch size 2, 32 heads, 1024 sequence length, and 64-dimensional heads using half-precision.
170170
"""
171171
test(2, 32, 1024, 64, HALF_DTYPE, device=DEVICE)
172172

examples/flex_attention.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def flex_fn(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor) -> torch.Tensor:
272272
def main() -> None:
273273
"""
274274
Main entry point that runs the attention kernel test with specific parameters.
275-
Tests with batch size 2, 32 heads, 1024 sequence length, and 64-dimensional heads using float16.
275+
Tests with batch size 2, 32 heads, 1024 sequence length, and 64-dimensional heads using half-precision.
276276
"""
277277
test(2, 32, 1024, 64, HALF_DTYPE)
278278
test(2, 4, 1024, 64, HALF_DTYPE, lambda score, *_: torch.tanh(score))

examples/fp8_gemm.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
====================================================
44
This example demonstrates an FP8 GEMM kernel implemented in Helion. The kernel performs
55
matrix multiplication on FP8 inputs, accumulating results in FP32 for accuracy, and
6-
outputs in FP16 format. It includes a reference PyTorch implementation using
6+
outputs in half-precision format. It includes a reference PyTorch implementation using
77
torch._scaled_mm for correctness comparison, and a test function to validate the kernel.
88
"""
99

@@ -17,6 +17,7 @@
1717

1818
import helion
1919
from helion._testing import DEVICE
20+
from helion._testing import HALF_DTYPE
2021
from helion._testing import run_example
2122
import helion.language as hl
2223

@@ -39,13 +40,13 @@ def fp8_gemm(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
3940
x (torch.Tensor): Input tensor of shape [m, k] in FP8 format.
4041
y (torch.Tensor): Input tensor of shape [k, n] in FP8 format.
4142
Returns:
42-
torch.Tensor: Output tensor of shape [m, n] in FP16 format.
43+
torch.Tensor: Output tensor of shape [m, n] in half-precision format.
4344
"""
4445
m, k = x.size()
4546
k2, n = y.size()
4647
assert k == k2, f"size mismatch {k} != {k2}"
47-
# Output is in FP16 to match tritonbench behavior
48-
out = torch.empty([m, n], dtype=torch.float16, device=x.device)
48+
# Output is in half-precision to match tritonbench behavior
49+
out = torch.empty([m, n], dtype=HALF_DTYPE, device=x.device)
4950
for tile_m, tile_n in hl.tile([m, n]):
5051
# Accumulate in FP32 for accuracy
5152
acc = hl.zeros([tile_m, tile_n], dtype=torch.float32)
@@ -55,7 +56,7 @@ def fp8_gemm(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
5556
y_tile = y[tile_k, tile_n]
5657
# Use hl.dot for FP8 GEMM
5758
acc = hl.dot(x_tile, y_tile, acc=acc)
58-
out[tile_m, tile_n] = acc.to(torch.float16)
59+
out[tile_m, tile_n] = acc.to(HALF_DTYPE)
5960
return out
6061

6162

@@ -69,14 +70,14 @@ def reference_fp8_gemm_pytorch(
6970
x_fp8 (torch.Tensor): Input tensor in FP8 format.
7071
y_fp8 (torch.Tensor): Input tensor in FP8 format.
7172
Returns:
72-
torch.Tensor: Output tensor in FP16 format.
73+
torch.Tensor: Output tensor in half-precision format.
7374
"""
7475
# torch._scaled_mm requires column-major for second operand
7576
y_fp8_t = y_fp8.T.contiguous().T
7677
scale_a = torch.tensor(1.0, device=x_fp8.device)
7778
scale_b = torch.tensor(1.0, device=x_fp8.device)
7879
return torch._scaled_mm(
79-
x_fp8, y_fp8_t, scale_a, scale_b, use_fast_accum=False, out_dtype=torch.float16
80+
x_fp8, y_fp8_t, scale_a, scale_b, use_fast_accum=False, out_dtype=HALF_DTYPE
8081
)
8182

8283

@@ -97,7 +98,7 @@ def fp8_gemm_tritonbench(
9798
scale_a (torch.Tensor): Scale factor for tensor a (unused in our implementation).
9899
scale_b (torch.Tensor): Scale factor for tensor b (unused in our implementation).
99100
Returns:
100-
Callable that returns output tensor in FP16 format.
101+
Callable that returns output tensor in half-precision format.
101102
"""
102103
return lambda: fp8_gemm(a, b)
103104

examples/layer_norm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Helion Layer Normalization Forward and Backward Example
33
=======================================================
44
This example demonstrates a Helion kernel implementation of 1D layer normalization
5-
with both forward and backward passes using FP16 inputs and compares it against
5+
with both forward and backward passes using half-precision inputs and compares it against
66
PyTorch's built-in layer_norm function.
77
"""
88

@@ -33,14 +33,14 @@ def layer_norm_fwd(
3333
"""
3434
Performs 1D layer normalization on the input tensor using Helion.
3535
Args:
36-
x (torch.Tensor): Input tensor of shape [batch_size, dim], expected to be FP16.
36+
x (torch.Tensor): Input tensor of shape [batch_size, dim], expected to be half-precision.
3737
normalized_shape (list[int]): List containing the dimension to normalize over (should be length 1).
3838
weight (torch.Tensor): Learnable scale parameter of shape [dim].
3939
bias (torch.Tensor | None): Optional learnable bias parameter of shape [dim].
4040
eps (float, optional): Small value added to variance for numerical stability. Default is 1e-5.
4141
Returns:
4242
tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
43-
- The layer-normalized output tensor of shape [batch_size, dim], in FP16.
43+
- The layer-normalized output tensor of shape [batch_size, dim], in half-precision.
4444
- Mean tensor of shape [batch_size], in FP32.
4545
- Reciprocal standard deviation tensor of shape [batch_size], in FP32.
4646
"""

examples/mamba2_chunk_scan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def fn(*args: int) -> torch.Tensor:
260260
def main() -> None:
261261
"""
262262
Main entry point that runs the attention kernel test with specific parameters.
263-
Tests with batch size 2, 32 heads, 1024 sequence length, and 64-dimensional heads using float16.
263+
Tests with batch size 2, 32 heads, 1024 sequence length, and 64-dimensional heads using half-precision.
264264
"""
265265
test("zzzzzzz", 8, 80, 1, 4096, 256, 64, 128)
266266
test("zrzzzzr", 8, 80, 1, 4096, 256, 64, 128) # D * x

test/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def test_bf16xint16(self):
529529
fn_name="_int16xbf16_gemm",
530530
)
531531

532-
@xfailIfPallas("Mosaic: Invalid vector type for load with f16 tiling")
532+
@xfailIfPallas("Mosaic: offset not aligned to sublanes")
533533
def test_rms_norm_fwd(self):
534534
args = (
535535
torch.randn([128, 256], device=DEVICE, dtype=HALF_DTYPE),

0 commit comments

Comments
 (0)