MCPcopy Create free account
hub / github.com/FusionBrainLab/SONAR-LLM / evaluate

Function evaluate

train_sonarllm.py:547–571  ·  view source on GitHub ↗
(model, val_dloader, device, distributed)

Source from the content-addressed store, hash-verified

545# 5) Evaluation function
546# ------------------------------------------------------------------------
547def evaluate(model, val_dloader, device, distributed):
548 model.eval()
549
550 total_loss = 0.0
551 total_count = 0
552 with torch.no_grad():
553 for batch in val_dloader:
554 embeddings_1024 = batch["embeddings"].to(device)
555 texts = batch["texts"]
556 seq_lens = batch["seq_lens"]
557
558 with autocast(enabled=False):
559 loss = model(embeddings_1024, texts, seq_lens)
560
561 bs = embeddings_1024.size(0)
562 total_loss += loss.item() * bs
563 total_count += bs
564
565 if distributed:
566 result = torch.tensor([total_loss, total_count], device=device, dtype=torch.float32)
567 dist.all_reduce(result, op=dist.ReduceOp.SUM)
568 total_loss, total_count = result[0].item(), result[1].item()
569
570 model.train()
571 return (total_loss / total_count) if total_count > 0 else 0.0
572
573# ------------------------------------------------------------------------
574# 6) Full-checkpoint Save & Load

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected