|
| 1 | +import pytest |
| 2 | + |
| 3 | +from triton_kernels_benchmark.benchmark_utils import BenchmarkCategory, BenchmarkConfigs |
| 4 | +from triton_kernels_benchmark.benchmark_shapes_parser import ShapePatternParser |
| 5 | + |
| 6 | +CONFIG_SHAPES = [ |
| 7 | + shape for config in BenchmarkConfigs._get_configs( # pylint: disable=W0212 |
| 8 | + configs_filter=BenchmarkConfigs._get_all_configs().keys(), # pylint: disable=W0212 |
| 9 | + categories_filter=[cat.value for cat in BenchmarkCategory], |
| 10 | + providers_filter=[], |
| 11 | + shape_pattern=None, |
| 12 | + ) for shape in config.shapes |
| 13 | +] |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + "input_string, expected", |
| 18 | + [ |
| 19 | + ("[1-2-3]", [1, 2, 3]), |
| 20 | + ("[16-32-1024-64]", [16, 32, 1024, 64]), |
| 21 | + ("[True-False-123]", ["True", "False", 123]), |
| 22 | + ], |
| 23 | +) |
| 24 | +def test_parse_valid_shapes(input_string, expected): |
| 25 | + assert ShapePatternParser.parse(input_string) == expected |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("known_shape", CONFIG_SHAPES) |
| 29 | +def test_parse_all_known_shapes(known_shape): |
| 30 | + ShapePatternParser.parse(known_shape) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + "pattern, tokens", |
| 35 | + [ |
| 36 | + ("[16-*-1024-*]", [16, "*", 1024, "*"]), |
| 37 | + ], |
| 38 | +) |
| 39 | +def test_parse_valid_pattern_shapes(pattern, tokens): |
| 40 | + parsed_tokens = ShapePatternParser.parse(pattern, pattern_shape=True) |
| 41 | + assert tokens == parsed_tokens |
| 42 | + |
| 43 | + |
| 44 | +def test_parse_bracketed_star_without_pattern_shape_fails(): |
| 45 | + with pytest.raises(ValueError): |
| 46 | + ShapePatternParser.parse("[*]") |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + "invalid_string", |
| 51 | + [ |
| 52 | + "1-2-3", |
| 53 | + "[]", |
| 54 | + "[ ]", |
| 55 | + "[ - ]", |
| 56 | + "[-]", |
| 57 | + "[--]", |
| 58 | + "[- -]", |
| 59 | + "[1-2-@-4]", |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_parse_bracketed_invalid(invalid_string): |
| 63 | + with pytest.raises(ValueError): |
| 64 | + ShapePatternParser.parse(invalid_string, pattern_shape=True) |
| 65 | + with pytest.raises(ValueError): |
| 66 | + ShapePatternParser(invalid_string) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize( |
| 70 | + "pattern", |
| 71 | + [ |
| 72 | + "[1-2-3]", |
| 73 | + "[a-b-c]", |
| 74 | + "[*-b-3]", |
| 75 | + ], |
| 76 | +) |
| 77 | +def test_init_valid(pattern): |
| 78 | + parser = ShapePatternParser(pattern) |
| 79 | + assert parser.pattern == pattern |
| 80 | + assert len(parser.pattern_tokens) == parser.pattern_dims |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def sample_shapes(): |
| 85 | + return [ |
| 86 | + "[16-32-1024-64-False-bwd]", |
| 87 | + "[16-64-1024-32-True-fwd]", |
| 88 | + "[32-32-512-64-False-bwd]", |
| 89 | + "[16-32-1024-64-False-fwd]", |
| 90 | + ] |
| 91 | + |
| 92 | + |
| 93 | +def test_filter_exact_match(sample_shapes): # pylint: disable=W0621 |
| 94 | + parser = ShapePatternParser("[16-32-1024-64-False-bwd]") |
| 95 | + assert parser.filter_by_pattern(sample_shapes) == ["[16-32-1024-64-False-bwd]"] |
| 96 | + |
| 97 | + |
| 98 | +def test_filter_wildcard_match(sample_shapes): # pylint: disable=W0621 |
| 99 | + parser = ShapePatternParser("[16-*-1024-64-False-*]") |
| 100 | + expected = [ |
| 101 | + "[16-32-1024-64-False-bwd]", |
| 102 | + "[16-32-1024-64-False-fwd]", |
| 103 | + ] |
| 104 | + assert parser.filter_by_pattern(sample_shapes) == expected |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + "pattern, shapes", |
| 109 | + [ |
| 110 | + ("[*]", ["bad-shape", "[foo]", "[123]"]), |
| 111 | + ], |
| 112 | +) |
| 113 | +def test_filter_invalid_shape_strings(pattern, shapes): |
| 114 | + parser = ShapePatternParser(pattern) |
| 115 | + with pytest.raises(ValueError): |
| 116 | + parser.filter_by_pattern(shapes) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize( |
| 120 | + "pattern, shapes", |
| 121 | + [ |
| 122 | + ("[1-2-3]", ["[1-2]"]), |
| 123 | + ], |
| 124 | +) |
| 125 | +def test_filter_dims_mismatch(pattern, shapes): |
| 126 | + parser = ShapePatternParser(pattern) |
| 127 | + with pytest.raises(ValueError) as exc_info: |
| 128 | + parser.filter_by_pattern(shapes) |
| 129 | + assert "mismatch" in str(exc_info.value) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.parametrize( |
| 133 | + "pattern, shapes", |
| 134 | + [ |
| 135 | + ("[*]", ["[foo]", "[bar]", "[123]"]), |
| 136 | + ], |
| 137 | +) |
| 138 | +def test_filter_single_wildcard_matches_all(pattern, shapes): |
| 139 | + parser = ShapePatternParser(pattern) |
| 140 | + assert parser.filter_by_pattern(shapes) == shapes |
0 commit comments