{
  "id": 400103,
  "title": "pytorch to tf to tflite conversion tf.Range issue",
  "url": "/competitions/asl-signs/discussion/400103",
  "author_name": "",
  "post_date": "2023-04-06T21:22:42.731342400Z",
  "votes": 3,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Error Message:</p>\n<pre><code>Some ops are  supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select \nTF Select ops: Range\nDetails:\n    tf.Range(tensor&lt;i64&gt;, tensor&lt;i64&gt;, tensor&lt;i64&gt;) -&gt; (tensor&lt;?xi64&gt;) : {device = }\n</code></pre>\n<p>'Normally' tflite conversion looks like supporting tf.range but this tf.Range with a capital 'R' is a questionmark. </p>\n<p>I have read the related thread. Seen solutions like: The onnx graph could be amended, or maybe explicitly stating the arguments of the functions in pytorch code could solve…not too easy, because this tf.Range could have been triggered by methods/functions wrapped in higher level methods/functions so might not be directly visible in pytorch code.<br>\nUsing select ops causes issues during submission, although it does the conversion..</p>\n<p>So I am stuck, <br>\nAny advice would be very welcome.</p>",
  "messages": [
    {
      "id": "2212551",
      "postDate": "04/06/2023 21:22:42",
      "content": "<p>Error Message:</p>\n<pre><code>Some ops are  supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select \nTF Select ops: Range\nDetails:\n    tf.Range(tensor&lt;i64&gt;, tensor&lt;i64&gt;, tensor&lt;i64&gt;) -&gt; (tensor&lt;?xi64&gt;) : {device = }\n</code></pre>\n<p>'Normally' tflite conversion looks like supporting tf.range but this tf.Range with a capital 'R' is a questionmark. </p>\n<p>I have read the related thread. Seen solutions like: The onnx graph could be amended, or maybe explicitly stating the arguments of the functions in pytorch code could solve…not too easy, because this tf.Range could have been triggered by methods/functions wrapped in higher level methods/functions so might not be directly visible in pytorch code.<br>\nUsing select ops causes issues during submission, although it does the conversion..</p>\n<p>So I am stuck, <br>\nAny advice would be very welcome.</p>",
      "rawMarkdown": "Error Message:\n\n```python\nSome ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select \nTF Select ops: Range\nDetails:\n\ttf.Range(tensor<i64>, tensor<i64>, tensor<i64>) -> (tensor<?xi64>) : {device = \"\"}\n```\n\n'Normally' tflite conversion looks like supporting tf.range but this tf.Range with a capital 'R' is a questionmark. \n\nI have read the related thread. Seen solutions like: The onnx graph could be amended, or maybe explicitly stating the arguments of the functions in pytorch code could solve...not too easy, because this tf.Range could have been triggered by methods/functions wrapped in higher level methods/functions so might not be directly visible in pytorch code.\nUsing select ops causes issues during submission, although it does the conversion..\n\nSo I am stuck, \nAny advice would be very welcome.",
      "votes": null
    },
    {
      "id": "2213947",
      "postDate": "04/08/2023 01:43:30",
      "content": "<p>Had this issue at one point, here are a few torch functions that I had to get rid of :</p>\n<ul>\n<li><code>torch.arange</code></li>\n<li><code>torch.linspace</code></li>\n<li><code>torch.gather</code></li>\n</ul>\n<p>(I'm assuming you're doing torch -&gt; onnx -&gt; tf -&gt; tflite)</p>",
      "rawMarkdown": "Had this issue at one point, here are a few torch functions that I had to get rid of :\n- `torch.arange`\n- `torch.linspace`\n- `torch.gather`\n\n(I'm assuming you're doing torch -> onnx -> tf -> tflite)",
      "votes": null
    },
    {
      "id": "2213970",
      "postDate": "04/08/2023 02:43:23",
      "content": "<p>Thank You.<br>\nYes currently I am doing torch onnx tf tflite pipeline. It is the final conversion to tflite that is hard to guess. I mean, I was using torch.arange and conversion was successful, than I connected another torch.arange to the graph, suddenly I got those tf.Range issues. So I guess it is not only about what you use, but also about how you use it, which affects how onnx decides to make the graph.<br>\nI have seen solutions which go into modifying the onnx graph, probably not very convenient at this point:-)</p>",
      "rawMarkdown": "Thank You.\nYes currently I am doing torch onnx tf tflite pipeline. It is the final conversion to tflite that is hard to guess. I mean, I was using torch.arange and conversion was successful, than I connected another torch.arange to the graph, suddenly I got those tf.Range issues. So I guess it is not only about what you use, but also about how you use it, which affects how onnx decides to make the graph.\nI have seen solutions which go into modifying the onnx graph, probably not very convenient at this point:-)",
      "votes": null
    },
    {
      "id": "2217795",
      "postDate": "04/11/2023 07:06:28",
      "content": "<p>I think I was able to fix it by replacing <code>torch.arange</code> with <code>torch.from_numpy(np.arange)</code></p>",
      "rawMarkdown": "I think I was able to fix it by replacing ```torch.arange``` with ```torch.from_numpy(np.arange)```",
      "votes": null
    },
    {
      "id": "2218500",
      "postDate": "04/11/2023 18:44:12",
      "content": "<p>I tried many different things. It turns out doing a complex preprocessing in pytorch layers and then converting it to tf to tflite, can become very unstable. For my code, I solved it without replacing  'torch.arange'. I had to deal with the arguments on how they evolved until the torch.arange line and made some changes here and there and finally it worked. Now I have other problems but I am progressing :-)</p>",
      "rawMarkdown": "I tried many different things. It turns out doing a complex preprocessing in pytorch layers and then converting it to tf to tflite, can become very unstable. For my code, I solved it without replacing  'torch.arange'. I had to deal with the arguments on how they evolved until the torch.arange line and made some changes here and there and finally it worked. Now I have other problems but I am progressing :-)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2213947,
      "author_name": "theoviel",
      "author_url": "",
      "post_date": "04/08/2023 01:43:30",
      "content": "<p>Had this issue at one point, here are a few torch functions that I had to get rid of :</p>\n<ul>\n<li><code>torch.arange</code></li>\n<li><code>torch.linspace</code></li>\n<li><code>torch.gather</code></li>\n</ul>\n<p>(I'm assuming you're doing torch -&gt; onnx -&gt; tf -&gt; tflite)</p>",
      "votes": null,
      "replies": [
        {
          "id": 2213970,
          "author_name": "abdulkadirguner",
          "author_url": "",
          "post_date": "04/08/2023 02:43:23",
          "content": "<p>Thank You.<br>\nYes currently I am doing torch onnx tf tflite pipeline. It is the final conversion to tflite that is hard to guess. I mean, I was using torch.arange and conversion was successful, than I connected another torch.arange to the graph, suddenly I got those tf.Range issues. So I guess it is not only about what you use, but also about how you use it, which affects how onnx decides to make the graph.<br>\nI have seen solutions which go into modifying the onnx graph, probably not very convenient at this point:-)</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2217795,
      "author_name": "martynoveduard",
      "author_url": "",
      "post_date": "04/11/2023 07:06:28",
      "content": "<p>I think I was able to fix it by replacing <code>torch.arange</code> with <code>torch.from_numpy(np.arange)</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 2218500,
          "author_name": "abdulkadirguner",
          "author_url": "",
          "post_date": "04/11/2023 18:44:12",
          "content": "<p>I tried many different things. It turns out doing a complex preprocessing in pytorch layers and then converting it to tf to tflite, can become very unstable. For my code, I solved it without replacing  'torch.arange'. I had to deal with the arguments on how they evolved until the torch.arange line and made some changes here and there and finally it worked. Now I have other problems but I am progressing :-)</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2212551": "Error Message:\n\n```python\nSome ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select \nTF Select ops: Range\nDetails:\n\ttf.Range(tensor<i64>, tensor<i64>, tensor<i64>) -> (tensor<?xi64>) : {device = \"\"}\n```\n\n'Normally' tflite conversion looks like supporting tf.range but this tf.Range with a capital 'R' is a questionmark. \n\nI have read the related thread. Seen solutions like: The onnx graph could be amended, or maybe explicitly stating the arguments of the functions in pytorch code could solve...not too easy, because this tf.Range could have been triggered by methods/functions wrapped in higher level methods/functions so might not be directly visible in pytorch code.\nUsing select ops causes issues during submission, although it does the conversion..\n\nSo I am stuck, \nAny advice would be very welcome.",
    "2213947": "Had this issue at one point, here are a few torch functions that I had to get rid of :\n- `torch.arange`\n- `torch.linspace`\n- `torch.gather`\n\n(I'm assuming you're doing torch -> onnx -> tf -> tflite)",
    "2213970": "Thank You.\nYes currently I am doing torch onnx tf tflite pipeline. It is the final conversion to tflite that is hard to guess. I mean, I was using torch.arange and conversion was successful, than I connected another torch.arange to the graph, suddenly I got those tf.Range issues. So I guess it is not only about what you use, but also about how you use it, which affects how onnx decides to make the graph.\nI have seen solutions which go into modifying the onnx graph, probably not very convenient at this point:-)",
    "2217795": "I think I was able to fix it by replacing ```torch.arange``` with ```torch.from_numpy(np.arange)```",
    "2218500": "I tried many different things. It turns out doing a complex preprocessing in pytorch layers and then converting it to tf to tflite, can become very unstable. For my code, I solved it without replacing  'torch.arange'. I had to deal with the arguments on how they evolved until the torch.arange line and made some changes here and there and finally it worked. Now I have other problems but I am progressing :-)"
  },
  "source": "meta"
}