TFLite interpreter stops working after a high cpu load on a different thread
Hi @FSet89 

We see that you are using TF 2.4. Can you please check on latest stable version (TF 2.12) and let us know if the issue still persists?

Thanks. Hi,
I think that the issue is more related to the tflite_runtime version on my embedded system (2.3.1) than the TF version on my PC that I used for training. However I can't update tflite_runtime at the moment.
As for TF version, if needed I can try retraining and quantizing the model with the latest version Hi @FSet89 

Thanks for the clarification. 

Is there anyway to update the tflite_runtime  to 2.12 and check the issue?

Thanks. I will try as soon as I manage to update my Yocto. In the meantime it would be great is someone could test my example code Update: the issue is not encountered on my PC running TF 2.12. Is it possible that it has something to do with NNAPI?
 Glad that your issue is resolved with the latest version.

Tensorflow team continuously work on improving the product by adding new features or by fixing the bugs to make the experience seamless for the user community.  

Since there a huge version difference between 2.12 and the version which you were using it, 
with the information we have from your code, it is hard to point to a specific area where the
problem could be coming from.

It is always suggested to use the latest version to make use of new updates and features.

Feel free to close the issue since the issue is resolved, Thank You!


 Hi, the issue is not resolved: it just does not occur on my PC. I am not sure if it is related to the TF version. I would like to update the version on the environment of interest (my iMX8MP board) but by looking at the [repo](https://github.com/nxp-imx/tensorflow-imx) it seems that the latest available release is the 2.3. In the documentation which you have shared above shows that the latest Tensorflow version is <p dir="auto" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(31, 35, 40); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, &quot;Noto Sans&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a href="https://badge.fury.io/py/tensorflow" rel="nofollow" style="box-sizing: border-box; background-color: transparent; color: var(--color-accent-fg); text-decoration: none;"><img src="https://camo.githubusercontent.com/a7b5b417de938c1faf3602c7f48f26fde8761a977be85390fd6c0d191e210ba8/68747470733a2f2f696d672e736869656c64732e696f2f707970692f707976657273696f6e732f74656e736f72666c6f772e7376673f7374796c653d706c6173746963" alt="Python" data-canonical-src="https://img.shields.io/pypi/pyversions/tensorflow.svg?style=plastic" style="box-sizing: content-box; border-style: none; max-width: 100%; background-color: var(--color-canvas-default);"></a><span> </span><a href="https://badge.fury.io/py/tensorflow" rel="nofollow" style="box-sizing: border-box; background-color: transparent; color: var(--color-accent-fg); text-decoration: none;"><img src="https://camo.githubusercontent.com/52c9f14cae5a90816da6b63cc5c6b57c20fbe2788e643cf0ab8160d3cd9a9ecf/68747470733a2f2f62616467652e667572792e696f2f70792f74656e736f72666c6f772e737667" alt="PyPI" data-canonical-src="https://badge.fury.io/py/tensorflow.svg" style="box-sizing: content-box; border-style: none; max-width: 100%; background-color: var(--color-canvas-default);"></a><span> </span><a href="https://doi.org/10.5281/zenodo.4724125" rel="nofollow" style="box-sizing: border-box; background-color: transparent; color: var(--color-accent-fg); text-decoration: none;"><img src="https://camo.githubusercontent.com/cbb1b583e9445f1dc96b629d833b9f51c1b32971f0def04f0bf4be181d08bff1/68747470733a2f2f7a656e6f646f2e6f72672f62616467652f444f492f31302e353238312f7a656e6f646f2e343732343132352e737667" alt="DOI" data-canonical-src="https://zenodo.org/badge/DOI/10.5281/zenodo.4724125.svg" style="box-sizing: content-box; border-style: none; max-width: 100%; background-color: var(--color-canvas-default);"></a></p>
The repository `nxp-imx/tensorflow-imx` is something we don't have control on since this repository is not maintained or owned by Google.
 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60563">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60563">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Yes

### Source

binary

### Tensorflow Version

2.4.1

### Custom Code

Yes

### OS Platform and Distribution

Yocto Linux

### Mobile device

_No response_

### Python version

3.7

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

I have a python script running on a IMX8MP device equipped with a NPU and NNAPI. The code performs inference using tflite_runtime and a quantized tflite model. I noticed that if I start a thread with a task that causes a high CPU load, the tflite interpreter stops working, producing always the same output regardless of the input. The problem persists even when the thread finishes. Is there a way to prevent this problem or, at least, to catch it?

### Standalone code to reproduce the issue

```shell
import multiprocessing
from multiprocessing import Queue, Process
import numpy as np
from threading import Thread
from random import random, randint
import tflite_runtime.interpreter as tflite
import time
import cv2 
import os
import sys
import psutil

class ClassificationModel(object):
	def __init__(self, path, mask_path=None):
		self.interpreter = tflite.Interpreter(model_path=path)
		self.interpreter.allocate_tensors()
		self.input_details = self.interpreter.get_input_details()
		self.output_details = self.interpreter.get_output_details()
		self.input_shape = self.input_details[0]['shape']



	def predict(self, img, resize=True):

		if resize:
			img = cv2.resize(img, (self.input_shape[2], self.input_shape[1]))		

		img = (img/255.0).astype(np.float32)
		img = np.expand_dims(img, 0)
		self.interpreter.set_tensor(self.input_details[0]['index'], img)				
		self.interpreter.invoke()			
		output = self.interpreter.get_tensor(self.output_details[0]['index'])
		output = np.squeeze(output)
		
		return output



def my_thread_1():
	print("Start threaded task 1")
	simulate_cpu_load()

	print("Task 1 completed")


def worker():
	while True:
		pass

def simulate_cpu_load():
	num_cores = multiprocessing.cpu_count()
	processes = []

	for _ in range(num_cores):
		p = multiprocessing.Process(target=worker)
		p.start()
		processes.append(p)

	time.sleep(3) 

	for p in processes:
		p.terminate()




if __name__ == '__main__':
	classifier_1 = ClassificationModel('mymodel.tflite)
	
	
	cap = cv2.VideoCapture()
	for i in range(5):
		cap.open(i)
		if cap.isOpened():
			break

	if not cap.isOpened():
		print("Could not open camera")
		exit()

	cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
	cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)	


	try:
		while True:		
			# get image			
			ret, img = cap.read()

			# predict			
			p1 = classifier_1.predict(img)
			print(p1)			

			# threaded task
			if random() < 0.1:
				t = Thread(target=my_thread_1)
				t.start()
			
	except(KeyboardInterrupt):
		exit()
```


### Relevant log output

_No response_</details>