Spaces:
Sleeping

File size: 7,575 Bytes
7fa4fd3
026c5f0
 
 
7fa4fd3
 
 
 
 
 
 
 
 
026c5f0
 
 
 
 
 
 
7fa4fd3
026c5f0
 
 
7fa4fd3
026c5f0
 
 
 
7fa4fd3
026c5f0
 
 
7fa4fd3
026c5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa4fd3
 
 
 
 
 
 
 
026c5f0
 
 
 
 
7fa4fd3
026c5f0
 
 
 
 
 
 
 
7fa4fd3
026c5f0
7fa4fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
026c5f0
 
 
7fa4fd3
026c5f0
7fa4fd3
 
026c5f0
 
7fa4fd3
026c5f0
7fa4fd3
 
026c5f0
 
 
 
 
 
 
 
7fa4fd3
 
 
026c5f0
7fa4fd3
 
 
 
 
 
 
 
 
 
 
 
 
026c5f0
 
 
 
7fa4fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
026c5f0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""DDP training through the trainer equals single-process training (CPU, gloo, 2 processes; no GPU, no data, no download).

``SynthScopeModelTrainer`` calls ``model.compute_loss(batch)``, which a ``DistributedDataParallel`` wrapper does not have;
``_compute_loss`` runs it through the wrapper's ``forward`` so the gradient reducer fires. Checked here, with a tiny model
(a trainable linear layer behind frozen parameters, like a frozen backbone), on a global batch of 16 samples per step:

* ``train_epoch`` (no accumulation): 2 processes x 8 equal one process x 16, and both ranks hold identical weights; the same
  with accumulation (2 processes x micro-batch 4 x accumulation 2),
* ``_train_with_timm_cosine`` (AIDE's path, with accumulation, 2 epochs): 2 processes x micro-batch 4 x accumulation 2 give
  the weights of one process x 16, of one process x 4 x accumulation 4, and of the same DDP run with ``no_sync`` disabled;
  the non-stepping micro-batches run under ``accelerator.no_sync`` (counted), the ranks stay identical,
* ``_train_with_step_budget`` (PGC/DDA path) with a final partial accumulation (5 micro-batches, accumulation 2): the last
  micro-batch syncs, so the ranks agree and match one process x 8,
* evaluation (``torch.no_grad`` + ``eval()``) works through the wrapper, and the inner module's ``forward`` is restored.

Run as a script: python tests/test_trainer_ddp.py
"""

from __future__ import annotations

import contextlib
import os
import socket
import sys
import tempfile
from pathlib import Path

import torch
import torch.multiprocessing as mp
from omegaconf import OmegaConf

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
N, FEAT = 32, 4      # samples; one epoch is 2 global steps of 16


class Tiny(torch.nn.Module):
    def __init__(self):
        super().__init__()
        torch.manual_seed(1)
        self.frozen = torch.nn.Linear(FEAT, FEAT)
        self.frozen.requires_grad_(False)
        self.head = torch.nn.Linear(FEAT, 2)

    def forward(self, x):
        return self.head(torch.relu(self.frozen(x)))

    def compute_loss(self, batch):
        x, y, _ = batch
        out = self(x)
        return {"loss": torch.nn.functional.cross_entropy(out, y), "outputs": out}


class _Logger:
    def log_metrics(self, *args, **kwargs):
        pass

    def finish(self):
        pass


def data():
    torch.manual_seed(0)
    return torch.randn(N, FEAT), torch.randint(0, 2, (N,)), torch.zeros(N, dtype=torch.long)


def build(micro):
    from accelerate import Accelerator
    from trainer import SynthScopeModelTrainer
    x, y, f = data()
    loader = torch.utils.data.DataLoader(list(zip(x, y, f)), batch_size=micro, shuffle=False)
    t = SynthScopeModelTrainer.__new__(SynthScopeModelTrainer)
    t.accelerator = Accelerator(cpu=True)
    t.model = Tiny()
    t.optimizer = torch.optim.SGD([p for p in t.model.parameters() if p.requires_grad], lr=0.1)
    t.scheduler, t.use_metrics, t.global_step, t.logger = None, False, 0, _Logger()
    t.model, t.optimizer, t.train_loader = t.accelerator.prepare(t.model, t.optimizer, loader)
    t.no_sync_calls = 0
    return t


def count_no_sync(t, enabled):
    """Count the accelerator.no_sync calls; with ``enabled=False`` they do nothing (the all-reduce runs on every micro-batch)."""
    real = t.accelerator.no_sync

    def no_sync(model):
        t.no_sync_calls += 1
        return real(model) if enabled else contextlib.nullcontext()

    t.accelerator.no_sync = no_sync


def run(mode, micro, accum, no_sync=True):
    t = build(micro)
    count_no_sync(t, no_sync)
    t.val = lambda: (0.0, {})
    t._save_model = lambda *args: None
    if mode == "train_epoch":
        t.train_epoch(accum=accum)
    elif mode == "timm_cosine":
        t.config = OmegaConf.create({"TrainerConfig": {"epochs": 2, "lr": 0.1, "save": False, "save_path": "unused"}})
        t._train_with_timm_cosine(OmegaConf.create({"warmup_epochs": 0, "warmup_lr": 0.0, "min_lr": 0.01,
                                                    "grad_accum_steps": accum, "log_every": 1000}))
    else:
        t.config = OmegaConf.create({"TrainerConfig": {"epochs": 1, "save_path": "unused"}})
        t._train_with_step_budget(OmegaConf.create({"grad_accum_steps": accum, "max_steps": 5, "save_every": 1000}))
    inner = t.accelerator.unwrap_model(t.model)
    assert "forward" not in inner.__dict__, "the inner forward was not restored"
    t.model.eval()
    x, y, f = data()
    with torch.no_grad():
        t._compute_loss((x[:4], y[:4], f[:4]))
    return inner.head.weight.detach().clone(), inner.head.bias.detach().clone(), t.global_step, t.no_sync_calls


def worker(rank, world, port, mode, micro, accum, no_sync, out):
    os.environ.update(RANK=str(rank), LOCAL_RANK=str(rank), WORLD_SIZE=str(world), MASTER_ADDR="127.0.0.1", MASTER_PORT=str(port))
    w, b, steps, calls = run(mode, micro, accum, no_sync)
    torch.save({"w": w, "b": b, "steps": steps, "calls": calls}, f"{out}.{rank}")


def free_port():
    with socket.socket() as s:
        s.bind(("", 0))
        return s.getsockname()[1]


def ddp(mode, micro, accum, tmp, no_sync=True):
    out = f"{tmp}/{mode}_{micro}_{accum}_{int(no_sync)}"
    mp.spawn(worker, args=(2, free_port(), mode, micro, accum, no_sync, out), nprocs=2, join=True)
    r0, r1 = torch.load(f"{out}.0"), torch.load(f"{out}.1")
    assert torch.equal(r0["w"], r1["w"]) and torch.equal(r0["b"], r1["b"]), f"{mode}: the ranks diverged"
    assert r0["steps"] == r1["steps"] and r0["calls"] == r1["calls"]
    return r0


def close(a, b, what):
    err = max((a["w"] - b["w"]).abs().max().item(), (a["b"] - b["b"]).abs().max().item())
    assert err < 1e-6, f"{what}: max difference {err}"


def single(mode, micro, accum):
    w, b, steps, calls = run(mode, micro, accum)
    return {"w": w, "b": b, "steps": steps, "calls": calls}


def test_ddp_equals_single_process():
    with tempfile.TemporaryDirectory() as tmp:
        # train_epoch, no accumulation: 2 x 8 = 16
        ref = single("train_epoch", 16, 1)
        got = ddp("train_epoch", 8, 1, tmp)
        assert got["steps"] == ref["steps"] == 2
        close(got, ref, "train_epoch")
        # train_epoch with accumulation (lr_step / FatFormer path): 2 procs x 4 x 2 = 16
        got = ddp("train_epoch", 4, 2, tmp)
        assert got["steps"] == ref["steps"] == 2
        close(got, ref, "train_epoch with accumulation")
        # timm_cosine with accumulation, 2 epochs x 2 steps: 2 procs x 4 x 2 = 16
        ref = single("timm_cosine", 16, 1)
        assert ref["steps"] == 4 and ref["calls"] == 0
        assert single("timm_cosine", 4, 4)["steps"] == 4
        close(single("timm_cosine", 4, 4), ref, "timm_cosine, one process x 4 x 4")
        with_sync = ddp("timm_cosine", 4, 2, tmp, no_sync=True)
        without = ddp("timm_cosine", 4, 2, tmp, no_sync=False)
        assert with_sync["steps"] == without["steps"] == 4
        assert with_sync["calls"] == 4 == 2 * 2 * (2 - 1), with_sync["calls"]   # epochs x steps x non-stepping micro-batches
        close(with_sync, ref, "timm_cosine, 2 procs x 4 x 2 vs one process x 16")
        close(with_sync, without, "timm_cosine, with vs without no_sync")
        # step_budget: 5 micro-batches of 8 (a final partial accumulation) = 3 optimizer steps
        ref = single("step_budget", 8, 2)
        got = ddp("step_budget", 4, 2, tmp)
        close(got, ref, "step_budget with a partial last accumulation")


if __name__ == "__main__":
    test_ddp_equals_single_process()
    print("DDP trainer checks passed")