Helper function that checks if model output and reference output are equal with some tolerance. Returns True if equal, False otherwise.
(
model_output, ref_output, atol=1e-03, rtol=1e-03, first_output_only=False
)
| 483 | |
| 484 | |
| 485 | def check_outputs_equal( |
| 486 | model_output, ref_output, atol=1e-03, rtol=1e-03, first_output_only=False |
| 487 | ): |
| 488 | """ |
| 489 | Helper function that checks if model output and reference output are equal with some tolerance. |
| 490 | Returns True if equal, False otherwise. |
| 491 | """ |
| 492 | # Convert OrderedDict to list if needed |
| 493 | if isinstance(ref_output, OrderedDict): |
| 494 | ref_output = list(ref_output.values()) |
| 495 | |
| 496 | # Compare the result from executor and eager mode directly |
| 497 | if isinstance(ref_output, tuple) or isinstance(ref_output, list): |
| 498 | # Multiple outputs executor always returns tuple, even if there is one output |
| 499 | if len(ref_output) != len(model_output): |
| 500 | print_tensor_comparison_errors(model_output, ref_output, atol, rtol) |
| 501 | return False |
| 502 | if first_output_only: |
| 503 | result = torch.allclose( |
| 504 | model_output[0], ref_output[0], atol=atol, rtol=rtol |
| 505 | ) |
| 506 | if not result: |
| 507 | print_tensor_comparison_errors( |
| 508 | model_output[0], ref_output[0], atol, rtol |
| 509 | ) |
| 510 | return result |
| 511 | else: |
| 512 | result = True |
| 513 | for i in range(len(ref_output)): |
| 514 | if isinstance(ref_output[i], torch.Tensor): |
| 515 | if not torch.allclose( |
| 516 | model_output[i], ref_output[i], atol=atol, rtol=rtol |
| 517 | ): |
| 518 | print(f"\n=== Output {i} comparison failed ===") |
| 519 | print_tensor_comparison_errors( |
| 520 | model_output[i], ref_output[i], atol, rtol |
| 521 | ) |
| 522 | result = False |
| 523 | elif isinstance(ref_output[i], int): |
| 524 | if not model_output[i] == ref_output[i]: |
| 525 | print(f"\n=== Output {i} comparison failed ===") |
| 526 | print(f"{model_output[i]} vs {ref_output[[i]]}") |
| 527 | result = False |
| 528 | else: |
| 529 | print(f"WARNING: Output {i} has type {type(ref_output[i])}") |
| 530 | return result |
| 531 | else: |
| 532 | # If one output, eager returns tensor while executor tuple of size 1 |
| 533 | result = torch.allclose(model_output[0], ref_output, atol=atol, rtol=rtol) |
| 534 | if not result: |
| 535 | print_tensor_comparison_errors(model_output[0], ref_output, atol, rtol) |
| 536 | return result |
| 537 | |
| 538 | |
| 539 | def run_and_check_output( |
no test coverage detected