MCPcopy Create free account
hub / github.com/pytorch/executorch / op_ablation_test

Function op_ablation_test

backends/vulkan/test/utils.py:852–1088  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

850
851
852def 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:

Callers

nothing calls this directly

Calls 9

exportFunction · 0.90
find_bad_operatorsFunction · 0.85
print_occurrencesFunction · 0.85
infoMethod · 0.80
moduleMethod · 0.80
nameMethod · 0.80
keysMethod · 0.80
exported_programMethod · 0.45

Tested by

no test coverage detected