Fast binary search utility function to determine which operators work correctly when delegated to Vulkan. This function uses a binary search approach to efficiently find bad operators: 1. Split operators into two halves (least frequent first, most frequent second) 2. Test each half
( # noqa: C901
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,
)
| 850 | |
| 851 | |
| 852 | def op_ablation_test( # noqa: C901 |
| 853 | model: torch.nn.Module, |
| 854 | sample_inputs: Tuple[torch.Tensor], |
| 855 | atol=1e-03, |
| 856 | rtol=1e-01, |
| 857 | dynamic_shapes=None, |
| 858 | test_inputs=None, |
| 859 | first_output_only=False, |
| 860 | ) -> dict: |
| 861 | """ |
| 862 | Fast binary search utility function to determine which operators work correctly when delegated to Vulkan. |
| 863 | |
| 864 | This function uses a binary search approach to efficiently find bad operators: |
| 865 | 1. Split operators into two halves (least frequent first, most frequent second) |
| 866 | 2. Test each half to see if it produces correct output |
| 867 | 3. Add good halves to known_good_ops and recursively search bad halves |
| 868 | 4. Continue until all operators are classified |
| 869 | |
| 870 | Args: |
| 871 | model: The PyTorch model to test |
| 872 | sample_inputs: Sample inputs for the model |
| 873 | atol: Absolute tolerance for output comparison |
| 874 | rtol: Relative tolerance for output comparison |
| 875 | dynamic_shapes: Optional dynamic shapes for export |
| 876 | test_inputs: Optional additional test inputs |
| 877 | first_output_only: Whether to compare only the first output |
| 878 | |
| 879 | Returns: |
| 880 | dict: Dictionary with keys: |
| 881 | - 'good_operators': List of operators that work correctly |
| 882 | - 'bad_operators': List of operators that cause failures |
| 883 | - 'operator_frequencies': Dictionary mapping operators to their occurrence count |
| 884 | - 'all_operators': List of all unique operators found in the graph |
| 885 | - 'test_count': Number of tests performed |
| 886 | """ |
| 887 | logger = logging.getLogger("") |
| 888 | logger.setLevel(logging.INFO) |
| 889 | |
| 890 | logger.info("Starting fast binary search operator ablation test...") |
| 891 | |
| 892 | # Step 1: Export model to get edge_program and extract operators |
| 893 | export_training_graph = export(model, sample_inputs, strict=True).module() |
| 894 | program = export( |
| 895 | export_training_graph, |
| 896 | sample_inputs, |
| 897 | dynamic_shapes=dynamic_shapes, |
| 898 | strict=True, |
| 899 | ) |
| 900 | edge_program = to_edge_transform_and_lower( |
| 901 | program, |
| 902 | partitioner=[], # No partitioner to get the full graph |
| 903 | transform_passes=None, |
| 904 | compile_config=None, |
| 905 | ) |
| 906 | |
| 907 | # Step 2: Scan edge_program.graph_module to obtain unique operators and their frequencies |
| 908 | operator_frequencies = {} |
| 909 | for node in edge_program.exported_program().graph.nodes: |
nothing calls this directly
no test coverage detected