Set logical device for a tf1 session

Set logical device for a tf1 session

I have two models in tensorflow v1. I intend to run two models in two separate logical devices. However, I don’t know how to set a logical device for a session. Hope someone can help me with this problem. I tried to use “device_filters” in tf.compat.v1.ConfigProto but it failed to find the logical device. Following is the code I tried:

import tensorflow as tf

# Configure gpu device
gpus = tf.config.experimental.list_physical_devices("GPU")
configs = [
    tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1000),
    tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1000),
]
tf.config.experimental.set_virtual_device_configuration(gpus[0], configs)
logical_devices = tf.config.experimental.list_logical_devices("GPU")

# Create and run session
tf.compat.v1.disable_eager_execution() # need to disable eager in TF2.x
with tf.device(logical_devices[1].name):
    # Build a graph.
    a = tf.constant(5.0)
    b = tf.constant(6.0)
    c = a * b

    # Launch the graph in a session.
    sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True, device_filters=[logical_devices[1].name]))
    # Evaluate the tensor `c`.
    print(sess.run(c)) # prints 30.0
Cannot assign a device for operation mul: {{node mul}} was explicitly assigned to /device:GPU:1 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:GPU:0 ]. Make sure the device specification refers to a valid device.
         [[mul]]

Hi @hieuhhong, The session belongs to the tensorflow 1.x version which is not actively supported. I recommend you to use TF 2.x version.

with tf.device("/GPU:0"):
    # Build a graph.
    a = tf.constant(5.0)
    b = tf.constant(6.0)
c = a * b
print(c.numpy())

The above code runs on gpu. Thank You.

Hi @Kiran_Sai_Ramineni ,

To use the TF 2.x version I have to migrate the training and inference code from tf1 to tf2. I believe it takes time for that. But sooner or later I must do it.

Thank you