Convert an ExportedProgram to MLX delegate payload. Args: edge_program: The ExportedProgram in edge dialect to compile. compile_specs: List of compilation options. Returns: PreprocessResult containing the serialized MLX program and
(
edge_program: ExportedProgram,
compile_specs: List[CompileSpec],
)
| 58 | |
| 59 | @staticmethod |
| 60 | def preprocess( |
| 61 | edge_program: ExportedProgram, |
| 62 | compile_specs: List[CompileSpec], |
| 63 | ) -> PreprocessResult: |
| 64 | """ |
| 65 | Convert an ExportedProgram to MLX delegate payload. |
| 66 | |
| 67 | Args: |
| 68 | edge_program: The ExportedProgram in edge dialect to compile. |
| 69 | compile_specs: List of compilation options. |
| 70 | |
| 71 | Returns: |
| 72 | PreprocessResult containing the serialized MLX program and |
| 73 | data_store_output with constant tensor data. |
| 74 | """ |
| 75 | logger.debug("MLXBackend.preprocess() called") |
| 76 | logger.debug(f"Edge program:\n{edge_program}") |
| 77 | |
| 78 | # Build MLXGraph from ExportedProgram |
| 79 | # Use a deterministic 4-hex prefix derived from the edge program to |
| 80 | # namespace named_data keys, avoiding collisions in multi-method |
| 81 | # programs where different methods may have lifted tensor constants |
| 82 | # with the same auto-generated name. |
| 83 | prefix = hashlib.sha256(str(edge_program).encode()).hexdigest()[:4] |
| 84 | builder = MLXProgramBuilder(edge_program, named_data_key_prefix=prefix) |
| 85 | mlx_graph = builder.build() |
| 86 | |
| 87 | # Get constant data as NamedDataStore (ET will own this data) |
| 88 | named_data_store = builder.get_named_data_store() |
| 89 | |
| 90 | logger.debug(f" named_data_store entries: {len(named_data_store.pte_data)}") |
| 91 | _log_mlx_graph(mlx_graph) |
| 92 | |
| 93 | # Serialize to bytes (no constant data embedded) |
| 94 | serialized = serialize_mlx_graph(mlx_graph) |
| 95 | |
| 96 | logger.debug(f"MLXBackend.preprocess() complete: {len(serialized)} bytes") |
| 97 | |
| 98 | return PreprocessResult( |
| 99 | processed_bytes=serialized, |
| 100 | data_store_output=named_data_store.get_named_data_store_output(), |
| 101 | ) |
| 102 | |
| 103 | |
| 104 | def _format_tensor_meta(meta) -> str: |
nothing calls this directly
no test coverage detected