File size: 7,709 Bytes
87e48ce
 
 
 
365cf2a
 
 
87e48ce
 
365cf2a
87e48ce
 
 
 
 
 
 
365cf2a
 
 
 
 
87e48ce
 
 
 
 
365cf2a
 
87e48ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365cf2a
 
 
 
 
 
87e48ce
 
365cf2a
 
 
 
87e48ce
 
365cf2a
 
 
 
87e48ce
 
 
 
 
 
 
365cf2a
 
87e48ce
365cf2a
 
 
 
87e48ce
 
365cf2a
87e48ce
 
 
 
 
 
 
365cf2a
 
 
 
 
87e48ce
 
365cf2a
 
 
 
 
 
87e48ce
 
 
 
 
 
 
 
365cf2a
 
87e48ce
 
 
 
 
 
 
 
 
365cf2a
 
 
 
 
87e48ce
365cf2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87e48ce
 
 
 
 
 
365cf2a
 
87e48ce
 
 
 
 
 
 
 
 
365cf2a
 
 
 
 
87e48ce
365cf2a
 
 
 
 
 
 
 
 
 
 
 
 
 
87e48ce
 
 
 
 
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
{
  "domain": "com.microsoft",
  "name": "BiasSoftmax",
  "sinceVersion": 1,
  "inputs": { "data": { "dtype": "T" }, "bias": { "dtype": "T" } },
  "outputs": { "output": { "dtype": "T", "rank": "ranks.data", "shape": "shapes.data" } },
  "attributes": { "axis": { "default": 1 }, "is_inner_broadcast": {} },
  "attributeConstraints": { "is_inner_broadcast": { "required": true } },
  "typeConstraints": { "T": ["float32", "float16"] },
  "tunables": { "WORKGROUP_SIZE": { "default": 256 }, "BLOCK_COLS": { "default": 2048 } },
  "derive": {
    "axisNorm": "attrs.axis if attrs.axis >= 0 else attrs.axis + ranks.data",
    "batchCount": "outer(shapes.data, axisNorm)",
    "blockSize": "dim(shapes.data, axisNorm) * inner(shapes.data, axisNorm)",
    "biasBlockCount": "numel(shapes.bias) / max(1, blockSize)",
    "biasContract": "(numel(shapes.data) == 0 and numel(shapes.bias) == 0) or (blockSize > 0 and biasBlockCount > 0 and numel(shapes.bias) % blockSize == 0 and biasBlockCount <= batchCount and batchCount % biasBlockCount == 0)"
  },
  "when": ["numel(shapes.data) == numel(shapes.output)", "ranks.data >= 1", "attrs.axis + ranks.data >= 0", "attrs.axis < ranks.data", "biasContract", "f16Ok(dtypes.T)"],
  "bindings": {
    "data": { "buffer": "read-only-storage", "elementType": "$scalar" },
    "bias": { "buffer": "read-only-storage", "elementType": "$scalar" },
    "output": { "buffer": "storage", "elementType": "$scalar" }
  },
  "variants": [
    {
      "id": "longrow_split",
      "priority": 40,
      "when": ["blockSize >= 65536", "batchCount > 0", "batchCount <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)", "ceilDiv(blockSize, tunables.BLOCK_COLS) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)"],
      "derive": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "combineSubgroups": false },
      "intermediates": [
        {
          "id": "blockMax",
          "dtype": "float32",
          "shape": "[outer(shapes.data, axisNorm) * ceilDiv(dim(shapes.data, axisNorm) * inner(shapes.data, axisNorm), tunables.BLOCK_COLS)]"
        },
        {
          "id": "blockSum",
          "dtype": "float32",
          "shape": "[outer(shapes.data, axisNorm) * ceilDiv(dim(shapes.data, axisNorm) * inner(shapes.data, axisNorm), tunables.BLOCK_COLS)]"
        },
        { "id": "rowMax", "dtype": "float32", "shape": "[outer(shapes.data, axisNorm)]" },
        { "id": "rowSum", "dtype": "float32", "shape": "[outer(shapes.data, axisNorm)]" }
      ],
      "passes": [
        {
          "id": "block_stats",
          "name": "BiasSoftmax.LongRowBlockStats",
          "shader": "bias-softmax-longrow-stats.wgsl.jinja",
          "derive": {
            "stage": "\"block\"",
            "isInnerBroadcast": "attrs.is_inner_broadcast != 0",
            "biasBlockCountSpec": "max(1, biasBlockCount)",
            "innerRepeat": "max(1, batchCount / max(1, biasBlockCount))"
          },
          "bindings": [
            "data",
            "bias",
            { "name": "blockMax", "buffer": "storage", "elementType": "f32" },
            { "name": "blockSum", "buffer": "storage", "elementType": "f32" },
            {
              "name": "params",
              "struct": [
                { "name": "blockSize", "type": "u32", "value": "blockSize" },
                { "name": "blocks", "type": "u32", "value": "ceilDiv(blockSize, tunables.BLOCK_COLS)" }
              ]
            }
          ],
          "dispatch": { "x": "ceilDiv(blockSize, tunables.BLOCK_COLS)", "y": "batchCount" }
        },
        {
          "id": "row_stats",
          "name": "BiasSoftmax.LongRowStats",
          "shader": "bias-softmax-longrow-stats.wgsl.jinja",
          "derive": { "stage": "\"row\"" },
          "bindings": [
            { "name": "blockMax", "buffer": "read-only-storage", "elementType": "f32" },
            { "name": "blockSum", "buffer": "read-only-storage", "elementType": "f32" },
            { "name": "rowMax", "buffer": "storage", "elementType": "f32" },
            { "name": "rowSum", "buffer": "storage", "elementType": "f32" },
            {
              "name": "params",
              "struct": [{ "name": "blocks", "type": "u32", "value": "ceilDiv(blockSize, tunables.BLOCK_COLS)" }]
            }
          ],
          "dispatch": { "x": "batchCount" }
        },
        {
          "id": "normalize",
          "name": "BiasSoftmax.LongRowNormalize",
          "shader": "bias-softmax-longrow-normalize.wgsl.jinja",
          "derive": {
            "isInnerBroadcast": "attrs.is_inner_broadcast != 0",
            "biasBlockCountSpec": "max(1, biasBlockCount)",
            "innerRepeat": "max(1, batchCount / max(1, biasBlockCount))"
          },
          "bindings": [
            "data",
            "bias",
            { "name": "rowMax", "buffer": "read-only-storage", "elementType": "f32" },
            { "name": "rowSum", "buffer": "read-only-storage", "elementType": "f32" },
            "output",
            { "name": "params", "struct": [{ "name": "blockSize", "type": "u32", "value": "blockSize" }] }
          ],
          "dispatch": { "x": "ceilDiv(blockSize, tunables.BLOCK_COLS)", "y": "batchCount" }
        }
      ]
    },
    {
      "id": "packed_rows",
      "priority": 30,
      "when": ["blockSize > 0", "blockSize <= 8", "batchCount >= 64"],
      "derive": {
        "scalar": "dtypes.T",
        "usesF16": "dtypes.T == \"f16\"",
        "combineSubgroups": false,
        "packedRows": true
      },
      "passes": [
        {
          "id": "main",
          "name": "BiasSoftmax.PackedRows",
          "shader": "bias-softmax.wgsl.jinja",
          "derive": {
            "isInnerBroadcast": "attrs.is_inner_broadcast != 0",
            "biasBlockCountSpec": "max(1, biasBlockCount)",
            "innerRepeat": "max(1, batchCount / max(1, biasBlockCount))"
          },
          "bindings": [
            "data",
            "bias",
            "output",
            {
              "name": "params",
              "struct": [
                { "name": "blockSize", "type": "u32", "value": "blockSize" },
                { "name": "batchCount", "type": "u32", "value": "batchCount" }
              ]
            }
          ],
          "dispatch": {
            "x": "min(ceilDiv((batchCount), (tunables.WORKGROUP_SIZE)), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
            "y": 1,
            "z": 1
          }
        }
      ]
    },
    {
      "id": "adaptive_row",
      "priority": 10,
      "when": ["numel(shapes.data) >= 0"],
      "derive": {
        "packedRows": false,
        "scalar": "dtypes.T",
        "usesF16": "dtypes.T == \"f16\"",
        "combineSubgroups": "device.features.has(\"subgroups\")"
      },
      "passes": [
        {
          "id": "main",
          "name": "BiasSoftmax.AdaptiveRow",
          "shader": "bias-softmax.wgsl.jinja",
          "derive": {
            "isInnerBroadcast": "attrs.is_inner_broadcast != 0",
            "biasBlockCountSpec": "max(1, biasBlockCount)",
            "innerRepeat": "max(1, batchCount / max(1, biasBlockCount))"
          },
          "bindings": [
            "data",
            "bias",
            "output",
            {
              "name": "params",
              "struct": [
                { "name": "blockSize", "type": "u32", "value": "blockSize" },
                { "name": "batchCount", "type": "u32", "value": "batchCount" }
              ]
            }
          ],
          "dispatch": { "x": "min(batchCount, 65535)", "y": "ceilDiv(batchCount, 65535)", "z": 1 },
          "subgroupCollectivesWidth": "portable"
        }
      ]
    }
  ]
}