MCPcopy Create free account
hub / github.com/Francis-Rings/StableAnimator / CallBackLogging

Class CallBackLogging

animation/helper/utils/utils_callbacks.py:68–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66
67
68class CallBackLogging(object):
69 def __init__(self, frequent, total_step, batch_size, start_step=0,writer=None):
70 self.frequent: int = frequent
71 self.rank: int = distributed.get_rank()
72 self.world_size: int = distributed.get_world_size()
73 self.time_start = time.time()
74 self.total_step: int = total_step
75 self.start_step: int = start_step
76 self.batch_size: int = batch_size
77 self.writer = writer
78
79 self.init = False
80 self.tic = 0
81
82 def __call__(self,
83 global_step: int,
84 loss: AverageMeter,
85 epoch: int,
86 fp16: bool,
87 learning_rate: float,
88 grad_scaler: torch.cuda.amp.GradScaler):
89 if self.rank == 0 and global_step > 0 and global_step % self.frequent == 0:
90 if self.init:
91 try:
92 speed: float = self.frequent * self.batch_size / (time.time() - self.tic)
93 speed_total = speed * self.world_size
94 except ZeroDivisionError:
95 speed_total = float('inf')
96
97 #time_now = (time.time() - self.time_start) / 3600
98 #time_total = time_now / ((global_step + 1) / self.total_step)
99 #time_for_end = time_total - time_now
100 time_now = time.time()
101 time_sec = int(time_now - self.time_start)
102 time_sec_avg = time_sec / (global_step - self.start_step + 1)
103 eta_sec = time_sec_avg * (self.total_step - global_step - 1)
104 time_for_end = eta_sec/3600
105 if self.writer is not None:
106 self.writer.add_scalar('time_for_end', time_for_end, global_step)
107 self.writer.add_scalar('learning_rate', learning_rate, global_step)
108 self.writer.add_scalar('loss', loss.avg, global_step)
109 if fp16:
110 msg = "Speed %.2f samples/sec Loss %.4f LearningRate %.6f Epoch: %d Global Step: %d " \
111 "Fp16 Grad Scale: %2.f Required: %1.f hours" % (
112 speed_total, loss.avg, learning_rate, epoch, global_step,
113 grad_scaler.get_scale(), time_for_end
114 )
115 else:
116 msg = "Speed %.2f samples/sec Loss %.4f LearningRate %.6f Epoch: %d Global Step: %d " \
117 "Required: %1.f hours" % (
118 speed_total, loss.avg, learning_rate, epoch, global_step, time_for_end
119 )
120 logging.info(msg)
121 loss.reset()
122 self.tic = time.time()
123 else:
124 self.init = True
125 self.tic = time.time()

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected