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 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

Use with @huggingface/kernels

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.

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 });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.