Revert "Delete functorch C extension entirely. (#163340)"

This reverts commit 1faf6367e396b1d0894e8735912a47ac465f469d.

Reverted https://github.com/pytorch/pytorch/pull/163340 on behalf of https://github.com/wdvr due to temporary revert to pull out #162659 ([comment](https://github.com/pytorch/pytorch/pull/163340#issuecomment-3317105243))
This commit is contained in:
PyTorch MergeBot
2025-09-22 06:20:04 +00:00
parent f0078941cf
commit ae5be038a6
12 changed files with 4883 additions and 0 deletions

View File

@ -382,6 +382,12 @@ def _get_package_path(package_name: str) -> Path:
BUILD_LIBTORCH_WHL = str2bool(os.getenv("BUILD_LIBTORCH_WHL"))
BUILD_PYTHON_ONLY = str2bool(os.getenv("BUILD_PYTHON_ONLY"))
# set up appropriate env variables
if BUILD_LIBTORCH_WHL:
# Set up environment variables for ONLY building libtorch.so and not libtorch_python.so
# functorch is not supported without python
os.environ["BUILD_FUNCTORCH"] = "OFF"
if BUILD_PYTHON_ONLY:
os.environ["BUILD_LIBTORCHLESS"] = "ON"
os.environ["LIBTORCH_LIB_PATH"] = (_get_package_path("torch") / "lib").as_posix()
@ -1244,6 +1250,21 @@ class build_ext(setuptools.command.build_ext.build_ext):
def build_extensions(self) -> None:
self.create_compile_commands()
build_lib = Path(self.build_lib).resolve()
# Copy functorch extension
for ext in self.extensions:
if ext.name != "functorch._C":
continue
fullname = self.get_ext_fullname(ext.name)
filename = Path(self.get_ext_filename(fullname))
src = filename.with_stem("functorch")
dst = build_lib / filename
if src.exists():
report(f"Copying {ext.name} from {src} to {dst}")
dst.parent.mkdir(parents=True, exist_ok=True)
self.copy_file(src, dst)
super().build_extensions()
def get_outputs(self) -> list[str]:
@ -1531,6 +1552,11 @@ def configure_extension_build() -> tuple[
)
ext_modules.append(C)
# These extensions are built by cmake and copied manually in build_extensions()
# inside the build_ext implementation
if cmake_cache_vars["BUILD_FUNCTORCH"]:
ext_modules.append(Extension(name="functorch._C", sources=[]))
cmdclass = {
"bdist_wheel": bdist_wheel,
"build_ext": build_ext,