Skip to content

Build fails with PyTorch 2.13 on macOS: -std=c++17 doesn't support features required by torch's MPS headers (std::type_identity, unordered_map::contains) #2

Description

@kanishkpatel1995

Environment

  • OS: macOS 14.6 (Apple Silicon, M1 Max)
  • Python: 3.11.9
  • PyTorch: 2.13.0
  • mps-bitsandbytes: 0.7.0 (PyPI; same setup.py as current master)
  • Xcode Command Line Tools + Metal Toolchain installed

What happened

pip install mps-bitsandbytes fails to build the native extension, with two compile
errors in mps_bitsandbytes/csrc/mps_bitsandbytes.mm, both originating from
<torch/include/ATen/native/mps/OperationUtils.h>:

error: no template named 'type_identity' in namespace 'std'; did you mean '__type_identity'?
error: no member named 'contains' in 'std::unordered_map<...>'

Root cause

std::type_identity and std::unordered_map::contains() are both C++20 library
features. setup.py hardcodes:

extra_compile_args=["-std=c++17", "-O3", "-DNDEBUG"],

but PyTorch 2.13's MPS backend headers now require C++20. Locally bumping this to
-std=c++20 resolves both errors and lets the extension build successfully.

Also worth noting (smaller, separate issue)

pip install mps-bitsandbytes also fails first with ModuleNotFoundError: No module named 'torch' during the build step, since torch isn't declared as a build-time
requirement -- pip's isolated build env doesn't have it. --no-build-isolation works
around this; might be worth declaring torch under [build-system] requires in
pyproject.toml, or documenting the flag in the README.

Suggested fix

- extra_compile_args=["-std=c++17", "-O3", "-DNDEBUG"],
+ extra_compile_args=["-std=c++20", "-O3", "-DNDEBUG"],

Happy to open a PR with this change (plus the pyproject.toml build-dependency fix)
if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions