Skip to content

read crashes in GradSNR.observe when the set of parameters with gradients changes between steps #19

Open

Description

@nekomario28

minagi/optim.py GradSNR.observe builds flat by concatenating grads of only the parameters where p.grad is not None:

gs = [p.grad for p in params if p.grad is not None]
flat = torch.cat([g.detach().float().reshape(-1) for g in gs])
self.m = flat.clone() if self.m is None else \
    self.m.mul_(self.beta).add_(flat, alpha=1 - self.beta)

self.m is sized by the first observed set. On a later step where a different subset of trunk parameters received grads, flat has a different length and the EMA update raises:

RuntimeError: The size of tensor a (333697) must match the size of tensor b (333568) at non-singleton dimension 0

Reproduced on unmodified main (efd4a16): train.py --device cpu read <corpus-dir> --weights-dir <dir> --save dies early into a multi-subject read once the active gradient set varies (the delta in my config was 129 elements — consistent with intermittently-used params such as deeper sampled rows / halting-adjacent tensors).

Since recurrence depth is sampled during training (mean 3.87 of 6 rows in my run), intermittent-grad parameters are the norm, not a corner case — any long read eventually hits this. The SNR is explicitly report-only ("Reported, not acted on"), so telemetry ends up killing training.

Minimal fix sketch that keeps semantics — reset the EMA when the observed set changes size:

if self.m is None or self.m.numel() != flat.numel():
    self.m = flat.clone()
else:
    self.m.mul_(self.beta).add_(flat, alpha=1 - self.beta)

(Alternatively keep a stable index of parameters and zero-fill missing grads.) Happy to open a PR if you'd like.

Environment: torch 2.11, CPU (device-independent — it's a shape mismatch on the flattened buffer).

Activity

  1. dreddnafious commented on Sep 23, 2026

    @dreddnafious

    Hit this too during a replication run. The varying element is the halting head: halt.weight + halt.bias = d_model + 1 elements. That's your 129 at d_model 128, and 257 at d_model 256 for me. A step whose sampled depth is 1 forces lam = 1 on the only row, so the halting head gets no gradient that step. At the shipped train_steps_mean 12.8 that's ~3e-6 per step, but at smaller depths it's routine (~1.4% of steps at mean 4.3).

    I'd suggest zero-filling rather than resetting the EMA. A missing grad is a zero grad for that step, and a reset would throw the meter's history away on every depth-1 step:

    if all(p.grad is None for p in params):
        return None
    flat = torch.cat([(p.grad if p.grad is not None else torch.zeros_like(p))
                      .detach().float().reshape(-1) for p in params])

    Since you offered to PR this, I hope you don't mind that I've opened #20 with the zero-fill version plus a regression test. It credits your report, and it ran ~8 GPU-hours of read without issue. If you'd rather land your own, I'm happy to close it.

  2. nekomario28 commented on Sep 23, 2026

    @nekomario28
    Author

    Thanks — your root-cause analysis is sharper than mine. The depth-1/lam=1 mechanism explains the intermittency precisely, and zero-fill is the right fix: a missing grad is a zero grad for that step, while my local workaround (resetting the EMA on shape change) threw the meter's history away on every depth-1 step.

    Please keep #20 — there's nothing worth landing from my side. If it merges, a Co-authored-by trailer would be appreciated, since the repro and diagnosis came from our run.

  3. added a commit that references this issue on Oct 3, 2026
    c201d51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions