flashrt-structures 0.2.0__py3-none-any.whl

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.
Files changed (122) hide show
  1. flashrt_structures/__init__.py +174 -0
  2. flashrt_structures/adapters/__init__.py +55 -0
  3. flashrt_structures/adapters/diffusers_attention.py +237 -0
  4. flashrt_structures/adapters/diffusers_rotary_attention.py +239 -0
  5. flashrt_structures/adapters/factored_qk_norm_rope.py +252 -0
  6. flashrt_structures/adapters/factored_two_way_attention.py +99 -0
  7. flashrt_structures/adapters/gemma_attention.py +226 -0
  8. flashrt_structures/adapters/packed_qkv_rope.py +342 -0
  9. flashrt_structures/adapters/packed_stream_qk_norm_rope.py +376 -0
  10. flashrt_structures/adapters/qwen_per_head_qk_norm_rope.py +231 -0
  11. flashrt_structures/adapters/sglang_engine.py +207 -0
  12. flashrt_structures/adapters/transformers_attention_interface.py +73 -0
  13. flashrt_structures/adapters/transformers_gated_delta.py +152 -0
  14. flashrt_structures/adapters/transformers_gated_delta_fused.py +96 -0
  15. flashrt_structures/adapters/vllm_engine.py +424 -0
  16. flashrt_structures/adjudicate.py +85 -0
  17. flashrt_structures/aot.py +191 -0
  18. flashrt_structures/autobuild.py +2052 -0
  19. flashrt_structures/beta/__init__.py +43 -0
  20. flashrt_structures/beta/conform.py +94 -0
  21. flashrt_structures/beta/joins.py +113 -0
  22. flashrt_structures/beta/negotiate.py +84 -0
  23. flashrt_structures/beta/ports.py +140 -0
  24. flashrt_structures/decisions.py +80 -0
  25. flashrt_structures/discover.py +623 -0
  26. flashrt_structures/explain.py +70 -0
  27. flashrt_structures/frontdoor.py +572 -0
  28. flashrt_structures/gates.py +465 -0
  29. flashrt_structures/guard.py +421 -0
  30. flashrt_structures/handle.py +189 -0
  31. flashrt_structures/impls/__init__.py +219 -0
  32. flashrt_structures/impls/adaln_producer/__init__.py +8 -0
  33. flashrt_structures/impls/adaln_producer/broker.py +116 -0
  34. flashrt_structures/impls/adaln_producer/fused.py +388 -0
  35. flashrt_structures/impls/adarms_stack/__init__.py +8 -0
  36. flashrt_structures/impls/adarms_stack/fp8_chain.py +832 -0
  37. flashrt_structures/impls/adarms_stack/region.py +102 -0
  38. flashrt_structures/impls/attention_core/__init__.py +132 -0
  39. flashrt_structures/impls/attention_core/fa2_seqused.py +458 -0
  40. flashrt_structures/impls/attention_core/fa4_cute.py +154 -0
  41. flashrt_structures/impls/attention_core/fa4_fp8.py +178 -0
  42. flashrt_structures/impls/attention_core/masked_mha.py +158 -0
  43. flashrt_structures/impls/attention_core/two_way_fa2.py +220 -0
  44. flashrt_structures/impls/cadence_static/__init__.py +18 -0
  45. flashrt_structures/impls/cadence_static/buffers.py +122 -0
  46. flashrt_structures/impls/cadence_static/cross_attention.py +187 -0
  47. flashrt_structures/impls/chain_elements.py +89 -0
  48. flashrt_structures/impls/decode_loop/__init__.py +0 -0
  49. flashrt_structures/impls/decode_loop/fp8_kv.py +206 -0
  50. flashrt_structures/impls/decode_loop/mtp_speculative.py +245 -0
  51. flashrt_structures/impls/decode_loop/whole_step.py +852 -0
  52. flashrt_structures/impls/decoder_block/__init__.py +6 -0
  53. flashrt_structures/impls/decoder_block/attn_sublayer.py +110 -0
  54. flashrt_structures/impls/decoder_block/fused.py +167 -0
  55. flashrt_structures/impls/decoder_ffn/__init__.py +0 -0
  56. flashrt_structures/impls/decoder_ffn/fp8_static.py +310 -0
  57. flashrt_structures/impls/decoder_ffn/fp8_static.yaml +22 -0
  58. flashrt_structures/impls/decoder_ffn/w4a16_static.py +221 -0
  59. flashrt_structures/impls/decoder_ffn/w8a16_static.py +183 -0
  60. flashrt_structures/impls/dit_stack/__init__.py +11 -0
  61. flashrt_structures/impls/dit_stack/fp4_chain.py +417 -0
  62. flashrt_structures/impls/dit_stack/region.py +86 -0
  63. flashrt_structures/impls/fixed_iter/__init__.py +29 -0
  64. flashrt_structures/impls/fixed_iter/openpi.py +264 -0
  65. flashrt_structures/impls/fixed_iter/protocol.py +94 -0
  66. flashrt_structures/impls/gated_delta_core/__init__.py +3 -0
  67. flashrt_structures/impls/gated_delta_core/fused_layer.py +545 -0
  68. flashrt_structures/impls/gated_delta_core/hub_v3.py +152 -0
  69. flashrt_structures/impls/graph_lowering/__init__.py +27 -0
  70. flashrt_structures/impls/graph_lowering/pi052_denoise.py +179 -0
  71. flashrt_structures/impls/graph_lowering/protocol.py +76 -0
  72. flashrt_structures/impls/graph_lowering/qwen3_vl.py +364 -0
  73. flashrt_structures/impls/linear_proj/__init__.py +0 -0
  74. flashrt_structures/impls/linear_proj/fp8_static.py +270 -0
  75. flashrt_structures/impls/linear_proj/nvfp4_balance.py +131 -0
  76. flashrt_structures/impls/linear_proj/nvfp4_dynamic.py +182 -0
  77. flashrt_structures/impls/linear_proj/w8a16_static.py +230 -0
  78. flashrt_structures/impls/modnorm_qkv_chain/__init__.py +0 -0
  79. flashrt_structures/impls/modnorm_qkv_chain/fp8_ptok_table.py +291 -0
  80. flashrt_structures/impls/moe_experts/__init__.py +9 -0
  81. flashrt_structures/impls/moe_experts/nvfp4_dynamic.py +208 -0
  82. flashrt_structures/impls/moe_experts/nvfp4_w4a16.py +129 -0
  83. flashrt_structures/impls/norm_fused/__init__.py +3 -0
  84. flashrt_structures/impls/norm_fused/bf16.py +94 -0
  85. flashrt_structures/impls/norm_fused/fp8_producer.py +84 -0
  86. flashrt_structures/impls/patch_projection/__init__.py +3 -0
  87. flashrt_structures/impls/patch_projection/bf16_flat.py +139 -0
  88. flashrt_structures/impls/prefill_tower/__init__.py +10 -0
  89. flashrt_structures/impls/prefill_tower/fp8_chain.py +955 -0
  90. flashrt_structures/impls/prefill_tower/region.py +99 -0
  91. flashrt_structures/impls/qk_norm_rope/__init__.py +12 -0
  92. flashrt_structures/impls/qk_norm_rope/per_head_gqa.py +199 -0
  93. flashrt_structures/impls/qk_norm_rope/projection_bf16.py +165 -0
  94. flashrt_structures/impls/qkv_pack/__init__.py +5 -0
  95. flashrt_structures/impls/qkv_pack/bf16.py +110 -0
  96. flashrt_structures/impls/qkv_pack/fp8_static.py +435 -0
  97. flashrt_structures/impls/qkv_pack/nvfp4_balance.py +218 -0
  98. flashrt_structures/impls/qkv_rope/__init__.py +3 -0
  99. flashrt_structures/impls/qkv_rope/packed_bias_bf16.py +143 -0
  100. flashrt_structures/impls/step_table.py +113 -0
  101. flashrt_structures/impls/vision_ffn/__init__.py +0 -0
  102. flashrt_structures/impls/vision_ffn/fp8_static.py +261 -0
  103. flashrt_structures/impls/vision_ffn/nvfp4_balance.py +211 -0
  104. flashrt_structures/impls/vision_tower/__init__.py +7 -0
  105. flashrt_structures/impls/vision_tower/fp8_chain.py +533 -0
  106. flashrt_structures/impls/vision_tower/region.py +91 -0
  107. flashrt_structures/matrix.py +126 -0
  108. flashrt_structures/points.py +368 -0
  109. flashrt_structures/prequantized.py +131 -0
  110. flashrt_structures/quantize_on_adopt.py +94 -0
  111. flashrt_structures/recipe.py +438 -0
  112. flashrt_structures/regions.py +208 -0
  113. flashrt_structures/schemes.py +490 -0
  114. flashrt_structures/stages.py +298 -0
  115. flashrt_structures/storage.py +255 -0
  116. flashrt_structures/swap.py +398 -0
  117. flashrt_structures/workspace.py +110 -0
  118. flashrt_structures-0.2.0.dist-info/METADATA +140 -0
  119. flashrt_structures-0.2.0.dist-info/RECORD +122 -0
  120. flashrt_structures-0.2.0.dist-info/WHEEL +5 -0
  121. flashrt_structures-0.2.0.dist-info/licenses/LICENSE +202 -0
  122. flashrt_structures-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,208 @@
1
+ """Region adjudication: structure-level winners are receipts, not order.
2
+
3
+ A region is a span of the host larger than one seat — a DiT block's
4
+ fused chain versus its seat-by-seat composition, a single-stream
5
+ attention takeover versus factored routes. Hardware wants different
6
+ shapes here: the same pipeline's fastest form on one device is a fused
7
+ launch chain, on another the seated composition. Until now the winner
8
+ at this level was whichever adapter registered first — a global
9
+ decision nobody signed. This module gives regions the pipeline the
10
+ precision bands already have: candidates declare factual
11
+ prerequisites, a production-form measurement run records the winner
12
+ per (device, region), and every tier consumes the receipt.
13
+
14
+ The tier discipline this module enforces:
15
+
16
+ - **Automatic reads receipts only** — author pin, then the decision
17
+ cache, then the seated floor. It never experiments at bind: a cold
18
+ box runs seated (correct, possibly not full speed) until a
19
+ measurement run records a winner. A receipt that names a candidate
20
+ this box cannot qualify falls through to seated with the reason on
21
+ the trail — a stale or foreign receipt degrades speed, never
22
+ correctness.
23
+ - **Explicit is maximum host replacement** — it pins winners at the
24
+ region key and claims regions discovery refuses; it consumes the
25
+ same candidate set, so anything it proves the automatic tier can
26
+ later inherit through the cache.
27
+ - **Every candidate is assembled from structure primitives** — seats,
28
+ producers, workspace leases, guards. A form that replaces a forward
29
+ wholesale with hand-written code has no seat here: it cannot carry
30
+ the ledger, the fallback contract, or the revert path, so there is
31
+ nothing for a receipt to certify.
32
+
33
+ Measurement itself is not in this module on purpose. A region winner
34
+ is a captured-form end-to-end number (seat-level micro-timing was
35
+ refuted in both directions); the measuring harness lives with the
36
+ family that owns the region and writes its result through
37
+ :func:`record`.
38
+ """
39
+
40
+ from __future__ import annotations
41
+
42
+ import os
43
+ import re
44
+ from dataclasses import dataclass, field
45
+ from typing import Any, Callable, Sequence
46
+
47
+ from . import decisions
48
+
49
+ #: the floor candidate, always in play: the region stays with the seat
50
+ #: scan's per-seat composition. Every resolution can land here, so the
51
+ #: worst case of a wrong, stale, or missing receipt is today's
52
+ #: behavior, never a crash and never a half-bound region.
53
+ SEATED = "seated"
54
+
55
+
56
+ @dataclass
57
+ class RegionCandidate:
58
+ """One structural form a region can take, with its prerequisites.
59
+
60
+ ``missing`` reports the factual prerequisites this box does not
61
+ meet — hub symbols absent, shapes outside the form's band, a
62
+ memory plan the allocator refuses. An empty report qualifies the
63
+ candidate; anything else disqualifies it and the report lands on
64
+ the trail verbatim. ``bind`` routes the form onto one region
65
+ occurrence and returns the adapter-result dict ({observed, revert,
66
+ ...}); it runs under the attach transaction like any adapter.
67
+ """
68
+
69
+ name: str
70
+ missing: Callable[[], Sequence[str]] = lambda: ()
71
+ bind: Callable[..., Any] | None = None
72
+ #: tie-break inside a measurement's win margin: lower = higher
73
+ #: precision (same convention as form adjudication)
74
+ precision_rank: int = 0
75
+
76
+
77
+ @dataclass
78
+ class RegionFamily:
79
+ """A region kind: how to find its occurrences, and its forms.
80
+
81
+ ``identify`` matches structural signatures on the module graph
82
+ (shape of the subtree, never class or model names) and returns the
83
+ qualified names of the region roots it claims.
84
+ """
85
+
86
+ family: str
87
+ identify: Callable[[Any], Sequence[str]]
88
+ candidates: list[RegionCandidate] = field(default_factory=list)
89
+
90
+ def candidate(self, name: str) -> RegionCandidate | None:
91
+ for cand in self.candidates:
92
+ if cand.name == name:
93
+ return cand
94
+ return None
95
+
96
+
97
+ _FAMILIES: dict[str, RegionFamily] = {}
98
+
99
+
100
+ def register_region_family(fam: RegionFamily) -> None:
101
+ """Register (or replace) the family owning a region kind."""
102
+ _FAMILIES[fam.family] = fam
103
+
104
+
105
+ def family(name: str) -> RegionFamily:
106
+ return _FAMILIES[name]
107
+
108
+
109
+ def registered() -> tuple[RegionFamily, ...]:
110
+ return tuple(_FAMILIES.values())
111
+
112
+
113
+ def _pin_env(fam: str) -> str:
114
+ return "FRT_REGION_" + re.sub(r"[^A-Za-z0-9]", "_", fam).upper()
115
+
116
+
117
+ def structural_signature(module) -> str:
118
+ """A host-structure fingerprint that scopes receipts to a host.
119
+
120
+ Two hosts on one box can carry the same region kind with different
121
+ winners (a 300M expert stack and a 2B one answer the same
122
+ identifier), so a receipt keyed by device and family alone would
123
+ leak across them. The fingerprint is structural — stack class,
124
+ depth, and the head layer's first projection widths — never a
125
+ model name: identical structures share receipts by construction,
126
+ which is exactly the transportability the cache promises.
127
+ """
128
+ import torch
129
+
130
+ parts = [type(module).__name__]
131
+ layers = getattr(module, "layers", None)
132
+ if isinstance(layers, torch.nn.ModuleList) and len(layers):
133
+ parts.append(f"L{len(layers)}")
134
+ for _name, sub in layers[0].named_modules():
135
+ if isinstance(sub, torch.nn.Linear):
136
+ parts.append(f"{sub.in_features}x{sub.out_features}")
137
+ break
138
+ return "-".join(parts)
139
+
140
+
141
+ def resolve(family_name: str, *, host_sig: str | None = None,
142
+ notes: dict | None = None) -> tuple[str, str]:
143
+ """Which form does this box run for this region kind, and why.
144
+
145
+ Author pin > decision cache > seated, with one discipline on top:
146
+ a pinned or cached name must be a known candidate that qualifies
147
+ on this box *right now* — otherwise it falls through, the reason
148
+ lands on the trail, and the next source is consulted. The return
149
+ is ``(winner, source)`` where source is one of ``pin``, ``cache``,
150
+ ``default``; the full fall-through trail goes to
151
+ ``notes["regions"]``.
152
+ """
153
+ fam = _FAMILIES[family_name]
154
+ fell_through: list[dict] = []
155
+ winner, source = SEATED, "default"
156
+ pin = os.environ.get(_pin_env(family_name))
157
+ # host-scoped receipt first; the unscoped key stays readable so
158
+ # every receipt measured before scoping existed keeps working
159
+ cached = (decisions.lookup(f"region:{family_name}@{host_sig}")
160
+ if host_sig else None)
161
+ if cached is None:
162
+ cached = decisions.lookup(f"region:{family_name}")
163
+ for src, name in (("pin", pin), ("cache", cached)):
164
+ if not name:
165
+ continue
166
+ if name == SEATED:
167
+ winner, source = SEATED, src
168
+ break
169
+ cand = fam.candidate(name)
170
+ if cand is None:
171
+ fell_through.append(
172
+ {"source": src, "name": name,
173
+ "reason": "unknown_candidate"})
174
+ continue
175
+ gaps = list(cand.missing())
176
+ if gaps:
177
+ fell_through.append(
178
+ {"source": src, "name": name,
179
+ "reason": f"missing: {', '.join(map(str, gaps))}"})
180
+ continue
181
+ winner, source = name, src
182
+ break
183
+ if notes is not None:
184
+ notes.setdefault("regions", []).append(
185
+ {"family": family_name, "winner": winner, "source": source,
186
+ "host_sig": host_sig, "fell_through": fell_through})
187
+ return winner, source
188
+
189
+
190
+ def record(family_name: str, winner: str, times_ms: dict,
191
+ host_sig: str | None = None):
192
+ """Write a measured region winner into the decision cache.
193
+
194
+ The receipt is what the automatic tier will obey unquestioningly,
195
+ so the door is strict: the family must be registered and the
196
+ winner must be ``seated`` or one of its declared candidates — a
197
+ typo'd measurement run must fail here, at the writer, not poison
198
+ every later bind at the reader.
199
+ """
200
+ fam = _FAMILIES[family_name]
201
+ if winner != SEATED and fam.candidate(winner) is None:
202
+ raise ValueError(
203
+ f"'{winner}' is not a candidate of region family "
204
+ f"'{family_name}' (have: "
205
+ f"{[c.name for c in fam.candidates]} + '{SEATED}')")
206
+ key = (f"region:{family_name}@{host_sig}" if host_sig
207
+ else f"region:{family_name}")
208
+ return decisions.record(key, winner, times_ms)