componentize-py icon indicating copy to clipboard operation
componentize-py copied to clipboard

Pytorch support

Open looksgood opened this issue 11 months ago • 6 comments

I introduced the pytorch module. When executing the compilation, the following error was reported. How should I solve this problem?

Building component my-server with `componentize-py -w spin-http componentize my_server -o target/app/my_server.wasm`
Traceback (most recent call last):
  File "/Users/ken/Work/Workspace/spin/myenv/bin/componentize-py", line 8, in <module>
    sys.exit(script())

AssertionError: Traceback (most recent call last):
  File "/0/my_server.py", line 4, in <module>
    from strategy import get_strategy_result
  File "/0/strategy.py", line 1, in <module>
    from torch import tensor
  File "/1/torch/__init__.py", line 14, in <module>
    import ctypes
  File "/python/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

the strategy.py is:

from torch import tensor

def get_strategy_result():
    # Create a tensor
    x = tensor([1.0, 2.0, 3.0])
    # Perform a simple operation
    y = x * 2
    return y
        
if __name__ == "__main__":
    print(get_strategy_result())

The version of componentize-py is 0.16.0 my plantform is MacBook Pro the version of Python is 3.11.10

looksgood avatar Feb 17 '25 08:02 looksgood

Hi @looksgood since pytorch is a native extension, it requires extra support, specifically being compiled for a WASI target.

Without a wheel for WASI, there won't be a way to load this as the wheel on your computer is built for another target architecture.

benbrandt avatar Feb 17 '25 10:02 benbrandt

@benbrandt thanks for your reply. Is there a schedule to solve this issue? There are still many requirements for Pthon that rely on external libraries.

Hi @looksgood since pytorch is a native extension, it requires extra support, specifically being compiled for a WASI target.

Without a wheel for WASI, there won't be a way to load this as the wheel on your computer is built for another target architecture.

looksgood avatar Feb 17 '25 13:02 looksgood

There are ongoing efforts to make this easier for sure. Python only dependencies should be mostly fine, it's the packages backed by C/C++/Rust the cause more challenges.

And pytorch is an interesting one because it may need extra support for GPU access as well

benbrandt avatar Feb 17 '25 17:02 benbrandt

@looksgood If you haven't seen it yet, I created this repo to enable cross-compilation of various Python packages which have native extensions to WASI. @benbrandt has also been doing great work refactoring that repo and adding support for auto-publishing WASI wheels.

AFAIK nobody has tried building Pytorch for WASI yet, and it looks like the Pyodide folks have not ported Pytorch to Wasm yet either, so I'm guessing it's non-trivial.

dicej avatar Feb 18 '25 15:02 dicej

@dicej got it. Thanks for all your great works.

I noticed torch project has uploaded the wheel archives on python wheels . @benbrandt How can I use wasi-wheels and componentize-py to build my project?

looksgood avatar Feb 19 '25 03:02 looksgood

@looksgood the readme shows the commands you need. However, most packages have wheels on PyPI, the issue is what kind.

They don't have a wheel with the wasi target which is what we were referring to. So wasi-wheels won't help you unless we have a custom build for torch, unfortunately.

benbrandt avatar Feb 19 '25 05:02 benbrandt