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

Method preprocess

backends/mlx/preprocess.py:60–101  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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
104def _format_tensor_meta(meta) -> str:

Callers

nothing calls this directly

Calls 9

buildMethod · 0.95
get_named_data_storeMethod · 0.95
MLXProgramBuilderClass · 0.90
serialize_mlx_graphFunction · 0.90
PreprocessResultClass · 0.90
_log_mlx_graphFunction · 0.85
debugMethod · 0.80
encodeMethod · 0.45

Tested by

no test coverage detected