tf.config.set_logical_device_configuration
Stay organized with collections
Save and categorize content based on your preferences.
Set the logical device configuration for a tf.config.PhysicalDevice
.
tf.config.set_logical_device_configuration(
device, logical_devices
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
A visible tf.config.PhysicalDevice
will by default have a single
tf.config.LogicalDevice
associated with it once the runtime is initialized.
Specifying a list of tf.config.LogicalDeviceConfiguration
objects allows
multiple devices to be created on the same tf.config.PhysicalDevice
.
Logical device configurations can be modified by calling this function as
long as the runtime is uninitialized. After the runtime is initialized
calling this function raises a RuntimeError.
The following example splits the CPU into 2 logical devices:
physical_devices = tf.config.list_physical_devices('CPU')
assert len(physical_devices) == 1, "No CPUs found"
# Specify 2 virtual CPUs. Note currently memory limit is not supported.
try:
tf.config.set_logical_device_configuration(
physical_devices[0],
[tf.config.LogicalDeviceConfiguration(),
tf.config.LogicalDeviceConfiguration()])
logical_devices = tf.config.list_logical_devices('CPU')
assert len(logical_devices) == 2
tf.config.set_logical_device_configuration(
physical_devices[0],
[tf.config.LogicalDeviceConfiguration(),
tf.config.LogicalDeviceConfiguration(),
tf.config.LogicalDeviceConfiguration(),
tf.config.LogicalDeviceConfiguration()])
except:
# Cannot modify logical devices once initialized.
pass
The following example splits the GPU into 2 logical devices with 100 MB each:
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.set_logical_device_configuration(
physical_devices[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=100),
tf.config.LogicalDeviceConfiguration(memory_limit=100)])
logical_devices = tf.config.list_logical_devices('GPU')
assert len(logical_devices) == len(physical_devices) + 1
tf.config.set_logical_device_configuration(
physical_devices[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=10),
tf.config.LogicalDeviceConfiguration(memory_limit=10)])
except:
# Invalid device or cannot modify logical devices once initialized.
pass
Args |
device
|
The PhysicalDevice to configure.
|
logical_devices
|
(optional) List of tf.config.LogicalDeviceConfiguration
objects to allocate for the specified PhysicalDevice . If None, the
default configuration will be used.
|
Raises |
ValueError
|
If argument validation fails.
|
RuntimeError
|
Runtime is already initialized.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.config.set_logical_device_configuration\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/config.py#L843-L912) |\n\nSet the logical device configuration for a [`tf.config.PhysicalDevice`](../../tf/config/PhysicalDevice).\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.config.experimental.set_virtual_device_configuration`](https://www.tensorflow.org/api_docs/python/tf/config/set_logical_device_configuration)\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`](https://www.tensorflow.org/api_docs/python/tf/config/set_logical_device_configuration), [`tf.compat.v1.config.set_logical_device_configuration`](https://www.tensorflow.org/api_docs/python/tf/config/set_logical_device_configuration)\n\n\u003cbr /\u003e\n\n tf.config.set_logical_device_configuration(\n device, logical_devices\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Use a GPU](https://www.tensorflow.org/guide/gpu) - [Distributed training with Core APIs and DTensor](https://www.tensorflow.org/guide/core/distribution) - [DTensor concepts](https://www.tensorflow.org/guide/dtensor_overview) - [Random number generation](https://www.tensorflow.org/guide/random_numbers) | - [Using DTensors with Keras](https://www.tensorflow.org/tutorials/distribute/dtensor_keras_tutorial) - [Distributed training with DTensors](https://www.tensorflow.org/tutorials/distribute/dtensor_ml_tutorial) - [Distributed Input](https://www.tensorflow.org/tutorials/distribute/input) - [TFF simulations with accelerators](https://www.tensorflow.org/federated/tutorials/simulations_with_accelerators) - [TFP Release Notes notebook (0.12.1)](https://www.tensorflow.org/probability/examples/TFP_Release_Notebook_0_12_1) |\n\nA visible [`tf.config.PhysicalDevice`](../../tf/config/PhysicalDevice) will by default have a single\n[`tf.config.LogicalDevice`](../../tf/config/LogicalDevice) associated with it once the runtime is initialized.\nSpecifying a list of [`tf.config.LogicalDeviceConfiguration`](../../tf/config/LogicalDeviceConfiguration) objects allows\nmultiple devices to be created on the same [`tf.config.PhysicalDevice`](../../tf/config/PhysicalDevice).\n\nLogical device configurations can be modified by calling this function as\nlong as the runtime is uninitialized. After the runtime is initialized\ncalling this function raises a RuntimeError.\n\nThe following example splits the CPU into 2 logical devices: \n\n physical_devices = tf.config.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 try:\n tf.config.set_logical_device_configuration(\n physical_devices[0],\n [tf.config.LogicalDeviceConfiguration(),\n tf.config.LogicalDeviceConfiguration()])\n logical_devices = tf.config.list_logical_devices('CPU')\n assert len(logical_devices) == 2\n\n tf.config.set_logical_device_configuration(\n physical_devices[0],\n [tf.config.LogicalDeviceConfiguration(),\n tf.config.LogicalDeviceConfiguration(),\n tf.config.LogicalDeviceConfiguration(),\n tf.config.LogicalDeviceConfiguration()])\n except:\n # Cannot modify logical devices once initialized.\n pass\n\nThe following example splits the GPU into 2 logical devices with 100 MB each: \n\n physical_devices = tf.config.list_physical_devices('GPU')\n try:\n tf.config.set_logical_device_configuration(\n physical_devices[0],\n [tf.config.LogicalDeviceConfiguration(memory_limit=100),\n tf.config.LogicalDeviceConfiguration(memory_limit=100)])\n\n logical_devices = tf.config.list_logical_devices('GPU')\n assert len(logical_devices) == len(physical_devices) + 1\n\n tf.config.set_logical_device_configuration(\n physical_devices[0],\n [tf.config.LogicalDeviceConfiguration(memory_limit=10),\n tf.config.LogicalDeviceConfiguration(memory_limit=10)])\n except:\n # Invalid device or cannot modify logical devices once initialized.\n pass\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `device` | The `PhysicalDevice` to configure. |\n| `logical_devices` | (optional) List of [`tf.config.LogicalDeviceConfiguration`](../../tf/config/LogicalDeviceConfiguration) objects to allocate for the specified `PhysicalDevice`. If None, the default configuration will be used. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|---------------------------------|\n| `ValueError` | If argument validation fails. |\n| `RuntimeError` | Runtime is already initialized. |\n\n\u003cbr /\u003e"]]