Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Remove python version specific usage within extensions #1270

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def get_extensions():
extension(
"torchao._C",
sources,
py_limited_api=True,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to have it, but imo not necessary. At least by default, linker will ignore the dependencies, unless one uses some symbols from that library (unless library is linked with -Wno-as-needed)

Copy link
Contributor Author

@janeyx99 janeyx99 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary only to get python to recognize the extension + therefore the wheel as python agnostic. e.g., for pypi packaging and naming purposes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also for our cppextension to know not to drag in libtorch_python

extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
)
Expand All @@ -136,4 +137,7 @@ def get_extensions():
long_description_content_type="text/markdown",
url="https://github.com/pytorch-labs/ao",
cmdclass={"build_ext": BuildExtension},
options={"bdist_wheel": {
"py_limited_api": "cp38"
}},
)
8 changes: 6 additions & 2 deletions torchao/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
)
if not _IS_FBCODE:
try:
from . import _C
from importlib.util import find_spec
from pathlib import Path
spec = find_spec("torchao")
assert spec is not None, "torchao python module spec is unexpectedly None"
SO_PATH = Path(spec.origin).parent / "_C.abi3.so"
Comment on lines +25 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easiest solution are usually the best one :)

Suggested change
from importlib.util import find_spec
from pathlib import Path
spec = find_spec("torchao")
assert spec is not None, "torchao python module spec is unexpectedly None"
SO_PATH = Path(spec.origin).parent / "_C.abi3.so"
SO_PATH = Path(__file__).parent / "_C.abi3.so"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work in the pip wheel -e . followed by the pip install *.whl case!

In that case the _C.abi3.so will live in site-packages (or wherever the user's install is) so we need a more encompassing way to access the location here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you share an example? And perhaps it talks about some gaps in setup.py as _C should have been installed in the same folder as __init__.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this should indeed work in the pip wheel/pip install case, because the init.py file and the _C.abi3.so are in the same folder under site-packages:
image

It's funny because I actually started off with what you had lol so dang I did some extra work :'/

Do you know where the logic lives to always place the _C under the same folder as init.py? I don't see mention of it in the ao setup.py, so is this a Python guarantee for custom extensions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know where the logic lives to always place the _C under the same folder as init.py?

@janeyx99 I think it's a setuptools thing

ao/setup.py

Lines 114 to 116 in 01dc7da

extension(
"torchao._C",
sources,

So it automatically understands that it should put _C.so under torchao folder. I played around with meson-python a bit for another project, and they don't do the same thing (meson-python will create torchao._C.so instead)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thanks for the info @gau-nernst!

torch.ops.load_library(SO_PATH)
from . import ops
except:
_C = None
logging.info("Skipping import of cpp extensions")

from torchao.quantization import (
Expand Down
3 changes: 0 additions & 3 deletions torchao/csrc/init.cpp

This file was deleted.

Loading