1515 )
1616
1717
18- def convert_unsafe (neutron_converter , tflite_model , cctx , queue ):
18+ def _build_compilation_context (compilation_opts ):
19+ """Build a CompilationContext from a plain dict of options."""
20+ cctx = neutron_converter .CompilationContext ()
21+ cctx .targetOpts = neutron_converter .getNeutronTarget (compilation_opts ["target" ])
22+ cctx .compilationOpts .minNumOpsPerGraph = compilation_opts ["minNumOpsPerGraph" ]
23+ cctx .compilationOpts .excludeGraphPasses = compilation_opts ["excludeGraphPasses" ]
24+ cctx .compilationOpts .fetchConstantsToSRAM = compilation_opts ["fetchConstantsToSRAM" ]
25+ cctx .compilationOpts .dumpKernelSelectionCode = compilation_opts [
26+ "dumpKernelSelectionCode"
27+ ]
28+ if hasattr (cctx .compilationOpts , "useNewFlowNeutronC" ):
29+ cctx .compilationOpts .useNewFlowNeutronC = compilation_opts ["useNewFlowNeutronC" ]
30+ return cctx
31+
32+
33+ def convert_unsafe (tflite_model , compilation_opts , queue ):
1934 """
20- Run neutron_converter on given tflite_model with compilation context cctx .
35+ Run neutron_converter on given tflite_model with the provided compilation options .
2136 This routine is supposed to run in a separate process.
2237 If properly finished, the output queue contains the converted model,
2338 otherwise the neutron_converter exits and the output queue is empty.
2439 """
40+ cctx = _build_compilation_context (compilation_opts )
2541 model_converted = neutron_converter .convertModel (list (tflite_model ), cctx )
2642 queue .put (model_converted )
2743
@@ -84,16 +100,14 @@ def convert(
84100 # Neutron converter crashes if we provide invalid target -> verify.
85101 self .verify_target (target )
86102
87- cctx = neutron_converter .CompilationContext ()
88- cctx .targetOpts = neutron_converter .getNeutronTarget (target )
89- cctx .compilationOpts .minNumOpsPerGraph = 1
90- cctx .compilationOpts .excludeGraphPasses = (
91- "HoistSliceAboveTranspose,MergeTranspose"
92- )
93- cctx .compilationOpts .fetchConstantsToSRAM = fetch_constants_to_sram
94- cctx .compilationOpts .dumpKernelSelectionCode = self .dump_kernel_selection_code
95- if hasattr (cctx .compilationOpts , "useNewFlowNeutronC" ):
96- cctx .compilationOpts .useNewFlowNeutronC = use_new_flow_neutron_c
103+ compilation_opts = {
104+ "target" : target ,
105+ "minNumOpsPerGraph" : 1 ,
106+ "excludeGraphPasses" : "HoistSliceAboveTranspose,MergeTranspose" ,
107+ "fetchConstantsToSRAM" : fetch_constants_to_sram ,
108+ "dumpKernelSelectionCode" : self .dump_kernel_selection_code ,
109+ "useNewFlowNeutronC" : use_new_flow_neutron_c ,
110+ }
97111
98112 # Try to use multiprocessing for isolation, but fall back to direct execution
99113 # if the environment doesn't support it (e.g., in sandcastle/build environments)
@@ -104,7 +118,7 @@ def convert(
104118
105119 process = multiprocessing .Process (
106120 target = convert_unsafe ,
107- args = (neutron_converter , tflite_model , cctx , queue ),
121+ args = (tflite_model , compilation_opts , queue ),
108122 )
109123 process .start ()
110124 process .join () # waits until the subprocess is complete
@@ -116,12 +130,13 @@ def convert(
116130
117131 model_converted = queue .get ()
118132 process .close ()
119- except (EOFError , OSError ) as e :
133+ except (EOFError , OSError , TypeError ) as e :
120134 # Multiprocessing failed (likely due to environment restrictions)
121135 # Fall back to direct execution
122136 logging .warning (
123137 f"Multiprocessing not available ({ e } ), running neutron converter directly"
124138 )
139+ cctx = _build_compilation_context (compilation_opts )
125140 model_converted = neutron_converter .convertModel (list (tflite_model ), cctx )
126141 if self .dump_kernel_selection_code :
127142 self ._rename_partition_kernel_selection_file (delegation_tag )
0 commit comments