File size: 8,102 Bytes
cc4bd23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "domain": "com.microsoft",
  "name": "GemmaRotaryEmbedding",
  "sinceVersion": 1,
  "description": "Fuses the Gemma rotary-embedding tail: computes `sin` and `cos` from float32 `emb`, casts them to float16, then evaluates `q * cos + q_rot * sin` and the corresponding expression for `k`. `emb` has shape `(batch, seq, dim)` and is broadcast over the head axis of the `(batch, heads, seq, dim)` operands. Each product is rounded to float16 before the addition.",
  "inputs": [
    {
      "role": "emb",
      "dtype": "U",
      "rank": 3,
      "description": "Rotary angles with shape `(batch_size, seq_len, dim)`, shared by every head."
    },
    {
      "role": "q",
      "dtype": "T",
      "rank": 4,
      "description": "Query state with shape `(batch_size, num_heads, seq_len, dim)`."
    },
    { "role": "q_rot", "dtype": "T", "rank": 4, "description": "Half-rotated query state, same shape as `q`." },
    { "role": "k", "dtype": "T", "rank": 4, "description": "Key state, same shape as `q`." },
    { "role": "k_rot", "dtype": "T", "rank": 4, "description": "Half-rotated key state, same shape as `q`." }
  ],
  "outputs": [
    {
      "role": "output1",
      "dtype": "T",
      "rank": "ranks.qT",
      "shape": "shapes.qT",
      "description": "Rotary-embedded query, same shape as `q`."
    },
    {
      "role": "output2",
      "dtype": "T",
      "rank": "ranks.qT",
      "shape": "shapes.qT",
      "description": "Rotary-embedded key, same shape as `q`."
    }
  ],
  "typeConstraints": { "T": ["float16"], "U": ["float32"] },
  "args": {
    "embT": { "kind": "tensor", "semantic": "emb", "role": "input", "dtype": "float32" },
    "qT": { "kind": "tensor", "semantic": "q", "role": "input" },
    "qRotT": { "kind": "tensor", "semantic": "q_rot", "role": "input" },
    "kT": { "kind": "tensor", "semantic": "k", "role": "input" },
    "kRotT": { "kind": "tensor", "semantic": "k_rot", "role": "input" },
    "output1T": { "kind": "tensor", "semantic": "output1", "role": "output" },
    "output2T": { "kind": "tensor", "semantic": "output2", "role": "output" }
  },
  "tunables": { "workgroupSize": 256 },
  "derive": {
    "batchSize": "dim(shapes.qT, 0)",
    "numHeads": "dim(shapes.qT, 1)",
    "seqLen": "dim(shapes.qT, 2)",
    "headDim": "dim(shapes.qT, 3)",
    "sameAsQ": "ranks.qRotT == 4 and ranks.kT == 4 and ranks.kRotT == 4 and sameShape(shapes.qRotT, shapes.qT) and sameShape(shapes.kT, shapes.qT) and sameShape(shapes.kRotT, shapes.qT)",
    "dtypesMatch": "tensorDtypes.qRotT == tensorDtypes.qT and tensorDtypes.kT == tensorDtypes.qT and tensorDtypes.kRotT == tensorDtypes.qT and tensorDtypes.output1T == tensorDtypes.qT and tensorDtypes.output2T == tensorDtypes.qT",
    "contract": "ranks.qT == 4 and ranks.embT == 3 and sameAsQ and dtypesMatch and tensorDtypes.qT == \"float16\" and f16Ok(dtypes.T) and tensorDtypes.embT == \"float32\" and dim(shapes.embT, 0) == batchSize and dim(shapes.embT, 1) == seqLen and dim(shapes.embT, 2) == headDim and sameShape(shapes.output1T, shapes.qT) and sameShape(shapes.output2T, shapes.qT)",
    "vec4Ok": "headDim % 4 == 0"
  },
  "bindingSets": {
    "scalar": [
      {
        "name": "emb",
        "arg": "embT",
        "semantic": "emb",
        "buffer": { "type": "read-only-storage" },
        "elementType": "f32"
      },
      { "name": "q", "arg": "qT", "semantic": "q", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
      {
        "name": "q_rot",
        "arg": "qRotT",
        "semantic": "q_rot",
        "buffer": { "type": "read-only-storage" },
        "elementType": "$scalar"
      },
      { "name": "k", "arg": "kT", "semantic": "k", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
      {
        "name": "k_rot",
        "arg": "kRotT",
        "semantic": "k_rot",
        "buffer": { "type": "read-only-storage" },
        "elementType": "$scalar"
      },
      {
        "name": "output1",
        "arg": "output1T",
        "semantic": "output1",
        "buffer": { "type": "storage" },
        "elementType": "$scalar"
      },
      {
        "name": "output2",
        "arg": "output2T",
        "semantic": "output2",
        "buffer": { "type": "storage" },
        "elementType": "$scalar"
      },
      {
        "name": "params",
        "semantic": "kernel.params",
        "buffer": { "type": "uniform" },
        "struct": {
          "name": "Params",
          "fields": [
            { "name": "count", "type": "u32", "value": "numel(shapes.qT)" },
            { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim)" },
            { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim)" }
          ]
        }
      }
    ],
    "vec4": [
      {
        "name": "emb",
        "arg": "embT",
        "semantic": "emb",
        "buffer": { "type": "read-only-storage" },
        "elementType": "vec4<f32>"
      },
      { "name": "q", "arg": "qT", "semantic": "q", "buffer": { "type": "read-only-storage" }, "elementType": "$vector" },
      {
        "name": "q_rot",
        "arg": "qRotT",
        "semantic": "q_rot",
        "buffer": { "type": "read-only-storage" },
        "elementType": "$vector"
      },
      { "name": "k", "arg": "kT", "semantic": "k", "buffer": { "type": "read-only-storage" }, "elementType": "$vector" },
      {
        "name": "k_rot",
        "arg": "kRotT",
        "semantic": "k_rot",
        "buffer": { "type": "read-only-storage" },
        "elementType": "$vector"
      },
      {
        "name": "output1",
        "arg": "output1T",
        "semantic": "output1",
        "buffer": { "type": "storage" },
        "elementType": "$vector"
      },
      {
        "name": "output2",
        "arg": "output2T",
        "semantic": "output2",
        "buffer": { "type": "storage" },
        "elementType": "$vector"
      },
      {
        "name": "params",
        "semantic": "kernel.params",
        "buffer": { "type": "uniform" },
        "struct": {
          "name": "Params",
          "fields": [
            { "name": "count", "type": "u32", "value": "numel(shapes.qT) / 4" },
            { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim) / 4" },
            { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim) / 4" }
          ]
        }
      }
    ]
  },
  "variants": [
    {
      "id": "vec4",
      "priority": 10,
      "requires": { "features": ["shader-f16"] },
      "when": ["contract", "vec4Ok", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"],
      "constants": {
        "vec4": true,
        "scalar": "dtypes.T",
        "vector": "\"vec4<f16>\"",
        "workgroupSize": "tunables.workgroupSize"
      },
      "passes": [
        {
          "id": "main",
          "name": "GemmaRotaryEmbedding.Vec4",
          "shader": "gemma-rotary-embedding.wgsl.jinja",
          "bindings": "vec4",
          "dispatch": { "threads": "numel(shapes.qT) / 4", "workgroupSize": "constants.workgroupSize" }
        }
      ]
    },
    {
      "id": "scalar",
      "priority": 0,
      "requires": { "features": ["shader-f16"] },
      "when": ["contract", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"],
      "constants": {
        "vec4": false,
        "scalar": "dtypes.T",
        "vector": "\"vec4<f16>\"",
        "workgroupSize": "tunables.workgroupSize"
      },
      "passes": [
        {
          "id": "main",
          "name": "GemmaRotaryEmbedding",
          "shader": "gemma-rotary-embedding.wgsl.jinja",
          "bindings": "scalar",
          "dispatch": { "threads": "max(1, numel(shapes.qT))", "workgroupSize": "constants.workgroupSize" }
        }
      ]
    }
  ]
}