Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 31 additions & 36 deletions python/test/unit/language/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Optional
import math
import textwrap
import tempfile

import numpy as np
import pytest
Expand Down Expand Up @@ -2589,7 +2588,7 @@ def kernel(X, Y, N, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, NUM_PID_N: tl.
@pytest.mark.parametrize("M, N", [[32, 16], [32, 32], [32, 64], [64, 32]])
@pytest.mark.parametrize("src_layout", scan_layouts)
@pytest.mark.parametrize("axis", [0, 1])
def test_scan_layouts(M, N, src_layout, axis, device):
def test_scan_layouts(M, N, src_layout, axis, device, tmp_path):

ir = f"""
#blocked = {src_layout}
Expand Down Expand Up @@ -2622,10 +2621,10 @@ def test_scan_layouts(M, N, src_layout, axis, device):
}}
"""

with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
kernel = triton.compile(f.name)
temp_file = tmp_path / "test_scan_layouts.ttgir"
temp_file.write_text(ir)
kernel = triton.compile(str(temp_file))

rs = RandomState(17)
x = rs.randint(-100, 100, (M, N)).astype('int32')

Expand Down Expand Up @@ -2662,7 +2661,7 @@ def test_scan_layouts(M, N, src_layout, axis, device):
@pytest.mark.parametrize("epilogue_kind", ['reduce1d', 'reduce2d', 'expand_reduce2d'])
@pytest.mark.parametrize("dtype_str", ["int32", "float32", "float16"])
@pytest.mark.parametrize("reduce_op", ["sum", "max"])
def test_reduce_layouts(M, N, src_layout, axis, epilogue_kind, dtype_str, reduce_op, device):
def test_reduce_layouts(M, N, src_layout, axis, epilogue_kind, dtype_str, reduce_op, device, tmp_path):
if isinstance(src_layout,
(MfmaLayout, MmaLayout)) and (M < src_layout.instr_shape[0] or N < src_layout.instr_shape[1]):
pytest.skip("Skipping because tensor shape is smaller than M(f)maLayout instr_shape")
Expand Down Expand Up @@ -2756,10 +2755,9 @@ def test_reduce_layouts(M, N, src_layout, axis, epilogue_kind, dtype_str, reduce
}}) {{axis = {axis} : i32}} : (tensor<{M}x{N}x{ty}, #src>) -> tensor<{rdims_1d}x{ty}, #{GPU_DIALECT}.slice<{{dim = {axis}, parent = #src}}>>
""" + epilogue

with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
kernel = triton.compile(f.name)
temp_file = tmp_path / "test_reduce_layouts.ttgir"
temp_file.write_text(ir)
kernel = triton.compile(str(temp_file))

rs = RandomState(17)
x = numpy_random((M, N), dtype_str=dtype_str, rs=rs, low=0, high=10)
Expand Down Expand Up @@ -2789,7 +2787,7 @@ def test_reduce_layouts(M, N, src_layout, axis, epilogue_kind, dtype_str, reduce

@pytest.mark.parametrize("M", [32, 64, 128, 256])
@pytest.mark.parametrize("src_layout", layouts)
def test_store_op(M, src_layout, device):
def test_store_op(M, src_layout, device, tmp_path):

ir = f"""
#src = {src_layout}
Expand All @@ -2810,10 +2808,9 @@ def test_store_op(M, src_layout, device):
}}
"""

with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
store_kernel = triton.compile(f.name)
temp_file = tmp_path / "test_store_op.ttgir"
temp_file.write_text(ir)
store_kernel = triton.compile(str(temp_file))

rs = RandomState(17)
x = rs.randint(0, 4, (M, 1)).astype('float32')
Expand All @@ -2840,7 +2837,7 @@ def test_store_op(M, src_layout, device):
@pytest.mark.parametrize("dst_layout", filter_layouts(layouts))
@pytest.mark.parametrize("src_dim", [0, 1])
@pytest.mark.parametrize("dst_dim", [0, 1])
def test_convert1d(M, src_layout, dst_layout, src_dim, dst_dim, device):
def test_convert1d(M, src_layout, dst_layout, src_dim, dst_dim, device, tmp_path):

ir = f"""
#dst = {dst_layout}
Expand All @@ -2860,10 +2857,9 @@ def test_convert1d(M, src_layout, dst_layout, src_dim, dst_dim, device):
}}
}}
"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
kernel = triton.compile(f.name)
temp_file = tmp_path / "test_convert1d.ttgir"
temp_file.write_text(ir)
kernel = triton.compile(str(temp_file))

rs = RandomState(17)
x = rs.randint(0, 4, (M, )).astype('int32')
Expand Down Expand Up @@ -2901,7 +2897,7 @@ def _welford_combine(mean_1, m2_1, weight_1, mean_2, m2_2, weight_2):
@pytest.mark.parametrize("src_layout", layouts)
@pytest.mark.parametrize("op", ["sum", "max"])
@pytest.mark.parametrize("first_axis", [0, 1])
def test_chain_reduce(M, N, src_layout, op, device, first_axis):
def test_chain_reduce(M, N, src_layout, op, device, first_axis, tmp_path):

op_str = ""
if op == "sum":
Expand Down Expand Up @@ -2942,10 +2938,9 @@ def test_chain_reduce(M, N, src_layout, op, device, first_axis):
}}
}}
"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
kernel = triton.compile(f.name)
temp_file = tmp_path / "test_chain_reduce.ttgir"
temp_file.write_text(ir)
kernel = triton.compile(str(temp_file))

rs = RandomState(17)
x = rs.randint(0, 4, (M, N)).astype('int32')
Expand Down Expand Up @@ -5260,7 +5255,7 @@ def compute_scratch_buffer_shape(src_layout, dst_layout, shape):
@pytest.mark.parametrize("src_layout", layouts)
@pytest.mark.parametrize("interm_layout", intermediate_layouts)
@pytest.mark.parametrize("dst_layout", layouts)
def test_convert2d(M, N, src_layout, interm_layout, dst_layout, dtype, device):
def test_convert2d(M, N, src_layout, interm_layout, dst_layout, dtype, device, tmp_path):
if str(src_layout) == str(dst_layout):
pytest.xfail("Do not convert same layout")
if is_hip() or is_xpu():
Expand Down Expand Up @@ -5329,10 +5324,10 @@ def test_convert2d(M, N, src_layout, interm_layout, dst_layout, dtype, device):
x = to_triton(numpy_random((M, N), dtype_str=dtype), device=device)
z = torch.empty_like(x, device=device)

with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
kernel = triton.compile(f.name)
temp_file = tmp_path / "test_convert2d.ttgir"
temp_file.write_text(ir)
kernel = triton.compile(str(temp_file))

kernel[(1, 1, 1)](x.data_ptr(), z.data_ptr())

assert torch.equal(z, x)
Expand Down Expand Up @@ -5385,7 +5380,7 @@ def test_convert2d(M, N, src_layout, interm_layout, dst_layout, dtype, device):
@pytest.mark.parametrize("M, N", [[64, 1], [1, 64], [64, 64], [128, 128], [256, 256]])
@pytest.mark.parametrize("dtype", ['float16'])
@pytest.mark.parametrize("mma_pair", mma_pairs)
def test_convertmma2mma(M, N, mma_pair, dtype, device):
def test_convertmma2mma(M, N, mma_pair, dtype, device, tmp_path):
if is_hip() or is_xpu():
pytest.xfail("test_mma2mma is not supported in HIP/XPU")

Expand Down Expand Up @@ -5442,10 +5437,10 @@ def do_test(src_layout, dst_layout):
x = to_triton(numpy_random((M, N), dtype_str=dtype), device=device)
z = torch.empty_like(x)

with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f:
f.write(ir)
f.flush()
kernel = triton.compile(f.name)
temp_file = tmp_path / "test_convertmma2mma.ttgir"
temp_file.write_text(ir)
kernel = triton.compile(str(temp_file))

kernel[(1, 1, 1)](x.data_ptr(), z.data_ptr())

assert torch.equal(z, x)
Expand Down
21 changes: 10 additions & 11 deletions python/test/unit/runtime/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import importlib.util
import itertools
import shutil
import tempfile

import pytest
import torch
Expand Down Expand Up @@ -128,28 +127,28 @@ def test_combine_fn_change():
seen_keys.add(key)


def write_and_load_module(code, num_extra_lines):
with tempfile.NamedTemporaryFile(mode='w+', suffix='.py') as f:
f.write(('# extra line\n' * num_extra_lines) + code)
f.flush()
spec = importlib.util.spec_from_file_location("module.name", f.name)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
def write_and_load_module(temp_file, code, num_extra_lines):
temp_file.write_text(('# extra line\n' * num_extra_lines) + code)
spec = importlib.util.spec_from_file_location("module.name", str(temp_file))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


def test_changed_line_numbers_invalidate_cache():
def test_changed_line_numbers_invalidate_cache(tmp_path):
from textwrap import dedent
code = dedent("""
import triton
@triton.jit
def test_kernel(i):
i = i + 1
""")
orig_mod = write_and_load_module(code, 0)
temp_file0 = tmp_path / "test_changed_line_numbers_invalidate_cache0.py"
orig_mod = write_and_load_module(temp_file0, code, 0)
orig_cache_key = orig_mod.test_kernel.cache_key

updated_mod = write_and_load_module(code, 1)
temp_file1 = tmp_path / "test_changed_line_numbers_invalidate_cache1.py"
updated_mod = write_and_load_module(temp_file1, code, 1)
updated_cache_key = updated_mod.test_kernel.cache_key
assert orig_cache_key != updated_cache_key

Expand Down
Loading