Evaluate text examples with optional contexts and images, returning various evaluation metrics. :param examples: A batched list of text examples or their encoded token lists. :param contexts: Optional; a list of context strings or token lists. If not None, each item
(self, examples: List[str|List[int]], contexts: Optional[List[str|List[int]]] = None,
images:Optional[torch.FloatTensor]=None, bos=True, eos=False)
| 297 | |
| 298 | @ torch.inference_mode() |
| 299 | def evaluate_examples(self, examples: List[str|List[int]], contexts: Optional[List[str|List[int]]] = None, |
| 300 | images:Optional[torch.FloatTensor]=None, bos=True, eos=False) -> Dict[str, List]: |
| 301 | """ |
| 302 | Evaluate text examples with optional contexts and images, returning various evaluation metrics. |
| 303 | |
| 304 | |
| 305 | :param examples: A batched list of text examples or their encoded token lists. |
| 306 | :param contexts: Optional; a list of context strings or token lists. If not None, each item |
| 307 | should be the preceding part of the corresponding example and is considered as context. |
| 308 | The calculation of evaluation metrics will be conducted only on the remaining part |
| 309 | of the examples. |
| 310 | :param images: Optional; batched image tensor to be used in conjunction with the text examples. |
| 311 | Shape: (bsz, channel, h, w). |
| 312 | :param bos: Whether to include begin-of-sequence tokens for tokenization. Only effective when items |
| 313 | in `examples` are strings. Default is True. |
| 314 | :param eos: Whether to include end-of-sequence tokens for tokenization. Only effective when items |
| 315 | in `examples` are strings. Default is False. |
| 316 | :return: A dictionary containing evaluation metrics including log likelihood, perplexity, max_equal, |
| 317 | and non_content_logits. |
| 318 | |
| 319 | :Examples: |
| 320 | >>> model = MetaModel(...) |
| 321 | >>> # evaluate on the entire examples |
| 322 | >>> model.evaluate_examples(["The best programming language is C", "The best programming language is Python"]) |
| 323 | >>> # treat "The best programming language is" as context and only evaluate on " C" and " Python" |
| 324 | >>> model.evaluate_examples( |
| 325 | >>> examples=["The best programming language is C", "The best programming language is Python"], |
| 326 | >>> contexts=["The best programming language is", "The best programming language is"] |
| 327 | >>> ) |
| 328 | """ |
| 329 | if isinstance(examples, str): |
| 330 | raise ValueError(f"{self.__class__}.generate expects a batched LIST of prompts, but str is given") |
| 331 | |
| 332 | if isinstance(examples[0], str): |
| 333 | examples = [self.tokenizer.encode(_, bos, eos) for _ in examples] |
| 334 | if contexts is not None: |
| 335 | contexts = [self.tokenizer.encode(_, bos, False) for _ in contexts] |
| 336 | if contexts is not None: |
| 337 | # when context is not None, `example == context + output` should hold, |
| 338 | # namely example should start with context |
| 339 | assert all([e[:len(c)] == c for e, c in zip(examples, contexts)]) |
| 340 | |
| 341 | if images is not None: |
| 342 | images = images.to(list(self.parameters())[0].device) |
| 343 | |
| 344 | logits = self.compute_logits(examples, images) |
| 345 | |
| 346 | loss_func = torch.nn.CrossEntropyLoss(reduction='none', ignore_index=0) |
| 347 | |
| 348 | result = {'log_likelihood': [], 'ppl': [], 'max_equal': [], 'non_context_logits': []} |
| 349 | for item_idx, item_logits in enumerate(logits): |
| 350 | if contexts is None: |
| 351 | logits_start = 0 |
| 352 | else: |
| 353 | logits_start = len(contexts[item_idx]) - 1 |
| 354 | assert logits_start >= 0 |
| 355 | |
| 356 | item_logits = item_logits[logits_start:-1] |
nothing calls this directly
no test coverage detected