File size: 4,474 Bytes
c4a6897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "domain": "com.microsoft",
  "name": "QuickGelu",
  "sinceVersion": 1,
  "description": "Computes `x * sigmoid(alpha * x)` elementwise, a fast approximation of GELU activation. The output has the same shape as the input. This WebGPU package implements float16 and float32; the schema-allowed double and bfloat16 types are not supported.",
  "inputs": [{ "role": "X", "dtype": "T", "description": "Input tensor of any shape." }],
  "outputs": [
    {
      "role": "Y",
      "dtype": "T",
      "rank": "ranks.X",
      "shape": "shapes.X",
      "description": "Output tensor; same shape as the input."
    }
  ],
  "attributes": { "alpha": 1.702 },
  "attributeDescriptions": {
    "alpha": "Scalar multiplier applied to `x` inside the sigmoid; defaults to 1.702, which approximates GELU."
  },
  "typeConstraints": { "T": ["float32", "float16"] },
  "args": {
    "X": { "kind": "tensor", "semantic": "X", "role": "input" },
    "Y": { "kind": "tensor", "semantic": "Y", "role": "output" }
  },
  "tunables": { "WORKGROUP_SIZE": 256 },
  "derive": {
    "deviceWorkgroupCap": "min(device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)",
    "workgroupOk": "tunables.WORKGROUP_SIZE > 0 and tunables.WORKGROUP_SIZE <= deviceWorkgroupCap",
    "baseOk": "workgroupOk and numel(shapes.X) == numel(shapes.Y) and f16Ok(dtypes.T)",
    "vec4Ok": "numel(shapes.X) > 0 and numel(shapes.X) % 4 == 0"
  },
  "bindingSets": {
    "scalarTail": [
      { "name": "x", "arg": "X", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
      { "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$scalar" },
      {
        "name": "params",
        "semantic": "kernel.params",
        "buffer": { "type": "uniform" },
        "struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] }
      }
    ]
  },
  "variants": [
    {
      "id": "vec4",
      "priority": 20,
      "when": ["baseOk", "vec4Ok"],
      "constants": {
        "scalar": "dtypes.T",
        "usesF16": "dtypes.T == \"f16\"",
        "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"",
        "vec4": true,
        "vec4Tail": false
      },
      "passes": [
        {
          "id": "main",
          "name": "QuickGelu.vec4",
          "source": { "shader": "quick-gelu.wgsl.jinja", "inputs": { "alpha": "attrs.alpha" } },
          "bindings": [
            {
              "name": "x",
              "arg": "X",
              "semantic": "X",
              "buffer": { "type": "read-only-storage" },
              "elementType": "$vectorScalar"
            },
            {
              "name": "y",
              "arg": "Y",
              "semantic": "Y",
              "buffer": { "type": "storage" },
              "elementType": "$vectorScalar"
            },
            {
              "name": "params",
              "semantic": "kernel.params",
              "buffer": { "type": "uniform" },
              "struct": {
                "name": "Params",
                "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X) / 4" }]
              }
            }
          ],
          "dispatch": { "threads": "numel(shapes.X) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }
        }
      ]
    },
    {
      "id": "vec4_tail",
      "priority": 10,
      "when": ["baseOk", "numel(shapes.X) > 0"],
      "constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "vec4": false, "vec4Tail": true },
      "passes": [
        {
          "id": "main",
          "name": "QuickGelu.vec4Tail",
          "source": { "shader": "quick-gelu.wgsl.jinja", "inputs": { "alpha": "attrs.alpha" } },
          "bindings": "scalarTail",
          "dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
        }
      ]
    },
    {
      "id": "scalar",
      "priority": 0,
      "when": ["baseOk", "true"],
      "constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "vec4": false, "vec4Tail": false },
      "passes": [
        {
          "id": "main",
          "name": "QuickGelu.scalar",
          "source": { "shader": "quick-gelu.wgsl.jinja", "inputs": { "alpha": "attrs.alpha" } },
          "bindings": "scalarTail",
          "dispatch": { "threads": "numel(shapes.X)", "workgroupSize": "tunables.WORKGROUP_SIZE" }
        }
      ]
    }
  ]
}