File size: 7,045 Bytes
0d00cf0
94aaaa3
0d00cf0
94aaaa3
 
 
 
0d00cf0
94aaaa3
 
 
 
 
 
163cdd1
94aaaa3
 
 
 
 
 
 
 
 
 
 
 
163cdd1
94aaaa3
 
 
 
 
 
163cdd1
94aaaa3
 
 
 
 
 
 
163cdd1
94aaaa3
 
 
 
 
 
 
 
 
 
 
163cdd1
 
 
 
94aaaa3
 
 
163cdd1
94aaaa3
 
 
163cdd1
 
 
 
 
 
94aaaa3
 
 
 
163cdd1
94aaaa3
 
163cdd1
 
94aaaa3
 
 
 
 
 
 
 
 
163cdd1
 
 
 
 
 
 
94aaaa3
 
 
 
 
 
 
163cdd1
94aaaa3
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
---
library_name: kernels
license: apache-2.0
tags:
- kernel
- webgpu
- wgsl
---
# com.microsoft.EngramGate

`com.microsoft`  ·  ONNX Runtime contrib operator  ·  contrib since_version 1

## Description

RMS-normalizes key/query rows, scales their dot by `1 / sqrt(hidden_size)`, then broadcasts `sigmoid(sign(dot) * sqrt(max(abs(dot), 1e-6))) * value` across the `hc_mult` hyper-connection streams (parallel copies of the residual stream). The fixed floor differs from `epsilon`; a zero dot gives gate `0.5`. Reductions use float32 and only stores narrow. Optional `gated_value_normed` adds RMS normalization with `conv_norm_scale`: float16 primary values round before its float32 reduction. The op runs in one pass, or two with `gated_value_normed`, with no temporary buffer. Bfloat16 is unsupported.

See the [ONNX Runtime `EngramGate` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.EngramGate) for the reference semantics.

## Inputs

| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- | --- |
| `keyT` | `key` | `T` | `4` | — | Projected Engram keys with shape `(batch_size, sequence_length, hc_mult, hidden_size)`. | required |
| `queryT` | `query` | `T` | `4` | — | Hidden-state queries, exactly the same shape as `key`. The upstream kernels require equality rather than broadcasting, and so does this one. | required |
| `valueT` | `value` | `T` | `3` | — | Projected Engram value shared by every hyper-connection, with shape `(batch_size, sequence_length, hidden_size)`. Each row of it is gated `hc_mult` times, once per stream. | required |
| `keyNormScaleT` | `key_norm_scale` | `T` | `2` | — | RMSNorm scale for keys with shape `(hc_mult, hidden_size)`: one weight row per hyper-connection stream, selected by the row's stream index. | required |
| `queryNormScaleT` | `query_norm_scale` | `T` | `2` | — | RMSNorm scale for queries with shape `(hc_mult, hidden_size)`, indexed by the same stream index as the key scale. | required |
| `convNormScaleT` | `conv_norm_scale` | `T` | `2` | — | Optional post-gate RMSNorm scale with shape `(hc_mult, hidden_size)` and dtype T. Required for `gated_value_normed`; validated even when that output is absent. | optional |

## Outputs

| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- | --- |
| `outputT` | `output` | `T` | `4` | same as `keyT` | The gated value tensor, with the same shape and dtype as `key`. Every element of a row carries the same gate, so the row's stream index is only observable through the scale lookup. | required |
| `gatedValueNormedT` | `gated_value_normed` | `T` | `4` | same as `keyT` | Optional RMS-normalized gated value, with the key shape and dtype. Reduces each hidden row after the primary output has rounded to T, then multiplies by its per-stream scale in float32 and narrows the final value to T. Requires `conv_norm_scale`; the primary output remains required. | optional |

## Attributes

Default values (overridable per request):

| Attribute | Default | Description |
| --- | --- | --- |
| `epsilon` | `0.00001` | Constant added to both mean-of-squares denominators before the reciprocal square root. It does not reach the gate: the `1e-6` floor under the square root is a separate fixed constant. The optional gated-value normalization uses this same epsilon. |

## Type constraints

| Variable | Allowed dtypes |
| --- | --- |
| `T` | `float32`, `float16` |

## Implementation variants

One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.

- `subgroup_rows_vec4` — Loads four channels per word and reduces each row within a subgroup. A workgroup tree handles devices whose lane layout cannot cover the row with a subgroup collective.
- `packed_rows_vec4` — Loads four channels per word and reduces each row in workgroup memory. Short rows share a workgroup, and rows too wide to stage in registers use a second input walk.
- `subgroup_rows_scalar` — Loads individual channels and reduces each row within a subgroup. A workgroup tree handles devices whose lane layout cannot cover the row with a subgroup collective.
- `packed_rows_scalar` — Loads individual channels and reduces each row in workgroup memory. Short rows share a workgroup, and rows too wide to stage in registers use a second input walk.

## Device requirements

Some implementation variants require `subgroups`. Other variants run without it; which variant runs also depends on the request's shapes and dtypes.

## Files

- [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.EngramGate/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)
- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.EngramGate/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)
- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.EngramGate/blob/v1/build/webgpu/test.json) — correctness cases
- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.EngramGate/blob/v1/build/webgpu/bench.json) — benchmark cases
- [`engram-gate-norm-rows.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.EngramGate/blob/v1/build/webgpu/engram-gate-norm-rows.wgsl.jinja)
- [`engram-gate-rows.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.EngramGate/blob/v1/build/webgpu/engram-gate-rows.wgsl.jinja)

## Use with `@huggingface/kernels`

```sh
npm install --save-exact @huggingface/kernels@0.0.1-preview.4
```

Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.

Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.

The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.

```js
import { getKernel } from "@huggingface/kernels";

const kernel = await getKernel("webgpu-kernels/com.microsoft.EngramGate", { version: 1 });

const keyTData = Float32Array.of(0, 2.5);
const queryTData = Float32Array.of(3, 4);
const valueTData = Float32Array.of(2, -1.5);
const keyNormScaleTData = Float32Array.of(1, 1);
const queryNormScaleTData = Float32Array.of(1, 1);

const { outputT } = await kernel({
  keyT: { data: keyTData, shape: [1, 1, 1, 2] },
  queryT: { data: queryTData, shape: [1, 1, 1, 2] },
  valueT: { data: valueTData, shape: [1, 1, 2] },
  keyNormScaleT: { data: keyNormScaleTData, shape: [1, 2] },
  queryNormScaleT: { data: queryNormScaleTData, shape: [1, 2] },
});
console.log({ outputT });
```