Skip to content

Commit d9fb0bb

Browse files
authored
Fix AOT non-power-of-two innermost sizes (#156)
* Add a test case for AOT non-power-of-two innermost sizes * Fix AOT non-power-of-two innermost sizes
1 parent 41afcf8 commit d9fb0bb

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/ninetoothed/generation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,16 @@ def _generate_offsets_and_mask(tensor, indices):
780780
@staticmethod
781781
def _generate_innermost_indices(tensor, use_power_of_2_sizes=True):
782782
class _NextPowerOfTwoMaker(ast.NodeTransformer):
783+
def visit_Constant(self, node):
784+
value = node.value
785+
786+
if isinstance(value, int) and not isinstance(value, bool) and value > 0:
787+
return ast.copy_location(
788+
ast.Constant(value=1 << (value - 1).bit_length()), node
789+
)
790+
791+
return self.generic_visit(node)
792+
783793
def visit_Name(self, node):
784794
name = node.id
785795

tests/test_aot.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,41 @@ def _application(input, scale, output):
343343
assert torch.allclose(output, expected)
344344

345345

346+
@pytest.mark.parametrize("device", get_available_devices())
347+
def test_aot_with_static_non_power_of_two_innermost_sizes(device):
348+
def _arrangement(input, output):
349+
return input.tile((3,)), output.tile((3,))
350+
351+
def _application(input, output):
352+
output = input # noqa: F841
353+
354+
tensors = (
355+
Tensor(1, dtype=ninetoothed.float32),
356+
Tensor(1, dtype=ninetoothed.float32),
357+
)
358+
359+
kernel_name = (
360+
f"static_non_power_of_two_innermost_sizes{_generate_kernel_name_suffix()}"
361+
)
362+
output_dir = ninetoothed.generation.CACHE_DIR
363+
364+
kernel = ninetoothed.make(
365+
_arrangement,
366+
_application,
367+
tensors,
368+
caller=device,
369+
kernel_name=kernel_name,
370+
output_dir=output_dir,
371+
)
372+
373+
input = torch.randn((3,), dtype=torch.float32, device=device)
374+
output = torch.empty_like(input)
375+
376+
kernel(input, output)
377+
378+
assert torch.allclose(input, output)
379+
380+
346381
def test_overflow_terms():
347382
terms = ninetoothed.aot._overflow_terms(("input", "scale"), (2, 0))
348383

0 commit comments

Comments
 (0)