From 7bf9a5bf46e2538553328ea299c22c6ff2c9d3f2 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:22:20 +0800 Subject: [PATCH] [FSDP] Route dict outputs through a single backward hook The forward post hook wrapped every hookable value of a dict output in its own FusionBackwardHook. The first value whose gradient arrives (e.g. an entry a later layer passes through untouched) then all-gathers the unit before its recompute backward runs, and _release_params_after_unit frees later units whose recompute backward has not run yet ("Tensor holds no memory"). Route all hookable dict values through one FusionBackwardHook, as the tuple branch already does, so the backward all_gather fires once, after every output gradient has arrived. Non-tensor, stop_gradient and exposed values are left untouched. --- .../distributed/fsdp/fully_shard_fusion.py | 37 ++++++---- .../test_fsdp_fully_shard_fusion.py | 70 +++++++++++++++++++ 2 files changed, 95 insertions(+), 12 deletions(-) diff --git a/python/paddle/distributed/fsdp/fully_shard_fusion.py b/python/paddle/distributed/fsdp/fully_shard_fusion.py index 5b10060c5041b..d1719edffb20b 100644 --- a/python/paddle/distributed/fsdp/fully_shard_fusion.py +++ b/python/paddle/distributed/fsdp/fully_shard_fusion.py @@ -2496,18 +2496,31 @@ def _forward_post_hook(layer, inputs, outputs): exposed = _find_exposed_param_names(outputs, own_param_names) if isinstance(outputs, dict): - for key, value in outputs.items(): - if ( - isinstance(value, paddle.Tensor) - and not value.stop_gradient - and value.name not in exposed - ): - outputs[key] = FusionBackwardHook.apply( - value, - layer=layer, - comm_manager=self.comm_manager, - recursive=recursive, - ) + # Route every hookable value through ONE identity PyLayer, as + # the tuple branch does. With a PyLayer per value, the first + # value whose gradient arrives (e.g. an entry a later layer + # passes through untouched) all-gathers this unit early, and + # _release_params_after_unit then frees later units whose + # recompute backward has not run yet. + keys = [ + key + for key, value in outputs.items() + if isinstance(value, paddle.Tensor) + and not value.stop_gradient + and value.name not in exposed + ] + if not keys: + return outputs + hooked = FusionBackwardHook.apply( + *[outputs[key] for key in keys], + layer=layer, + comm_manager=self.comm_manager, + recursive=recursive, + ) + if not isinstance(hooked, (tuple, list)): + hooked = (hooked,) + for key, value in zip(keys, hooked): + outputs[key] = value return outputs elif isinstance(outputs, tuple): # Exposed parameters are leaf vars that require grad, which the diff --git a/test/legacy_test/test_fsdp_fully_shard_fusion.py b/test/legacy_test/test_fsdp_fully_shard_fusion.py index a95d0c1357397..58ef8a7271ad5 100644 --- a/test/legacy_test/test_fsdp_fully_shard_fusion.py +++ b/test/legacy_test/test_fsdp_fully_shard_fusion.py @@ -22,6 +22,8 @@ * the fused-kernel fp32 gradient hand-off inside the grad hook. * the tuple branch of the forward post hook that splices exposed parameters back untouched while routing the rest through the backward all_gather hook. +* the dict branch of the forward post hook, which must route all hookable + values through a single backward hook so the unit is all-gathered once. * ``unshard_params`` -- the synchronous gather / reshard context manager and the double-buffer bookkeeping it restores on exit. * the grads buffer ``grad_div`` under context parallel. @@ -224,6 +226,74 @@ def test_no_exposed_params_routes_all_through_hook(self): np.testing.assert_array_equal(result[1].numpy(), b.numpy()) +class _GatherCountingCommManager(_NoopCommManager): + """Counts the backward all_gather calls issued by ``FusionBackwardHook``.""" + + def __init__(self): + super().__init__() + self.backward_gathers = 0 + + def all_gather_params(self, params, is_backward=False): + if is_backward: + self.backward_gathers += 1 + + +class TestForwardPostHookDictBranch(unittest.TestCase): + """Covers the dict branch of the forward post hook built by + ``_register_fusion_layer_hooks``.""" + + def _capture_post_hook(self, param_names): + fsdp = object.__new__(fsf.FullyShardFusion) + fsdp.comm_manager = _GatherCountingCommManager() + layer = _FakeLayer([_NamedParam(n) for n in param_names]) + fsdp._register_fusion_layer_hooks(layer, recursive=False) + self.assertIsNotNone(layer.post_hook) + return fsdp, layer + + def test_multiple_values_all_gather_once_in_backward(self): + # A PyLayer per value would all-gather the unit once per value, the + # first time as soon as any single value's gradient arrives. + fsdp, layer = self._capture_post_hook([]) + x = paddle.to_tensor([1.0, 2.0], stop_gradient=False) + outputs = {"a": x * 1.0, "b": x * 2.0} + result = layer.post_hook(layer, (), outputs) + (result["a"].sum() + result["b"].sum()).backward() + self.assertEqual(fsdp.comm_manager.backward_gathers, 1) + np.testing.assert_allclose(x.grad.numpy(), [3.0, 3.0]) + + def test_unhookable_values_kept_untouched(self): + hidden = paddle.to_tensor([1.0, 2.0], stop_gradient=False) * 1.0 + frozen = paddle.to_tensor([5.0]) # stop_gradient=True + weight = paddle.to_tensor([3.0, 4.0]) # exposed LM-head weight + _, layer = self._capture_post_hook([weight.name]) + outputs = {"h": hidden, "frozen": frozen, "w": weight, "n": 7} + result = layer.post_hook(layer, (), outputs) + self.assertEqual(list(result.keys()), ["h", "frozen", "w", "n"]) + self.assertIs(result["frozen"], frozen) + self.assertIs(result["w"], weight) + self.assertEqual(result["n"], 7) + np.testing.assert_array_equal(result["h"].numpy(), [1.0, 2.0]) + + def test_nothing_hookable_returns_outputs_without_hook(self): + _, layer = self._capture_post_hook([]) + outputs = {"frozen": paddle.to_tensor([1.0]), "n": 3} + with mock.patch.object(fsf.FusionBackwardHook, "apply") as apply: + result = layer.post_hook(layer, (), outputs) + apply.assert_not_called() + self.assertIs(result, outputs) + + def test_single_hookable_value_written_back(self): + # FusionBackwardHook.forward returns a bare tensor for one input. + fsdp, layer = self._capture_post_hook([]) + x = paddle.to_tensor([1.0, 2.0], stop_gradient=False) + outputs = {"a": x * 2.0, "n": 1} + result = layer.post_hook(layer, (), outputs) + self.assertIsInstance(result["a"], paddle.Tensor) + result["a"].sum().backward() + self.assertEqual(fsdp.comm_manager.backward_gathers, 1) + np.testing.assert_allclose(x.grad.numpy(), [2.0, 2.0]) + + class _FakeParamsBuffer: def __init__(self, status, is_sharded=True): self.status = status