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

Function _extract_delegate_payload

exir/_serialize/_program.py:773–837  ·  view source on GitHub ↗

Extract a delegate payload from a serialized PTE file. Parses the PTE file structure, finds the delegate matching the given backend ID, and returns its raw payload bytes. Handles both inline delegate data and segment-based storage. Args: pte_data: Raw bytes of the PTE file.

(
    pte_data: bytes, backend_id: str, delegate_index: int = 0
)

Source from the content-addressed store, hash-verified

771
772
773def _extract_delegate_payload(
774 pte_data: bytes, backend_id: str, delegate_index: int = 0
775) -> Optional[bytes]:
776 """Extract a delegate payload from a serialized PTE file.
777
778 Parses the PTE file structure, finds the delegate matching the given
779 backend ID, and returns its raw payload bytes. Handles both inline
780 delegate data and segment-based storage.
781
782 Args:
783 pte_data: Raw bytes of the PTE file.
784 backend_id: ID substring to match (case-insensitive).
785 For example, 'mlx' matches 'MLXBackend'.
786 delegate_index: Which matching delegate to extract (0-based).
787 Defaults to 0 (first match).
788
789 Returns:
790 Delegate payload bytes, or None if not found.
791 """
792 # Parse the extended header
793 extended_header = _get_extended_header(pte_data)
794
795 # Determine program size from header or use full data
796 if extended_header is not None:
797 program_size = extended_header.program_size
798 else:
799 program_size = len(pte_data)
800
801 # Parse the program flatbuffer
802 program: Program = _json_to_program(
803 _program_flatbuffer_to_json(pte_data[:program_size])
804 )
805
806 # Search for the matching delegate
807 match_count = 0
808 for plan in program.execution_plan:
809 for delegate in plan.delegates:
810 if backend_id.lower() not in delegate.id.lower():
811 continue
812 if match_count != delegate_index:
813 match_count += 1
814 continue
815
816 processed = delegate.processed
817
818 # Inline data
819 if processed.location == DataLocation.INLINE:
820 inline_data = program.backend_delegate_data[processed.index]
821 if inline_data.data:
822 return bytes(inline_data.data)
823 return None
824
825 # Segment data
826 if processed.location == DataLocation.SEGMENT:
827 if extended_header is None:
828 return None
829
830 segment = program.segments[processed.index]

Callers

nothing calls this directly

Calls 3

_json_to_programFunction · 0.85
_get_extended_headerFunction · 0.70

Tested by

no test coverage detected