Repository navigation
[SDC] Add deterministic replay engine - #4333
Conversation
There was a problem hiding this comment.
folder recently refactored -- need to put in either cpu/ or gpu/
| num_replays: int = 1 | ||
| """Number of candidate executions compared with the reference execution.""" | ||
|
|
||
| def validate(self) -> None: |
| class SDCReplay: | ||
| """Coordinates state restoration, repeated execution, and comparison.""" | ||
|
|
||
| Config = SDCReplayConfig |
There was a problem hiding this comment.
Can just define it here as SDCReplay.Config (take Trainer.Config as an example)
| """Enable deterministic forward/backward replay for SDC detection.""" | ||
|
|
||
| num_steps: int = 1 | ||
| """Number of attempt-local optimizer steps to check; -1 checks every step.""" |
There was a problem hiding this comment.
"attempt-local" sounds confusing, could you expand a bit?
| num_steps: int = 1 | ||
| """Number of attempt-local optimizer steps to check; -1 checks every step.""" | ||
|
|
||
| num_replays: int = 1 |
There was a problem hiding this comment.
As someone who doesn't know exactly when and what span SDC is replaying, the two fields are hard difficult for me to understand.
- would appreciate more education
- is this the only way of doing SDC replay?
| """Number of attempt-local optimizer steps to check; -1 checks every step.""" | ||
|
|
||
| num_replays: int = 1 | ||
| """Number of candidate executions compared with the reference execution.""" |
There was a problem hiding this comment.
what is "candidate execution" unit?
| return None | ||
|
|
||
|
|
||
| class SDCReplay: |
There was a problem hiding this comment.
| class SDCReplay: | |
| class SDCReplayer: |
There was a problem hiding this comment.
not very distributed to me, maybe should put under observability/
9dfc9b2 to
7640988
Compare
There was a problem hiding this comment.
please rename to sdc_replayer.py to be consistent with structured_logger and tensor_logger (wip) in this folder
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class ReplaySignature: |
There was a problem hiding this comment.
should this be
| class ReplaySignature: | |
| class _ReplaySignature: |
if it's only used within this file; similar comment to other dataclasses
|
|
||
| @dataclass(kw_only=True, slots=True) | ||
| class Config(Configurable.Config): | ||
| enabled: bool = False |
There was a problem hiding this comment.
I'd prefer not having this field, and rely on sdc_replayer being None or not to trigger it.
There's some historical reason to use enabled style args (e.g. CLI), but we are moving away.
| gradients = tuple( | ||
| _GradientSnapshot( | ||
| parameter=parameter, | ||
| original=parameter.grad, | ||
| value=( | ||
| None if parameter.grad is None else _clone_tensor(parameter.grad) | ||
| ), | ||
| ) | ||
| for parameter in parameters | ||
| ) |
There was a problem hiding this comment.
If we are only playing the first step of grad accumulation, why we'd need to capture this?
There was a problem hiding this comment.
We do need to capture the param.grad to reset the state to before the reference capture. If we don't do the clone, we could branch again on CG zero_grad(set_to_none=self.config.training.disable_cuda_graphs), grads can be None or zero tensor. I like the decoupling copying buys you, but it does come at a copy kernel cost.
There was a problem hiding this comment.
changed my mind, perf is important if we want to have sdc run periodically and grads are large. The common case should be either None grads or zero grads, so let's bake it in. I added docstrings to SDCReplayer, but did not add any checks that would introduce more kernels.
There was a problem hiding this comment.
Then that means we couldn't replay other than the first grad accum step, which is fine for now.
Add a model-agnostic deterministic replay engine for detecting silent data corruption across repeated forward/backward executions. For each checked optimizer step, SDCReplayer: 1. captures the semantic baseline state; 2. executes the step's first forward/backward and records a reference signature; 3. restores the baseline before every replay; 4. compares each replay with the reference across all ranks; and 5. discards the reference and intermediate executions so only the final matching execution remains committed. The checked forward/backward is one gradient-accumulation group, which under pipeline parallelism is one complete pipeline schedule; when a step has multiple accumulation groups only the first is checked, a cost choice rather than an engine limitation since the engine replays any forward/backward callable. The captured state includes Python, CPU, and accelerator RNG state, registered module buffers, and caller-owned scalars passed through ScalarStateAccessor (e.g. a token counter the forward/backward mutates). Gradient values are never snapshotted: run_fwd_bwd requires entry gradients to be empty (None or zeros, the post-zero_grad state), and restore rebuilds that entry state directly from per-parameter None markers and tensor identities -- parameters that entered without a gradient return to None, and entry gradient tensors are zeroed in place. This avoids gradient-sized clone and copy kernels on checked steps while keeping gradient storage addresses stable for CUDA graphs, without coupling the engine to any caller-provided reset. Replay signatures cover the loss, gradients, buffers, RNG advancement, and scalar state, with DTensors compared and restored through their local shards. SDCReplayer follows the Configurable pattern with a nested Config validated in __post_init__, and lives in torchtitan/observability/sdc_replayer.py next to the other observability components. The Config carries no enabled flag: callers express "disabled" by not constructing a replayer. Module-internal signature and snapshot types are underscore-private; the public surface is SDCReplayer, ScalarStateAccessor, and SDCReplayMismatch. The replayer owns its check schedule: run_fwd_bwd counts the optimizer steps it has seen (steps_since_reset) and checks the first num_steps of them; reset_schedule restarts the count (e.g. after a checkpoint load). A mismatch raises SDCReplayMismatch on every rank, reporting the global step, the step's position in the current check schedule (local_step), the replay number, the originating rank, and the first divergent signature entry. Tensor digests remain on-device and are compared as one batch. The matching path performs one host synchronization after the distributed mismatch reduction; detailed per-entry comparison runs only after a mismatch. The implementation uses the existing XOR-based torch.hash_tensor mode; that checksum is order-insensitive, so permutations and some repeated-value corruptions can collide. Unregistered execution scratch state is intentionally outside the replay contract when each invocation overwrites it before reading it. - pytest -q tests/unit_tests/cpu/observability/test_sdc_replayer.py - 18 passed Coverage includes state restoration, final-execution commit semantics, compiled execution, DTensor state, replay scheduling and re-arming, batched digest comparison, and local and remote mismatch reporting. stack-info: PR: #4333, branch: xmfan/stack/44
Stacked PRs:
[SDC] Add deterministic replay engine
Add a model-agnostic deterministic replay engine for detecting silent data
corruption across repeated forward/backward executions.
For each checked optimizer step, SDCReplayer:
signature;
matching execution remains committed.
The checked forward/backward is one gradient-accumulation group, which under
pipeline parallelism is one complete pipeline schedule; when a step has
multiple accumulation groups only the first is checked, a cost choice rather
than an engine limitation since the engine replays any forward/backward
callable.
The captured state includes Python, CPU, and accelerator RNG state,
registered module buffers, and caller-owned scalars passed through
ScalarStateAccessor (e.g. a token counter the forward/backward mutates).
Gradient values are never snapshotted: run_fwd_bwd requires entry gradients
to be empty (None or zeros, the post-zero_grad state), and restore rebuilds
that entry state directly from per-parameter None markers and tensor
identities -- parameters that entered without a gradient return to None,
and entry gradient tensors are zeroed in place. This avoids gradient-sized
clone and copy kernels on checked steps while keeping gradient storage
addresses stable for CUDA graphs, without coupling the engine to any
caller-provided reset. Replay signatures cover
the loss, gradients, buffers, RNG advancement, and scalar state, with
DTensors compared and restored through their local shards.
SDCReplayer follows the Configurable pattern with a nested Config validated
in post_init, and lives in torchtitan/observability/sdc_replayer.py next
to the other observability components. The Config carries no enabled flag:
callers express "disabled" by not constructing a replayer. Module-internal
signature and snapshot types are underscore-private; the public surface is
SDCReplayer, ScalarStateAccessor, and SDCReplayMismatch. The replayer owns
its check schedule: run_fwd_bwd counts the optimizer steps it has seen
(steps_since_reset) and checks the first num_steps of them; reset_schedule
restarts the count (e.g. after a checkpoint load). A mismatch raises
SDCReplayMismatch on every rank, reporting the global step, the step's
position in the current check schedule (local_step), the replay number, the
originating rank, and the first divergent signature entry.
Tensor digests remain on-device and are compared as one batch. The matching
path performs one host synchronization after the distributed mismatch
reduction; detailed per-entry comparison runs only after a mismatch. The
implementation uses the existing XOR-based torch.hash_tensor mode; that
checksum is order-insensitive, so permutations and some repeated-value
corruptions can collide. Unregistered execution scratch state is
intentionally outside the replay contract when each invocation overwrites it
before reading it.
Coverage includes state restoration, final-execution commit semantics,
compiled execution, DTensor state, replay scheduling and re-arming, batched
digest comparison, and local and remote mismatch reporting.