hyperonnx 2.0.0__tar.gz → 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/PKG-INFO +1 -1
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/__init__.py +1 -1
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/bundle.py +198 -50
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/capture.py +55 -14
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass.py +13 -4
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass_kernels/config.py +21 -7
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass_kernels/conv.py +48 -17
- hyperonnx-2.1.0/hyperonnx/compile/cutlass_kernels/mm.py +808 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass_kernels/run.py +82 -31
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/testing.py +153 -51
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/typing.py +26 -7
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/exporter/_onnxscript.py +29 -2
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/exporter/_onnxscript_legacy.py +12 -3
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/exporter/torchscript.py +23 -2
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/function_rewriter.py +25 -15
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/hyper_export.py +70 -22
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/transformers/cache.py +8 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/transformers/recurrent.py +3 -0
- hyperonnx-2.1.0/hyperonnx/typing.py +163 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/pyproject.toml +4 -4
- hyperonnx-2.0.0/hyperonnx/compile/cutlass_kernels/autotune.py +0 -158
- hyperonnx-2.0.0/hyperonnx/compile/cutlass_kernels/mm.py +0 -211
- hyperonnx-2.0.0/hyperonnx/typing.py +0 -85
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/LICENSE +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/README.md +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/auto.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/__init__.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass_bundle.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass_kernels/__init__.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/cutlass_kernels/extract.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/grid_ast.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/compile/inductor.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/exporter/__init__.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/exporter/dynamo.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/exporter/utils.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/torch_export.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/transformers/__init__.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/transformers/attention.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/transformers/mamba.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/transformers/patch.py +0 -0
- {hyperonnx-2.0.0 → hyperonnx-2.1.0}/hyperonnx/utils.py +0 -0
|
@@ -19,6 +19,9 @@ import ast as pyast
|
|
|
19
19
|
import importlib
|
|
20
20
|
import json
|
|
21
21
|
import re
|
|
22
|
+
from collections import Counter
|
|
23
|
+
from collections.abc import Mapping
|
|
24
|
+
from itertools import groupby
|
|
22
25
|
from pathlib import Path
|
|
23
26
|
from typing import TYPE_CHECKING, Any
|
|
24
27
|
|
|
@@ -120,8 +123,9 @@ def _contiguous_span(shape: list) -> int:
|
|
|
120
123
|
return max(1, n)
|
|
121
124
|
|
|
122
125
|
|
|
123
|
-
def _registry_span(entry:
|
|
124
|
-
"""Storage span a
|
|
126
|
+
def _registry_span(entry: Mapping[str, Any]) -> int:
|
|
127
|
+
"""Storage span (in elements) a layout dict demands — registry entry or
|
|
128
|
+
graph-table meta (same shape/stride keys)."""
|
|
125
129
|
shape = entry.get("shape")
|
|
126
130
|
if not shape:
|
|
127
131
|
return 0
|
|
@@ -133,6 +137,21 @@ def _registry_span(entry: BufferEntry) -> int:
|
|
|
133
137
|
return _contiguous_span(shape)
|
|
134
138
|
|
|
135
139
|
|
|
140
|
+
_DTYPE_SIZE = {
|
|
141
|
+
"bool": 1,
|
|
142
|
+
"int8": 1,
|
|
143
|
+
"uint8": 1,
|
|
144
|
+
"float16": 2,
|
|
145
|
+
"bfloat16": 2,
|
|
146
|
+
"float32": 4,
|
|
147
|
+
"int32": 4,
|
|
148
|
+
"float64": 8,
|
|
149
|
+
"int64": 8,
|
|
150
|
+
}
|
|
151
|
+
"""Element width per dtype string; unknown dtypes fall back to 4 bytes
|
|
152
|
+
(replay's own ``getattr(torch, dtype, torch.float32)`` fallback)."""
|
|
153
|
+
|
|
154
|
+
|
|
136
155
|
def _static_arg_descriptor(arg: Any, table: dict[str, dict], owner: str) -> dict:
|
|
137
156
|
"""Build a unified ``KernelArgDescriptor`` from a static wrapper arg.
|
|
138
157
|
|
|
@@ -329,11 +348,16 @@ def _finalize_extern_step(step: dict, table: dict[str, dict]) -> None:
|
|
|
329
348
|
if meta.get("alias_of") == out_name or meta.get("view_of") == out_name:
|
|
330
349
|
out_meta = meta
|
|
331
350
|
break
|
|
332
|
-
|
|
351
|
+
out_desc: dict = {
|
|
333
352
|
"name": out_name,
|
|
334
353
|
"buffer_id": (out_meta or {}).get("buffer_id"),
|
|
335
354
|
"direction": "out",
|
|
336
355
|
}
|
|
356
|
+
if out_meta:
|
|
357
|
+
for key in ("shape", "stride", "dtype"):
|
|
358
|
+
if key in out_meta:
|
|
359
|
+
out_desc[key] = out_meta[key]
|
|
360
|
+
step["output"] = out_desc
|
|
337
361
|
|
|
338
362
|
|
|
339
363
|
def _finalize_as_strided_step(step: dict, table: dict[str, dict]) -> None:
|
|
@@ -366,6 +390,7 @@ def _finalize_pipeline(
|
|
|
366
390
|
pipeline: list[dict],
|
|
367
391
|
kernel_entries: list[KernelEntry],
|
|
368
392
|
runtime_buffers: "list[BufferInfo]",
|
|
393
|
+
symbol_to_traces: "dict[str, list[LaunchTraceEntry]] | None" = None,
|
|
369
394
|
) -> list[dict]:
|
|
370
395
|
"""Merge the wrapper pipeline with kernel entries and validate buffers.
|
|
371
396
|
|
|
@@ -383,10 +408,15 @@ def _finalize_pipeline(
|
|
|
383
408
|
(shape, dtype), attaching ``buffer_id`` on match; undefined names
|
|
384
409
|
are flagged and warned about.
|
|
385
410
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
(carrying ``buffer_id``\\s)
|
|
389
|
-
|
|
411
|
+
When ``symbol_to_traces`` is given (per-symbol queues of call-site
|
|
412
|
+
traces, in launch order), every ``triton_kernel`` step consumes its
|
|
413
|
+
own call-site trace: the trace's ``args`` (carrying ``buffer_id``\\s)
|
|
414
|
+
seed the table and become the step's launch descriptors, and its
|
|
415
|
+
grid/warps/shared-mem overlay the step's ``launch`` — so repeated
|
|
416
|
+
call sites of one symbol each keep their own buffers and launch
|
|
417
|
+
config. Steps past the end of their queue fall back to table
|
|
418
|
+
resolution (``launch_missing``). Without traces, each entry's
|
|
419
|
+
overlaid ``args`` seed only the first occurrence of their symbol.
|
|
390
420
|
|
|
391
421
|
Mismatches are logged and annotated, never fatal.
|
|
392
422
|
"""
|
|
@@ -449,6 +479,16 @@ def _finalize_pipeline(
|
|
|
449
479
|
if stype == "extern_kernel":
|
|
450
480
|
views, vbuf_seq = _hoist_reinterpret_views(step, table, vbuf_seq)
|
|
451
481
|
kept.extend(views)
|
|
482
|
+
# Extern outputs have no allocate line (aten allocates
|
|
483
|
+
# internally); register the name from the step's serialized
|
|
484
|
+
# layout so the table (and executors) know the contract.
|
|
485
|
+
out_name = step.get("output")
|
|
486
|
+
existing = table.get(out_name) if isinstance(out_name, str) else None
|
|
487
|
+
if existing is None or existing.get("undefined"):
|
|
488
|
+
meta = {
|
|
489
|
+
k: step[k] for k in ("shape", "stride", "dtype") if k in step
|
|
490
|
+
}
|
|
491
|
+
table[out_name] = {**meta, "kind": "extern_out"}
|
|
452
492
|
if stype == "triton_kernel":
|
|
453
493
|
entry = symbol_to_entry.get(step["kernel"])
|
|
454
494
|
step["_static_args"] = step.pop("args")
|
|
@@ -463,25 +503,39 @@ def _finalize_pipeline(
|
|
|
463
503
|
|
|
464
504
|
# Resolve graph inputs by FX placeholder target (exact name match).
|
|
465
505
|
_map_graph_inputs_by_source(table, runtime_buffers, rt_claimed)
|
|
466
|
-
# Seed name→buffer_id
|
|
467
|
-
# of
|
|
468
|
-
#
|
|
506
|
+
# Seed name→buffer_id per call site. With launch traces, every
|
|
507
|
+
# occurrence of a symbol consumes its own trace (in launch order),
|
|
508
|
+
# so repeated call sites each seed their own buffer ids and launch
|
|
509
|
+
# config. Without traces, only the first occurrence of each symbol
|
|
510
|
+
# is seeded from the entry args; later call sites resolve via the
|
|
511
|
+
# table.
|
|
469
512
|
seeded_symbols: set[str] = set()
|
|
470
513
|
for step in kept:
|
|
471
514
|
if step["type"] != "triton_kernel" or "cubin" not in step:
|
|
472
515
|
continue
|
|
473
|
-
entry = symbol_to_entry.get(step["kernel"])
|
|
474
|
-
entry_args = entry["args"] if entry else None
|
|
475
516
|
kernel_sym = step["kernel"]
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
517
|
+
entry = symbol_to_entry.get(kernel_sym)
|
|
518
|
+
trace: LaunchTraceEntry | None = None
|
|
519
|
+
if symbol_to_traces is not None:
|
|
520
|
+
queue = symbol_to_traces.get(kernel_sym)
|
|
521
|
+
trace = queue.pop(0) if queue else None
|
|
522
|
+
launch_args = trace.args if trace is not None else None
|
|
523
|
+
elif kernel_sym in seeded_symbols:
|
|
524
|
+
launch_args = None
|
|
525
|
+
else:
|
|
526
|
+
seeded_symbols.add(kernel_sym)
|
|
527
|
+
launch_args = (entry["args"] if entry else None) or None
|
|
528
|
+
step["_launch_args"] = launch_args or None
|
|
529
|
+
if trace is not None and entry is not None:
|
|
530
|
+
# Per-call-site launch config — a later call site of the
|
|
531
|
+
# same symbol may launch with a different grid.
|
|
532
|
+
launch = dict(entry["launch"])
|
|
533
|
+
_overlay_launch(launch, trace)
|
|
534
|
+
step["launch"] = launch
|
|
535
|
+
if not launch_args:
|
|
482
536
|
continue
|
|
483
537
|
static = step["_static_args"]
|
|
484
|
-
for i, ka in enumerate(
|
|
538
|
+
for i, ka in enumerate(launch_args):
|
|
485
539
|
if ka.get("kind") != "tensor":
|
|
486
540
|
continue
|
|
487
541
|
ka_bid = ka.get("buffer_id")
|
|
@@ -493,6 +547,14 @@ def _finalize_pipeline(
|
|
|
493
547
|
if "buffer_id" not in meta:
|
|
494
548
|
meta["buffer_id"] = ka_bid
|
|
495
549
|
rt_claimed.add(ka_bid)
|
|
550
|
+
# Backfill layout from the runtime launch arg — the
|
|
551
|
+
# strides the compiled kernel actually indexes with
|
|
552
|
+
# (e.g. a channels-last extern conv output) are more
|
|
553
|
+
# authoritative than a layout-free table entry.
|
|
554
|
+
ka_shape, ka_stride = ka.get("shape"), ka.get("stride")
|
|
555
|
+
if ka_shape and ka_stride and not meta.get("stride"):
|
|
556
|
+
meta["shape"] = list(ka_shape)
|
|
557
|
+
meta["stride"] = list(ka_stride)
|
|
496
558
|
|
|
497
559
|
# Alias/view pairs share storage: propagate buffer_id bidirectionally.
|
|
498
560
|
for name, meta in table.items():
|
|
@@ -587,51 +649,127 @@ def _reconcile_registry_with_allocate(
|
|
|
587
649
|
a too-small shape. The full-size ``[2,…]`` shape exists only in the
|
|
588
650
|
graph-table ``allocate`` entry (from inductor IR ``buf.get_size()``).
|
|
589
651
|
|
|
590
|
-
For every graph-table
|
|
591
|
-
``buffer_id``, compute the
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
652
|
+
For every graph-table ``allocate``/``view`` entry with a resolved
|
|
653
|
+
``buffer_id``, compute the layout's storage span *in bytes* (one
|
|
654
|
+
storage can host allocations of different dtypes across its lifetime —
|
|
655
|
+
e.g. an f32 scratch followed by an fp16 output on the same id — so an
|
|
656
|
+
element-span compare can under-size the storage); if the registry entry
|
|
657
|
+
covers fewer bytes, overwrite its shape with a contiguous layout big
|
|
658
|
+
enough for the widest use, so replay allocates enough storage for every
|
|
659
|
+
view the kernels take. The registry dtype follows the LAST allocate on
|
|
660
|
+
the id (the final writer — the output materialization reads through
|
|
661
|
+
it).
|
|
595
662
|
|
|
596
663
|
This is the only position with full information: launch-trace-side
|
|
597
664
|
capture cannot observe allocations that never appear as kernel args.
|
|
598
665
|
Mutates ``registry`` in place.
|
|
599
666
|
"""
|
|
600
667
|
by_id: dict[int, BufferEntry] = {b["id"]: b for b in registry}
|
|
668
|
+
need_bytes: dict[int, int] = {}
|
|
601
669
|
for graph in pipeline:
|
|
602
670
|
table = graph.get("buffers", {})
|
|
603
671
|
for meta in table.values():
|
|
604
|
-
if meta.get("kind")
|
|
672
|
+
if meta.get("kind") not in ("allocate", "view"):
|
|
605
673
|
continue
|
|
606
674
|
bid = meta.get("buffer_id")
|
|
607
|
-
if bid is None:
|
|
608
|
-
continue
|
|
609
675
|
entry = by_id.get(bid)
|
|
610
676
|
if entry is None:
|
|
611
677
|
continue
|
|
612
|
-
|
|
613
|
-
if not
|
|
678
|
+
span = _registry_span(meta)
|
|
679
|
+
if not span:
|
|
680
|
+
continue
|
|
681
|
+
size = _DTYPE_SIZE.get(meta.get("dtype") or entry.get("dtype", ""), 4)
|
|
682
|
+
need_bytes[bid] = max(need_bytes.get(bid, 0), span * size)
|
|
683
|
+
if meta.get("kind") != "allocate":
|
|
614
684
|
continue
|
|
615
685
|
alloc_dtype = meta.get("dtype")
|
|
616
686
|
if alloc_dtype and entry.get("dtype") != alloc_dtype:
|
|
617
687
|
entry["dtype"] = alloc_dtype
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
if
|
|
621
|
-
s = _layout_span(alloc_shape, alloc_stride)
|
|
622
|
-
alloc_span = s if s is not None else _contiguous_span(alloc_shape)
|
|
623
|
-
else:
|
|
624
|
-
alloc_span = _contiguous_span(alloc_shape)
|
|
625
|
-
if alloc_span <= _registry_span(entry):
|
|
688
|
+
# Both sides scale by the same (synced) dtype width → bytes
|
|
689
|
+
# cancel; cross-dtype under-sizing is caught by need_bytes below.
|
|
690
|
+
if span <= _registry_span(entry):
|
|
626
691
|
continue
|
|
627
|
-
# Overwrite: registry must hold at least the allocate
|
|
628
|
-
entry["shape"] = [int(x) for x in
|
|
629
|
-
|
|
692
|
+
# Overwrite: registry must hold at least the allocate's bytes.
|
|
693
|
+
entry["shape"] = [int(x) for x in meta["shape"]]
|
|
694
|
+
alloc_stride = meta.get("stride")
|
|
695
|
+
if alloc_stride and len(alloc_stride) == len(meta["shape"]):
|
|
630
696
|
entry["stride"] = [int(x) for x in alloc_stride]
|
|
631
697
|
elif "stride" in entry:
|
|
632
698
|
# Old stride was for a smaller shape; drop it so replay
|
|
633
699
|
# falls back to a contiguous view of the new shape.
|
|
634
700
|
del entry["stride"]
|
|
701
|
+
# Views / wider-dtype uses no single allocate layout covered (e.g. an
|
|
702
|
+
# f32 scratch on storage whose frozen shape is fp16): grow flat.
|
|
703
|
+
for bid, entry in by_id.items():
|
|
704
|
+
size = _DTYPE_SIZE.get(entry.get("dtype", ""), 4)
|
|
705
|
+
if need_bytes.get(bid, 0) <= _registry_span(entry) * size:
|
|
706
|
+
continue
|
|
707
|
+
entry["shape"] = [-(-need_bytes[bid] // size)]
|
|
708
|
+
entry.pop("stride", None)
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
def _launch_fingerprint(entry: "LaunchTraceEntry") -> tuple:
|
|
712
|
+
"""Arg identity of a launch: tensor args contribute their ``buffer_id``,
|
|
713
|
+
scalar args their ``value``. Benchmark reps of a call site share its
|
|
714
|
+
fingerprint; a launch on different buffers starts a new segment."""
|
|
715
|
+
return tuple(
|
|
716
|
+
("tensor", a.get("buffer_id"))
|
|
717
|
+
if a.get("kind") == "tensor"
|
|
718
|
+
else ("scalar", a.get("value"))
|
|
719
|
+
for a in entry.args
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
def _overlay_launch(launch: dict, trace: "LaunchTraceEntry") -> None:
|
|
724
|
+
"""Overlay a trace's per-call-site launch config onto a launch dict."""
|
|
725
|
+
launch["captured_grid"] = list(trace.grid)
|
|
726
|
+
launch["shared_mem_bytes"] = trace.shared_mem
|
|
727
|
+
launch["num_warps"] = trace.num_warps
|
|
728
|
+
launch["num_scratch_args"] = trace.num_scratch_args
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
def _call_site_traces(
|
|
732
|
+
all_launches: "list[LaunchTraceEntry]", step_counts: dict[str, int]
|
|
733
|
+
) -> "list[LaunchTraceEntry]":
|
|
734
|
+
"""Extract the per-call-site production launches from the raw stream.
|
|
735
|
+
|
|
736
|
+
The autotuner fires a benchmark storm before the first call site's
|
|
737
|
+
production launch; later call sites of the same kernel reuse the
|
|
738
|
+
cached winner and launch once each — possibly right after the
|
|
739
|
+
previous site (no other symbol in between), so one call site is not
|
|
740
|
+
one consecutive symbol run. Instead, split each consecutive
|
|
741
|
+
same-symbol run into fingerprint segments: the last launch of every
|
|
742
|
+
segment is a production candidate (benchmark reps share their site's
|
|
743
|
+
fingerprint and precede its production launch inside the segment),
|
|
744
|
+
and the pipeline's per-symbol step count selects the trailing
|
|
745
|
+
candidates — dropping pure-benchmark segments (scratch-buffer
|
|
746
|
+
benchmarks of a site whose production launch carries different
|
|
747
|
+
args). The trailing-n selection spans ALL runs of a symbol (a symbol
|
|
748
|
+
recurs as separate runs whenever other symbols interleave between its
|
|
749
|
+
call sites): selecting per run instead would keep a leading
|
|
750
|
+
storm candidate and shift every site onto the wrong trace. Symbols
|
|
751
|
+
with no pipeline steps contribute no traces.
|
|
752
|
+
"""
|
|
753
|
+
cands_by_symbol: dict[str, list[LaunchTraceEntry]] = {}
|
|
754
|
+
for symbol, grp in groupby(all_launches, key=lambda e: e.symbol):
|
|
755
|
+
run = list(grp)
|
|
756
|
+
cands: list[LaunchTraceEntry] = []
|
|
757
|
+
prev_fp: tuple | None = None
|
|
758
|
+
prev: LaunchTraceEntry | None = None
|
|
759
|
+
for e in run:
|
|
760
|
+
fp = _launch_fingerprint(e)
|
|
761
|
+
if prev is not None and fp != prev_fp:
|
|
762
|
+
cands.append(prev)
|
|
763
|
+
prev_fp, prev = fp, e
|
|
764
|
+
if prev is not None:
|
|
765
|
+
cands.append(prev)
|
|
766
|
+
cands_by_symbol.setdefault(symbol, []).extend(cands)
|
|
767
|
+
entries: list[LaunchTraceEntry] = []
|
|
768
|
+
for symbol, cands in cands_by_symbol.items():
|
|
769
|
+
n = step_counts.get(symbol, 0)
|
|
770
|
+
if n:
|
|
771
|
+
entries.extend(cands[-n:])
|
|
772
|
+
return entries
|
|
635
773
|
|
|
636
774
|
|
|
637
775
|
def write_kernel_bundle(
|
|
@@ -680,15 +818,27 @@ def write_kernel_bundle(
|
|
|
680
818
|
bundle_dir.mkdir(parents=True, exist_ok=True)
|
|
681
819
|
|
|
682
820
|
symbol_to_trace: dict[str, LaunchTraceEntry] = {}
|
|
821
|
+
symbol_to_traces: dict[str, list[LaunchTraceEntry]] = {}
|
|
683
822
|
winning_keys: set[str] | None = None
|
|
684
823
|
winning_hashes: set[str] = set()
|
|
685
824
|
if launch_trace is not None:
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
825
|
+
if wrapper_graph:
|
|
826
|
+
# Per-call-site traces, in launch order: one per pipeline
|
|
827
|
+
# triton step of the symbol (see _call_site_traces).
|
|
828
|
+
step_counts = Counter(
|
|
829
|
+
step["kernel"]
|
|
830
|
+
for graph in wrapper_graph
|
|
831
|
+
for step in graph["steps"]
|
|
832
|
+
if step.get("type") == "triton_kernel" and step.get("kernel")
|
|
833
|
+
)
|
|
834
|
+
traces = _call_site_traces(launch_trace.all_launches, step_counts)
|
|
835
|
+
else:
|
|
836
|
+
traces = launch_trace.entries
|
|
837
|
+
for e in traces:
|
|
838
|
+
symbol_to_trace.setdefault(e.symbol, e)
|
|
839
|
+
symbol_to_traces.setdefault(e.symbol, []).append(e)
|
|
840
|
+
winning_keys = {f"{e.symbol}|{e.shared_mem}|{e.num_warps}" for e in traces}
|
|
841
|
+
winning_hashes = {e.kernel_hash for e in traces if e.kernel_hash}
|
|
692
842
|
|
|
693
843
|
entries: list[KernelEntry] = []
|
|
694
844
|
for k in kernels:
|
|
@@ -723,10 +873,7 @@ def write_kernel_bundle(
|
|
|
723
873
|
|
|
724
874
|
trace: LaunchTraceEntry | None = symbol_to_trace.get(k["symbol"])
|
|
725
875
|
if trace is not None:
|
|
726
|
-
launch
|
|
727
|
-
launch["shared_mem_bytes"] = trace.shared_mem
|
|
728
|
-
launch["num_warps"] = trace.num_warps
|
|
729
|
-
launch["num_scratch_args"] = trace.num_scratch_args
|
|
876
|
+
_overlay_launch(launch, trace)
|
|
730
877
|
args = trace.args
|
|
731
878
|
|
|
732
879
|
entries.append(
|
|
@@ -772,6 +919,7 @@ def write_kernel_bundle(
|
|
|
772
919
|
wrapper_graph,
|
|
773
920
|
entries,
|
|
774
921
|
launch_trace.buffers if launch_trace is not None else [],
|
|
922
|
+
symbol_to_traces if launch_trace is not None else None,
|
|
775
923
|
)
|
|
776
924
|
else:
|
|
777
925
|
manifest["pipeline"] = [
|
|
@@ -291,6 +291,20 @@ def _storage_span(shape: tuple[int, ...], stride: tuple[int, ...]) -> int:
|
|
|
291
291
|
return n
|
|
292
292
|
|
|
293
293
|
|
|
294
|
+
def _is_contiguous(shape: tuple[int, ...], stride: tuple[int, ...]) -> bool:
|
|
295
|
+
"""True when ``stride`` is empty or exactly row-major for ``shape``."""
|
|
296
|
+
if not stride:
|
|
297
|
+
return True
|
|
298
|
+
if len(stride) != len(shape):
|
|
299
|
+
return False
|
|
300
|
+
acc = 1
|
|
301
|
+
expect = [0] * len(shape)
|
|
302
|
+
for i in range(len(shape) - 1, -1, -1):
|
|
303
|
+
expect[i] = acc
|
|
304
|
+
acc *= int(shape[i])
|
|
305
|
+
return tuple(int(s) for s in stride) == tuple(expect)
|
|
306
|
+
|
|
307
|
+
|
|
294
308
|
def _line_type_name(line: Any) -> str:
|
|
295
309
|
"""``KernelCallLine`` → ``kernel_call``; ``AllocateLine`` → ``allocate``."""
|
|
296
310
|
name = type(line).__name__
|
|
@@ -410,6 +424,12 @@ def _serialize_wrapper_line(line: Any) -> dict | None:
|
|
|
410
424
|
step["output"] = node.get_name()
|
|
411
425
|
step["args"] = _json_safe(list(node.codegen_args()))
|
|
412
426
|
step["kwargs"] = _json_safe(list(node.codegen_kwargs()))
|
|
427
|
+
# Output layout contract: extern outputs (aten-allocated, no
|
|
428
|
+
# allocate line) must carry shape/stride/dtype in the manifest
|
|
429
|
+
# or downstream consumers' layout expectations are lost.
|
|
430
|
+
meta = _serialize_buffer(node)
|
|
431
|
+
if meta:
|
|
432
|
+
step.update(meta)
|
|
413
433
|
except Exception: # pylint: disable=broad-except
|
|
414
434
|
step["detail"] = repr(node)
|
|
415
435
|
return step
|
|
@@ -629,7 +649,10 @@ class LaunchTraceSink:
|
|
|
629
649
|
with a *larger* storage span (e.g. the full allocation is passed to
|
|
630
650
|
a kernel after an earlier smaller view was seen). In that case the
|
|
631
651
|
recorded layout grows to cover the larger span so the registry
|
|
632
|
-
reflects the true allocation size.
|
|
652
|
+
reflects the true allocation size. An equal-span resight also
|
|
653
|
+
upgrades the layout when the frozen one was layout-free (empty or
|
|
654
|
+
row-major) and the resight carries real strides (e.g. a
|
|
655
|
+
channels-last conv output first sighted as a flat view).
|
|
633
656
|
"""
|
|
634
657
|
if data_ptr not in self._ptr_to_buf:
|
|
635
658
|
buf = BufferInfo(
|
|
@@ -645,7 +668,18 @@ class LaunchTraceSink:
|
|
|
645
668
|
self._next_id += 1
|
|
646
669
|
return buf.buffer_id
|
|
647
670
|
buf = self._ptr_to_buf[data_ptr]
|
|
648
|
-
|
|
671
|
+
new_span = _storage_span(shape, stride)
|
|
672
|
+
old_span = _storage_span(buf.shape, buf.stride)
|
|
673
|
+
if new_span > old_span or (
|
|
674
|
+
new_span == old_span
|
|
675
|
+
and stride
|
|
676
|
+
and not _is_contiguous(shape, stride)
|
|
677
|
+
and _is_contiguous(buf.shape, buf.stride)
|
|
678
|
+
):
|
|
679
|
+
# Equal-span upgrade: the frozen layout carried no layout
|
|
680
|
+
# information (empty/row-major — same span as any view of the
|
|
681
|
+
# storage), while the resight does (e.g. channels-last). Adopt
|
|
682
|
+
# it so the registry records the layout kernels index with.
|
|
649
683
|
buf.shape = shape
|
|
650
684
|
buf.stride = stride
|
|
651
685
|
return buf.buffer_id
|
|
@@ -671,6 +705,7 @@ class LaunchTraceSink:
|
|
|
671
705
|
kind=kind,
|
|
672
706
|
dtype=dtype,
|
|
673
707
|
shape=tuple(shape),
|
|
708
|
+
stride=tuple(stride),
|
|
674
709
|
name=name,
|
|
675
710
|
buffer_id=self._next_id,
|
|
676
711
|
)
|
|
@@ -717,19 +752,25 @@ class LaunchTraceSink:
|
|
|
717
752
|
self._ptr_to_buf[ptr].kind = "output"
|
|
718
753
|
|
|
719
754
|
def finalize(self) -> None:
|
|
720
|
-
"""
|
|
721
|
-
|
|
722
|
-
Triton's autotuner
|
|
723
|
-
|
|
724
|
-
|
|
755
|
+
"""Collapse autotune runs, keeping one entry per call site.
|
|
756
|
+
|
|
757
|
+
Triton's autotuner benchmarks its configs back-to-back before a
|
|
758
|
+
call site's production launch, then launches the winner once and
|
|
759
|
+
the wrapper moves on to the next kernel — so one call site is
|
|
760
|
+
one consecutive run of its symbol, and the production launch is
|
|
761
|
+
the run's last entry. Benchmark runs may carry different
|
|
762
|
+
(scratch) buffers than production, so neither config nor args
|
|
763
|
+
identify the winner; position does. A symbol launched at
|
|
764
|
+
multiple call sites keeps one entry per call site, in launch
|
|
765
|
+
order, so the bundle can seed each pipeline occurrence from its
|
|
766
|
+
own trace.
|
|
725
767
|
"""
|
|
726
|
-
seen: set[str] = set()
|
|
727
768
|
winners: list[LaunchTraceEntry] = []
|
|
728
|
-
for entry in
|
|
729
|
-
if
|
|
730
|
-
|
|
769
|
+
for entry in self.all_launches:
|
|
770
|
+
if winners and winners[-1].symbol == entry.symbol:
|
|
771
|
+
winners[-1] = entry
|
|
772
|
+
else:
|
|
731
773
|
winners.append(entry)
|
|
732
|
-
winners.reverse()
|
|
733
774
|
self.entries = winners
|
|
734
775
|
|
|
735
776
|
def vendor_lib_gaps(self) -> list[BufferInfo]:
|
|
@@ -778,8 +819,8 @@ def capture_launch_trace(
|
|
|
778
819
|
input_kwargs: keyword input tensors (optional).
|
|
779
820
|
|
|
780
821
|
Yields:
|
|
781
|
-
LaunchTraceSink with ``.entries`` (
|
|
782
|
-
``.buffers`` (all device buffers seen).
|
|
822
|
+
LaunchTraceSink with ``.entries`` (one launch record per call
|
|
823
|
+
site) and ``.buffers`` (all device buffers seen).
|
|
783
824
|
|
|
784
825
|
Limitation: only triton launches via ``StaticallyLaunchedCudaKernel.run``
|
|
785
826
|
are observed. Ops inductor delegates to a vendor library (cuDNN convs,
|
|
@@ -99,7 +99,7 @@ def annotate_cutlass_config(
|
|
|
99
99
|
if op_filter and op_short not in op_filter:
|
|
100
100
|
continue
|
|
101
101
|
|
|
102
|
-
# Idempotency: skip already-tuned steps
|
|
102
|
+
# Idempotency: skip already-tuned steps (null counts as tuned)
|
|
103
103
|
if "cutlass_config" in step:
|
|
104
104
|
continue
|
|
105
105
|
|
|
@@ -110,16 +110,25 @@ def annotate_cutlass_config(
|
|
|
110
110
|
|
|
111
111
|
try:
|
|
112
112
|
args = step.get("args", [])
|
|
113
|
-
config = tuner(args, buffers, arch)
|
|
113
|
+
config, bench = tuner(args, buffers, arch, kwargs=step.get("kwargs"))
|
|
114
|
+
# ``cutlass_config`` always carries the best CUTLASS config
|
|
115
|
+
# (a naive fallback when none is eligible). ``cutlass_bench``
|
|
116
|
+
# records cuBLAS vs CUTLASS timings and ``winner``; the runner
|
|
117
|
+
# picks the kernel from ``winner`` at replay time.
|
|
114
118
|
step["cutlass_config"] = config.to_dict()
|
|
115
|
-
|
|
119
|
+
step["cutlass_bench"] = bench
|
|
120
|
+
if not config.naive:
|
|
121
|
+
tuned += 1
|
|
116
122
|
except Exception as exc:
|
|
117
123
|
warning(f"CUTLASS tuning failed for {kernel_name}: {exc}")
|
|
118
124
|
continue
|
|
119
125
|
|
|
120
126
|
if tuned > 0:
|
|
121
127
|
manifest_path.write_text(json.dumps(manifest, indent=2))
|
|
122
|
-
info(
|
|
128
|
+
info(
|
|
129
|
+
f"annotated {tuned} extern_kernel step(s) with CUTLASS config "
|
|
130
|
+
f"(loser steps keep their cuBLAS/cuDNN call, see cutlass_bench)"
|
|
131
|
+
)
|
|
123
132
|
else:
|
|
124
133
|
debug("no extern_kernel steps annotated")
|
|
125
134
|
|
|
@@ -28,6 +28,15 @@ class CutlassConfig:
|
|
|
28
28
|
tile_k: int
|
|
29
29
|
num_stages: int
|
|
30
30
|
num_warps: int
|
|
31
|
+
naive: bool = False
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def m_warps(self) -> int:
|
|
35
|
+
return max(1, self.num_warps // 2)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def n_warps(self) -> int:
|
|
39
|
+
return max(1, self.num_warps // self.m_warps)
|
|
31
40
|
|
|
32
41
|
def to_dict(self) -> dict:
|
|
33
42
|
return asdict(self)
|
|
@@ -40,19 +49,24 @@ class CutlassConfig:
|
|
|
40
49
|
tile_k=d["tile_k"],
|
|
41
50
|
num_stages=d["num_stages"],
|
|
42
51
|
num_warps=d["num_warps"],
|
|
52
|
+
naive=d.get("naive", False),
|
|
43
53
|
)
|
|
44
54
|
|
|
45
55
|
|
|
46
56
|
MM_CONFIGS: list[CutlassConfig] = [
|
|
47
|
-
CutlassConfig(128,
|
|
48
|
-
CutlassConfig(64, 128, 32, 2, 2),
|
|
49
|
-
CutlassConfig(256, 128, 64, 4, 4),
|
|
57
|
+
CutlassConfig(128, 128, 32, 4, 4),
|
|
50
58
|
CutlassConfig(128, 128, 64, 3, 4),
|
|
51
|
-
CutlassConfig(
|
|
52
|
-
CutlassConfig(128,
|
|
59
|
+
CutlassConfig(128, 256, 32, 4, 8),
|
|
60
|
+
CutlassConfig(128, 256, 32, 3, 8),
|
|
61
|
+
CutlassConfig(128, 128, 32, 3, 4),
|
|
62
|
+
CutlassConfig(128, 128, 32, 4, 8),
|
|
53
63
|
]
|
|
54
64
|
|
|
55
65
|
CONV_CONFIGS: list[CutlassConfig] = [
|
|
56
|
-
CutlassConfig(128, 128,
|
|
57
|
-
CutlassConfig(
|
|
66
|
+
CutlassConfig(128, 128, 32, 3, 4),
|
|
67
|
+
CutlassConfig(128, 256, 32, 3, 8),
|
|
58
68
|
]
|
|
69
|
+
|
|
70
|
+
# Fallback config kept in the manifest when no tiled CUTLASS kernel can run
|
|
71
|
+
# (fp32, unaligned shapes). The bench record still documents cuBLAS winning.
|
|
72
|
+
NAIVE_CONFIG = CutlassConfig(32, 32, 8, 1, 1, naive=True)
|