from torch.profiler import ( profile, record_function, schedule, tensorboard_trace_handler ) with profile( schedule=schedule(wait=0, warmup=WARMUP_STEPS, active=PROFILE_STEPS, repeat=1), on_trace_ready=tensorboard_trace_handler('./baseline'), record_shapes=True, with_stack=True ) as prof: for i in range(TOTAL_STEPS): with record_function("get batch"): batch = next(data_iter) with record_function("copy batch"): batch = copy_data(batch) with record_function("compute"): compute_step(model, batch, optimizer) prof.step()