|
11 | 11 | from helion._testing import RefEagerTestBase |
12 | 12 | from helion._testing import TestCase |
13 | 13 | from helion._testing import code_and_output |
| 14 | +from helion._testing import skipIfLowVRAM |
14 | 15 | from helion._testing import skipIfNormalMode |
15 | 16 | from helion._testing import skipIfRefEager |
16 | 17 | from helion._testing import skipIfRocm |
@@ -241,6 +242,93 @@ def test_block_size_access(x: torch.Tensor) -> torch.Tensor: |
241 | 242 | expected = torch.full_like(x, 1, dtype=torch.int32) |
242 | 243 | torch.testing.assert_close(result, expected) |
243 | 244 |
|
| 245 | + @skipIfRefEager( |
| 246 | + "IndexOffsetOutOfRangeForInt32 error is not raised in ref eager mode" |
| 247 | + ) |
| 248 | + @skipIfLowVRAM("Test requires high VRAM") |
| 249 | + def test_int32_offset_out_of_range_error(self): |
| 250 | + repro_config = helion.Config( |
| 251 | + block_sizes=[32, 32], |
| 252 | + flatten_loops=[False], |
| 253 | + indexing="pointer", |
| 254 | + l2_groupings=[1], |
| 255 | + loop_orders=[[0, 1]], |
| 256 | + num_stages=3, |
| 257 | + num_warps=4, |
| 258 | + pid_type="flat", |
| 259 | + range_flattens=[None], |
| 260 | + range_multi_buffers=[None], |
| 261 | + range_num_stages=[], |
| 262 | + range_unroll_factors=[0], |
| 263 | + range_warp_specializes=[], |
| 264 | + ) |
| 265 | + |
| 266 | + def make_kernel(*, index_dtype: torch.dtype): |
| 267 | + kwargs = {"config": repro_config, "static_shapes": True} |
| 268 | + kwargs["index_dtype"] = index_dtype |
| 269 | + decorator = helion.kernel(**kwargs) |
| 270 | + |
| 271 | + @decorator |
| 272 | + def repro_bf16_add(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 273 | + x, y = torch.broadcast_tensors(x, y) |
| 274 | + out = torch.empty( |
| 275 | + x.shape, |
| 276 | + dtype=torch.promote_types(x.dtype, y.dtype), |
| 277 | + device=x.device, |
| 278 | + ) |
| 279 | + for tile in hl.tile(out.size()): |
| 280 | + out[tile] = x[tile] + y[tile] |
| 281 | + return out |
| 282 | + |
| 283 | + return repro_bf16_add |
| 284 | + |
| 285 | + def run_case( |
| 286 | + shape, *, index_dtype, expect_int64_in_code=False, expect_error=False |
| 287 | + ): |
| 288 | + kernel = make_kernel(index_dtype=index_dtype) |
| 289 | + x = torch.randn(*shape, device=DEVICE, dtype=torch.bfloat16) |
| 290 | + y = torch.randn(*shape, device=DEVICE, dtype=torch.bfloat16) |
| 291 | + torch.cuda.synchronize() |
| 292 | + if expect_error: |
| 293 | + with self.assertRaisesRegex( |
| 294 | + helion.exc.IndexOffsetOutOfRangeForInt32, |
| 295 | + f"index_dtype is {index_dtype}", |
| 296 | + ): |
| 297 | + code_and_output(kernel, (x, y)) |
| 298 | + torch.cuda.synchronize() |
| 299 | + return |
| 300 | + |
| 301 | + code, out = code_and_output(kernel, (x, y)) |
| 302 | + torch.cuda.synchronize() |
| 303 | + checker = self.assertIn if expect_int64_in_code else self.assertNotIn |
| 304 | + checker("tl.int64", code) |
| 305 | + torch.cuda.synchronize() |
| 306 | + ref_out = torch.add(x, y) |
| 307 | + torch.cuda.synchronize() |
| 308 | + torch.testing.assert_close(out, ref_out, rtol=1e-2, atol=1e-2) |
| 309 | + |
| 310 | + small_shape = (128, 128) |
| 311 | + large_shape = (51200, 51200) |
| 312 | + |
| 313 | + run_case( |
| 314 | + small_shape, |
| 315 | + index_dtype=torch.int32, |
| 316 | + expect_int64_in_code=False, |
| 317 | + expect_error=False, |
| 318 | + ) |
| 319 | + run_case( |
| 320 | + large_shape, |
| 321 | + index_dtype=torch.int32, |
| 322 | + expect_int64_in_code=False, |
| 323 | + expect_error=True, |
| 324 | + ) |
| 325 | + run_case( |
| 326 | + large_shape, |
| 327 | + index_dtype=torch.int64, |
| 328 | + expect_int64_in_code=True, |
| 329 | + expect_error=False, |
| 330 | + ) |
| 331 | + |
244 | 332 | def test_assign_int(self): |
245 | 333 | @helion.kernel |
246 | 334 | def fn(x: torch.Tensor) -> torch.Tensor: |
|
0 commit comments