22

I've seen several questions about GPU Memory with Tensorflow but I've installed it on a Pine64 with no GPU support.

That means I'm running it with very limited resources (CPU and RAM only) and Tensorflow seems to want it all, completely freezing my machine.


Is there a way to limit the amount of processing power and memory allocated to Tensorflow? Something similar to bazel's own --local_resources flag?

pekapa
  • 881
  • 1
  • 11
  • 25

2 Answers2

16

This will create a session that runs one op at a time, and only one thread per op

sess = tf.Session(config=
    tf.ConfigProto(inter_op_parallelism_threads=1,
                   intra_op_parallelism_threads=1))

Not sure about limiting memory, it seems to be allocated on demand, I've had TensorFlow freeze my machine when my network wanted 100GB of RAM, so my solution was to make networks that need less RAM

Charles Chou
  • 179
  • 1
  • 15
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
  • 2
    This threw me the exception `TypeError: target must be a string, but got Exception AttributeError: "'Session' object has no attribute '_session'" in > ignored`, but adding `config` keyword (i.e. `sess = tf.Session(config=tf.ConfigProto(inter_op_parallelism_threads=1, intra_op_parallelism_threads=1))`)fixed the problem. – Laurynas Tamulevičius Jan 13 '18 at 19:48
  • I try to use it on TF 2.2.0 and it throws an error: module 'tensorflow' has no attribute 'Session'. Solved it by sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(inter_op_parallelism_threads=1, intra_op_parallelism_threads=1)), but it didn't solve my problem: CPU is loaded on 100% – Alex Ivanov Jun 09 '20 at 07:24
  • @AlexIvanov please see my answer – Romeo Kienzler Jul 14 '20 at 10:08
5

For TensorFlow 2.x this has been answered in the following thread:

In Tensorflow 2.x, there is no session anymore. Directly use the config API to set the parallelism at the start of the program.

import tensorflow as tf

tf.config.threading.set_intra_op_parallelism_threads(2)
tf.config.threading.set_inter_op_parallelism_threads(2)
with tf.device('/CPU:0'):
    model = tf.keras.models.Sequential([...

https://www.tensorflow.org/api_docs/python/tf/config/threading

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58