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
34 changes: 32 additions & 2 deletions backends/nxp/backend/neutron_converter_manager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# Copyright 2024 NXP
# Copyright 2024-2025 NXP
#
# This source code is licensed under the BSD-style license found in the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Copyright 2024-2025 NXP

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅

# 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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A documentation comment would be useful here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jirioc, can you please add it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"""
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
Expand Down Expand Up @@ -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)
Loading