zanderjiang commited on
Commit
2b1098c
·
1 Parent(s): d807c80

update gdn definitions

Browse files
definitions/gdn/{gdn_decode_qk16_v32_d128_k_last.json → gdn_decode_qk4_v8_d128_k_last.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "gdn_decode_qk16_v32_d128_k_last",
3
- "description": "Gated Delta Net decode with GVA configuration and k-last state layout. Single-token generation with recurrent state update. Captured from Qwen3 Next linear attention layers.",
4
  "op_type": "gdn",
5
  "tags": [
6
  "stage:decode",
@@ -20,18 +20,18 @@
20
  },
21
  "num_q_heads": {
22
  "type": "const",
23
- "value": 16,
24
- "description": "Number of query heads (same as key heads in GVA mode)."
25
  },
26
  "num_k_heads": {
27
  "type": "const",
28
- "value": 16,
29
- "description": "Number of key heads."
30
  },
31
  "num_v_heads": {
32
  "type": "const",
33
- "value": 32,
34
- "description": "Number of value heads (GVA: more value heads than query heads)."
35
  },
36
  "head_size": {
37
  "type": "const",
@@ -45,43 +45,75 @@
45
  ],
46
  "inputs": {
47
  "q": {
48
- "shape": ["batch_size", "seq_len", "num_q_heads", "head_size"],
 
 
 
 
 
49
  "dtype": "bfloat16",
50
  "description": "Query tensor for single token decode."
51
  },
52
  "k": {
53
- "shape": ["batch_size", "seq_len", "num_k_heads", "head_size"],
 
 
 
 
 
54
  "dtype": "bfloat16",
55
  "description": "Key tensor for single token decode."
56
  },
57
  "v": {
58
- "shape": ["batch_size", "seq_len", "num_v_heads", "head_size"],
 
 
 
 
 
59
  "dtype": "bfloat16",
60
  "description": "Value tensor for single token decode."
61
  },
62
  "state": {
63
- "shape": ["batch_size", "num_v_heads", "head_size", "head_size"],
 
 
 
 
 
64
  "dtype": "float32",
65
  "description": "Recurrent state in k-last layout [B, H, V, K].",
66
  "optional": true
67
  },
68
  "A_log": {
69
- "shape": ["num_v_heads"],
 
 
70
  "dtype": "float32",
71
  "description": "Log decay parameter (learnable). Used to compute g = exp(-exp(A_log) * softplus(a + dt_bias))."
72
  },
73
  "a": {
74
- "shape": ["batch_size", "seq_len", "num_v_heads"],
 
 
 
 
75
  "dtype": "bfloat16",
76
  "description": "Input-dependent decay from projection."
77
  },
78
  "dt_bias": {
79
- "shape": ["num_v_heads"],
 
 
80
  "dtype": "float32",
81
  "description": "Decay bias (learnable). Added to 'a' before softplus."
82
  },
83
  "b": {
84
- "shape": ["batch_size", "seq_len", "num_v_heads"],
 
 
 
 
85
  "dtype": "bfloat16",
86
  "description": "Update gate input from projection. beta = sigmoid(b)."
87
  },
@@ -93,15 +125,25 @@
93
  },
94
  "outputs": {
95
  "output": {
96
- "shape": ["batch_size", "seq_len", "num_v_heads", "head_size"],
 
 
 
 
 
97
  "dtype": "bfloat16",
98
  "description": "Attention output. Shape follows num_v_heads in GVA mode."
99
  },
100
  "new_state": {
101
- "shape": ["batch_size", "num_v_heads", "head_size", "head_size"],
 
 
 
 
 
102
  "dtype": "float32",
103
  "description": "Updated recurrent state in k-last layout [B, H, V, K]."
104
  }
105
  },
106
- "reference": "import math\nimport torch\nimport torch.nn.functional as F\n\n\ndef matmul(a: torch.Tensor, b: torch.Tensor):\n \"\"\"Float32 matmul for numerical stability.\"\"\"\n return a.float() @ b.float()\n\n\n@torch.no_grad()\ndef run(q, k, v, state, A_log, a, dt_bias, b, scale):\n \"\"\"\n Gated Delta Net decode reference implementation (k-last layout).\n \n State layout: [B, H, V, K] (k-last, K dimension at the end)\n \n Gate computation:\n g = exp(-exp(A_log) * softplus(a + dt_bias))\n beta = sigmoid(b)\n \n Delta rule update:\n state_new = g * state_old + k^T @ (beta * v + (1-beta) * k @ state_old) - k^T @ (k @ state_old)\n output = scale * q @ state_new\n \"\"\"\n B, T, num_q_heads, K = q.shape\n _, _, num_k_heads, _ = k.shape\n _, _, num_v_heads, V = v.shape\n num_heads = num_v_heads\n device = q.device\n \n assert num_q_heads == 16\n assert num_k_heads == 16\n assert num_v_heads == 32\n assert K == 128 and V == 128\n assert T == 1\n \n if scale is None or scale == 0.0:\n scale = 1.0 / math.sqrt(K)\n \n # Compute g and beta from raw parameters\n x = a.float() + dt_bias.float() # [B, 1, HV]\n g = torch.exp(-torch.exp(A_log.float()) * F.softplus(x)) # [B, 1, HV]\n beta = torch.sigmoid(b.float()) # [B, 1, HV]\n \n q_f32 = q.squeeze(1).float()\n k_f32 = k.squeeze(1).float()\n v_f32 = v.squeeze(1).float()\n g_f32 = g.squeeze(1).float()\n beta_f32 = beta.squeeze(1).float()\n \n if state is not None:\n state_f32 = state.float()\n else:\n state_f32 = torch.zeros(B, num_heads, V, K, dtype=torch.float32, device=device)\n \n q_exp = q_f32.repeat_interleave(num_v_heads // num_q_heads, dim=1)\n k_exp = k_f32.repeat_interleave(num_v_heads // num_k_heads, dim=1)\n \n new_state = torch.zeros_like(state_f32)\n output = torch.zeros(B, num_heads, V, dtype=torch.float32, device=device)\n \n for b_idx in range(B):\n for h_idx in range(num_heads):\n q_h = q_exp[b_idx, h_idx]\n k_h = k_exp[b_idx, h_idx]\n v_h = v_f32[b_idx, h_idx]\n h_state = state_f32[b_idx, h_idx].clone().transpose(-1, -2) # [V,K] -> [K,V]\n g_val = g_f32[b_idx, h_idx]\n beta_val = beta_f32[b_idx, h_idx]\n \n old_state = g_val * h_state\n old_v = k_h @ old_state\n new_v = beta_val * v_h + (1 - beta_val) * old_v\n state_remove = k_h.unsqueeze(1) @ old_v.unsqueeze(0)\n state_update = k_h.unsqueeze(1) @ new_v.unsqueeze(0)\n h_state = old_state - state_remove + state_update\n \n output[b_idx, h_idx] = scale * (q_h @ h_state)\n new_state[b_idx, h_idx] = h_state.transpose(-1, -2) # [K,V] -> [V,K]\n \n output = output.unsqueeze(1).to(torch.bfloat16)\n return {\"output\": output, \"new_state\": new_state}"
107
  }
 
1
  {
2
+ "name": "gdn_decode_qk4_v8_d128_k_last",
3
+ "description": "Gated Delta Net decode with GVA configuration and k-last state layout. Single-token generation with recurrent state update. Captured from Qwen3 Next linear attention layers (TP=4).",
4
  "op_type": "gdn",
5
  "tags": [
6
  "stage:decode",
 
20
  },
21
  "num_q_heads": {
22
  "type": "const",
23
+ "value": 4,
24
+ "description": "Number of query heads (same as key heads in GVA mode, TP=4, 16/4=4)."
25
  },
26
  "num_k_heads": {
27
  "type": "const",
28
+ "value": 4,
29
+ "description": "Number of key heads (TP=4, 16/4=4)."
30
  },
31
  "num_v_heads": {
32
  "type": "const",
33
+ "value": 8,
34
+ "description": "Number of value heads (GVA: more value heads than query heads, TP=4, 32/4=8)."
35
  },
36
  "head_size": {
37
  "type": "const",
 
45
  ],
46
  "inputs": {
47
  "q": {
48
+ "shape": [
49
+ "batch_size",
50
+ "seq_len",
51
+ "num_q_heads",
52
+ "head_size"
53
+ ],
54
  "dtype": "bfloat16",
55
  "description": "Query tensor for single token decode."
56
  },
57
  "k": {
58
+ "shape": [
59
+ "batch_size",
60
+ "seq_len",
61
+ "num_k_heads",
62
+ "head_size"
63
+ ],
64
  "dtype": "bfloat16",
65
  "description": "Key tensor for single token decode."
66
  },
67
  "v": {
68
+ "shape": [
69
+ "batch_size",
70
+ "seq_len",
71
+ "num_v_heads",
72
+ "head_size"
73
+ ],
74
  "dtype": "bfloat16",
75
  "description": "Value tensor for single token decode."
76
  },
77
  "state": {
78
+ "shape": [
79
+ "batch_size",
80
+ "num_v_heads",
81
+ "head_size",
82
+ "head_size"
83
+ ],
84
  "dtype": "float32",
85
  "description": "Recurrent state in k-last layout [B, H, V, K].",
86
  "optional": true
87
  },
88
  "A_log": {
89
+ "shape": [
90
+ "num_v_heads"
91
+ ],
92
  "dtype": "float32",
93
  "description": "Log decay parameter (learnable). Used to compute g = exp(-exp(A_log) * softplus(a + dt_bias))."
94
  },
95
  "a": {
96
+ "shape": [
97
+ "batch_size",
98
+ "seq_len",
99
+ "num_v_heads"
100
+ ],
101
  "dtype": "bfloat16",
102
  "description": "Input-dependent decay from projection."
103
  },
104
  "dt_bias": {
105
+ "shape": [
106
+ "num_v_heads"
107
+ ],
108
  "dtype": "float32",
109
  "description": "Decay bias (learnable). Added to 'a' before softplus."
110
  },
111
  "b": {
112
+ "shape": [
113
+ "batch_size",
114
+ "seq_len",
115
+ "num_v_heads"
116
+ ],
117
  "dtype": "bfloat16",
118
  "description": "Update gate input from projection. beta = sigmoid(b)."
119
  },
 
125
  },
126
  "outputs": {
127
  "output": {
128
+ "shape": [
129
+ "batch_size",
130
+ "seq_len",
131
+ "num_v_heads",
132
+ "head_size"
133
+ ],
134
  "dtype": "bfloat16",
135
  "description": "Attention output. Shape follows num_v_heads in GVA mode."
136
  },
137
  "new_state": {
138
+ "shape": [
139
+ "batch_size",
140
+ "num_v_heads",
141
+ "head_size",
142
+ "head_size"
143
+ ],
144
  "dtype": "float32",
145
  "description": "Updated recurrent state in k-last layout [B, H, V, K]."
146
  }
147
  },
148
+ "reference": "import math\nimport torch\nimport torch.nn.functional as F\n\n\ndef matmul(a: torch.Tensor, b: torch.Tensor):\n \"\"\"Float32 matmul for numerical stability.\"\"\"\n return a.float() @ b.float()\n\n\n@torch.no_grad()\ndef run(q, k, v, state, A_log, a, dt_bias, b, scale):\n \"\"\"\n Gated Delta Net decode reference implementation (k-last layout).\n \n State layout: [B, H, V, K] (k-last, K dimension at the end)\n \n Gate computation:\n g = exp(-exp(A_log) * softplus(a + dt_bias))\n beta = sigmoid(b)\n \n Delta rule update:\n state_new = g * state_old + k^T @ (beta * v + (1-beta) * k @ state_old) - k^T @ (k @ state_old)\n output = scale * q @ state_new\n \"\"\"\n B, T, num_q_heads, K = q.shape\n _, _, num_k_heads, _ = k.shape\n _, _, num_v_heads, V = v.shape\n num_heads = num_v_heads\n device = q.device\n \n assert num_q_heads == 4\n assert num_k_heads == 4\n assert num_v_heads == 8\n assert K == 128 and V == 128\n assert T == 1\n \n if scale is None or scale == 0.0:\n scale = 1.0 / math.sqrt(K)\n \n # Compute g and beta from raw parameters\n x = a.float() + dt_bias.float() # [B, 1, HV]\n g = torch.exp(-torch.exp(A_log.float()) * F.softplus(x)) # [B, 1, HV]\n beta = torch.sigmoid(b.float()) # [B, 1, HV]\n \n q_f32 = q.squeeze(1).float()\n k_f32 = k.squeeze(1).float()\n v_f32 = v.squeeze(1).float()\n g_f32 = g.squeeze(1).float()\n beta_f32 = beta.squeeze(1).float()\n \n if state is not None:\n state_f32 = state.float()\n else:\n state_f32 = torch.zeros(B, num_heads, V, K, dtype=torch.float32, device=device)\n \n q_exp = q_f32.repeat_interleave(num_v_heads // num_q_heads, dim=1)\n k_exp = k_f32.repeat_interleave(num_v_heads // num_k_heads, dim=1)\n \n new_state = torch.zeros_like(state_f32)\n output = torch.zeros(B, num_heads, V, dtype=torch.float32, device=device)\n \n for b_idx in range(B):\n for h_idx in range(num_heads):\n q_h = q_exp[b_idx, h_idx]\n k_h = k_exp[b_idx, h_idx]\n v_h = v_f32[b_idx, h_idx]\n h_state = state_f32[b_idx, h_idx].clone().transpose(-1, -2) # [V,K] -> [K,V]\n g_val = g_f32[b_idx, h_idx]\n beta_val = beta_f32[b_idx, h_idx]\n \n old_state = g_val * h_state\n old_v = k_h @ old_state\n new_v = beta_val * v_h + (1 - beta_val) * old_v\n state_remove = k_h.unsqueeze(1) @ old_v.unsqueeze(0)\n state_update = k_h.unsqueeze(1) @ new_v.unsqueeze(0)\n h_state = old_state - state_remove + state_update\n \n output[b_idx, h_idx] = scale * (q_h @ h_state)\n new_state[b_idx, h_idx] = h_state.transpose(-1, -2) # [K,V] -> [V,K]\n \n output = output.unsqueeze(1).to(torch.bfloat16)\n return output, new_state"
149
  }
definitions/gdn/{gdn_prefill_qk16_v32_d128_k_last.json → gdn_prefill_qk4_v8_d128_k_last.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "gdn_prefill_qk16_v32_d128_k_last",
3
- "description": "Gated Delta Net prefill with GVA configuration and k-last state layout. The state is in k-last layout [N, H, V, K]. Captured from Qwen3 Next linear attention layers.",
4
  "op_type": "gdn",
5
  "tags": [
6
  "stage:prefill",
@@ -17,18 +17,18 @@
17
  },
18
  "num_q_heads": {
19
  "type": "const",
20
- "value": 16,
21
- "description": "Number of query heads (same as key heads in GVA mode)."
22
  },
23
  "num_k_heads": {
24
  "type": "const",
25
- "value": 16,
26
- "description": "Number of key heads."
27
  },
28
  "num_v_heads": {
29
  "type": "const",
30
- "value": 32,
31
- "description": "Number of value heads (GVA: more value heads than query heads)."
32
  },
33
  "head_size": {
34
  "type": "const",
@@ -45,48 +45,77 @@
45
  ],
46
  "inputs": {
47
  "q": {
48
- "shape": ["total_seq_len", "num_q_heads", "head_size"],
 
 
 
 
49
  "dtype": "bfloat16",
50
  "description": "Query tensor."
51
  },
52
  "k": {
53
- "shape": ["total_seq_len", "num_k_heads", "head_size"],
 
 
 
 
54
  "dtype": "bfloat16",
55
  "description": "Key tensor."
56
  },
57
  "v": {
58
- "shape": ["total_seq_len", "num_v_heads", "head_size"],
 
 
 
 
59
  "dtype": "bfloat16",
60
  "description": "Value tensor."
61
  },
62
  "state": {
63
- "shape": ["num_seqs", "num_v_heads", "head_size", "head_size"],
 
 
 
 
 
64
  "dtype": "float32",
65
  "description": "Recurrent state in k-last layout [N, H, V, K].",
66
  "optional": true
67
  },
68
  "A_log": {
69
- "shape": ["num_v_heads"],
 
 
70
  "dtype": "float32",
71
  "description": "Log decay parameter (learnable). Used to compute g = exp(-exp(A_log) * softplus(a + dt_bias))."
72
  },
73
  "a": {
74
- "shape": ["total_seq_len", "num_v_heads"],
 
 
 
75
  "dtype": "bfloat16",
76
  "description": "Input-dependent decay from projection."
77
  },
78
  "dt_bias": {
79
- "shape": ["num_v_heads"],
 
 
80
  "dtype": "float32",
81
  "description": "Decay bias (learnable). Added to 'a' before softplus."
82
  },
83
  "b": {
84
- "shape": ["total_seq_len", "num_v_heads"],
 
 
 
85
  "dtype": "bfloat16",
86
  "description": "Update gate input from projection. beta = sigmoid(b)."
87
  },
88
  "cu_seqlens": {
89
- "shape": ["len_cu_seqlens"],
 
 
90
  "dtype": "int64",
91
  "description": "Cumulative sequence lengths for variable-length batching."
92
  },
@@ -98,15 +127,24 @@
98
  },
99
  "outputs": {
100
  "output": {
101
- "shape": ["total_seq_len", "num_v_heads", "head_size"],
 
 
 
 
102
  "dtype": "bfloat16",
103
  "description": "Attention output. Shape follows num_v_heads in GVA mode."
104
  },
105
  "new_state": {
106
- "shape": ["num_seqs", "num_v_heads", "head_size", "head_size"],
 
 
 
 
 
107
  "dtype": "float32",
108
  "description": "Updated recurrent state in k-last layout [N, H, V, K]."
109
  }
110
  },
111
- "reference": "import math\nimport torch\nimport torch.nn.functional as F\n\n\ndef matmul(a: torch.Tensor, b: torch.Tensor):\n \"\"\"Float32 matmul for numerical stability.\"\"\"\n return a.float() @ b.float()\n\n\n@torch.no_grad()\ndef run(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale):\n \"\"\"\n Gated Delta Net prefill reference implementation (k-last layout).\n \n State layout: [H, V, K] (k-last, K dimension at the end)\n \n Gate computation:\n g = exp(-exp(A_log) * softplus(a + dt_bias))\n beta = sigmoid(b)\n \n Delta rule update:\n state_new = g * state_old + k^T @ (beta * v + (1-beta) * k @ state_old) - k^T @ (k @ state_old)\n output = scale * q @ state_new\n \"\"\"\n total_seq_len, num_q_heads, head_size = q.shape\n num_v_heads = v.shape[1]\n num_k_heads = k.shape[1]\n num_sab_heads = max(num_q_heads, num_v_heads)\n num_seqs = cu_seqlens.size(0) - 1\n device = q.device\n\n assert num_q_heads == 16\n assert num_k_heads == 16\n assert num_v_heads == 32\n assert head_size == 128\n\n if scale is None or scale == 0.0:\n scale = 1.0 / math.sqrt(head_size)\n\n # Compute g and beta from raw parameters\n x = a.float() + dt_bias.float() # [total_seq_len, HV]\n g = torch.exp(-torch.exp(A_log.float()) * F.softplus(x)) # [total_seq_len, HV]\n beta = torch.sigmoid(b.float()) # [total_seq_len, HV]\n\n q_exp = q.repeat_interleave(num_v_heads // num_q_heads, dim=1)\n k_exp = k.repeat_interleave(num_v_heads // num_k_heads, dim=1)\n\n output = torch.zeros(\n (total_seq_len, num_sab_heads, head_size), dtype=torch.bfloat16, device=device\n )\n new_state = torch.zeros(\n (num_seqs, num_sab_heads, head_size, head_size), dtype=torch.float32, device=device\n )\n\n for seq_idx in range(num_seqs):\n seq_start = int(cu_seqlens[seq_idx].item())\n seq_end = int(cu_seqlens[seq_idx + 1].item())\n seq_len = seq_end - seq_start\n\n if seq_len <= 0:\n continue\n\n if state is not None:\n state_HKV = state[seq_idx].clone().float().transpose(-1, -2) # [H,V,K] -> [H,K,V]\n else:\n state_HKV = torch.zeros(\n (num_sab_heads, head_size, head_size), dtype=torch.float32, device=device\n )\n\n for i in range(seq_len):\n t = seq_start + i\n q_H1K = q_exp[t].unsqueeze(1).float()\n k_H1K = k_exp[t].unsqueeze(1).float()\n v_H1V = v[t].unsqueeze(1).float()\n g_H11 = g[t].unsqueeze(1).unsqueeze(2)\n beta_H11 = beta[t].unsqueeze(1).unsqueeze(2)\n\n old_state_HKV = g_H11 * state_HKV\n old_v_H1V = matmul(k_H1K, old_state_HKV)\n new_v_H1V = beta_H11 * v_H1V + (1 - beta_H11) * old_v_H1V\n state_remove = torch.einsum('hkl,hlv->hkv', k_H1K.transpose(-1, -2), old_v_H1V)\n state_update = torch.einsum('hkl,hlv->hkv', k_H1K.transpose(-1, -2), new_v_H1V)\n state_HKV = old_state_HKV - state_remove + state_update\n\n o_H1V = scale * matmul(q_H1K, state_HKV)\n output[t] = o_H1V.squeeze(1).to(torch.bfloat16)\n\n new_state[seq_idx] = state_HKV.transpose(-1, -2) # [H,K,V] -> [H,V,K]\n\n return {\"output\": output, \"new_state\": new_state}"
112
  }
 
1
  {
2
+ "name": "gdn_prefill_qk4_v8_d128_k_last",
3
+ "description": "Gated Delta Net prefill with GVA configuration and k-last state layout. The state is in k-last layout [N, H, V, K]. Captured from Qwen3 Next linear attention layers (TP=4).",
4
  "op_type": "gdn",
5
  "tags": [
6
  "stage:prefill",
 
17
  },
18
  "num_q_heads": {
19
  "type": "const",
20
+ "value": 4,
21
+ "description": "Number of query heads (same as key heads in GVA mode, TP=4, 16/4=4)."
22
  },
23
  "num_k_heads": {
24
  "type": "const",
25
+ "value": 4,
26
+ "description": "Number of key heads (TP=4, 16/4=4)."
27
  },
28
  "num_v_heads": {
29
  "type": "const",
30
+ "value": 8,
31
+ "description": "Number of value heads (GVA: more value heads than query heads, TP=4, 32/4=8)."
32
  },
33
  "head_size": {
34
  "type": "const",
 
45
  ],
46
  "inputs": {
47
  "q": {
48
+ "shape": [
49
+ "total_seq_len",
50
+ "num_q_heads",
51
+ "head_size"
52
+ ],
53
  "dtype": "bfloat16",
54
  "description": "Query tensor."
55
  },
56
  "k": {
57
+ "shape": [
58
+ "total_seq_len",
59
+ "num_k_heads",
60
+ "head_size"
61
+ ],
62
  "dtype": "bfloat16",
63
  "description": "Key tensor."
64
  },
65
  "v": {
66
+ "shape": [
67
+ "total_seq_len",
68
+ "num_v_heads",
69
+ "head_size"
70
+ ],
71
  "dtype": "bfloat16",
72
  "description": "Value tensor."
73
  },
74
  "state": {
75
+ "shape": [
76
+ "num_seqs",
77
+ "num_v_heads",
78
+ "head_size",
79
+ "head_size"
80
+ ],
81
  "dtype": "float32",
82
  "description": "Recurrent state in k-last layout [N, H, V, K].",
83
  "optional": true
84
  },
85
  "A_log": {
86
+ "shape": [
87
+ "num_v_heads"
88
+ ],
89
  "dtype": "float32",
90
  "description": "Log decay parameter (learnable). Used to compute g = exp(-exp(A_log) * softplus(a + dt_bias))."
91
  },
92
  "a": {
93
+ "shape": [
94
+ "total_seq_len",
95
+ "num_v_heads"
96
+ ],
97
  "dtype": "bfloat16",
98
  "description": "Input-dependent decay from projection."
99
  },
100
  "dt_bias": {
101
+ "shape": [
102
+ "num_v_heads"
103
+ ],
104
  "dtype": "float32",
105
  "description": "Decay bias (learnable). Added to 'a' before softplus."
106
  },
107
  "b": {
108
+ "shape": [
109
+ "total_seq_len",
110
+ "num_v_heads"
111
+ ],
112
  "dtype": "bfloat16",
113
  "description": "Update gate input from projection. beta = sigmoid(b)."
114
  },
115
  "cu_seqlens": {
116
+ "shape": [
117
+ "len_cu_seqlens"
118
+ ],
119
  "dtype": "int64",
120
  "description": "Cumulative sequence lengths for variable-length batching."
121
  },
 
127
  },
128
  "outputs": {
129
  "output": {
130
+ "shape": [
131
+ "total_seq_len",
132
+ "num_v_heads",
133
+ "head_size"
134
+ ],
135
  "dtype": "bfloat16",
136
  "description": "Attention output. Shape follows num_v_heads in GVA mode."
137
  },
138
  "new_state": {
139
+ "shape": [
140
+ "num_seqs",
141
+ "num_v_heads",
142
+ "head_size",
143
+ "head_size"
144
+ ],
145
  "dtype": "float32",
146
  "description": "Updated recurrent state in k-last layout [N, H, V, K]."
147
  }
148
  },
149
+ "reference": "import math\nimport torch\nimport torch.nn.functional as F\n\n\ndef matmul(a: torch.Tensor, b: torch.Tensor):\n \"\"\"Float32 matmul for numerical stability.\"\"\"\n return a.float() @ b.float()\n\n\n@torch.no_grad()\ndef run(q, k, v, state, A_log, a, dt_bias, b, cu_seqlens, scale):\n \"\"\"\n Gated Delta Net prefill reference implementation (k-last layout).\n \n State layout: [H, V, K] (k-last, K dimension at the end)\n \n Gate computation:\n g = exp(-exp(A_log) * softplus(a + dt_bias))\n beta = sigmoid(b)\n \n Delta rule update:\n state_new = g * state_old + k^T @ (beta * v + (1-beta) * k @ state_old) - k^T @ (k @ state_old)\n output = scale * q @ state_new\n \"\"\"\n total_seq_len, num_q_heads, head_size = q.shape\n num_v_heads = v.shape[1]\n num_k_heads = k.shape[1]\n num_sab_heads = max(num_q_heads, num_v_heads)\n num_seqs = cu_seqlens.size(0) - 1\n device = q.device\n\n assert num_q_heads == 4\n assert num_k_heads == 4\n assert num_v_heads == 8\n assert head_size == 128\n\n if scale is None or scale == 0.0:\n scale = 1.0 / math.sqrt(head_size)\n\n # Compute g and beta from raw parameters\n x = a.float() + dt_bias.float() # [total_seq_len, HV]\n g = torch.exp(-torch.exp(A_log.float()) * F.softplus(x)) # [total_seq_len, HV]\n beta = torch.sigmoid(b.float()) # [total_seq_len, HV]\n\n q_exp = q.repeat_interleave(num_v_heads // num_q_heads, dim=1)\n k_exp = k.repeat_interleave(num_v_heads // num_k_heads, dim=1)\n\n output = torch.zeros(\n (total_seq_len, num_sab_heads, head_size), dtype=torch.bfloat16, device=device\n )\n new_state = torch.zeros(\n (num_seqs, num_sab_heads, head_size, head_size), dtype=torch.float32, device=device\n )\n\n for seq_idx in range(num_seqs):\n seq_start = int(cu_seqlens[seq_idx].item())\n seq_end = int(cu_seqlens[seq_idx + 1].item())\n seq_len = seq_end - seq_start\n\n if seq_len <= 0:\n continue\n\n if state is not None:\n state_HKV = state[seq_idx].clone().float().transpose(-1, -2) # [H,V,K] -> [H,K,V]\n else:\n state_HKV = torch.zeros(\n (num_sab_heads, head_size, head_size), dtype=torch.float32, device=device\n )\n\n for i in range(seq_len):\n t = seq_start + i\n q_H1K = q_exp[t].unsqueeze(1).float()\n k_H1K = k_exp[t].unsqueeze(1).float()\n v_H1V = v[t].unsqueeze(1).float()\n g_H11 = g[t].unsqueeze(1).unsqueeze(2)\n beta_H11 = beta[t].unsqueeze(1).unsqueeze(2)\n\n old_state_HKV = g_H11 * state_HKV\n old_v_H1V = matmul(k_H1K, old_state_HKV)\n new_v_H1V = beta_H11 * v_H1V + (1 - beta_H11) * old_v_H1V\n state_remove = torch.einsum('hkl,hlv->hkv', k_H1K.transpose(-1, -2), old_v_H1V)\n state_update = torch.einsum('hkl,hlv->hkv', k_H1K.transpose(-1, -2), new_v_H1V)\n state_HKV = old_state_HKV - state_remove + state_update\n\n o_H1V = scale * matmul(q_H1K, state_HKV)\n output[t] = o_H1V.squeeze(1).to(torch.bfloat16)\n\n new_state[seq_idx] = state_HKV.transpose(-1, -2) # [H,K,V] -> [H,V,K]\n\n return output, new_state"
150
  }