Skip to content
  • Peter Goldsborough's avatar
    6f2307ba
    Allow building libraries with setuptools that dont have abi suffix (#14130) · 6f2307ba
    Peter Goldsborough authored
    Summary:
    When using `setuptools` to build a Python extension, setuptools will automatically add an ABI suffix like `cpython-37m-x86_64-linux-gnu` to the shared library name when using Python 3. This is required for extensions meant to be imported as Python modules. When we use setuptools to build shared libraries not meant as Python modules, for example libraries that define and register TorchScript custom ops, having your library called `my_ops.cpython-37m-x86_64-linux-gnu.so` is a bit annoying compared to just `my_ops.so`, especially since you have to reference the library name when loading it with `torch.ops.load_library` in Python.
    
    This PR fixes this by adding a `with_options` class method to the `torch.utils.cpp_extension.BuildExtension` which allows configuring the `BuildExtension`. In this case, the first option we add is `no_python_abi_suffix`, which we then use in `get_ext_filename` (override from `setuptools.build_ext`) to throw away the ABI suffix.
    
    I've added a test `setup.py` in a `no_python_abi_suffix_test` folder.
    
    Fixes https://github.com/pytorch/pytorch/issues/14188
    
    t-vi fmassa soumith
    Pull Request resolved: https://github.com/pytorch/pytorch/pull/14130
    
    Differential Revision: D13216575
    
    Pulled By: goldsborough
    
    fbshipit-source-id: 67dc345c1278a1a4ee4ca907d848bc1fb4956cfa
    6f2307ba
    Allow building libraries with setuptools that dont have abi suffix (#14130)
    Peter Goldsborough authored
    Summary:
    When using `setuptools` to build a Python extension, setuptools will automatically add an ABI suffix like `cpython-37m-x86_64-linux-gnu` to the shared library name when using Python 3. This is required for extensions meant to be imported as Python modules. When we use setuptools to build shared libraries not meant as Python modules, for example libraries that define and register TorchScript custom ops, having your library called `my_ops.cpython-37m-x86_64-linux-gnu.so` is a bit annoying compared to just `my_ops.so`, especially since you have to reference the library name when loading it with `torch.ops.load_library` in Python.
    
    This PR fixes this by adding a `with_options` class method to the `torch.utils.cpp_extension.BuildExtension` which allows configuring the `BuildExtension`. In this case, the first option we add is `no_python_abi_suffix`, which we then use in `get_ext_filename` (override from `setuptools.build_ext`) to throw away the ABI suffix.
    
    I've added a test `setup.py` in a `no_python_abi_suffix_test` folder.
    
    Fixes https://github.com/pytorch/pytorch/issues/14188
    
    t-vi fmassa soumith
    Pull Request resolved: https://github.com/pytorch/pytorch/pull/14130
    
    Differential Revision: D13216575
    
    Pulled By: goldsborough
    
    fbshipit-source-id: 67dc345c1278a1a4ee4ca907d848bc1fb4956cfa
Loading