pytorch / pytorch/pytorch

BuildExtension(no_python_abi_suffix=True).get_ext_filename result is incorrect when ABI part is not present in the filename.

Open
#170,542 3 comments 0 reactions 0 assignees View on GitHub
module: cpp-extensions topic: bug fixes triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

As in the title.

When `get_ext_filename(ext_name)` is called from `BuildExtension(no_python_abi_suffix=True)`, there exist an assumption that ABI component exists in an extension file name:
https://github.com/pytorch/pytorch/blob/3854d691ce36d00f5acbebc03c1f53f0f1e00d9b/torch/utils/cpp_extension.py#L1162-L1164

However, on Windows, the ABI component may not be present (as we discovered in torchaudio) and `get_ext_filename("torchaudio.lib.libtorchaudio")` returns `"pyd"` while the expected return value would be `"torchaudio\\lib\\libtorchaudio.pyd"`, for instance.

As a fix, I suggest changing the relevant code in `get_ext_filename` as follows:
```python
# Remove ABI component only if it actually exists in a file name.
if len(ext_filename_parts) > 2:
# Omit the second to last element.
without_abi = ext_filename_parts[:-2] + ext_filename_parts[-1:]
ext_filename = '.'.join(without_abi)
```
to avoid pointless return values such as `"pyd"`.

A workaround to this issue is to specify `no_python_abi_suffix=False`.

cc @janeyx99

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.