From c3f5a69e47319b6dd5b06caa02e11e524b61a4b4 Mon Sep 17 00:00:00 2001 From: Jiri Ocenasek Date: Fri, 25 Jul 2025 15:45:58 +0200 Subject: [PATCH] NXP backend: Catching the converter failures. --- .../nxp/backend/neutron_converter_manager.py | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/backends/nxp/backend/neutron_converter_manager.py b/backends/nxp/backend/neutron_converter_manager.py index 99d7715ebf0..2bc4380f89b 100644 --- a/backends/nxp/backend/neutron_converter_manager.py +++ b/backends/nxp/backend/neutron_converter_manager.py @@ -1,13 +1,26 @@ -# Copyright 2024 NXP +# Copyright 2024-2025 NXP # # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. import importlib +import logging +import multiprocessing import pkgutil from executorch.backends.nxp.backend.ir.converter.node_converter import Target +def convert_unsafe(neutron_converter, tflite_model, cctx, queue): + """ + Run neutron_converter on given tflite_model with compilation context cctx. + This routine is supposed to run in a separate process. + If properly finished, the output queue contains the converted model, + otherwise the neutron_converter exits and the output queue is empty. + """ + model_converted = neutron_converter.convertModel(list(tflite_model), cctx) + queue.put(model_converted) + + class NeutronConverterManager: """ Manager for conversion of TFLite model in flatbuffers format into TFLite model that @@ -52,6 +65,23 @@ def convert( cctx.targetOpts = neutron_converter.getNeutronTarget(target) # New switch since Neutron Converter SDK_25.06 cctx.compilationOpts.minNumOpsPerGraph = 1 - model_converted = neutron_converter.convertModel(list(tflite_model), cctx) + logger = multiprocessing.log_to_stderr() + logger.setLevel(logging.WARNING) + queue = multiprocessing.Manager().Queue() + + process = multiprocessing.Process( + target=convert_unsafe, args=(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}" + ) + + model_converted = queue.get() + + process.close() return bytes(model_converted)