Skip to content

[tflite] Loading tflite with await import('...') should work #6402

@josephrocca

Description

@josephrocca
Contributor

This code correctly loads the tf-tflite library:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@3.16.0/dist/tf-core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu@3.16.0/dist/tf-backend-cpu.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite@0.0.1-alpha.8/dist/tf-tflite.min.js"></script>

But this doesn't:

<script type="module">
  await import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@3.16.0/dist/tf-core.min.js");
  await import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu@3.16.0/dist/tf-backend-cpu.min.js");
  await import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite@0.0.1-alpha.8/dist/tf-tflite.min.js");
</script>

If I understand correctly, the two should work equivalently (it does with other libraries that I've tried). The latter code uses the wrong location for the tflite_web_api_cc_simd.js file which results in a request error. I think this probably has something to do with the scripts trying to use document.currentScript.src instead of import.meta.url. It should use the latter if running in a module.

Activity

rthadur

rthadur commented on May 12, 2022

@rthadur
Contributor

@josephrocca please share the error message if possible . Thank you

josephrocca

josephrocca commented on May 13, 2022

@josephrocca
ContributorAuthor

@rthadur Sure:

Uncaught (in promise) TypeError: $[(b.tfliteWebApiName + "_ModuleFactory")] is not a function
    at $jscomp.generator.Engine_.program_ (tf-tflite.min.js:17:756379)
    at $jscomp.generator.Engine_.nextStep_ (tf-tflite.min.js:17:12479)
    at $jscomp.generator.Engine_.next_ (tf-tflite.min.js:17:11443)
    at $jscomp.generator.Generator_.next (tf-tflite.min.js:17:12924)
    at t (tf-tflite.min.js:17:13343)

Note that it incorrectly makes a request to https://null.jsbin.com/tflite_web_api_cc_simd.js in this example: https://jsbin.com/lifelugivi/edit?html,output

jsbin.com for some reason returns an empty 202 response (rather than 404) when it makes that request, so there's no request error. If you copy and paste these lines into Chrome browser console on a site that appropriately returns a 404 for non-existent files, then you'll see a request and/or JS-parsing related error:

await import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@3.16.0/dist/tf-core.min.js");
await import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu@3.16.0/dist/tf-backend-cpu.min.js");
await import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite@0.0.1-alpha.8/dist/tf-tflite.min.js");

Again, I'm fairly sure this is just because import.meta.url needs to be used to get the correct relative/base URL.

shmishra99

shmishra99 commented on May 10, 2023

@shmishra99
Contributor

Hi @josephrocca ,
I apologize for the delay in my response. After replicating the issue, I have encountered the same error message tflite_web_api_client.js:1687 GET file:///Users/sh/TFJS/issue6402/tflite_web_api_cc_simd.js net::ERR_FILE_NOT_FOUND. It appears that the file tflite_web_api_cc_simd.js is being loaded from the local directory instead of the server. Thank you for bringing this issue to our attention.
CC: @jinjingforever

self-assigned this
on May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @pyu10055@josephrocca@jinjingforever@rthadur@shmishra99

      Issue actions

        [tflite] Loading tflite with `await import('...')` should work · Issue #6402 · tensorflow/tfjs