File size: 7,449 Bytes
5ce7237
 
 
 
296e50f
 
8929c9c
296e50f
 
 
 
5ce7237
296e50f
 
 
 
 
 
 
5ce7237
 
 
 
 
 
 
 
296e50f
5ce7237
296e50f
 
5ce7237
 
296e50f
 
 
5ce7237
 
296e50f
8929c9c
296e50f
5ce7237
8929c9c
296e50f
8929c9c
 
 
 
 
296e50f
 
 
 
8929c9c
 
 
296e50f
 
 
 
 
8929c9c
 
296e50f
 
8929c9c
 
 
296e50f
 
 
8929c9c
 
 
296e50f
 
 
 
8929c9c
 
 
 
296e50f
5ce7237
 
 
8929c9c
 
296e50f
5ce7237
8929c9c
 
5ce7237
 
 
 
 
8929c9c
 
 
 
296e50f
8929c9c
 
296e50f
 
5ce7237
8929c9c
 
5ce7237
 
8929c9c
296e50f
5ce7237
 
 
 
 
 
 
8929c9c
296e50f
8929c9c
 
296e50f
 
 
 
 
5ce7237
 
 
 
 
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
{
  "domain": "com.microsoft",
  "name": "MRotaryEmbedding",
  "sinceVersion": 1,
  "inputs": {
    "x": { "onnx": "input", "dtype": "T" },
    "positionIds": { "onnx": "position_ids", "dtype": "M", "rank": 3, "storage": "int32", "narrowing": "saturating" },
    "cos": { "onnx": "cos_cache", "dtype": "T", "rank": 2 },
    "sin": { "onnx": "sin_cache", "dtype": "T", "rank": 2 }
  },
  "outputs": { "y": { "onnx": "output", "dtype": "T", "rank": "ranks.x", "shape": "shapes.x" } },
  "attributes": {
    "interleaved": { "default": 0 },
    "is_packed_batching": { "default": 0 },
    "mrope_layout": { "default": 0 },
    "num_heads": { "default": 0 },
    "rotary_embedding_dim": { "default": 0 },
    "scale": { "default": 1 },
    "mrope_section": {}
  },
  "attributeConstraints": {
    "interleaved": { "values": [0, 1] },
    "is_packed_batching": { "values": [0] },
    "mrope_layout": { "values": [0, 1] },
    "mrope_section": { "required": true }
  },
  "typeConstraints": { "T": ["float32", "float16"], "M": ["int64"] },
  "tunables": { "WORKGROUP_SIZE": { "default": 256 } },
  "derive": {
    "rank3HeadSize": "dim(shapes.x, 2) / attrs.num_heads if attrs.num_heads is defined and attrs.num_heads > 0 else 0",
    "headSize": "rank3HeadSize if ranks.x == 3 else dim(shapes.x, 3)",
    "effectiveRotaryDim": "attrs.rotary_embedding_dim if attrs.rotary_embedding_dim != 0 else headSize",
    "tasksPerHead": "ceilDiv(headSize, 2)",
    "pairCount": "(numel(shapes.x) / max(1, headSize)) * tasksPerHead",
    "pairDispatchOk": "ceilDiv(pairCount, tunables.WORKGROUP_SIZE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) * min(device.limits.maxComputeWorkgroupsPerDimension, 65535)",
    "halfRotaryDim": "dim(shapes.cos, 1)",
    "sectionsDefined": "attrs.mrope_section is defined and (attrs.mrope_section | length) == 3",
    "sectionsValid": "sectionsDefined and attrs.mrope_section[0] >= 0 and attrs.mrope_section[1] >= 0 and attrs.mrope_section[2] >= 0 and attrs.mrope_section[0] + attrs.mrope_section[1] + attrs.mrope_section[2] == halfRotaryDim",
    "commonContract": "f16Ok(dtypes.T) and sameShape(shapes.x, shapes.y) and (attrs.interleaved == 0 or attrs.interleaved == 1) and (attrs.mrope_layout == 0 or attrs.mrope_layout == 1) and attrs.num_heads >= 0 and attrs.rotary_embedding_dim >= 0 and (attrs.rotary_embedding_dim == 0 or attrs.num_heads > 0) and sameShape(shapes.cos, shapes.sin) and ranks.cos == 2 and ranks.sin == 2 and sectionsValid and ranks.positionIds == 3 and dim(shapes.positionIds, 0) == 3 and dim(shapes.positionIds, 1) == dim(shapes.x, 0) and dim(shapes.positionIds, 2) == dim(shapes.x, 1 if ranks.x == 3 else 2)",
    "rank3Contract": "commonContract and ranks.x == 3 and ranks.y == 3 and attrs.num_heads is defined and attrs.num_heads >= 1 and dim(shapes.x, 2) % attrs.num_heads == 0 and (rank3HeadSize > 0 or numel(shapes.x) == 0) and effectiveRotaryDim % 2 == 0 and (attrs.rotary_embedding_dim == 0 or attrs.rotary_embedding_dim <= rank3HeadSize) and halfRotaryDim * 2 == effectiveRotaryDim",
    "rank4Contract": "commonContract and ranks.x == 4 and ranks.y == 4 and dim(shapes.x, 3) > 0 and effectiveRotaryDim % 2 == 0 and (attrs.rotary_embedding_dim == 0 or attrs.rotary_embedding_dim <= dim(shapes.x, 3)) and halfRotaryDim * 2 == effectiveRotaryDim"
  },
  "when": ["pairDispatchOk", "rank3Contract or rank4Contract"],
  "bindings": {
    "x": { "elementType": "$T" },
    "position_ids": { "arg": "positionIds", "elementType": "i32" },
    "cos_cache": { "arg": "cos", "elementType": "$T" },
    "sin_cache": { "arg": "sin", "elementType": "$T" },
    "y": { "elementType": "$T" },
    "params": {
      "struct": [
        { "name": "pairCount", "type": "u32", "value": "pairCount" },
        { "name": "batchSize", "type": "u32", "value": "dim(shapes.x, 0)" },
        { "name": "sequenceLength", "type": "u32", "value": "dim(shapes.x, 1 if ranks.x == 3 else 2)" },
        { "name": "numHeads", "type": "u32", "value": "attrs.num_heads if ranks.x == 3 else dim(shapes.x, 1)" },
        { "name": "headSize", "type": "u32", "value": "headSize" },
        { "name": "rotaryDim", "type": "u32", "value": "halfRotaryDim * 2" },
        { "name": "halfRotaryDim", "type": "u32", "value": "halfRotaryDim" },
        { "name": "section0", "type": "u32", "value": "attrs.mrope_section[0]" },
        { "name": "section1", "type": "u32", "value": "attrs.mrope_section[1]" },
        { "name": "section2", "type": "u32", "value": "attrs.mrope_section[2]" },
        { "name": "scale", "type": "f32", "value": "attrs.scale" },
        { "name": "maxSequenceLength", "type": "u32", "value": "dim(shapes.cos, 0)" }
      ]
    },
    "x_vector": { "arg": "x", "name": "x", "elementType": "$vectorScalar" },
    "y_vector": { "arg": "y", "name": "y", "elementType": "$vectorScalar" },
    "params_vector": {
      "struct": [
        { "name": "pairCount", "type": "u32", "value": "pairCount" },
        { "name": "batchSize", "type": "u32", "value": "dim(shapes.x, 0)" },
        { "name": "sequenceLength", "type": "u32", "value": "dim(shapes.x, 1 if ranks.x == 3 else 2)" },
        { "name": "numHeads", "type": "u32", "value": "attrs.num_heads if ranks.x == 3 else dim(shapes.x, 1)" },
        { "name": "headSize", "type": "u32", "value": "headSize" },
        { "name": "halfRotaryDim", "type": "u32", "value": "halfRotaryDim" },
        { "name": "section0", "type": "u32", "value": "attrs.mrope_section[0]" },
        { "name": "section1", "type": "u32", "value": "attrs.mrope_section[1]" },
        { "name": "section2", "type": "u32", "value": "attrs.mrope_section[2]" },
        { "name": "scale", "type": "f32", "value": "attrs.scale" },
        { "name": "maxSequenceLength", "type": "u32", "value": "dim(shapes.cos, 0)" }
      ],
      "name": "params"
    }
  },
  "variants": [
    {
      "id": "pairs4",
      "when": ["headSize % 8 == 0", "effectiveRotaryDim == headSize", "attrs.interleaved == 0"],
      "derive": {
        "mropeSectioned": "attrs.mrope_layout == 0",
        "pairWidth": "4",
        "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"",
        "scalar": "dtypes.T"
      },
      "passes": [
        {
          "id": "main",
          "name": "MRotaryEmbedding.Vector",
          "shader": "mrotary-embedding-vector.wgsl.jinja",
          "derive": { "rank": "ranks.x" },
          "bindings": ["x_vector", "position_ids", "cos_cache", "sin_cache", "y_vector", "params_vector"],
          "dispatch": {
            "x": "min(ceilDiv((pairCount / 4), (tunables.WORKGROUP_SIZE)), 65535)",
            "y": "ceilDiv(ceilDiv((pairCount / 4), (tunables.WORKGROUP_SIZE)), 65535)",
            "z": 1
          }
        }
      ],
      "priority": 10
    },
    {
      "id": "pairs",
      "derive": {
        "interleaved": "attrs.interleaved != 0",
        "mropeSectioned": "attrs.mrope_layout == 0",
        "scalar": "dtypes.T"
      },
      "passes": [
        {
          "id": "main",
          "name": "MRotaryEmbedding",
          "shader": "mrotary-embedding.wgsl.jinja",
          "derive": { "rank": "ranks.x" },
          "bindings": ["x", "position_ids", "cos_cache", "sin_cache", "y", "params"],
          "dispatch": {
            "x": "min(ceilDiv((pairCount), (tunables.WORKGROUP_SIZE)), 65535)",
            "y": "ceilDiv(ceilDiv((pairCount), (tunables.WORKGROUP_SIZE)), 65535)",
            "z": 1
          }
        }
      ]
    }
  ]
}