I want to limit CPU cores and threads. So I found three ways to limit these.
1) "Keras backend + Tensorflow"
from keras import backend as K
import tensorflow as tf
config = tf.ConfigProto(intra_op_parallelism_threads=2, \
inter_op_parallelism_threads=4, \
allow_soft_placement=True, \
device_count = {'CPU': 1})
session = tf.Session(config=config)
K.set_session(session)
2) "Keras from Tensorflow"
import tensorflow as tf
from tensorflow import keras
tf.config.threading.set_intra_op_parallelism_threads(2)
tf.config.threading.set_inter_op_parallelism_threads(4)
3) "keras from Tensorflow"
import os
os.environ['TF_NUM_INTRAOP_THREADS'] = '2'
os.environ['TF_NUM_INTEROP_THREADS'] = '4'
These three ways are same affects?
Lastly I understood for the parameters like I wrote below
is this right? If I miss-understanding please let me know.
Thank you.
Not exactly, it's not as simple as that. As per official documentation -
intra_op_parallelism_threads - Certain operations like matrix multiplication and reductions can utilize parallel threads for speedups. A value of 0 means the system picks an appropriate number. Refer this
inter_op_parallelism_threads - Determines the number of parallel threads used by independent non-blocking operations. 0 means the system picks an appropriate number. Refer this
So technically you can not limit the number of CPUs but only the number of parallel threads, which, for the sake of limiting resource consumption, is sufficient.
Regarding the methods, you are using -
The third approach allows you to directly set the environment variables using os
library.
import os
os.environ['TF_NUM_INTRAOP_THREADS'] = '2'
os.environ['TF_NUM_INTEROP_THREADS'] = '4'
The second approach is a method in tf2 that does exactly the same (sets environment variables), the difference being that Keras is packaged into tf2 now.
import tensorflow as tf
from tensorflow import keras
tf.config.threading.set_intra_op_parallelism_threads(2)
tf.config.threading.set_inter_op_parallelism_threads(4)
The first approach is for standalone Keras. This approach will work if keras is set to tensorflow backend. Again, it does the same thing which is set environment variables indirectly.
from keras import backend as K
import tensorflow as tf
config = tf.ConfigProto(intra_op_parallelism_threads=2, \
inter_op_parallelism_threads=4, \
allow_soft_placement=True, \
device_count = {'CPU': 1})
session = tf.Session(config=config)
K.set_session(session)
If you still have doubts, you can check what happens to the environment variables after running all 3 independently and then check the specific variable using os
with -
print(os.environ.get('KEY_THAT_MIGHT_EXIST'))
For a better understanding of the topic, you can check this link that details it out quite well.
TLDR; You can use the second or third approach if you are working with tf2. Else use the first or third approach if you are using standalone Keras with tensorflow backend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With