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]]