Search code examples
pythonmultithreadingtensorflowkeraslimit

Keras/TF CPU creating too many threads


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?


Solution

  • 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.