|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import sys |
| 4 | +import types |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +from vllm.triton_utils.importing import (TritonLanguagePlaceholder, |
| 8 | + TritonPlaceholder) |
| 9 | + |
| 10 | + |
| 11 | +def test_triton_placeholder_is_module(): |
| 12 | + triton = TritonPlaceholder() |
| 13 | + assert isinstance(triton, types.ModuleType) |
| 14 | + assert triton.__name__ == "triton" |
| 15 | + |
| 16 | + |
| 17 | +def test_triton_language_placeholder_is_module(): |
| 18 | + triton_language = TritonLanguagePlaceholder() |
| 19 | + assert isinstance(triton_language, types.ModuleType) |
| 20 | + assert triton_language.__name__ == "triton.language" |
| 21 | + |
| 22 | + |
| 23 | +def test_triton_placeholder_decorators(): |
| 24 | + triton = TritonPlaceholder() |
| 25 | + |
| 26 | + @triton.jit |
| 27 | + def foo(x): |
| 28 | + return x |
| 29 | + |
| 30 | + @triton.autotune |
| 31 | + def bar(x): |
| 32 | + return x |
| 33 | + |
| 34 | + @triton.heuristics |
| 35 | + def baz(x): |
| 36 | + return x |
| 37 | + |
| 38 | + assert foo(1) == 1 |
| 39 | + assert bar(2) == 2 |
| 40 | + assert baz(3) == 3 |
| 41 | + |
| 42 | + |
| 43 | +def test_triton_placeholder_decorators_with_args(): |
| 44 | + triton = TritonPlaceholder() |
| 45 | + |
| 46 | + @triton.jit(debug=True) |
| 47 | + def foo(x): |
| 48 | + return x |
| 49 | + |
| 50 | + @triton.autotune(configs=[], key="x") |
| 51 | + def bar(x): |
| 52 | + return x |
| 53 | + |
| 54 | + @triton.heuristics( |
| 55 | + {"BLOCK_SIZE": lambda args: 128 if args["x"] > 1024 else 64}) |
| 56 | + def baz(x): |
| 57 | + return x |
| 58 | + |
| 59 | + assert foo(1) == 1 |
| 60 | + assert bar(2) == 2 |
| 61 | + assert baz(3) == 3 |
| 62 | + |
| 63 | + |
| 64 | +def test_triton_placeholder_language(): |
| 65 | + lang = TritonLanguagePlaceholder() |
| 66 | + assert isinstance(lang, types.ModuleType) |
| 67 | + assert lang.__name__ == "triton.language" |
| 68 | + assert lang.constexpr is None |
| 69 | + assert lang.dtype is None |
| 70 | + assert lang.int64 is None |
| 71 | + |
| 72 | + |
| 73 | +def test_triton_placeholder_language_from_parent(): |
| 74 | + triton = TritonPlaceholder() |
| 75 | + lang = triton.language |
| 76 | + assert isinstance(lang, TritonLanguagePlaceholder) |
| 77 | + |
| 78 | + |
| 79 | +def test_no_triton_fallback(): |
| 80 | + # clear existing triton modules |
| 81 | + sys.modules.pop("triton", None) |
| 82 | + sys.modules.pop("triton.language", None) |
| 83 | + sys.modules.pop("vllm.triton_utils", None) |
| 84 | + sys.modules.pop("vllm.triton_utils.importing", None) |
| 85 | + |
| 86 | + # mock triton not being installed |
| 87 | + with mock.patch.dict(sys.modules, {"triton": None}): |
| 88 | + from vllm.triton_utils import HAS_TRITON, tl, triton |
| 89 | + assert HAS_TRITON is False |
| 90 | + assert triton.__class__.__name__ == "TritonPlaceholder" |
| 91 | + assert triton.language.__class__.__name__ == "TritonLanguagePlaceholder" |
| 92 | + assert tl.__class__.__name__ == "TritonLanguagePlaceholder" |
0 commit comments