Skip to content

Instantly share code, notes, and snippets.

@rish-16
Last active August 17, 2019 01:17
Show Gist options
  • Select an option

  • Save rish-16/5214b342f1df63f42e5a34bcbafbf588 to your computer and use it in GitHub Desktop.

Select an option

Save rish-16/5214b342f1df63f42e5a34bcbafbf588 to your computer and use it in GitHub Desktop.
# Detect hardware
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
except ValueError: # If TPU not found, resolve to GPU instead
tpu = None
gpus = tf.config.experimental.list_logical_devices("GPU")
# Select appropriate distribution strategy
if tpu:
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu, steps_per_run=128)
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
else:
strategy = tf.distribute.get_strategy() # Default strategy that works on CPU and single GPU
print('Running on CPU')
print("Number of accelerators: ", strategy.num_replicas_in_sync)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment