Forward step.
(data_iterator, model, args, timers)
| 66 | |
| 67 | |
| 68 | def forward_step(data_iterator, model, args, timers): |
| 69 | """Forward step.""" |
| 70 | |
| 71 | # Get the batch. |
| 72 | timers('batch generator').start() |
| 73 | tokens, labels, attention_mask, position_ids, loss_mask = get_batch( |
| 74 | data_iterator, args, timers) |
| 75 | timers('batch generator').stop() |
| 76 | |
| 77 | logits, *mems = model(tokens, position_ids, attention_mask) |
| 78 | # pred = ((logits.contiguous().float().squeeze(-1)) * loss_mask).sum(dim=-1) / loss_mask.sum(dim=-1) |
| 79 | pred = logits.contiguous().float().squeeze(-1)[..., 0] |
| 80 | loss = torch.nn.functional.binary_cross_entropy_with_logits( |
| 81 | pred, |
| 82 | labels.float() |
| 83 | ) |
| 84 | acc = ((pred > 0.).long() == labels).sum() / labels.numel() |
| 85 | return loss, {'acc': acc} |
| 86 | |
| 87 | |
| 88 | from sat.data_utils import load_hf_dataset |