| 224 | value.requires_grad = True |
| 225 | |
| 226 | def forward(self, examples, labels, images=None): |
| 227 | with torch.no_grad(): |
| 228 | non_zero_ = torch.count_nonzero(labels, dim=0) |
| 229 | pos = non_zero_.shape[0] - 1 |
| 230 | while pos >= 0: |
| 231 | if non_zero_[pos] == 0: |
| 232 | pos -= 1 |
| 233 | else: |
| 234 | break |
| 235 | |
| 236 | if pos == -1: # nothing to predict in the whole batch |
| 237 | print(f"[RANK {dist.get_rank()}] nothing to predict in the whole batch!", force=True) |
| 238 | print(examples.cpu().tolist(), force=True) |
| 239 | pos = 2 |
| 240 | examples = examples[:, :pos+1] |
| 241 | labels = labels[:, :pos+1] |
| 242 | |
| 243 | output = self.llma(examples, images) |
| 244 | if isinstance(output, tuple): |
| 245 | output, additional_loss = output |
| 246 | else: |
| 247 | additional_loss = {} |
| 248 | output = output[:, :-1, :] |
| 249 | labels = labels[:, 1:] |
| 250 | |
| 251 | if labels.sum() == 0: |
| 252 | c_loss = output.mean() * 0 |
| 253 | else: |
| 254 | c_loss = self.criterion(output.reshape(-1, self.tokenizer.n_words), labels.flatten()) |
| 255 | return c_loss, additional_loss |
| 256 | |
| 257 | @ torch.inference_mode() |
| 258 | def compute_logits(self, examples: List[str|List[int]], images:Optional[torch.FloatTensor]=None, |