Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions backends/nxp/backend/neutron_converter_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,35 @@ def convert(self, tflite_model: bytes, target: str) -> bytes:
cctx.compilationOpts.minNumOpsPerGraph = 1
cctx.compilationOpts.excludeGraphPasses = "MergeTranspose"

logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.WARNING)
queue = multiprocessing.Manager().Queue()
# Try to use multiprocessing for isolation, but fall back to direct execution
# if the environment doesn't support it (e.g., in sandcastle/build environments)
try:
logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.WARNING)
queue = multiprocessing.Manager().Queue()

process = multiprocessing.Process(
target=convert_unsafe,
args=(self.neutron_converter, tflite_model, cctx, queue),
)
process.start()
process.join() # waits until the subprocess is complete

process = multiprocessing.Process(
target=convert_unsafe,
args=(self.neutron_converter, tflite_model, cctx, queue),
)
process.start()
process.join() # waits until the subprocess is complete
if queue.empty(): # signals the unsafe task did not run till the end
raise RuntimeError(
f"Neutron converter module terminated unexpectedly with exit code {process.exitcode}"
)

if queue.empty(): # signals the unsafe task did not run till the end
raise RuntimeError(
f"Neutron converter module terminated unexpectedly with exit code {process.exitcode}"
model_converted = queue.get()
process.close()
except (EOFError, OSError) as e:
# Multiprocessing failed (likely due to environment restrictions)
# Fall back to direct execution
logging.warning(
f"Multiprocessing not available ({e}), running neutron converter directly"
)
model_converted = self.neutron_converter.convertModel(
list(tflite_model), cctx
)

model_converted = queue.get()

process.close()
return bytes(model_converted)
Loading