pygame-web / pkg-porting-wasm

Tracks porting efforts on some modules you'd like to have working in pygbag
https://pygame-web.github.io/wiki/pkg/
5 stars 0 forks source link

How to add Tensorflow? #24

Open ThorvaldAagaard opened 1 year ago

ThorvaldAagaard commented 1 year ago

My game is using Tensorflow, and it runs fine locally, but when I deploy it as a pygbag-solution I get the following error

1234: want='tensorflow' found : False
PKG NOT FOUND : want='tensorflow', resume=<function <module> at 0x11a10e0>, ex=ModuleNotFoundError("No module named 'tensorflow'")

How can I fix that?

pmp-p commented 1 year ago

You should ask (not here it is completely out of scope for pygbag runtime) for a proper wasm port of Tensorflow to cpython-wasm or more likely pyodide.

From a quick search it is not impossible see https://blog.tensorflow.org/2020/03/introducing-webassembly-backend-for-tensorflow-js.html

Of course your game won't run of most of already deployed mobile devices because of wasm extensions used that are NOT implemented, or not available because lack of updates.

transerfing issue to porting section

ThorvaldAagaard commented 1 year ago

That was a major setback. I am mostly interested in getting it to work on the web, Mobile devices in the distant future, I now have a plying application, but I have no idea how to port tensorflow for use in pygbag. Are there any descriptions of how to make the port, or anyone that can perform the port. I will be happy to help testing it.

pmp-p commented 1 year ago

I don't know much about gpu computing and tensorflow but i guess most people will wait for webgpu+wasm2 to do that kind of - very - complicated stuff.

ThorvaldAagaard commented 1 year ago

All my models are locally trained, and it is just the usage of the models in a web-context, that is interesting, so no gpu-computing needed.

Implementation is at: https://github.com/ThorvaldAagaard/bridge-with-ben and looking thru the code, there are not much tensorflow usage besides opening the models, and calling then.

pmp-p commented 1 year ago

so basically you only need a small part of tensorflow like a "runtime" only ? if so cannot you call the existing wasm implementation via javascript window object from pygbag ?

ThorvaldAagaard commented 1 year ago

Yes, a sort of runtime.

I am only accessing these classes: tf.Graph() tf.compat.v1.Session() tf.compat.v1.train.import_meta_graph() tf.nn.softmax()

I have been looking at how to use the generated models from javascript, but not found a solution (yet), as the input to calling the models are numpy arrays (and very complex). If I could get that to work the game would probably be implemented in javascript/typescript/flutter :-)

But where can I read more about how to use the javascript window object from pygbag?

pmp-p commented 1 year ago

sorry no doc about that but most objects are 1:1 mapping to python except promises, but the goal is not to "use" js it is more "you can pass pointers and do memcpy with js", it would only serve as a transport beetween wasm memories ( that if the above wasm implementation of runtime cannot be linked or dlopened directly : i don't have time to investigate now so idk)