Skip to content
Merged
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
8 changes: 4 additions & 4 deletions flashinfer/experimental/cake_kda_det_train_kcp/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"closure_sha256": "b2cecbeaa985c3caafaedabf58ec08a2cd4a5c6edcfb6b6308e751a5343664a1",
"entry": "run",
"gencode": "-gencode=arch=compute_100a,code=sm_100a",
"min_cuda": "12.8",
"min_cuda": "12.9",
"name": "cake_kda_det_train_051de8b4660b1587fc85_kcp_sm_100a",
"nvcc_flags": [
"--use_fast_math"
Expand Down Expand Up @@ -380,7 +380,7 @@
"closure_sha256": "c5a70a061085e0155c6997862348d281d06a495a338e45fbdf7ee76bad480d75",
"entry": "run",
"gencode": "-gencode=arch=compute_100a,code=sm_100a",
"min_cuda": "12.8",
"min_cuda": "12.9",
"name": "cake_kda_det_train_5e6f3ba0b9b429241ab2_kcp_sm_100a",
"nvcc_flags": [
"--use_fast_math"
Expand Down Expand Up @@ -926,7 +926,7 @@
"closure_sha256": "064c2d6958f38bcf57fe940b02456bc97dfc421888ee54f3a10a2ee1f05eb2cd",
"entry": "run",
"gencode": "-gencode=arch=compute_100a,code=sm_100a",
"min_cuda": "12.8",
"min_cuda": "12.9",
"name": "cake_kda_det_train_c811c1b52ed680d0a515_kcp_sm_100a",
"nvcc_flags": [
"--use_fast_math"
Expand Down Expand Up @@ -989,7 +989,7 @@
"closure_sha256": "a8053543882e8eead9e51e2c10044bfaa191a59acbe7c8d8a54e9cc4f5e461fd",
"entry": "run",
"gencode": "-gencode=arch=compute_100a,code=sm_100a",
"min_cuda": "12.8",
"min_cuda": "12.9",
"name": "cake_kda_det_train_f227dd21f1f3cb464b29_sm_100a",
"nvcc_flags": [
"--use_fast_math"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"closure_sha256": "523d4209f61ed5d1e055957ef2223a526058879ed660e874afd482c91a28e2f2",
"entry": "run",
"gencode": "-gencode=arch=compute_100a,code=sm_100a",
"min_cuda": "12.8",
"min_cuda": "12.9",
"name": "cake_kda_det_train_3808ea4d567b82ea21a4_kcp_sm_100a",
"nvcc_flags": [
"--use_fast_math"
Expand Down Expand Up @@ -254,7 +254,7 @@
"closure_sha256": "9e16ae94164b8e2fd4d3b09f931b81b3705a6d22fe43f7d95982c12f3dd87e60",
"entry": "run",
"gencode": "-gencode=arch=compute_100a,code=sm_100a",
"min_cuda": "12.8",
"min_cuda": "12.9",
"name": "cake_kda_det_train_9cfe05bce78410f1304b_kcp_sm_100a",
"nvcc_flags": [
"--use_fast_math"
Expand Down
19 changes: 13 additions & 6 deletions flashinfer/experimental/cake_kda_det_train_kcp/cake_kcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,16 @@ def transpose_tiles(
def _walk_form_of(call: Any, programs: Any, name: str, args: dict[str, Any]) -> str:
"""The record form of walk launch ``name`` (``"full"`` / ``"split"``): the registry's rule
(``cake_kcp_jit.walk_form``) on the launch's chains (the chunk counts of its listed segments), the call's head
count, the device's SM count and (a per-architecture rule) its architecture, under the call's ``walk_form`` knob
-- decided once per ``(program, walk list)`` of the call's table set and looked up on every later launch
(:meth:`_Tables.walk_form`); ``"full"`` with a registry that has no form rule (one record per walk program)."""
count, the device's SM count and, for a per-architecture rule, its architecture, under the call's ``walk_form``
knob -- decided once per ``(program, walk list)`` of the call's table set and looked up on every later launch
(:meth:`_Tables.walk_form`); ``"full"`` with a registry that has no form rule (one record per walk program).
The provider's ``walk_form`` is called as ``walk_form(program, chains, num_heads, sm_count, *, knob)``, plus
``arch=`` (the jit's ``device_arch`` of the launch's device) only when ``WALK_FORM_RULES[name]`` is a mapping
(a per-architecture rule): a provider whose rules are all strings needs no ``arch`` parameter."""
jit = getattr(programs, "jit", None)
rule = getattr(jit, "walk_form", None)
if rule is None or name not in getattr(jit, "WALK_FORM_RULES", {}):
rules = getattr(jit, "WALK_FORM_RULES", {})
if rule is None or name not in rules:
return "full"
return call.tb.walk_form(
name,
Expand All @@ -792,7 +796,9 @@ def _walk_form_of(call: Any, programs: Any, name: str, args: dict[str, Any]) ->
int(jit.sm_count(call.device.index)),
str(call.walk_form),
rule,
arch_of=getattr(jit, "device_arch", None),
arch_of=getattr(jit, "device_arch", None)
if isinstance(rules[name], Mapping)
else None,
device_index=call.device.index,
)

Expand Down Expand Up @@ -3442,7 +3448,8 @@ def walk_form(
"""The record form (``"full"`` / ``"split"``) of walk launch ``name`` over the segments ``walk``: the registry's
rule ``rule`` (``cake_kcp_jit.walk_form``) on the launch's chains (the chunk counts of its listed segments), the
head count ``H``, the device's SM count, the ``walk_form`` knob and the architecture ``arch_of(device_index)``
(passed to the rule where ``arch_of`` is given; read on the first launch of a key only) -- evaluated once per
(passed to the rule as ``arch=`` only where ``arch_of`` is given, which :func:`_walk_form_of` does for a
per-architecture rule alone; read on the first launch of a key only) -- evaluated once per
``(name, walk list, H, SM count, knob, rule, device index)`` of this table set and looked up on every later
launch. The rule's inputs are static per
``(ctx, H)`` (the walk lists and ``hp.seg_chunk_start`` are this set's, never mutated), so the lookup returns
Expand Down
90 changes: 84 additions & 6 deletions tests/experimental/test_cake_kda_det_train_kcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5265,8 +5265,8 @@ def test_walk_form_of_decides_once_per_table_set() -> None:
"""``_walk_form_of`` (the record form of a K1 / K2 walk launch) evaluates the registry's rule once per
``(program, walk list, H, SM count, knob, device)`` of the call's table set and looks the form up on every later
launch: the forms of a per-launch evaluation (the rule's inputs are static per ``(ctx, H)``; the device's
architecture is read on the first launch of a key only), a fresh equal list finding the same entry, the knob's
forced forms passing through; a provider without a form rule gives ``"full"``."""
architecture is read, for the per-architecture rule alone, on the first launch of a key only), a fresh equal list
finding the same entry, the knob's forced forms passing through; a provider without a form rule gives ``"full"``."""
part = kp.partition_det_kcp((0, 64 * 200, 64 * 350), 4, layout="contiguous")
ctx = kp.build_context_det_kcp(part, None, 0, "cpu", 4, sm_count=148)
tb = cake_kcp._Tables(ctx, 64)
Expand All @@ -5275,7 +5275,9 @@ def test_walk_form_of_decides_once_per_table_set() -> None:

def rule(program, chains, num_heads, sm_count, *, knob="auto", arch=None):
evaluated.append((program, tuple(chains), num_heads, sm_count, knob))
assert arch == "sm_100a"
# the architecture reaches the per-architecture rule (a mapping) alone
per_arch = not isinstance(cake_kcp_jit.WALK_FORM_RULES[program], str)
assert arch == ("sm_100a" if per_arch else None), (program, arch)
return cake_kcp_jit.walk_form(
program, chains, num_heads, sm_count, knob=knob, arch=arch
)
Expand Down Expand Up @@ -5339,9 +5341,12 @@ def call(knob: str):
for (knob, name, walk) in forms
), (len(evaluated), len(forms))
assert all(form == knob for (knob, _, _), form in forms.items() if knob != "auto")
assert len(arch_reads) == len(
evaluated
) # the architecture on a key's first launch only
assert (
len(arch_reads)
== sum(
not isinstance(cake_kcp_jit.WALK_FORM_RULES[e[0]], str) for e in evaluated
)
) # the architecture on a key's first launch only, for the per-architecture rule alone
assert {
forms[("auto", name, tuple(tb.walk_offset))]
for name in cake_kcp_jit.WALK_FORM_RULES
Expand All @@ -5355,6 +5360,79 @@ def call(knob: str):
)


def test_walk_form_of_passes_arch_to_a_per_architecture_rule_alone() -> None:
"""A provider whose ``walk_form`` takes ``(program, chains, num_heads, sm_count, *, knob)`` and no ``arch``, beside
a ``device_arch``, serves its string rules (``"fit"`` / ``"split"``) without a ``TypeError`` and without an
architecture read: ``_walk_form_of`` passes ``arch`` only for a per-architecture rule (a mapping of
``WALK_FORM_RULES``), which still receives it."""
part = kp.partition_det_kcp((0, 64 * 200, 64 * 350), 4, layout="contiguous")
ctx = kp.build_context_det_kcp(part, None, 0, "cpu", 4, sm_count=148)
tb = cake_kcp._Tables(ctx, 64)
walk = tb.walk_offset
assert walk
arch_reads: list = []

def device_arch(index):
arch_reads.append(index)
return "sm_103a"

def call():
return types.SimpleNamespace(
tb=tb, H=64, device=torch.device("cpu"), walk_form="auto"
)

old_calls: list = []

def old_rule(program, chains, num_heads, sm_count, *, knob="auto"):
old_calls.append((program, knob))
return {"kcp_fwdh_offset": "full", "kcp_fwdh_tape": "split"}[program]

old = types.SimpleNamespace(
jit=types.SimpleNamespace(
walk_form=old_rule,
WALK_FORM_RULES={"kcp_fwdh_offset": "fit", "kcp_fwdh_tape": "split"},
sm_count=lambda index: 148,
device_arch=device_arch,
)
)
assert (
cake_kcp._walk_form_of(call(), old, "kcp_fwdh_offset", {"walk": walk}) == "full"
)
assert (
cake_kcp._walk_form_of(call(), old, "kcp_fwdh_tape", {"walk": walk}) == "split"
)
assert old_calls == [("kcp_fwdh_offset", "auto"), ("kcp_fwdh_tape", "auto")]
assert arch_reads == []

seen: list = []

def rule(program, chains, num_heads, sm_count, *, knob="auto", arch=None):
seen.append((program, arch))
return cake_kcp_jit.walk_form(
program, chains, num_heads, sm_count, knob=knob, arch=arch
)

registry = types.SimpleNamespace(
jit=types.SimpleNamespace(
walk_form=rule,
WALK_FORM_RULES=cake_kcp_jit.WALK_FORM_RULES,
sm_count=lambda index: 148,
device_arch=device_arch,
)
)
seg = [int(x) for x in tb.hp.seg_chunk_start]
chains = [seg[s + 1] - seg[s] for s in walk]
for name, form_rule in cake_kcp_jit.WALK_FORM_RULES.items():
want = cake_kcp_jit.walk_form(name, chains, 64, 148, arch="sm_103a")
assert cake_kcp._walk_form_of(call(), registry, name, {"walk": walk}) == want
assert seen[-1] == (name, None if isinstance(form_rule, str) else "sm_103a")
assert (
len(arch_reads)
== sum(not isinstance(r, str) for r in cake_kcp_jit.WALK_FORM_RULES.values())
>= 1
)


class _FakeGraphRt:
"""A CUDA-runtime double of the capture-graph entry points of the kernel-node cap forms: one
active capture whose next operation depends on one kernel node; records every call."""
Expand Down
27 changes: 27 additions & 0 deletions tests/experimental/test_cake_kda_det_train_kcp_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import hashlib
import json
import re
from pathlib import Path

import pytest
Expand Down Expand Up @@ -663,6 +664,32 @@ def test_shipped_manifest_lists_the_kcp_programs_and_no_test_only_one() -> None:
assert not names & test_only


# The PTX ISA 8.8 instructions (CUDA 12.9; CUDA 12.8's ptxas refuses them): a 256-bit ld / st, a three-input
# min / max.
_PTX_ISA_8_8 = re.compile(
rb"\b(?:min|max)(?:\.\w+)*\s+%[^;,\"]+(?:,[^;,\"]+){3};"
rb"|\b(?:ld|st)(?:\.[\w:]+)*\.v8\.[bfsu]32\b"
)


@pytest.mark.parametrize("manifest", ["build.json", "build.test.json"])
def test_an_entry_whose_units_use_ptx_isa_8_8_needs_cuda_12_9(manifest: str) -> None:
"""An entry whose units use a PTX ISA 8.8 instruction (the 256-bit stores of the fused forward walk, the output
stages and the full-width tape walks) states ``min_cuda`` 12.9 or newer: CUDA 12.8 cannot build it."""
path = PACKAGE_DIR / manifest
if not path.is_file():
pytest.skip(f"{manifest} is not shipped here")
wide = []
for entry in json.loads(path.read_text())["libraries"]:
if any(
_PTX_ISA_8_8.search((PACKAGE_DIR / source).read_bytes())
for source in entry["sources"]
):
wide.append(entry["name"])
assert tuple(map(int, entry["min_cuda"].split("."))) >= (12, 9), entry
assert any(name.endswith("_sm_100a") for name in wide), wide


def test_test_manifest_is_the_test_only_programs_under_the_opt_in(
monkeypatch: pytest.MonkeyPatch,
) -> None:
Expand Down
Loading