|
| 1 | +import functools |
| 2 | +import importlib.util |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +pytest.importorskip("clang.cindex") |
| 9 | + |
| 10 | + |
| 11 | +@functools.lru_cache(maxsize=1) |
| 12 | +def _load_generator(): |
| 13 | + script = Path(__file__).parents[1] / "scripts" / "generate_wrappers.py" |
| 14 | + spec = importlib.util.spec_from_file_location("generate_wrappers", script) |
| 15 | + module = importlib.util.module_from_spec(spec) |
| 16 | + spec.loader.exec_module(module) |
| 17 | + |
| 18 | + return module |
| 19 | + |
| 20 | + |
| 21 | +def _generate_binding(op_name, tmp_path, monkeypatch, source): |
| 22 | + generator = _load_generator() |
| 23 | + src_dir = tmp_path / "src" |
| 24 | + base_dir = src_dir / "base" |
| 25 | + base_dir.mkdir(parents=True) |
| 26 | + (base_dir / f"{op_name}.h").write_text(source) |
| 27 | + monkeypatch.setattr(generator, "_SRC_DIR", src_dir) |
| 28 | + monkeypatch.setattr(generator, "_BASE_DIR", base_dir) |
| 29 | + operator = generator._OperatorExtractor()(op_name) |
| 30 | + |
| 31 | + return generator._generate_pybind11(operator) |
| 32 | + |
| 33 | + |
| 34 | +def test_mha_varlen_fwd_requires_out_binding(tmp_path, monkeypatch): |
| 35 | + text = _generate_binding( |
| 36 | + "mha_varlen_fwd", |
| 37 | + tmp_path, |
| 38 | + monkeypatch, |
| 39 | + """ |
| 40 | +#include <cstdint> |
| 41 | +#include <optional> |
| 42 | +
|
| 43 | +namespace infini::ops { |
| 44 | +
|
| 45 | +struct Tensor {}; |
| 46 | +
|
| 47 | +template <typename T> |
| 48 | +class Operator {}; |
| 49 | +
|
| 50 | +class MhaVarlenFwd : public Operator<MhaVarlenFwd> { |
| 51 | + public: |
| 52 | + MhaVarlenFwd(const Tensor q, const Tensor k, const Tensor v, Tensor out, |
| 53 | + const Tensor cu_seqlens_q, const Tensor cu_seqlens_k, |
| 54 | + std::optional<Tensor> block_table, float softmax_scale, |
| 55 | + bool is_causal, int64_t num_splits = 0) {} |
| 56 | +
|
| 57 | + virtual void operator()(const Tensor q, const Tensor k, const Tensor v, |
| 58 | + Tensor out, const Tensor cu_seqlens_q, |
| 59 | + const Tensor cu_seqlens_k, |
| 60 | + std::optional<Tensor> block_table, |
| 61 | + float softmax_scale, bool is_causal, |
| 62 | + int64_t num_splits = 0) const = 0; |
| 63 | +}; |
| 64 | +
|
| 65 | +} // namespace infini::ops |
| 66 | +""", |
| 67 | + ) |
| 68 | + |
| 69 | + assert 'py::arg("out"), py::arg("cu_seqlens_q")' in text |
| 70 | + assert 'py::arg("num_splits") = 0' in text |
| 71 | + assert 'py::arg("out") = py::none()' not in text |
| 72 | + assert "std::optional<py::object> out" not in text |
| 73 | + assert "OptionalTensorFromPybind11Handle(out)" not in text |
| 74 | + |
| 75 | + |
| 76 | +def test_mha_fwd_kvcache_requires_out_binding(tmp_path, monkeypatch): |
| 77 | + text = _generate_binding( |
| 78 | + "mha_fwd_kvcache", |
| 79 | + tmp_path, |
| 80 | + monkeypatch, |
| 81 | + """ |
| 82 | +#include <cstdint> |
| 83 | +#include <optional> |
| 84 | +
|
| 85 | +namespace infini::ops { |
| 86 | +
|
| 87 | +struct Tensor {}; |
| 88 | +
|
| 89 | +template <typename T> |
| 90 | +class Operator {}; |
| 91 | +
|
| 92 | +class MhaFwdKvcache : public Operator<MhaFwdKvcache> { |
| 93 | + public: |
| 94 | + MhaFwdKvcache(const Tensor q, const Tensor kcache, const Tensor vcache, |
| 95 | + std::optional<Tensor> k, std::optional<Tensor> v, Tensor out, |
| 96 | + float softmax_scale, bool is_causal, |
| 97 | + int64_t num_splits = 0) {} |
| 98 | +
|
| 99 | + virtual void operator()(const Tensor q, const Tensor kcache, |
| 100 | + const Tensor vcache, std::optional<Tensor> k, |
| 101 | + std::optional<Tensor> v, Tensor out, |
| 102 | + float softmax_scale, bool is_causal, |
| 103 | + int64_t num_splits = 0) const = 0; |
| 104 | +}; |
| 105 | +
|
| 106 | +} // namespace infini::ops |
| 107 | +""", |
| 108 | + ) |
| 109 | + |
| 110 | + assert 'py::arg("out"), py::arg("softmax_scale")' in text |
| 111 | + assert 'py::arg("num_splits") = 0' in text |
| 112 | + assert 'py::arg("out") = py::none()' not in text |
| 113 | + assert "std::optional<py::object> out" not in text |
| 114 | + assert "OptionalTensorFromPybind11Handle(out)" not in text |
0 commit comments