MCPcopy Create free account
hub / github.com/pytorch/tutorials / forward

Function forward

advanced_source/cuda_graph_annotations_tutorial.py:166–193  ·  view source on GitHub ↗

Forward pass with annotated regions.

()

Source from the content-addressed store, hash-verified

164 }
165
166 def forward():
167 """Forward pass with annotated regions."""
168 B, T, D, H = batch_size, seq_len, dim, num_heads
169 hd = head_dim
170
171 # Annotate QKV projection
172 with mark_kernels({"name": "qkv_proj"}):
173 qkv = params["x"] @ params["Wqkv"]
174
175 # Reshape for multi-head attention
176 q, k, v = qkv.split(D, dim=-1)
177 q = q.view(B, T, H, hd).transpose(1, 2)
178 k = k.view(B, T, H, hd).transpose(1, 2)
179 v = v.view(B, T, H, hd).transpose(1, 2)
180
181 # Annotate attention computation (optionally on a custom stream)
182 with mark_kernels({"name": "attention", "stream": 62}):
183 scores = (q @ k.transpose(-1, -2)) / math.sqrt(hd)
184 attn = torch.softmax(scores, dim=-1)
185 ctx = (attn @ v).transpose(1, 2).reshape(B, T, D)
186
187 # Annotate output projection
188 with mark_kernels({"name": "out_proj"}):
189 o = ctx @ params["Wo"]
190
191 # Annotate MLP (on another custom stream)
192 with mark_kernels({"name": "mlp", "stream": 61}):
193 return torch.nn.functional.gelu(o @ params["W1"]) @ params["W2"]
194
195 return forward
196

Callers

nothing calls this directly

Calls 1

annotate_collectiveFunction · 0.85

Tested by

no test coverage detected