1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Rope — rotary position embedding with caller-supplied cos/sin
//! tables. Increment C slice 1, T6 — the fifth op migrated to a portable
//! `PatternNode` DATA recipe (after SoftmaxLastDim), and the first to carry
//! `DimExpr` slice offsets (the rotate-half split).
//!
//! Provides:
//! - [`entry`] — the metadata-side `FusedOpEntry` (decompose function,
//! pattern matcher, shape/dtype rules).
//! - `recipe` — the op's primitive subgraph as portable, shape-/rank-
//! polymorphic data (9 nodes; the two leading-1 rank-pad `Reshape`s are
//! materialized by the emit resolver on a rank-raise, D4, not baked here).
//! - [`decompose`] — re-emits `recipe` through the
//! [`crate::registry::decompose_via_recipe`] bridge (mirrors
//! [`crate::NodeHandle::rope_with_tables_decomposed`]).
//! - [`canonical_pattern`] — placeholder returning `None`. The Rope
//! decomposition is structurally large (slice/concat + per-axis broadcast
//! prep); until a canonical matcher lands, fusion fires only through the
//! builder, never through pattern recognition.
//!
//! The decomposition is provided so backends without a native Rope
//! kernel can synthesize from primitives (today every backend has
//! one, but the lowering rule is wired regardless for completeness
//! and so cross-checks against the primitive path remain available).
use crate;
use crate::;
use ;
use ;
use ;
use OnceLock;
/// Metadata-side registry entry for Rope.
/// Shape rule: Rope preserves the x input's shape (input 0).
/// Dtype rule: Rope preserves the x input's dtype (input 0).
/// Rope's primitive recipe as **portable data** (Increment C slice 1, T6 —
/// the fifth op migrated, and the first with `DimExpr` slice offsets). Shape-
/// AND rank-polymorphic: the two cos/sin broadcasts target `SameAs { operand:
/// 0 }` (x's shape over the Bind space, D2); the rotate-half split is two
/// last-axis `Slice`s whose start/len are `DimExpr`s over x's last extent
/// `E = Extent { operand: 0, axis: LAST }` — the reference-doc worked example:
///
/// ```text
/// first_half = Slice(axis_last, start = Const(0), len = E / 2)(x)
/// second_half = Slice(axis_last, start = E / 2, len = E − E / 2)(x)
/// ```
///
/// (the `Sub` remainder form is exact for the even `E` Rope requires, and is
/// the general last-axis-remainder spelling). Nothing in the datum bakes a
/// shape or a rank.
///
/// The datum is **9 nodes**; its EMISSION is byte-identical to the legacy
/// 11-node imperative form wherever the broadcast RANK-RAISES (the real
/// attention consumer — cos/sin `[seq, d]`, x `[.., seq, d]` rank ≥ 3): the
/// emit resolver materializes the two leading-1-padded prep `Reshape`s (D4).
/// At EQUAL rank (x itself `[seq, d]`) it emits the leaner 9-node form,
/// eliding legacy's no-op `Reshape([seq,d] → [seq,d])` — numerically
/// identical. Binds: `0 = x`, `1 = cos`, `2 = sin`.
///
/// ```text
/// cos_bcast = BroadcastTo(SameAs 0)(cos) # +Reshape pad on rank-raise
/// sin_bcast = BroadcastTo(SameAs 0)(sin) # +Reshape pad on rank-raise
/// first_half = Slice(axis_last, 0, E/2)(x)
/// second_half = Slice(axis_last, E/2, E−E/2)(x)
/// rotated = Concat(axis_last)(Neg(second_half), first_half)
/// left = Mul(x, cos_bcast)
/// right = Mul(rotated, sin_bcast)
/// out = Add(left, right)
/// ```
/// Per-entry scalar projection: Rope is parameterless (its cos/sin tables are
/// INPUTS, not baked scalars), so the right payload projects to ZERO open-slot
/// scalars and any other payload is a typed decline (`None` ⇒ the bridge
/// returns the node unchanged — G2).
/// Lower a fused Rope node to its primitive subgraph and return the new root
/// id — since T6 a re-emit of `recipe`'s data through the
/// [`decompose_via_recipe`] bridge (the fused node's three inputs are the
/// binds `[x, cos, sin]`; the resolving emit derives every interior
/// shape/dtype and materializes the D4 rank-pad `Reshape`s). Any failure —
/// wrong params payload, a resolution decline at these shapes (symbolic
/// extent, …) — returns `id` (fixpoint, surfaced gap, never a panic): exactly
/// the G2 posture the imperative body had.
///
/// The fused node `id` may be either `Op::Rope` (legacy emission) or
/// `Op::Fused(FusedOps::ROPE, FusedOpParams::Rope)` (the builder path); the
/// decomposition is identical for both. Mirrors
/// [`crate::NodeHandle::rope_with_tables_decomposed`].
/// Placeholder matcher: returns `None` for every input. The Rope
/// decomposition is structurally large (slice/concat/reshape +
/// per-axis broadcast prep), and the migration bar is "the registry entry
/// exists, builder emits Op::Fused, dispatch works." A canonical
/// matcher recognizing the full pattern + single-consumer
/// guards is follow-up work. Until then this reads as a one-way
/// migration: builder→fused works, hand-built decomposed forms (e.g.
/// `rope_with_tables_decomposed`) stay decomposed.