Skip to content

Commit 01c58e6

Browse files
author
Han Wang
committed
Revert "perf(dpmodel): contract the DPA4 grid-branch router with matmul"
This reverts commit 7518a41. Measurement did not support it. An op-level profile attributed the 45.6 ms reduce to ExpandBackward0, not to this multiply, and re-benchmarking after the change moved DPA4 eager training by nothing (1.514 -> 1.552 s/step, i.e. run-to-run noise) while the offending kernel stayed byte-identical at 410.7 ms. The GridBranch product is well under the size that would matter. Since matmul is autocast-listed where mul/sum are not, keeping it would have silently moved this contraction into bf16 under the autocast region for no measured gain. The actual site is the broadcast weight in so3.py, fixed separately.
1 parent 7518a41 commit 01c58e6

1 file changed

Lines changed: 2 additions & 16 deletions

File tree

deepmd/dpmodel/descriptor/dpa4_nn/grid_net.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -401,22 +401,8 @@ def call(
401401
router = self.router(scalar_pair)
402402
router = xp.exp(router - xp.max(router, axis=-1, keepdims=True))
403403
router = router / xp.sum(router, axis=-1, keepdims=True)
404-
# einsum "ngfhc,nfh->ngfc", expressed as a batched matmul.
405-
#
406-
# NOT as ``xp.sum(value * router[:, None, :, :, None], axis=3)``: that
407-
# broadcast-then-reduce materialises the whole (N, G, F, H, C) product
408-
# -- ~0.8 GB at this example's grid resolution -- and reads it straight
409-
# back, which measured as the single most expensive kernel of a DPA4
410-
# training step. ``matmul`` contracts H in place, so only the
411-
# (N, G, F, C) result is written. ``matmul`` broadcasts its leading
412-
# batch axes, so the router's (N, 1, F, 1, H) view lines up with
413-
# value's (N, G, F, H, C) without permuting (a permute here would
414-
# reintroduce the very copy this avoids).
415-
router_row = xp.reshape(
416-
router, (n_batch, 1, n_focus, 1, self.n_branches)
417-
) # (N, 1, F, 1, H)
418-
out = xp.matmul(router_row, value) # (N, G, F, 1, C)
419-
out = xp.reshape(out, (n_batch, n_grid, n_focus, self.channels))
404+
# einsum "ngfhc,nfh->ngfc" as a broadcast sum over the branch axis
405+
out = xp.sum(value * router[:, None, :, :, None], axis=3) # (N, G, F, C)
420406

421407
# === Step 3. Project back to coefficients and mix output channels ===
422408
return _project_frames(from_grid(out), self.out_proj, self.n_frames)

0 commit comments

Comments
 (0)