tf.config.experimental.set_virtual_device_configuration
Stay organized with collections
Save and categorize content based on your preferences.
Set the virtual device configuration for a PhysicalDevice.
tf.config.experimental.set_virtual_device_configuration(
device, virtual_devices
)
A PhysicalDevice marked as visible will by default have a single LogicalDevice
allocated to it once the runtime is configured. Specifying a list of
tf.config.experimental.VirtualDeviceConfiguration objects allows multiple
devices to be configured that utilize the same PhysicalDevice.
The following example splits the CPU into 2 virtual devices:
physical_devices = tf.config.experimental.list_physical_devices('CPU')
assert len(physical_devices) == 1, "No CPUs found"
# Specify 2 virtual CPUs. Note currently memory limit is not supported.
tf.config.experimental.set_virtual_device_configuration(
physical_devices[0],
[tf.config.experimental.VirtualDeviceConfiguration(),
tf.config.experimental.VirtualDeviceConfiguration()])
logical_devices = tf.config.experimental.list_logical_devices('CPU')
assert len(logical_devices) == 2
try:
tf.config.experimental.set_virtual_device_configuration(
physical_devices[0],
[tf.config.experimental.VirtualDeviceConfiguration(),
tf.config.experimental.VirtualDeviceConfiguration(),
tf.config.experimental.VirtualDeviceConfiguration(),
tf.config.experimental.VirtualDeviceConfiguration()])
except:
print('Cannot modify the virtual devices once they have been initialized.')
The following example splits the GPU into 2 virtual devices with 100 MB each:
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "No GPUs found"
tf.config.experimental.set_virtual_device_configuration(
physical_devices[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=100),
tf.config.experimental.VirtualDeviceConfiguration(memory_limit=100)])
try:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
print('Cannot set memory growth when virtual devices configured')
logical_devices = tf.config.experimental.list_logical_devices('GPU')
assert len(logical_devices) == len(physical_devices) + 1
try:
tf.config.experimental.set_virtual_device_configuration(
physical_devices[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=10),
tf.config.experimental.VirtualDeviceConfiguration(memory_limit=10)])
except:
print('Cannot modify the virtual devices once they have been initialized.')
Args |
device
|
(optional) Need to update
|
virtual_devices
|
(optional) Need to update
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.config.experimental.set_virtual_device_configuration\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/config/experimental/set_virtual_device_configuration) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/framework/config.py#L492-L556) |\n\nSet the virtual device configuration for a PhysicalDevice.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.config.experimental.set_virtual_device_configuration`](/api_docs/python/tf/config/set_logical_device_configuration)\n\n\u003cbr /\u003e\n\n tf.config.experimental.set_virtual_device_configuration(\n device, virtual_devices\n )\n\nA PhysicalDevice marked as visible will by default have a single LogicalDevice\nallocated to it once the runtime is configured. Specifying a list of\ntf.config.experimental.VirtualDeviceConfiguration objects allows multiple\ndevices to be configured that utilize the same PhysicalDevice.\n\nThe following example splits the CPU into 2 virtual devices: \n\n physical_devices = tf.config.experimental.list_physical_devices('CPU')\n assert len(physical_devices) == 1, \"No CPUs found\"\n # Specify 2 virtual CPUs. Note currently memory limit is not supported.\n tf.config.experimental.set_virtual_device_configuration(\n physical_devices[0],\n [tf.config.experimental.VirtualDeviceConfiguration(),\n tf.config.experimental.VirtualDeviceConfiguration()])\n logical_devices = tf.config.experimental.list_logical_devices('CPU')\n assert len(logical_devices) == 2\n\n try:\n tf.config.experimental.set_virtual_device_configuration(\n physical_devices[0],\n [tf.config.experimental.VirtualDeviceConfiguration(),\n tf.config.experimental.VirtualDeviceConfiguration(),\n tf.config.experimental.VirtualDeviceConfiguration(),\n tf.config.experimental.VirtualDeviceConfiguration()])\n except:\n print('Cannot modify the virtual devices once they have been initialized.')\n\nThe following example splits the GPU into 2 virtual devices with 100 MB each: \n\n physical_devices = tf.config.experimental.list_physical_devices('GPU')\n assert len(physical_devices) \u003e 0, \"No GPUs found\"\n tf.config.experimental.set_virtual_device_configuration(\n physical_devices[0],\n [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=100),\n tf.config.experimental.VirtualDeviceConfiguration(memory_limit=100)])\n\n try:\n tf.config.experimental.set_memory_growth(physical_devices[0], True)\n except:\n print('Cannot set memory growth when virtual devices configured')\n\n logical_devices = tf.config.experimental.list_logical_devices('GPU')\n assert len(logical_devices) == len(physical_devices) + 1\n\n try:\n tf.config.experimental.set_virtual_device_configuration(\n physical_devices[0],\n [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=10),\n tf.config.experimental.VirtualDeviceConfiguration(memory_limit=10)])\n except:\n print('Cannot modify the virtual devices once they have been initialized.')\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|---------------------------|\n| `device` | (optional) Need to update |\n| `virtual_devices` | (optional) Need to update |\n\n\u003cbr /\u003e"]]