MCPcopy Create free account
hub / github.com/Pints-AI/1.5-Pints / on_train_batch_end

Method on_train_batch_end

lit_gpt/speed_monitor.py:250–338  ·  view source on GitHub ↗
(
        self,
        samples: int,  # total samples seen (per device)
        train_elapsed: float,  # total training time (seconds)
        world_size: int,
        step_count: int,
        flops_per_batch: Optional[int] = None,  # (per device)
        lengths: Optional[int] = None,  # total length of the samples seen (per device)
        train_loss: Optional[float] = None,
    )

Source from the content-addressed store, hash-verified

248 self.iter = -1
249
250 def on_train_batch_end(
251 self,
252 samples: int, # total samples seen (per device)
253 train_elapsed: float, # total training time (seconds)
254 world_size: int,
255 step_count: int,
256 flops_per_batch: Optional[int] = None, # (per device)
257 lengths: Optional[int] = None, # total length of the samples seen (per device)
258 train_loss: Optional[float] = None,
259 ):
260 self.iter += 1
261 metrics = {}
262
263 self.history_samples.append(samples)
264 self.history_training_loss.append(train_loss)
265 if lengths is not None:
266 self.history_lengths.append(lengths)
267 # if lengths are passed, there should be as many values as samples
268 assert len(self.history_samples) == len(self.history_lengths)
269 self.history_wct.append(train_elapsed)
270 if len(self.history_wct) == self.history_wct.maxlen:
271 elapsed_batches = len(self.history_samples) - 1
272 elapsed_samples = self.history_samples[-1] - self.history_samples[0]
273 elapsed_wct = self.history_wct[-1] - self.history_wct[0]
274 samples_per_sec = elapsed_samples * world_size / elapsed_wct
275 dev_samples_per_sec = elapsed_samples / elapsed_wct
276 metrics.update(
277 {
278 'throughput/batches_per_sec': elapsed_batches
279 * world_size
280 / elapsed_wct,
281 'throughput/samples_per_sec': samples_per_sec,
282 'throughput/device/batches_per_sec': elapsed_batches / elapsed_wct,
283 'throughput/device/samples_per_sec': dev_samples_per_sec,
284 }
285 )
286 if lengths is not None:
287 elapsed_lengths = int(self.history_lengths[-1]) - int(
288 self.history_lengths[0]
289 )
290 avg_length = elapsed_lengths / elapsed_batches
291 metrics.update(
292 {
293 'throughput/tokens_per_sec': samples_per_sec * avg_length,
294 'throughput/device/tokens_per_sec': dev_samples_per_sec
295 * avg_length,
296 'total_tokens': avg_length * world_size * samples,
297 }
298 )
299 if train_loss is not None:
300 avg_loss = sum(self.history_training_loss) / len(
301 self.history_training_loss
302 )
303 metrics.update(
304 {
305 'metric/train_loss': avg_loss,
306 'metric/train_ppl': math.exp(avg_loss),
307 }

Callers 3

on_train_batch_endMethod · 0.45
on_train_batch_endMethod · 0.45
trainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected