File size: 5,187 Bytes
cc4bd23
 
 
 
1092006
 
 
 
 
 
cc4bd23
1092006
 
 
 
 
 
cc4bd23
 
 
 
 
 
 
 
1092006
cc4bd23
1092006
cc4bd23
 
 
 
1092006
cc4bd23
1092006
cc4bd23
 
 
 
 
 
 
 
 
 
1092006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc4bd23
 
 
 
 
 
 
1092006
cc4bd23
 
 
 
 
 
 
 
 
 
1092006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "domain": "com.microsoft",
  "name": "GemmaRotaryEmbedding",
  "sinceVersion": 1,
  "inputs": {
    "embT": { "onnx": "emb", "dtype": "U", "rank": 3, "storage": "float32" },
    "qT": { "onnx": "q", "dtype": "T", "rank": 4 },
    "qRotT": { "onnx": "q_rot", "dtype": "T", "rank": 4 },
    "kT": { "onnx": "k", "dtype": "T", "rank": 4 },
    "kRotT": { "onnx": "k_rot", "dtype": "T", "rank": 4 }
  },
  "outputs": {
    "output1T": { "onnx": "output1", "dtype": "T", "rank": "ranks.qT", "shape": "shapes.qT" },
    "output2T": { "onnx": "output2", "dtype": "T", "rank": "ranks.qT", "shape": "shapes.qT" }
  },
  "typeConstraints": { "T": ["float16"], "U": ["float32"] },
  "tunables": { "workgroupSize": { "default": 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": "(seqLen * headDim) % 4 == 0"
  },
  "when": ["contract", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"],
  "variants": [
    {
      "id": "vec4",
      "priority": 10,
      "when": ["vec4Ok"],
      "requires": { "features": ["shader-f16"] },
      "derive": {
        "vec4": true,
        "scalar": "dtypes.T",
        "vector": "\"vec4<f16>\"",
        "workgroupSize": "tunables.workgroupSize"
      },
      "passes": [
        {
          "id": "main",
          "name": "GemmaRotaryEmbedding.Vec4",
          "shader": "gemma-rotary-embedding.wgsl.jinja",
          "bindings": [
            { "arg": "embT", "name": "emb", "elementType": "vec4<f32>" },
            { "arg": "qT", "name": "q", "elementType": "$vector" },
            { "arg": "qRotT", "name": "q_rot", "elementType": "$vector" },
            { "arg": "kT", "name": "k", "elementType": "$vector" },
            { "arg": "kRotT", "name": "k_rot", "elementType": "$vector" },
            { "arg": "output1T", "name": "output1", "elementType": "$vector" },
            { "arg": "output2T", "name": "output2", "elementType": "$vector" },
            {
              "name": "params",
              "struct": [
                { "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" }
              ]
            }
          ],
          "dispatch": {
            "x": "min(ceilDiv((numel(shapes.qT) / 4), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((numel(shapes.qT) / 4), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    },
    {
      "id": "scalar",
      "priority": 0,
      "requires": { "features": ["shader-f16"] },
      "derive": {
        "vec4": false,
        "scalar": "dtypes.T",
        "vector": "\"vec4<f16>\"",
        "workgroupSize": "tunables.workgroupSize"
      },
      "passes": [
        {
          "id": "main",
          "name": "GemmaRotaryEmbedding",
          "shader": "gemma-rotary-embedding.wgsl.jinja",
          "bindings": [
            { "arg": "embT", "name": "emb" },
            { "arg": "qT", "name": "q", "elementType": "$scalar" },
            { "arg": "qRotT", "name": "q_rot", "elementType": "$scalar" },
            { "arg": "kT", "name": "k", "elementType": "$scalar" },
            { "arg": "kRotT", "name": "k_rot", "elementType": "$scalar" },
            { "arg": "output1T", "name": "output1", "elementType": "$scalar" },
            { "arg": "output2T", "name": "output2", "elementType": "$scalar" },
            {
              "name": "params",
              "struct": [
                { "name": "count", "type": "u32", "value": "numel(shapes.qT)" },
                { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim)" },
                { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim)" }
              ]
            }
          ],
          "dispatch": {
            "x": "min(ceilDiv((max(1, numel(shapes.qT))), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((max(1, numel(shapes.qT))), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    }
  ]
}