{
  "id": 200744,
  "title": "UnimplementedError: TPU Compilation failed",
  "url": "/competitions/hubmap-kidney-segmentation/discussion/200744",
  "author_name": "",
  "post_date": "2020-12-01T17:08:52.001653300Z",
  "votes": 3,
  "comment_count": 3,
  "views": 0,
  "content": "<p>While working with TPU I come across this error. </p>\n<p><code>UnimplementedError: {{function_node __inference_train_function_53064}} Compilation failure: CustomCall is not supported to have a dynamic dimension TPU compilation failed [[{{node tpu_compile_succeeded_assert/_12959906650459098508/_6}}]]</code></p>\n<p>After a bit of research, I found this <a href=\"https://github.com/tensorflow/tensorflow/issues/35708\" target=\"_blank\">discussion</a> here. This problem occurs when we use UpSampling2D() while working with Images and masks and  <strong>not tfrecords</strong> since Marcos Novaes used tfrecord and UpSampling2D() in his <a href=\"https://www.kaggle.com/marcosnovaes/hubmap-unet-keras-model-fit-with-tpu/notebook\" target=\"_blank\">notebook</a> here which works perfectly fine with TPU.</p>\n<p>So when I tried to work with <a href=\"https://github.com/qubvel/segmentation_models\" target=\"_blank\">segmentation_models</a> by Pavel Yakubovskiy. This throws the above error again and the same occurred when I tried to use the model from this <a href=\"https://www.kaggle.com/mistag/train-keras-u-net-with-tfrecords-input\" target=\"_blank\">notebook</a>. I guess the problem may not occur if we use <strong>tfrecords</strong>. I hope this helps. </p>",
  "messages": [
    {
      "id": "1098492",
      "postDate": "12/01/2020 17:08:52",
      "content": "<p>While working with TPU I come across this error. </p>\n<p><code>UnimplementedError: {{function_node __inference_train_function_53064}} Compilation failure: CustomCall is not supported to have a dynamic dimension TPU compilation failed [[{{node tpu_compile_succeeded_assert/_12959906650459098508/_6}}]]</code></p>\n<p>After a bit of research, I found this <a href=\"https://github.com/tensorflow/tensorflow/issues/35708\" target=\"_blank\">discussion</a> here. This problem occurs when we use UpSampling2D() while working with Images and masks and  <strong>not tfrecords</strong> since Marcos Novaes used tfrecord and UpSampling2D() in his <a href=\"https://www.kaggle.com/marcosnovaes/hubmap-unet-keras-model-fit-with-tpu/notebook\" target=\"_blank\">notebook</a> here which works perfectly fine with TPU.</p>\n<p>So when I tried to work with <a href=\"https://github.com/qubvel/segmentation_models\" target=\"_blank\">segmentation_models</a> by Pavel Yakubovskiy. This throws the above error again and the same occurred when I tried to use the model from this <a href=\"https://www.kaggle.com/mistag/train-keras-u-net-with-tfrecords-input\" target=\"_blank\">notebook</a>. I guess the problem may not occur if we use <strong>tfrecords</strong>. I hope this helps. </p>",
      "rawMarkdown": "While working with TPU I come across this error. \n\n`UnimplementedError: {{function_node __inference_train_function_53064}} Compilation failure: CustomCall is not supported to have a dynamic dimension TPU compilation failed [[{{node tpu_compile_succeeded_assert/_12959906650459098508/_6}}]]`\n\nAfter a bit of research, I found this [discussion](https://github.com/tensorflow/tensorflow/issues/35708) here. This problem occurs when we use UpSampling2D() while working with Images and masks and  **not tfrecords** since Marcos Novaes used tfrecord and UpSampling2D() in his [notebook](https://www.kaggle.com/marcosnovaes/hubmap-unet-keras-model-fit-with-tpu/notebook) here which works perfectly fine with TPU.\n\nSo when I tried to work with [segmentation_models](https://github.com/qubvel/segmentation_models) by Pavel Yakubovskiy. This throws the above error again and the same occurred when I tried to use the model from this [notebook](https://www.kaggle.com/mistag/train-keras-u-net-with-tfrecords-input). I guess the problem may not occur if we use **tfrecords**. I hope this helps.",
      "votes": null
    },
    {
      "id": "1098523",
      "postDate": "12/01/2020 17:27:43",
      "content": "<p>Hello <a href=\"https://www.kaggle.com/kool777\" target=\"_blank\">@kool777</a>:</p>\n<p>I did encounter this error before, as did other competitors. The reason is that TPU compilation does not support \"dynamic\" shapes for some layers. You will notice in my TFRecord code that I use LITERALS when I define the shape of the tiles I am using (ex. TILE_SIZE = 512). After reading the TFRecord, I explicitly call tf.reshape and pass the specific dimensions as LITERALS. That way the matrix shape is \"fixed\" as opposed to dynamic, and everything works. In my first notebook (read data and build TFRecords) I had this problem, as I was determining size dynamically, ex:</p>\n<p>img_array = tf.reshape( img_bytes, (img_height, img_width, num_channels))</p>\n<p>In my TPU notebook, you will noticed I changed it to use literals for this reason:</p>\n<p>img_array = tf.reshape( img_bytes, (512, 512, 3))</p>\n<p>I am about to publish another TFRecord notebook has a little more elegant solution passing a literal as a parameter, but in the end the shape needs to resolve to a literal at TPU compile time. Hope this helps!! </p>\n<p>Cheers,</p>\n<p>--Marcos</p>",
      "rawMarkdown": "Hello @kool777:\n\nI did encounter this error before, as did other competitors. The reason is that TPU compilation does not support \"dynamic\" shapes for some layers. You will notice in my TFRecord code that I use LITERALS when I define the shape of the tiles I am using (ex. TILE_SIZE = 512). After reading the TFRecord, I explicitly call tf.reshape and pass the specific dimensions as LITERALS. That way the matrix shape is \"fixed\" as opposed to dynamic, and everything works. In my first notebook (read data and build TFRecords) I had this problem, as I was determining size dynamically, ex:\n\nimg_array = tf.reshape( img_bytes, (img_height, img_width, num_channels))\n\nIn my TPU notebook, you will noticed I changed it to use literals for this reason:\n\nimg_array = tf.reshape( img_bytes, (512, 512, 3))\n\nI am about to publish another TFRecord notebook has a little more elegant solution passing a literal as a parameter, but in the end the shape needs to resolve to a literal at TPU compile time. Hope this helps!! \n\nCheers,\n\n--Marcos",
      "votes": null
    },
    {
      "id": "1099630",
      "postDate": "12/02/2020 13:45:41",
      "content": "<p>Thank you very much for the reply. </p>\n<p><strong>The reason is that TPU compilation does not support \"dynamic\" shapes for some layers.</strong> I think I found the solution to use <a href=\"https://github.com/qubvel/segmentation_models\" target=\"_blank\">qubvel/segmentation_models</a> with TPU. After checking the source code I come to know that we can pass <code>decoder_block_type == 'transpose'</code> argument inside the </p>\n<p><code>Unet('seresnet18', input_shape=(256, 256, 3), encoder_weights=None, decoder_block_type='transpose')</code> function which will use <strong>Conv2DTranspose</strong><br>\n instead of <strong>UpSampling2D</strong> which solved the compilation problem. </p>\n<p>I guess now we can run experiments with various segmentation models using TPU with fewer efforts since <a href=\"https://github.com/qubvel/segmentation_models\" target=\"_blank\">qubvel/segmentation_models</a> is working fine with tf-Keras on TPU.  </p>\n<h3>[NOTE] - This is not the problem with tfrecords.</h3>",
      "rawMarkdown": "Thank you very much for the reply. \n\n**The reason is that TPU compilation does not support \"dynamic\" shapes for some layers.** I think I found the solution to use [qubvel/segmentation_models](https://github.com/qubvel/segmentation_models) with TPU. After checking the source code I come to know that we can pass `decoder_block_type == 'transpose'` argument inside the \n\n`Unet('seresnet18', input_shape=(256, 256, 3), encoder_weights=None, decoder_block_type='transpose')` function which will use **Conv2DTranspose**\n instead of **UpSampling2D** which solved the compilation problem. \n\nI guess now we can run experiments with various segmentation models using TPU with fewer efforts since [qubvel/segmentation_models](https://github.com/qubvel/segmentation_models) is working fine with tf-Keras on TPU.  \n\n###[NOTE] - This is not the problem with tfrecords.",
      "votes": null
    },
    {
      "id": "1103023",
      "postDate": "12/05/2020 15:18:04",
      "content": "<p>Hi, I was facing the same issue and I was able to solve it by using the <strong>drop_remainder</strong> parameter in dataset.batch(BATCH_SIZE, drop_remainder=True) in the data pipeline</p>",
      "rawMarkdown": "Hi, I was facing the same issue and I was able to solve it by using the **drop_remainder** parameter in dataset.batch(BATCH_SIZE, drop_remainder=True) in the data pipeline",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1098523,
      "author_name": "marcosnovaes",
      "author_url": "",
      "post_date": "12/01/2020 17:27:43",
      "content": "<p>Hello <a href=\"https://www.kaggle.com/kool777\" target=\"_blank\">@kool777</a>:</p>\n<p>I did encounter this error before, as did other competitors. The reason is that TPU compilation does not support \"dynamic\" shapes for some layers. You will notice in my TFRecord code that I use LITERALS when I define the shape of the tiles I am using (ex. TILE_SIZE = 512). After reading the TFRecord, I explicitly call tf.reshape and pass the specific dimensions as LITERALS. That way the matrix shape is \"fixed\" as opposed to dynamic, and everything works. In my first notebook (read data and build TFRecords) I had this problem, as I was determining size dynamically, ex:</p>\n<p>img_array = tf.reshape( img_bytes, (img_height, img_width, num_channels))</p>\n<p>In my TPU notebook, you will noticed I changed it to use literals for this reason:</p>\n<p>img_array = tf.reshape( img_bytes, (512, 512, 3))</p>\n<p>I am about to publish another TFRecord notebook has a little more elegant solution passing a literal as a parameter, but in the end the shape needs to resolve to a literal at TPU compile time. Hope this helps!! </p>\n<p>Cheers,</p>\n<p>--Marcos</p>",
      "votes": null,
      "replies": [
        {
          "id": 1099630,
          "author_name": "kool777",
          "author_url": "",
          "post_date": "12/02/2020 13:45:41",
          "content": "<p>Thank you very much for the reply. </p>\n<p><strong>The reason is that TPU compilation does not support \"dynamic\" shapes for some layers.</strong> I think I found the solution to use <a href=\"https://github.com/qubvel/segmentation_models\" target=\"_blank\">qubvel/segmentation_models</a> with TPU. After checking the source code I come to know that we can pass <code>decoder_block_type == 'transpose'</code> argument inside the </p>\n<p><code>Unet('seresnet18', input_shape=(256, 256, 3), encoder_weights=None, decoder_block_type='transpose')</code> function which will use <strong>Conv2DTranspose</strong><br>\n instead of <strong>UpSampling2D</strong> which solved the compilation problem. </p>\n<p>I guess now we can run experiments with various segmentation models using TPU with fewer efforts since <a href=\"https://github.com/qubvel/segmentation_models\" target=\"_blank\">qubvel/segmentation_models</a> is working fine with tf-Keras on TPU.  </p>\n<h3>[NOTE] - This is not the problem with tfrecords.</h3>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1103023,
      "author_name": "ashish2001",
      "author_url": "",
      "post_date": "12/05/2020 15:18:04",
      "content": "<p>Hi, I was facing the same issue and I was able to solve it by using the <strong>drop_remainder</strong> parameter in dataset.batch(BATCH_SIZE, drop_remainder=True) in the data pipeline</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1098492": "While working with TPU I come across this error. \n\n`UnimplementedError: {{function_node __inference_train_function_53064}} Compilation failure: CustomCall is not supported to have a dynamic dimension TPU compilation failed [[{{node tpu_compile_succeeded_assert/_12959906650459098508/_6}}]]`\n\nAfter a bit of research, I found this [discussion](https://github.com/tensorflow/tensorflow/issues/35708) here. This problem occurs when we use UpSampling2D() while working with Images and masks and  **not tfrecords** since Marcos Novaes used tfrecord and UpSampling2D() in his [notebook](https://www.kaggle.com/marcosnovaes/hubmap-unet-keras-model-fit-with-tpu/notebook) here which works perfectly fine with TPU.\n\nSo when I tried to work with [segmentation_models](https://github.com/qubvel/segmentation_models) by Pavel Yakubovskiy. This throws the above error again and the same occurred when I tried to use the model from this [notebook](https://www.kaggle.com/mistag/train-keras-u-net-with-tfrecords-input). I guess the problem may not occur if we use **tfrecords**. I hope this helps.",
    "1098523": "Hello @kool777:\n\nI did encounter this error before, as did other competitors. The reason is that TPU compilation does not support \"dynamic\" shapes for some layers. You will notice in my TFRecord code that I use LITERALS when I define the shape of the tiles I am using (ex. TILE_SIZE = 512). After reading the TFRecord, I explicitly call tf.reshape and pass the specific dimensions as LITERALS. That way the matrix shape is \"fixed\" as opposed to dynamic, and everything works. In my first notebook (read data and build TFRecords) I had this problem, as I was determining size dynamically, ex:\n\nimg_array = tf.reshape( img_bytes, (img_height, img_width, num_channels))\n\nIn my TPU notebook, you will noticed I changed it to use literals for this reason:\n\nimg_array = tf.reshape( img_bytes, (512, 512, 3))\n\nI am about to publish another TFRecord notebook has a little more elegant solution passing a literal as a parameter, but in the end the shape needs to resolve to a literal at TPU compile time. Hope this helps!! \n\nCheers,\n\n--Marcos",
    "1099630": "Thank you very much for the reply. \n\n**The reason is that TPU compilation does not support \"dynamic\" shapes for some layers.** I think I found the solution to use [qubvel/segmentation_models](https://github.com/qubvel/segmentation_models) with TPU. After checking the source code I come to know that we can pass `decoder_block_type == 'transpose'` argument inside the \n\n`Unet('seresnet18', input_shape=(256, 256, 3), encoder_weights=None, decoder_block_type='transpose')` function which will use **Conv2DTranspose**\n instead of **UpSampling2D** which solved the compilation problem. \n\nI guess now we can run experiments with various segmentation models using TPU with fewer efforts since [qubvel/segmentation_models](https://github.com/qubvel/segmentation_models) is working fine with tf-Keras on TPU.  \n\n###[NOTE] - This is not the problem with tfrecords.",
    "1103023": "Hi, I was facing the same issue and I was able to solve it by using the **drop_remainder** parameter in dataset.batch(BATCH_SIZE, drop_remainder=True) in the data pipeline"
  },
  "source": "meta"
}