|
| 1 | +from torch.utils import cpp_extension |
| 2 | +import os |
| 3 | + |
| 4 | +_HERE = os.path.abspath(__file__) |
| 5 | +_EXECUTORCH_PATH = os.path.dirname(os.path.dirname(_HERE)) |
| 6 | + |
| 7 | +def load_inline(name, |
| 8 | + cpp_sources, |
| 9 | + functions=None, |
| 10 | + extra_cflags=None, |
| 11 | + extra_ldflags=None, |
| 12 | + extra_include_paths=None, |
| 13 | + build_directory=None, |
| 14 | + verbose=False, |
| 15 | + is_python_module=True, |
| 16 | + with_pytorch_error_handling=True, |
| 17 | + keep_intermediates=True, |
| 18 | + use_pch=False): |
| 19 | + # Register the code into PyTorch |
| 20 | + aten_extra_cflags = ["-DUSE_ATEN_LIB"] + (extra_cflags if extra_cflags else []) |
| 21 | + extra_ldflags = [f"-L{_EXECUTORCH_PATH}", f"-Wl,-rpath,{_EXECUTORCH_PATH}", "-lexecutorch"] + (extra_ldflags if extra_ldflags else []) |
| 22 | + module = cpp_extension.load_inline( |
| 23 | + name, |
| 24 | + cpp_sources, |
| 25 | + functions=functions, |
| 26 | + extra_cflags=aten_extra_cflags, |
| 27 | + extra_ldflags=extra_ldflags, |
| 28 | + extra_include_paths=extra_include_paths, |
| 29 | + build_directory=build_directory, |
| 30 | + verbose=verbose, |
| 31 | + is_python_module=is_python_module, |
| 32 | + with_pytorch_error_handling=with_pytorch_error_handling, |
| 33 | + keep_intermediates=keep_intermediates, |
| 34 | + use_pch=use_pch, |
| 35 | + ) |
| 36 | + # Now register the code into ExecuTorch |
| 37 | + cpp_extension.load_inline( |
| 38 | + name, |
| 39 | + cpp_sources, |
| 40 | + functions=None, # leave this out since we are not passing out any python module |
| 41 | + extra_cflags=extra_cflags, |
| 42 | + extra_ldflags=extra_ldflags, |
| 43 | + extra_include_paths=extra_include_paths, |
| 44 | + build_directory=build_directory, |
| 45 | + verbose=verbose, |
| 46 | + is_python_module=False, # don't register as a python module. Load shared library as a side effect. |
| 47 | + with_pytorch_error_handling=with_pytorch_error_handling, |
| 48 | + keep_intermediates=keep_intermediates, |
| 49 | + use_pch=use_pch, |
| 50 | + ) |
| 51 | + return module |
0 commit comments