Skip to content

Add macOS (Apple Silicon / arm64) build, packaging, and CI support - #1123

Open
vruotolo wants to merge 5 commits into
tensorflow:masterfrom
vruotolo:fix-469454306
Open

vruotolo wants to merge 5 commits into
tensorflow:masterfrom
vruotolo:fix-469454306

Conversation

@vruotolo

@vruotolo vruotolo commented Oct 14, 2026 •

Copy link
Copy Markdown

Summary

Adds official build, wheel packaging, and GitHub Actions CI support for macOS (Apple Silicon / arm64, macOS 12.0+) with TensorFlow 2.19.1:

  • Build Configuration (configure.sh, third_party/tf/tf_configure.bzl, third_party/tf/BUILD.tpl):
    • Detect libtensorflow_framework*.dylib on Darwin and configure @local_config_tf//:libtensorflow_framework to output libtensorflow_framework.dylib on macOS.
    • Guard Linux-only libstdc++ ABI (_GLIBCXX_USE_CXX11_ABI=1) and GCC-only warning flags (-Wno-stringop-overflow, -Wno-maybe-uninitialized) on macOS.
    • Add Darwin Bazel flags (--macos_minimum_os=12.0, --host_cxxopt=-std=c++17, -DGRPC_BAZEL_BUILD, -Wl,-undefined,dynamic_lookup, and @loader_path rpaths to ../tensorflow).
    • Enforce CPU builds (TF_NEED_CUDA=0) on macOS.
  • Packaging & Custom Op Loading (release/, tensorflow_quantum/core/ops/load_module.py):
    • Update release/build_pip_package.sh for BSD/macOS compatibility (cp -LR, avoid clobbering system TMPDIR, clean up temporary build directory on exit).
    • Replace distutils.sysconfig.get_python_lib (removed in Python 3.12) with standard library sysconfig.get_path("platlib") in tensorflow_quantum/core/ops/load_module.py.
    • Add native macOS build and delocate-wheel (excluding libtensorflow_framework.2.dylib) repair workflows to release/build_distribution.sh, release/clean_distribution.sh, and release/build_release.sh.
  • CI, Tests & Documentation (.github/workflows/, scripts/, docs/):
    • Update scripts/ci_install.sh to install Bazel 6.5.0 on Darwin (arm64/x86_64) and guard x86-only -msse* flags behind x86_64 checks.
    • Relax single-precision state norm tolerance in spin_system_test.py from 1e-6 to 2e-6 to account for arm64 FMA rounding differences.
    • Add macos-14 jobs (Python 3.10, 3.11, 3.12) to .github/workflows/ci.yaml and .github/workflows/ci-nightly-build-test.yaml.
    • Update OS classifiers in release/setup.py and documentation in README.md, docs/install.md, and release/README.md.

Testing & Validation

  • Verified formatting (scripts/format_check.sh) and linting (scripts/lint_all.sh).
  • Verified full Bazel build, pip wheel packaging, and unit/package test suite (scripts/test_all.sh, scripts/build_pip_package_test.sh) across Linux (ubuntu-latest) and macOS (macos-14 arm64, Python 3.10–3.12) in GitHub Actions CI.

Enable building, testing, and packaging TensorFlow Quantum on macOS
(Apple Silicon / arm64) with TensorFlow 2.19.1:

- configure.sh & third_party/tf/:
  - Detect `libtensorflow_framework*.dylib` on Darwin and configure
    `@local_config_tf//:libtensorflow_framework` to output
    `libtensorflow_framework.dylib` on macOS.
  - Guard Linux-only `libstdc++` ABI and GCC warning flags (`-Wno-stringop-overflow`,
    `-Wno-maybe-uninitialized`) on macOS.
  - Add macOS Bazel flags (`--macos_minimum_os=12.0`, `--host_cxxopt=-std=c++17`,
    `-DGRPC_BAZEL_BUILD`, `-Wl,-undefined,dynamic_lookup`, and `@loader_path`
    rpaths to `tensorflow`).
  - Enforce CPU build (`TF_NEED_CUDA=0`) when configuring on macOS.
- Packaging & custom op loading:
  - Update `release/build_pip_package.sh` to dereference Bazel runfiles symlinks
    portably on BSD/macOS `cp` (`-LR`), avoid clobbering the macOS `TMPDIR`
    environment variable, and normalize relative destination paths.
  - Replace removed-in-3.12 `distutils.sysconfig.get_python_lib` with stdlib
    `sysconfig.get_path("platlib")` in `tensorflow_quantum/core/ops/load_module.py`.
  - Add native macOS build and `delocate-wheel` repair paths to
    `release/build_distribution.sh`, `release/clean_distribution.sh`, and
    `release/build_release.sh`.
- CI & scripts:
  - Update `scripts/ci_install.sh` to install Bazel 6.5.0 on Darwin (`arm64`/`x86_64`).
  - Guard x86-only `-msse2`/`-msse3`/`-msse4` flags behind `uname -m == x86_64` in
    test and benchmark scripts.
  - Add `macos-14` jobs to `.github/workflows/ci.yaml` and
    `.github/workflows/ci-nightly-build-test.yaml`.
- Docs & metadata:
  - Add macOS and Linux OS classifiers in `release/setup.py` and update
    `README.md`, `docs/install.md`, and `release/README.md`.
@vruotolo
vruotolo marked this pull request as ready for review October 15, 2026 19:23

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for building and running TensorFlow Quantum on macOS (Apple Silicon / arm64) alongside Linux. Key changes include updating the configuration scripts, Bazel build configurations, and distribution release scripts to support macOS-specific tools like delocate and .dylib shared libraries, as well as making compiler flags conditional on the CPU architecture. Additionally, deprecated distutils usage is replaced with sysconfig, and test tolerances are adjusted. Feedback on the changes suggests adding a trap in the pip package build script to ensure the temporary build directory is cleaned up if the script exits prematurely.

Comment thread release/build_pip_package.sh
@vruotolo

Copy link
Copy Markdown
Author

Hi @mhucka @psamanoelton, this PR is ready for review. All Linux and macOS (macos-14 arm64) CI checks are passing.

@psamanoelton psamanoelton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hello Vincenzo this looks very good! Thanks a lot!

To be sure help me solving few comments and help me checking also the /benchmarks, hope you can ensure if lines like this doesnt break on Apple Silicon and the scripts in the folder works too.

Note:
I am not too familiar with macOS, on ci you used macos-14, but this line says macOS 12.0+, one of my check on versions macos-14 is scheduled for removal Nov 2... it is great that this works on macOS 12.0+ but not sure if future changes will, and the CI will only check 14-15, checking older versions might get harder and we should say the version we do checks on the CI.

- name: Check out a copy of the TFQ git repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

- name: Set up Python ${{inputs.py_version || env.py_version}}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we use inputs.py_version/env.py_version consistently with the declared py_version keys in both nightly jobs? As written, the macOS job uses the runner’s default Python, so it does not reliably test the requested or scheduled version.

Comment thread .github/workflows/ci.yaml
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-14]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could these jobs use macos-15? macos-14 is scheduled for removal on November 2, so the new checks will soon stop running.

Comment thread docs/install.md
## Build from source

The following steps are tested for Ubuntu-like systems.
The following steps are tested for Ubuntu-like Linux systems and macOS (Apple Silicon / `arm64`).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not sure if the next steps tells to run apt, install a Linux .deb, and use apt-mark works on macOS, if not Give macOS its own executable setup path or label those command blocks Linux-only or varian for macOS

Comment thread docs/install.md

### 4. Build TensorFlow from source

TensorFlow Quantum is compatible with TensorFlow version 2.19.1. To build

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see   in this line, can you help me to fix this?

"-w" "/tfq/wheelhouse"
)
fi
if [[ "$(uname -s)" == "Darwin" ]]; then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could a macOS release check run clean_distribution.sh, install the repaired wheel in a fresh environment, and run the import example? Please also verify the resulting wheel tag and macOS 12 minimum target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants