Helper testing function that takes a torch.nn.Module and lowers it to Vulkan with the given sample inputs. It then runs the lowered module and compares its outputs with the outputs of the eager module. Returns: bool: True if all comparisons pass, False otherwise.
(
model: torch.nn.Module,
sample_inputs: Tuple[torch.Tensor],
atol=1e-03,
rtol=1e-01,
dynamic_shapes=None,
test_inputs=None,
first_output_only=False,
operator_blocklist=None,
operator_allowlist=None,
nn_module_allowlist=None,
nn_module_blocklist=None,
xnnpack=False,
)
| 592 | |
| 593 | |
| 594 | def lower_module_and_test_output( |
| 595 | model: torch.nn.Module, |
| 596 | sample_inputs: Tuple[torch.Tensor], |
| 597 | atol=1e-03, |
| 598 | rtol=1e-01, |
| 599 | dynamic_shapes=None, |
| 600 | test_inputs=None, |
| 601 | first_output_only=False, |
| 602 | operator_blocklist=None, |
| 603 | operator_allowlist=None, |
| 604 | nn_module_allowlist=None, |
| 605 | nn_module_blocklist=None, |
| 606 | xnnpack=False, |
| 607 | ) -> bool: |
| 608 | """ |
| 609 | Helper testing function that takes a torch.nn.Module and lowers it to Vulkan with |
| 610 | the given sample inputs. It then runs the lowered module and compares its |
| 611 | outputs with the outputs of the eager module. |
| 612 | |
| 613 | Returns: |
| 614 | bool: True if all comparisons pass, False otherwise. |
| 615 | """ |
| 616 | # Export model to Vulkan using the helper function |
| 617 | if xnnpack: |
| 618 | executorch_program = export_model_to_xnnpack( |
| 619 | model, |
| 620 | make_copy_of_inputs(sample_inputs), |
| 621 | dynamic_shapes, |
| 622 | operator_blocklist, |
| 623 | operator_allowlist, |
| 624 | nn_module_blocklist, |
| 625 | nn_module_allowlist, |
| 626 | ) |
| 627 | else: |
| 628 | executorch_program = export_model_to_vulkan( |
| 629 | model, |
| 630 | make_copy_of_inputs(sample_inputs), |
| 631 | dynamic_shapes, |
| 632 | operator_blocklist=operator_blocklist, |
| 633 | operator_allowlist=operator_allowlist, |
| 634 | nn_module_blocklist=nn_module_blocklist, |
| 635 | nn_module_allowlist=nn_module_allowlist, |
| 636 | ) |
| 637 | |
| 638 | executorch_module = _load_for_executorch_from_buffer(executorch_program.buffer) |
| 639 | |
| 640 | inputs_flattened, _ = tree_flatten(sample_inputs) |
| 641 | |
| 642 | model_output = executorch_module.run_method("forward", tuple(inputs_flattened)) |
| 643 | ref_output = model(*make_copy_of_inputs(sample_inputs)) |
| 644 | |
| 645 | if not check_outputs_equal( |
| 646 | model_output, |
| 647 | ref_output, |
| 648 | atol=atol, |
| 649 | rtol=rtol, |
| 650 | first_output_only=first_output_only, |
| 651 | ): |