-
Notifications
You must be signed in to change notification settings - Fork 458
Include CPU ISA hash in Warp kernel cache key #2452
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
Open
adenzler-nvidia
wants to merge
2
commits into
newton-physics:main
Choose a base branch
from
adenzler-nvidia:adenzler/fix-warp-cache-cpu-isa
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| #!/usr/bin/env python3 | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 The Newton Developers | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Print a short hash of the host CPU's ISA feature set. | ||
|
|
||
| Warp 1.13+ compiles CPU kernels with ``-march=native``, so cached object | ||
| files are only safe to reuse on a CPU with the same instruction set. | ||
| This script detects the ISA features and prints a 16-character hex hash | ||
| suitable for use in a CI cache key. | ||
|
|
||
| Supported platforms: | ||
| - x86_64 Linux / macOS / Windows (via system C compiler) | ||
| - AArch64 Linux (via /proc/cpuinfo) | ||
| - AArch64 macOS (via sysctl) | ||
| """ | ||
|
|
||
| import hashlib | ||
| import platform | ||
| import subprocess | ||
| import sys | ||
|
|
||
| # ISA-related macro keywords emitted by ``cc -march=native -dM -E``. | ||
| # These correspond to the instruction set extensions that affect codegen. | ||
| _X86_ISA_KEYWORDS = ( | ||
| "ADX", | ||
| "AES", | ||
| "AVX", | ||
| "BMI", | ||
| "CLFLUSH", | ||
| "F16C", | ||
| "FMA", | ||
| "LZCNT", | ||
| "MMX", | ||
| "MOVBE", | ||
| "PCLMUL", | ||
| "POPCNT", | ||
| "RDRAND", | ||
| "RDSEED", | ||
| "SHA", | ||
| "SSE", | ||
| "VAES", | ||
| "VPCLMUL", | ||
| ) | ||
|
|
||
|
|
||
| def _x86_features() -> str: | ||
| """Query ISA features by asking the system C compiler what -march=native enables.""" | ||
| for cc in ("cc", "gcc", "clang"): | ||
| try: | ||
| out = subprocess.check_output( | ||
| [cc, "-march=native", "-dM", "-E", "-x", "c", "-"], | ||
| input="", | ||
| text=True, | ||
| stderr=subprocess.DEVNULL, | ||
| ) | ||
| except (FileNotFoundError, subprocess.CalledProcessError): | ||
| continue | ||
|
|
||
| macros = sorted( | ||
| line.split()[1] | ||
| for line in out.splitlines() | ||
| if line.startswith("#define __") and any(k in line for k in _X86_ISA_KEYWORDS) | ||
| ) | ||
| if macros: | ||
| return " ".join(macros) | ||
|
|
||
| return "" | ||
|
|
||
|
|
||
| def _aarch64_features() -> str: | ||
| """Query ISA features from /proc/cpuinfo (Linux) or sysctl (macOS).""" | ||
| # Linux: kernel exposes HWCAP flags in /proc/cpuinfo. | ||
| try: | ||
| with open("/proc/cpuinfo") as f: | ||
| for line in f: | ||
| if line.startswith("Features"): | ||
| return " ".join(sorted(line.split(":")[1].split())) | ||
| except FileNotFoundError: | ||
| pass | ||
|
|
||
| # macOS: sysctl exposes CPU features. | ||
| try: | ||
| out = subprocess.check_output( | ||
| ["sysctl", "-n", "machdep.cpu.features"], | ||
| text=True, | ||
| stderr=subprocess.DEVNULL, | ||
| ) | ||
| if out.strip(): | ||
| return " ".join(sorted(out.strip().split())) | ||
| except (FileNotFoundError, subprocess.CalledProcessError): | ||
| pass | ||
|
|
||
| return "" | ||
|
|
||
|
|
||
| def get_features() -> str: | ||
| machine = platform.machine().lower() | ||
| if machine in ("x86_64", "amd64", "x86", "i686"): | ||
| return _x86_features() | ||
| if machine in ("aarch64", "arm64"): | ||
| return _aarch64_features() | ||
| return "" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| features = get_features() or platform.processor() | ||
| h = hashlib.sha256(features.encode()).hexdigest()[:16] | ||
| print(f"features: {features}", file=sys.stderr) | ||
| print(h) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡
machdep.cpu.featuresonly exists on Intel Macs, so on Apple Silicon runners thesysctl -n machdep.cpu.featurescall below raisesCalledProcessError,_aarch64_features()returns"", andmain()falls back toplatform.processor()(which is"arm"on Apple Silicon). Onmacos-latestthe cache key ends up not being derived from ISA features at all, so that runner effectively keeps the pre-change behavior and one of the four CI targets does not benefit from this change.The fallback itself is safe (no crash, stable hash), but the module docstring on line 15 advertises "AArch64 macOS (via sysctl)" support that is not actually working on Apple Silicon.
On Apple Silicon,
sysctl hw.optionalenumerates per-feature flags such ashw.optional.neonandhw.optional.armv8_2_sha3. Parsing and sorting those keys gives a real ISA fingerprint.Example macOS ARM branch
Worth updating the docstring once the macOS ARM path actually contributes ISA features to the hash.