# tf.config.set_logical_device_configuration

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/framework/config.py#L837-L906) |

Set the logical device configuration for a [`tf.config.PhysicalDevice`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/config/PhysicalDevice).

#### View aliases


**Main aliases**

[`tf.config.experimental.set_virtual_device_configuration`](https://www.tensorflow.org/api_docs/python/tf/config/set_logical_device_configuration)
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`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)

<br />

    tf.config.set_logical_device_configuration(
        device, logical_devices
    )

A visible [`tf.config.PhysicalDevice`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/config/PhysicalDevice) will by default have a single
[`tf.config.LogicalDevice`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/config/LogicalDevice) associated with it once the runtime is initialized.
Specifying a list of [`tf.config.LogicalDeviceConfiguration`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/config/LogicalDeviceConfiguration) objects allows
multiple devices to be created on the same [`tf.config.PhysicalDevice`](https://www.tensorflow.org/versions/r2.14/api_docs/python/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

<br />

<br />

<br />

| ## Args ||
|---|---|
| `device` | The `PhysicalDevice` to configure. |
| `logical_devices` | (optional) List of [`tf.config.LogicalDeviceConfiguration`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/config/LogicalDeviceConfiguration) objects to allocate for the specified `PhysicalDevice`. If None, the default configuration will be used. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If argument validation fails. |
| `RuntimeError` | Runtime is already initialized. |

<br />