TorchTitan provides structured logging for distributed training, emitting per-rank JSONL events for phase timing, diagnostics, and analysis. To use it, call init_logger() and sl.init_structured_logger() once per process before any trace calls. Every record automatically includes rank, source, caller (file:line:function), time_us, step, relative_step, and step_tags.
from torchtitan.tools.logging import init_logger
from torchtitan.observability import structured_logger as sl
# console logger (stdout, [titan] prefix)
init_logger()
# Register handlers (e.g., to save to a local jsonl)
sl.init_structured_logger(source="training", output_dir="./outputs")
# Register a point-in-time marker
sl.log_trace_instant("training_start")
loaded_step = 0
for step in range(loaded_step + 1, num_steps + 1):
# Stamp subsequent records with `step` and `relative_step`
sl.set_step(step, relative_step=step - loaded_step)
if should_garbage_collect:
# Annotate the current step; tags reset at the next set_step()
sl.add_step_tag("gc")
with sl.log_trace_span("gc_collect"):
run_gc()
with sl.log_trace_span("fwd_bwd"):
output = model(batch)
loss.backward()
with sl.log_trace_span("Optimizer"):
optimizer.step()
# Register scalars for debugging
sl.log_trace_scalar({
"num_trainable_tokens": num_trainable_tokens,
"batch_size": bsz
})