1、tf.config.experimental.set_visible_devices
2、tf.config.experimental.list_physical_devices
3、tf.config.experimental.set_memory_growth
4、tf.config.experimental.list_logical_devices
Public API for tf.config namespace.
Modules:
experimental
module: Public API for tf.config.experimental namespace.optimizer
module: Public API for tf.config.optimizer namespace.threading
module: Public API for tf.config.threading namespace.Functions:
experimental_connect_to_cluster(...)
: Connects to the given cluster.experimental_connect_to_host(...)
: Connects to a single machine to enable remote execution on it.experimental_list_devices(...)
: List the names of the available devices.experimental_run_functions_eagerly(...)
: Enables / disables eager execution of tf.function
s.get_soft_device_placement(...)
: Get if soft device placement is enabled.set_soft_device_placement(...)
: Set if soft device placement is enabled.experimental模块
Classes:
class VirtualDeviceConfiguration
: Configuration class for virtual devices for a PhysicalDevice.Functions:
get_device_policy(...)
: Gets the current device policy.get_memory_growth(...)
: Get if memory growth is enabled for a PhysicalDevice.get_synchronous_execution(...)
: Gets whether operations are executed synchronously or asynchronously.get_virtual_device_configuration(...)
: Get the virtual device configuration for a PhysicalDevice.get_visible_devices(...)
: Get the list of visible physical devices.list_logical_devices(...)
: Return a list of logical devices created by runtime.list_physical_devices(...)
: Return a list of physical devices visible to the runtime.set_device_policy(...)
: Sets the current thread device policy.set_memory_growth(...)
: Set if memory growth should be enabled for a PhysicalDevice.set_synchronous_execution(...)
: Specifies whether operations are executed synchronously or asynchronously.set_virtual_device_configuration(...)
: Set the virtual device configuration for a PhysicalDevice.set_visible_devices(...)
: Set the list of visible devices.Set the list of visible devices.
tf.config.experimental.set_visible_devices(
devices,
device_type=None
)
Used in the guide:
Sets the list of PhysicalDevices to be marked as visible to the runtime. Any devices that are not marked as visible means TensorFlow will not allocate memory on it and will not be able to place any operations on it as no LogicalDevice will be created on it. By default all discovered devices are marked as visible.
The following example demonstrates disabling the first GPU on the machine.
physical_devices = config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
# Disable first GPU
tf.config.experimental.set_visible_devices(physical_devices[1:], 'GPU')
logical_devices = config.experimental.list_logical_devices('GPU')
# Logical device was not created for first GPU
assert len(logical_devices) == len(physical_devices) - 1
Args:
devices
: (optional) List of PhysicalDevice objects to make visibledevice_type
: (optional) Device types to limit visibility configuration to. Other device types will be left unaltered.Compat aliases
tf.compat.v1.config.experimental.set_visible_devices
tf.compat.v2.config.experimental.set_visible_devices
Return a list of physical devices visible to the runtime.
tf.config.experimental.list_physical_devices(device_type=None)
Used in the guide:
Used in the tutorials:
Physical devices are hardware devices locally present on the current machine. By default all discovered CPU and GPU devices are considered visible. The list_physical_devices
allows querying the hardware prior to runtime initialization.
The following example ensures the machine can see at least 1 GPU.
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "No GPUs found."
Args:
device_type
: (optional) Device type to filter by such as "CPU" or "GPU"Returns:
Compat aliases
tf.compat.v1.config.experimental.list_physical_devices
tf.compat.v2.config.experimental.list_physical_devices
Set if memory growth should be enabled for a PhysicalDevice.
tf.config.experimental.set_memory_growth(
device,
enable
)
Used in the guide:
A PhysicalDevice with memory growth set will not allocate all memory on the device upfront. Memory growth cannot be configured on a PhysicalDevice with virtual devices configured.
For example:
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
tf.config.experimental.set_memory_growth(physical_devices[0], True)
Args:
device
: PhysicalDevice to configureenable
: Whether to enable or disable memory growthCompat aliases
tf.compat.v1.config.experimental.set_memory_growth
tf.compat.v2.config.experimental.set_memory_growth
Return a list of logical devices created by runtime.
tf.config.experimental.list_logical_devices(device_type=None)
Used in the guide:
Logical devices may correspond to physical devices or remote devices in the cluster. Operations and tensors may be placed on these devices by using the name
of the LogicalDevice.
For example:
logical_devices = tf.config.experimental.list_logical_devices('GPU')
# Allocate on GPU:0
with tf.device(logical_devices[0].name):
one = tf.constant(1)
# Allocate on GPU:1
with tf.device(logical_devices[1].name):
two = tf.constant(2)
Args:
device_type
: (optional) Device type to filter by such as "CPU" or "GPU"Returns:
List of LogicalDevice objects
Compat aliases