RuntimeProfiler#
- class RuntimeProfiler(forward=True, backward=True, interrupt=False, warm_up=2, sort_by='cuda_time_total', row_limit=30, memory=True)[source]#
Bases:
HookBaseRun a short
torch.profilertrace before normal training proceeds.Runs once in
before_train: iterateswarm_up + 1batches from the train loader undertorch.profiler.profile(CPU+CUDA activities), running forward (and backward, on the"loss"output) per theforward/backwardflags. It writes a TensorBoard trace into<save_path>/logdir/, logs the top operators sorted bysort_by, and — whenmemory=True— records and dumps a CUDA memory snapshot to<save_path>/memory_snapshot.pickle. Withinterrupt=Trueit callssys.exit(0)after profiling so the process stops before real training. Registered asRuntimeProfiler.- Parameters:
forward (bool) – Run the model forward pass during profiling. Defaults to
True.backward (bool) – Run
loss.backward()during profiling. Defaults toTrue.interrupt (bool) – Exit the process (
sys.exit(0)) after profiling instead of continuing into training. Defaults toFalse.warm_up (int) – Number of warm-up iterations before the active profiling window (the loop runs
warm_up + 1steps). Defaults to2.sort_by (str) – Key for sorting the printed operator table, e.g.
"cuda_time_total". Defaults to"cuda_time_total".row_limit (int) – Maximum rows in the printed operator table. Defaults to
30.memory (bool) – Record and dump a CUDA memory-history snapshot. Defaults to
True.
Note
Intended as a diagnostic run, typically with
interrupt=Trueso the job exits after collecting the trace. Requires CUDA for the memory snapshot.Example
Add to
cfg.hooksfor a one-off profiling run; once inbefore_trainit traces a few steps, then (withinterrupt=True) exits before real training begins:hooks = [dict(type="RuntimeProfiler", warm_up=2, interrupt=True)] # → runs warm_up+1 batches under torch.profiler, writes a TensorBoard # trace to <save_path>/logdir/, dumps <save_path>/memory_snapshot.pickle, # logs the top-30 ops by cuda_time_total, then calls sys.exit(0)