|
| 1 | +# isort: off |
| 2 | +# fmt: off |
| 3 | +import pytest |
| 4 | +import types |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +import triton_kernels.matmul_ogs_details.opt_flags as opt_flags |
| 9 | + |
| 10 | + |
| 11 | +class _DummyPrecisionConfig: |
| 12 | + def __init__(self): |
| 13 | + self.weight_scale = None |
| 14 | + self.max_num_imprecise_acc = None |
| 15 | + self.act_scale = None |
| 16 | + self.out_scale = None |
| 17 | + self.enforce_bitwise_invariance = False |
| 18 | + |
| 19 | + |
| 20 | +def _stub_cuda_props(*_args, **_kwargs): |
| 21 | + return types.SimpleNamespace(multi_processor_count=16) |
| 22 | + |
| 23 | + |
| 24 | +def setup_amd(monkeypatch): |
| 25 | + monkeypatch.setattr(opt_flags, "get_cdna_version", lambda: 3) |
| 26 | + monkeypatch.setattr(opt_flags.torch.cuda, "get_device_properties", _stub_cuda_props) |
| 27 | + monkeypatch.setattr( |
| 28 | + opt_flags.opt_flags_amd, |
| 29 | + "compute_block_nk", |
| 30 | + lambda *args, **kwargs: (64, 32), |
| 31 | + ) |
| 32 | + |
| 33 | + fake_target = types.SimpleNamespace(backend="hip") |
| 34 | + monkeypatch.setattr( |
| 35 | + "triton.runtime.driver.active.get_current_target", |
| 36 | + lambda: fake_target, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def setup_nvidia(monkeypatch): |
| 41 | + monkeypatch.setattr(opt_flags.torch.cuda, "get_device_properties", _stub_cuda_props) |
| 42 | + monkeypatch.setattr(opt_flags.torch.cuda, "get_device_capability", lambda: (9, 0)) |
| 43 | + monkeypatch.setattr( |
| 44 | + opt_flags.opt_flags_nvidia, |
| 45 | + "compute_block_n", |
| 46 | + lambda n, arch, precision_config: (64, 32), |
| 47 | + ) |
| 48 | + monkeypatch.setattr( |
| 49 | + opt_flags.opt_flags_nvidia, |
| 50 | + "compute_grid_size", |
| 51 | + lambda routing_data, batch_size, m, n, block_m, block_n: 4, |
| 52 | + ) |
| 53 | + monkeypatch.setattr( |
| 54 | + opt_flags.opt_flags_nvidia, |
| 55 | + "compute_block_k", |
| 56 | + lambda m, k, is_persistent, lhs_dtype, rhs_dtype, precision_config, has_y_acc_in: 32, |
| 57 | + ) |
| 58 | + monkeypatch.setattr( |
| 59 | + opt_flags.opt_flags_nvidia, |
| 60 | + "compute_split_k", |
| 61 | + lambda block_k, k, estimated_actual_grid_size: 1, |
| 62 | + ) |
| 63 | + monkeypatch.setattr( |
| 64 | + opt_flags.opt_flags_nvidia, |
| 65 | + "compute_num_stages", |
| 66 | + lambda *args, **kwargs: 2, |
| 67 | + ) |
| 68 | + monkeypatch.setattr( |
| 69 | + opt_flags.opt_flags_nvidia, |
| 70 | + "compute_num_warps", |
| 71 | + lambda block_m, block_n, is_persistent, precision_config: 4, |
| 72 | + ) |
| 73 | + |
| 74 | + fake_target = types.SimpleNamespace(backend="cuda") |
| 75 | + monkeypatch.setattr( |
| 76 | + "triton.runtime.driver.active.get_current_target", |
| 77 | + lambda: fake_target, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def test_make_default_opt_flags_amd_split_k_constraint(monkeypatch): |
| 82 | + setup_amd(monkeypatch) |
| 83 | + |
| 84 | + precision_config = _DummyPrecisionConfig() |
| 85 | + flags = opt_flags.make_default_opt_flags_amd( |
| 86 | + torch.float16, |
| 87 | + torch.float16, |
| 88 | + torch.float16, |
| 89 | + precision_config, |
| 90 | + 2, |
| 91 | + 128, |
| 92 | + 64, |
| 93 | + 32, |
| 94 | + None, |
| 95 | + False, |
| 96 | + False, |
| 97 | + False, |
| 98 | + 0, |
| 99 | + False, |
| 100 | + False, |
| 101 | + {"split_k": 5}, |
| 102 | + ) |
| 103 | + |
| 104 | + assert flags.split_k == 5 |
| 105 | + |
| 106 | + |
| 107 | +def test_make_default_opt_flags_nvidia_split_k_constraint(monkeypatch): |
| 108 | + setup_nvidia(monkeypatch) |
| 109 | + |
| 110 | + precision_config = _DummyPrecisionConfig() |
| 111 | + flags = opt_flags.make_default_opt_flags_nvidia( |
| 112 | + torch.float16, |
| 113 | + torch.float16, |
| 114 | + torch.float16, |
| 115 | + precision_config, |
| 116 | + 4, |
| 117 | + 256, |
| 118 | + 128, |
| 119 | + 64, |
| 120 | + None, |
| 121 | + False, |
| 122 | + False, |
| 123 | + False, |
| 124 | + 0, |
| 125 | + False, |
| 126 | + False, |
| 127 | + {"split_k": 3}, |
| 128 | + ) |
| 129 | + |
| 130 | + assert flags.split_k == 3 |
| 131 | + |
| 132 | +def test_max_allowable_mn_and_split_k_constraints(monkeypatch): |
| 133 | + setup_nvidia(monkeypatch) |
| 134 | + |
| 135 | + opt_flags._opt_flags = None |
| 136 | + opt_flags.reset_opt_flags_constraints() |
| 137 | + opt_flags.update_opt_flags_constraints( |
| 138 | + { |
| 139 | + "max_allowable_mn": 256, |
| 140 | + # Without split_k, this should raise an error |
| 141 | + } |
| 142 | + ) |
| 143 | + |
| 144 | + with pytest.raises(opt_flags.InapplicableConstraint): |
| 145 | + opt_flags.make_opt_flags( |
| 146 | + torch.float16, |
| 147 | + torch.float16, |
| 148 | + torch.float16, |
| 149 | + _DummyPrecisionConfig(), |
| 150 | + 1, |
| 151 | + 256, |
| 152 | + 256, |
| 153 | + 256, |
| 154 | + None, |
| 155 | + False, |
| 156 | + False, |
| 157 | + False, |
| 158 | + 0, |
| 159 | + False, |
| 160 | + None, |
| 161 | + ) |
| 162 | + |
| 163 | +def test_max_allowable_mn(monkeypatch): |
| 164 | + setup_nvidia(monkeypatch) |
| 165 | + |
| 166 | + batch_size, m, n, k = 1, 256, 256, 256 |
| 167 | + |
| 168 | + def get_flags(split_k, max_mn): |
| 169 | + opt_flags._opt_flags = None |
| 170 | + opt_flags.reset_opt_flags_constraints() |
| 171 | + opt_flags.update_opt_flags_constraints( |
| 172 | + { |
| 173 | + "split_k": split_k, |
| 174 | + "max_allowable_mn": max_mn, |
| 175 | + } |
| 176 | + ) |
| 177 | + return opt_flags.make_opt_flags( |
| 178 | + torch.float16, |
| 179 | + torch.float16, |
| 180 | + torch.float16, |
| 181 | + _DummyPrecisionConfig(), |
| 182 | + batch_size, |
| 183 | + m, |
| 184 | + n, |
| 185 | + k, |
| 186 | + None, |
| 187 | + False, |
| 188 | + False, |
| 189 | + False, |
| 190 | + 0, |
| 191 | + False, |
| 192 | + None, |
| 193 | + ) |
| 194 | + |
| 195 | + split_k = 6 |
| 196 | + # Allowable mn is less than actual mn, so split_k should be set to 1 |
| 197 | + max_mn = (m * n) // 2 |
| 198 | + flags = get_flags(split_k, max_mn) |
| 199 | + assert flags.split_k == 1 |
| 200 | + |
| 201 | + split_k = 6 |
| 202 | + # Allowable mn is more than actual mn, so split_k should be unchanged |
| 203 | + max_mn = (m * n) * 2 |
| 204 | + flags = get_flags(split_k, max_mn) |
| 205 | + assert flags.split_k == split_k |
0 commit comments