|
1 |
| -# Copyright 2024 NXP |
| 1 | +# Copyright 2024-2025 NXP |
2 | 2 | #
|
3 | 3 | # This source code is licensed under the BSD-style license found in the
|
4 | 4 | # LICENSE file in the root directory of this source tree.
|
5 | 5 | import importlib
|
| 6 | +import logging |
| 7 | +import multiprocessing |
6 | 8 | import pkgutil
|
7 | 9 |
|
8 | 10 | from executorch.backends.nxp.backend.ir.converter.node_converter import Target
|
9 | 11 |
|
10 | 12 |
|
| 13 | +def convert_unsafe(neutron_converter, tflite_model, cctx, queue): |
| 14 | + """ |
| 15 | + Run neutron_converter on given tflite_model with compilation context cctx. |
| 16 | + This routine is supposed to run in a separate process. |
| 17 | + If properly finished, the output queue contains the converted model, |
| 18 | + otherwise the neutron_converter exits and the output queue is empty. |
| 19 | + """ |
| 20 | + model_converted = neutron_converter.convertModel(list(tflite_model), cctx) |
| 21 | + queue.put(model_converted) |
| 22 | + |
| 23 | + |
11 | 24 | class NeutronConverterManager:
|
12 | 25 | """
|
13 | 26 | Manager for conversion of TFLite model in flatbuffers format into TFLite model that
|
@@ -52,6 +65,23 @@ def convert(
|
52 | 65 | cctx.targetOpts = neutron_converter.getNeutronTarget(target)
|
53 | 66 | # New switch since Neutron Converter SDK_25.06
|
54 | 67 | cctx.compilationOpts.minNumOpsPerGraph = 1
|
55 |
| - model_converted = neutron_converter.convertModel(list(tflite_model), cctx) |
56 | 68 |
|
| 69 | + logger = multiprocessing.log_to_stderr() |
| 70 | + logger.setLevel(logging.WARNING) |
| 71 | + queue = multiprocessing.Manager().Queue() |
| 72 | + |
| 73 | + process = multiprocessing.Process( |
| 74 | + target=convert_unsafe, args=(neutron_converter, tflite_model, cctx, queue) |
| 75 | + ) |
| 76 | + process.start() |
| 77 | + process.join() # waits until the subprocess is complete |
| 78 | + |
| 79 | + if queue.empty(): # signals the unsafe task did not run till the end |
| 80 | + raise RuntimeError( |
| 81 | + f"Neutron converter module terminated unexpectedly with exit code {process.exitcode}" |
| 82 | + ) |
| 83 | + |
| 84 | + model_converted = queue.get() |
| 85 | + |
| 86 | + process.close() |
57 | 87 | return bytes(model_converted)
|
0 commit comments