Skip to content

add tensorboard to training script #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ dist/

# Torch
cifar/

output/
29 changes: 23 additions & 6 deletions train_diloco.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from torch.distributed.elastic.multiprocessing.errors import record
from torch.distributed.pipelining import SplitPoint, pipeline
from torch.export import export
from torch.utils.tensorboard import SummaryWriter
from torchdata.stateful_dataloader import StatefulDataLoader

from torchft import (
Expand All @@ -41,7 +42,11 @@
@record
def main() -> None:
REPLICA_GROUP_ID = int(os.environ.get("REPLICA_GROUP_ID", 0))
NUM_REPLICA_GROUPS = int(os.environ.get("NUM_REPLICA_GROUPS", 2))
RUN = int(os.environ.get("RUN", 0))

output_folder = f"output/replica-{REPLICA_GROUP_ID}"

writer = SummaryWriter(f"{output_folder}/tensorboard", max_queue=1000)

def load_state_dict(state_dict):
m.load_state_dict(state_dict["model"])
Expand Down Expand Up @@ -171,12 +176,12 @@ def forward(self, x):
num_params = sum(p.numel() for p in m.parameters())
print(f"Total number of parameters: {num_params}")

sort_by_keyword = "self_" + device + "_time_total"

def trace_handler(p):
p.export_chrome_trace(
f"/home/tushar00jain/trace_{p.step_num}_{REPLICA_GROUP_ID}.json"
)
dir = f"{output_folder}/profiles"
if not os.path.exists(dir):
os.makedirs(dir, exist_ok=True)

p.export_chrome_trace(f"{dir}/step-{p.step_num}.json")

# You can use an epoch based training but with faults it's easier to use step
# based training.
Expand All @@ -188,6 +193,7 @@ def trace_handler(p):
)

prof.start()
tensorboard_key_prefix = f"Run:{RUN}"
with DiLoCo(
manager,
module_partitions if USE_STREAMING else [m],
Expand All @@ -210,16 +216,27 @@ def trace_handler(p):
out = m(inputs)
loss = criterion(out, labels)

writer.add_scalar(f"{tensorboard_key_prefix}/loss", loss, i)

loss.backward()

inner_optimizer.step()

writer.add_scalar(
f"{tensorboard_key_prefix}/num_participants",
manager.num_participants(),
i,
)
writer.add_scalar(
f"{tensorboard_key_prefix}/current_step", manager.current_step(), i
)
if manager.current_step() % 100 == 0:
print(f"[{manager.current_step()}] loss = {loss.item()}")

if manager.current_step() >= 15:
# complete training
prof.stop()
writer.flush()
exit()


Expand Down
Loading