File size: 9,796 Bytes
62b03c9
 
 
 
6dbed36
 
 
 
 
 
 
 
 
62b03c9
 
6dbed36
62b03c9
6dbed36
 
 
 
 
 
 
62b03c9
 
 
 
 
 
6dbed36
62b03c9
 
 
 
 
6dbed36
62b03c9
 
6dbed36
 
 
 
62b03c9
 
e18dfab
 
 
 
62b03c9
6dbed36
62b03c9
6dbed36
 
e18dfab
 
 
 
6dbed36
 
 
 
 
 
 
 
 
e18dfab
6dbed36
 
 
 
 
 
 
 
 
e18dfab
 
6dbed36
 
 
 
 
 
 
 
 
 
e18dfab
6dbed36
 
 
 
 
 
 
 
 
 
62b03c9
 
 
 
 
6dbed36
e18dfab
62b03c9
 
 
 
6dbed36
62b03c9
6dbed36
 
 
62b03c9
 
 
 
 
e18dfab
62b03c9
e18dfab
 
62b03c9
 
 
e18dfab
 
62b03c9
e18dfab
 
6dbed36
62b03c9
 
 
 
 
e18dfab
62b03c9
e18dfab
 
62b03c9
 
 
 
e18dfab
62b03c9
6dbed36
 
 
62b03c9
 
 
 
 
e18dfab
62b03c9
e18dfab
 
62b03c9
 
 
e18dfab
 
62b03c9
e18dfab
 
6dbed36
62b03c9
 
 
 
 
 
 
6dbed36
e18dfab
62b03c9
 
 
 
e18dfab
62b03c9
6dbed36
 
 
62b03c9
 
 
 
 
 
 
6dbed36
e18dfab
62b03c9
 
 
 
e18dfab
62b03c9
6dbed36
 
 
62b03c9
 
 
 
 
 
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
{
  "domain": "com.microsoft",
  "name": "GatherBlockQuantized",
  "sinceVersion": 1,
  "inputs": {
    "dataT": { "onnx": "data", "dtype": "T1", "rank": 2 },
    "indicesT": { "onnx": "indices", "dtype": "Tind", "rank": 1, "storage": "uint32", "narrowing": "checked" },
    "scalesT": { "onnx": "scales", "dtype": "T2", "rank": 2 },
    "zeroPointsT": { "onnx": "zero_points", "dtype": "T1", "rank": 2, "optional": true }
  },
  "outputs": {
    "outputT": {
      "onnx": "output",
      "dtype": "T2",
      "rank": 2,
      "shape": "[dim(shapes.indicesT, 0), dim(shapes.dataT, 1) * (8 / attrs.bits)]"
    }
  },
  "attributes": {
    "bits": { "default": 4 },
    "block_size": { "default": 128 },
    "gather_axis": { "default": 0 },
    "quantize_axis": { "default": 1 }
  },
  "attributeConstraints": {
    "bits": { "values": [4, 8] },
    "gather_axis": { "values": [0] },
    "quantize_axis": { "values": [1] }
  },
  "typeConstraints": { "T1": ["uint8"], "T2": ["float32"], "Tind": ["int64"] },
  "tunables": { "WORKGROUP_SIZE": { "default": 64 } },
  "derive": {
    "deviceWorkgroupCap": "min(device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)",
    "bits": "attrs.bits",
    "blockSize": "attrs.block_size",
    "blockSizeOk": "blockSize >= 16 and pow2ceil(blockSize) == blockSize",
    "outBlocks": "ceilDiv(dim(shapes.outputT, 1), blockSize)",
    "zeroPointCols": "ceilDiv(outBlocks, 2) if bits == 4 else outBlocks",
    "workgroupSize": "min(tunables.WORKGROUP_SIZE, deviceWorkgroupCap)",
    "commonShapeValid": "blockSizeOk and ranks.dataT == 2 and ranks.indicesT == 1 and ranks.scalesT == 2 and ranks.outputT == 2 and tensorDtypes.indicesT == \"uint32\" and tensorDtypes.scalesT == \"float32\" and tensorDtypes.outputT == \"float32\" and dim(shapes.outputT, 0) == dim(shapes.indicesT, 0) and dim(shapes.scalesT, 0) == dim(shapes.dataT, 0) and dim(shapes.scalesT, 1) == outBlocks",
    "q4ShapeValid": "commonShapeValid and tensorDtypes.dataT == \"uint8\" and dim(shapes.outputT, 1) == dim(shapes.dataT, 1) * 2",
    "q8ShapeValid": "commonShapeValid and tensorDtypes.dataT == \"uint8\" and dim(shapes.outputT, 1) == dim(shapes.dataT, 1)",
    "zeroPointsValid": "present.zeroPointsT and ranks.zeroPointsT == 2 and tensorDtypes.zeroPointsT == \"uint8\" and dim(shapes.zeroPointsT, 0) == dim(shapes.dataT, 0) and dim(shapes.zeroPointsT, 1) == zeroPointCols",
    "noZeroMode": "not present.zeroPointsT",
    "zeroMode": "zeroPointsValid",
    "hasZero": "zeroMode",
    "indexScalar": "\"u32\"",
    "scaleScalar": "\"f32\"",
    "zeroPointElement": "\"u32\"",
    "workgroupFits": "workgroupSize > 0",
    "foldedDispatchFits": "ceil(ceil(numel(shapes.outputT) / min(device.limits.maxComputeWorkgroupsPerDimension, 65535)) / workgroupSize) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)"
  },
  "when": ["foldedDispatchFits", "workgroupFits"],
  "bindings": {
    "data": { "arg": "dataT", "elementType": "$dataElement" },
    "indices": { "arg": "indicesT", "elementType": "$indexScalar" },
    "scales": { "arg": "scalesT", "elementType": "$scaleScalar" },
    "output": { "arg": "outputT", "elementType": "$outputElement" },
    "params": {
      "struct": [
        { "name": "indexCount", "type": "u32", "value": "dim(shapes.indicesT, 0)" },
        { "name": "cols", "type": "u32", "value": "dim(shapes.outputT, 1)" },
        { "name": "blocks", "type": "u32", "value": "dim(shapes.scalesT, 1)" },
        { "name": "blockSize", "type": "u32", "value": "blockSize" },
        { "name": "rows", "type": "u32", "value": "dim(shapes.dataT, 0)" }
      ]
    },
    "params_main": {
      "name": "params",
      "struct": [
        { "name": "indexCount", "type": "u32", "value": "dim(shapes.indicesT, 0)" },
        { "name": "packedCols", "type": "u32", "value": "dim(shapes.dataT, 1)" },
        { "name": "blocks", "type": "u32", "value": "dim(shapes.scalesT, 1)" },
        { "name": "blockSize", "type": "u32", "value": "blockSize" },
        { "name": "rows", "type": "u32", "value": "dim(shapes.dataT, 0)" }
      ]
    },
    "zero_points": { "arg": "zeroPointsT", "elementType": "$zeroPointElement" },
    "params__uniform": {
      "name": "params",
      "struct": [
        { "name": "indexCount", "type": "u32", "value": "dim(shapes.indicesT, 0)" },
        { "name": "packedCols", "type": "u32", "value": "dim(shapes.dataT, 1)" },
        { "name": "blocks", "type": "u32", "value": "dim(shapes.scalesT, 1)" },
        { "name": "zeroPointCols", "type": "u32", "value": "zeroPointCols" },
        { "name": "blockSize", "type": "u32", "value": "blockSize" },
        { "name": "rows", "type": "u32", "value": "dim(shapes.dataT, 0)" }
      ]
    },
    "params_q8_zero_tail4": {
      "name": "params",
      "struct": [
        { "name": "indexCount", "type": "u32", "value": "dim(shapes.indicesT, 0)" },
        { "name": "cols", "type": "u32", "value": "dim(shapes.outputT, 1)" },
        { "name": "packedCols", "type": "u32", "value": "dim(shapes.dataT, 1)" },
        { "name": "blocks", "type": "u32", "value": "dim(shapes.scalesT, 1)" },
        { "name": "blockSize", "type": "u32", "value": "blockSize" },
        { "name": "rows", "type": "u32", "value": "dim(shapes.dataT, 0)" }
      ]
    }
  },
  "variants": [
    {
      "id": "q8_no_zero_vec4",
      "priority": 10,
      "when": ["q8ShapeValid", "noZeroMode", "bits == 8", "blockSize % 4 == 0", "dim(shapes.outputT, 1) % 4 == 0"],
      "derive": { "scalarTail": false, "dataElement": "\"vec4<u32>\"", "outputElement": "\"vec4<f32>\"" },
      "passes": [
        {
          "id": "main",
          "shader": "gather-block-quantized-q8-vec4.wgsl.jinja",
          "bindings": ["data", "indices", "scales", "output", "params"],
          "dispatch": {
            "x": "min(ceilDiv((dim(shapes.indicesT, 0) * (dim(shapes.outputT, 1) / 4)), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((dim(shapes.indicesT, 0) * (dim(shapes.outputT, 1) / 4)), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    },
    {
      "id": "q8_zero_vec4",
      "priority": 10,
      "when": ["q8ShapeValid", "zeroMode", "bits == 8", "blockSize % 4 == 0", "dim(shapes.outputT, 1) % 4 == 0"],
      "derive": { "scalarTail": false, "dataElement": "\"vec4<u32>\"", "outputElement": "\"vec4<f32>\"" },
      "passes": [
        {
          "id": "main",
          "shader": "gather-block-quantized-q8-vec4.wgsl.jinja",
          "bindings": ["data", "indices", "scales", "zero_points", "output", "params"],
          "dispatch": {
            "x": "min(ceilDiv((dim(shapes.indicesT, 0) * (dim(shapes.outputT, 1) / 4)), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((dim(shapes.indicesT, 0) * (dim(shapes.outputT, 1) / 4)), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    },
    {
      "id": "q4_no_zero_pair",
      "priority": 10,
      "when": ["q4ShapeValid", "noZeroMode", "bits == 4"],
      "derive": { "dataElement": "\"u32\"", "outputElement": "\"vec2<f32>\"" },
      "passes": [
        {
          "id": "main",
          "shader": "gather-block-quantized-q4-pair.wgsl.jinja",
          "bindings": ["data", "indices", "scales", "output", "params_main"],
          "dispatch": {
            "x": "min(ceilDiv((dim(shapes.indicesT, 0) * dim(shapes.dataT, 1)), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((dim(shapes.indicesT, 0) * dim(shapes.dataT, 1)), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    },
    {
      "id": "q4_zero_pair",
      "priority": 10,
      "when": ["q4ShapeValid", "zeroMode", "bits == 4"],
      "derive": { "dataElement": "\"u32\"", "outputElement": "\"vec2<f32>\"" },
      "passes": [
        {
          "id": "main",
          "shader": "gather-block-quantized-q4-pair.wgsl.jinja",
          "bindings": ["data", "indices", "scales", "zero_points", "output", "params__uniform"],
          "dispatch": {
            "x": "min(ceilDiv((dim(shapes.indicesT, 0) * dim(shapes.dataT, 1)), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((dim(shapes.indicesT, 0) * dim(shapes.dataT, 1)), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    },
    {
      "id": "q8_no_zero_tail4",
      "priority": 5,
      "when": ["q8ShapeValid", "noZeroMode", "bits == 8"],
      "derive": { "scalarTail": true, "dataElement": "\"u32\"", "outputElement": "\"f32\"" },
      "passes": [
        {
          "id": "main",
          "shader": "gather-block-quantized-q8-vec4.wgsl.jinja",
          "bindings": ["data", "indices", "scales", "output", "params_q8_zero_tail4"],
          "dispatch": {
            "x": "min(ceilDiv((dim(shapes.indicesT, 0) * ceilDiv(dim(shapes.outputT, 1), 4)), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((dim(shapes.indicesT, 0) * ceilDiv(dim(shapes.outputT, 1), 4)), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    },
    {
      "id": "q8_zero_tail4",
      "priority": 5,
      "when": ["q8ShapeValid", "zeroMode", "bits == 8"],
      "derive": { "scalarTail": true, "dataElement": "\"u32\"", "outputElement": "\"f32\"" },
      "passes": [
        {
          "id": "main",
          "shader": "gather-block-quantized-q8-vec4.wgsl.jinja",
          "bindings": ["data", "indices", "scales", "zero_points", "output", "params_q8_zero_tail4"],
          "dispatch": {
            "x": "min(ceilDiv((dim(shapes.indicesT, 0) * ceilDiv(dim(shapes.outputT, 1), 4)), (workgroupSize)), 65535)",
            "y": "ceilDiv(ceilDiv((dim(shapes.indicesT, 0) * ceilDiv(dim(shapes.outputT, 1), 4)), (workgroupSize)), 65535)",
            "z": 1
          }
        }
      ]
    }
  ]
}