{
  "id": 512298,
  "title": "Making TPU work with Keras and JAX",
  "url": "/competitions/leap-atmospheric-physics-ai-climsim/discussion/512298",
  "author_name": "Federico Peccia",
  "post_date": "2024-06-14T11:49:10.455000",
  "votes": 0,
  "comment_count": 5,
  "views": 0,
  "content": "<p>I was trying to add TPU capabilities to the current highest-scored public notebook. I am running into some problems, particularly an error that says \"A driver for /dev/accel0 was already created at…\" (similar to what we can find <a href=\"https://github.com/google/jax/issues/13260\" target=\"_blank\">here </a> and <a href=\"https://github.com/huggingface/accelerate/issues/525\" target=\"_blank\">here</a>).</p>\n<p>If someone could point me in the right direction, I would really appreciate it. I have shared the notebook with my changes: <a href=\"https://www.kaggle.com/code/fpeccia/keras-baseline-seq2seq-tpu\" target=\"_blank\">https://www.kaggle.com/code/fpeccia/keras-baseline-seq2seq-tpu</a></p>",
  "messages": [
    {
      "id": 2872007,
      "postDate": "2024-06-14T14:14:22.740Z",
      "content": "<p>I managed to make everything work by switching to tensorflow backend and updating keras and tensorflow to the latest version.</p>\n<p>To save the model do something like that</p>\n<pre><code> strategy.scope():\n    tpu_model = build_model()\ntpu_model.fit(...)\n\nlocal_model = build_model()\nlocal_model.set_weights(tpu_model.get_weights())\nlocal_model.save()\n</code></pre>",
      "rawMarkdown": "I managed to make everything work by switching to tensorflow backend and updating keras and tensorflow to the latest version.\n\nTo save the model do something like that\n\n```python\nwith strategy.scope():\n    tpu_model = build_model()\ntpu_model.fit(...)\n\nlocal_model = build_model()\nlocal_model.set_weights(tpu_model.get_weights())\nlocal_model.save(\"model.keras\")\n```",
      "votes": 2,
      "replies": [
        {
          "id": 2872009,
          "postDate": "2024-06-14T14:16:13.313Z",
          "content": "<p>As I suspected hahaha. Good job.</p>",
          "rawMarkdown": "As I suspected hahaha. Good job.",
          "replies": [
            {
              "id": 2872028,
              "postDate": "2024-06-14T14:24:03.937Z",
              "content": "<p>Thanks both, I had just tested changing to tensorflow backend and it worked :D</p>",
              "rawMarkdown": "Thanks both, I had just tested changing to tensorflow backend and it worked :D",
              "votes": 1
            }
          ]
        }
      ]
    },
    {
      "id": 2871957,
      "postDate": "2024-06-14T13:42:48.363Z",
      "content": "<p>My gut feeling is that Jax is the culprit. However, I have never used Jax, so I won't dive into this. If you don't have a lot of experience with TPU, I STRONGLY recommend building on a working TPU notebook. TPU is hard enough when building on a working pipeline. Creating a new working TPU pipeline without a lot of experience will be a hard path full of tears and frustration…haha</p>",
      "rawMarkdown": "My gut feeling is that Jax is the culprit. However, I have never used Jax, so I won't dive into this. If you don't have a lot of experience with TPU, I STRONGLY recommend building on a working TPU notebook. TPU is hard enough when building on a working pipeline. Creating a new working TPU pipeline without a lot of experience will be a hard path full of tears and frustration...haha",
      "votes": 2
    },
    {
      "id": 2871767,
      "postDate": "2024-06-14T11:49:10.457Z",
      "content": "<p>I was trying to add TPU capabilities to the current highest-scored public notebook. I am running into some problems, particularly an error that says \"A driver for /dev/accel0 was already created at…\" (similar to what we can find <a href=\"https://github.com/google/jax/issues/13260\" target=\"_blank\">here </a> and <a href=\"https://github.com/huggingface/accelerate/issues/525\" target=\"_blank\">here</a>).</p>\n<p>If someone could point me in the right direction, I would really appreciate it. I have shared the notebook with my changes: <a href=\"https://www.kaggle.com/code/fpeccia/keras-baseline-seq2seq-tpu\" target=\"_blank\">https://www.kaggle.com/code/fpeccia/keras-baseline-seq2seq-tpu</a></p>",
      "rawMarkdown": "I was trying to add TPU capabilities to the current highest-scored public notebook. I am running into some problems, particularly an error that says \"A driver for /dev/accel0 was already created at...\" (similar to what we can find [here ](https://github.com/google/jax/issues/13260) and [here](https://github.com/huggingface/accelerate/issues/525)).\n\nIf someone could point me in the right direction, I would really appreciate it. I have shared the notebook with my changes: https://www.kaggle.com/code/fpeccia/keras-baseline-seq2seq-tpu\n\n"
    },
    {
      "id": 2881671,
      "postDate": "2024-06-20T22:32:27.313Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 2872007,
      "author_name": "Amedeo Biolatti",
      "author_url": "",
      "post_date": "2024-06-14T14:14:22.740000",
      "content": "<p>I managed to make everything work by switching to tensorflow backend and updating keras and tensorflow to the latest version.</p>\n<p>To save the model do something like that</p>\n<pre><code> strategy.scope():\n    tpu_model = build_model()\ntpu_model.fit(...)\n\nlocal_model = build_model()\nlocal_model.set_weights(tpu_model.get_weights())\nlocal_model.save()\n</code></pre>",
      "votes": 2,
      "replies": [
        {
          "id": 2872009,
          "author_name": "greySnow",
          "author_url": "",
          "post_date": "2024-06-14T14:16:13.313000",
          "content": "<p>As I suspected hahaha. Good job.</p>",
          "votes": 0,
          "replies": [
            {
              "id": 2872028,
              "author_name": "Federico Peccia",
              "author_url": "",
              "post_date": "2024-06-14T14:24:03.937000",
              "content": "<p>Thanks both, I had just tested changing to tensorflow backend and it worked :D</p>",
              "votes": 1,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2871957,
      "author_name": "greySnow",
      "author_url": "",
      "post_date": "2024-06-14T13:42:48.363000",
      "content": "<p>My gut feeling is that Jax is the culprit. However, I have never used Jax, so I won't dive into this. If you don't have a lot of experience with TPU, I STRONGLY recommend building on a working TPU notebook. TPU is hard enough when building on a working pipeline. Creating a new working TPU pipeline without a lot of experience will be a hard path full of tears and frustration…haha</p>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 2881671,
      "author_name": "",
      "author_url": "",
      "post_date": "2024-06-20T22:32:27.313000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2872007": "I managed to make everything work by switching to tensorflow backend and updating keras and tensorflow to the latest version.\n\nTo save the model do something like that\n\n```python\nwith strategy.scope():\n    tpu_model = build_model()\ntpu_model.fit(...)\n\nlocal_model = build_model()\nlocal_model.set_weights(tpu_model.get_weights())\nlocal_model.save(\"model.keras\")\n```",
    "2871957": "My gut feeling is that Jax is the culprit. However, I have never used Jax, so I won't dive into this. If you don't have a lot of experience with TPU, I STRONGLY recommend building on a working TPU notebook. TPU is hard enough when building on a working pipeline. Creating a new working TPU pipeline without a lot of experience will be a hard path full of tears and frustration...haha",
    "2871767": "I was trying to add TPU capabilities to the current highest-scored public notebook. I am running into some problems, particularly an error that says \"A driver for /dev/accel0 was already created at...\" (similar to what we can find [here ](https://github.com/google/jax/issues/13260) and [here](https://github.com/huggingface/accelerate/issues/525)).\n\nIf someone could point me in the right direction, I would really appreciate it. I have shared the notebook with my changes: https://www.kaggle.com/code/fpeccia/keras-baseline-seq2seq-tpu\n\n",
    "2881671": ""
  }
}