问题
Even after setting tf.config.threading.set_inter_op_parallelism_threads(1)
and tf.config.threading.set_intra_op_parallelism_threads(1)
Keras with Tensorflow CPU (running a simple CNN model fit) on a linux machine is creating too many threads. Whatever I try it seems to be creating 94 threads while going through the fitting epochs. Have tried playing with tf.compat.v1.ConfigProto
settings but nothing helps. How do I limit the number of threads?
回答1:
This is why tensorflow created many threads.
Using the mentioned 2 types of parallelism (inter and intra) you have limited control over the number of threads generated by TensorFlow. The minimum number of threads that you can get by setting these two variables is N, where N is the number of cores on your cpu (I don't know if you use gpu).
intra_op_parallelism_threads = 1
inter_op_parallelism_threads = 1
Even by setting the environment variables OMP_NUM_THREADS and MKL_NUM_THREADS can't help in further reducing the number of threads.
The following discussions suggest that without changing the source code of TensorFlow, it is not possible to reduce the number threads below N.
- How can I confine TensorFlow C API to use one and only one thread in total
- How to disable Tensorflow's multi-threading?
- How to stop TensorFlow from multi-threading
- https://github.com/tensorflow/tensorflow/issues/42510
- https://github.com/tensorflow/tensorflow/issues/33627
来源:https://stackoverflow.com/questions/62141945/keras-tf-cpu-creating-too-many-threads