MCPcopy
hub / github.com/deepspeedai/DeepSpeedExamples / evaluate

Function evaluate

Megatron-LM/pretrain_gpt2.py:468–495  ·  view source on GitHub ↗

Evaluation.

(data_iterator, model, args, timers, verbose=False)

Source from the content-addressed store, hash-verified

466
467
468def evaluate(data_iterator, model, args, timers, verbose=False):
469 """Evaluation."""
470
471 # Turn on evaluation mode which disables dropout.
472 model.eval()
473
474 total_lm_loss = 0
475
476 with torch.no_grad():
477 iteration = 0
478 while iteration < args.eval_iters:
479 iteration += 1
480 if verbose and iteration % args.log_interval == 0:
481 print_rank_0('Evaluating iter {}/{}'.format(iteration, args.eval_iters))
482 # Forward evaluation.
483 lm_loss = forward_step(data_iterator, model, args, timers)
484 # Reduce across processes.
485 if isinstance(model, DDP):
486 torch.distributed.all_reduce(lm_loss.data)
487 lm_loss.data = lm_loss.data / args.world_size
488
489 total_lm_loss += lm_loss.data.detach().float().item()
490
491 # Move model back to the train mode.
492 model.train()
493
494 total_lm_loss /= args.eval_iters
495 return total_lm_loss
496
497
498def evaluate_and_print_results(prefix, data_iterator, model,

Callers 1

Calls 4

print_rank_0Function · 0.90
evalMethod · 0.80
trainMethod · 0.80
forward_stepFunction · 0.70

Tested by

no test coverage detected