當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.distribute.TPUStrategy.experimental_assign_to_logical_device用法及代碼示例

用法

experimental_assign_to_logical_device(
    tensor, logical_device_id
)

參數

  • tensor 輸入張量進行注釋。
  • logical_device_id 張量將分配到的邏輯核心的 ID。

拋出

  • ValueError 呈現的邏輯設備 id 與設備分配指定的分區總數不一致,或者 TPUStrategy 是用 experimental_spmd_xla_partitioning=True 構造的。

返回

  • tensor 具有相同值的帶注釋的張量。

添加將tensor 分配給邏輯設備的注釋。

這將向 tensor 添加注釋,指定將在邏輯核心設備 ID logical_device_id 上調用 tensor 上的操作。使用模型並行時,默認行為是所有操作都放置在zero-th 邏輯設備上。

# Initializing TPU system with 2 logical devices and 4 replicas.
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')
tf.config.experimental_connect_to_cluster(resolver)
topology = tf.tpu.experimental.initialize_tpu_system(resolver)
device_assignment = tf.tpu.experimental.DeviceAssignment.build(
    topology,
    computation_shape=[1, 1, 1, 2],
    num_replicas=4)
strategy = tf.distribute.TPUStrategy(
    resolver, experimental_device_assignment=device_assignment)
iterator = iter(inputs)

@tf.function()
def step_fn(inputs):
  output = tf.add(inputs, inputs)

  # Add operation will be executed on logical device 0.
  output = strategy.experimental_assign_to_logical_device(output, 0)
  return output

strategy.run(step_fn, args=(next(iterator),))

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.distribute.TPUStrategy.experimental_assign_to_logical_device。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。