{
  "id": 399653,
  "title": "How to use TPU VM",
  "url": "/competitions/asl-signs/discussion/399653",
  "author_name": "chemdatafarmer",
  "post_date": "2023-04-05T01:39:14.357000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Hi Everyone,</p>\n<p>I've been curious about TPUs, so I thought I'd try out the ones here on Kaggle. I activate the TPU VM on the accelerator and run the following code in my notebook (see link for where I found the code): <a href=\"https://www.kaggle.com/code/chemdatafarmer/tpu-1vm-a-simple-tf-2-1-notebook/edit\" target=\"_blank\">https://www.kaggle.com/code/chemdatafarmer/tpu-1vm-a-simple-tf-2-1-notebook/edit</a></p>\n<h1>CHANGED FOR TPU 1VM:</h1>\n<h1>Detect hardware, return appropriate distribution strategy</h1>\n<p>try:<br>\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=\"local\") # \"local\" for 1VM TPU<br>\n    strategy = tf.distribute.TPUStrategy(tpu)<br>\n    print(\"on TPU\")<br>\nexcept tf.errors.NotFoundError:<br>\n    print(\"not on TPU\")<br>\n    strategy = tf.distribute.MirroredStrategy()</p>\n<p>Unfortunately, it seems the VM fails to detect the TPU and it prints \"not on TPU\". Does anyone know why this might be?</p>",
  "messages": [
    {
      "id": 2209859,
      "postDate": "2023-04-05T01:39:14.357Z",
      "content": "<p>Hi Everyone,</p>\n<p>I've been curious about TPUs, so I thought I'd try out the ones here on Kaggle. I activate the TPU VM on the accelerator and run the following code in my notebook (see link for where I found the code): <a href=\"https://www.kaggle.com/code/chemdatafarmer/tpu-1vm-a-simple-tf-2-1-notebook/edit\" target=\"_blank\">https://www.kaggle.com/code/chemdatafarmer/tpu-1vm-a-simple-tf-2-1-notebook/edit</a></p>\n<h1>CHANGED FOR TPU 1VM:</h1>\n<h1>Detect hardware, return appropriate distribution strategy</h1>\n<p>try:<br>\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=\"local\") # \"local\" for 1VM TPU<br>\n    strategy = tf.distribute.TPUStrategy(tpu)<br>\n    print(\"on TPU\")<br>\nexcept tf.errors.NotFoundError:<br>\n    print(\"not on TPU\")<br>\n    strategy = tf.distribute.MirroredStrategy()</p>\n<p>Unfortunately, it seems the VM fails to detect the TPU and it prints \"not on TPU\". Does anyone know why this might be?</p>",
      "rawMarkdown": "Hi Everyone,\n\nI've been curious about TPUs, so I thought I'd try out the ones here on Kaggle. I activate the TPU VM on the accelerator and run the following code in my notebook (see link for where I found the code): https://www.kaggle.com/code/chemdatafarmer/tpu-1vm-a-simple-tf-2-1-notebook/edit\n\n# CHANGED FOR TPU 1VM:\n# Detect hardware, return appropriate distribution strategy\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=\"local\") # \"local\" for 1VM TPU\n    strategy = tf.distribute.TPUStrategy(tpu)\n    print(\"on TPU\")\nexcept tf.errors.NotFoundError:\n    print(\"not on TPU\")\n    strategy = tf.distribute.MirroredStrategy()\n\nUnfortunately, it seems the VM fails to detect the TPU and it prints \"not on TPU\". Does anyone know why this might be?"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2209859": "Hi Everyone,\n\nI've been curious about TPUs, so I thought I'd try out the ones here on Kaggle. I activate the TPU VM on the accelerator and run the following code in my notebook (see link for where I found the code): https://www.kaggle.com/code/chemdatafarmer/tpu-1vm-a-simple-tf-2-1-notebook/edit\n\n# CHANGED FOR TPU 1VM:\n# Detect hardware, return appropriate distribution strategy\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=\"local\") # \"local\" for 1VM TPU\n    strategy = tf.distribute.TPUStrategy(tpu)\n    print(\"on TPU\")\nexcept tf.errors.NotFoundError:\n    print(\"not on TPU\")\n    strategy = tf.distribute.MirroredStrategy()\n\nUnfortunately, it seems the VM fails to detect the TPU and it prints \"not on TPU\". Does anyone know why this might be?"
  }
}