Conversation
| "relu_metal/relu.mm", | ||
| "relu_metal/relu.metal", | ||
| "relu_metal/common.h", | ||
| "relu_metal_cpp/relu.cpp", | ||
| "relu_metal_cpp/metallib_loader.mm", | ||
| "relu_metal_cpp/relu_cpp.metal", | ||
| "relu_metal_cpp/common.h", |
There was a problem hiding this comment.
I think it would be best to keep relu as is and add an extra relu-metal-cpp example.
There was a problem hiding this comment.
great point, updated to its own example in the latest PR
|
|
||
| # Build inputs | ||
| apple-sdk_15, | ||
| metal-cpp, |
There was a problem hiding this comment.
Let's keep them lexicographically sorted.
There was a problem hiding this comment.
oh yes will make sure this is sorted in latest
| if [[ "$*" =~ "metallib" ]]; then | ||
|
|
||
| # If metallib is requested, find the air-lld from the Metal toolchain | ||
| METALLIB_BIN=$(ls /var/run/com.apple.security.cryptexd/mnt/com.apple.MobileAsset.MetalToolchain*/Metal.xctoolchain/usr/bin/air-lld 2>/dev/null | head -n 1) |
There was a problem hiding this comment.
metallib is a binary too :)
Also, this path is not stable, e.g. it doesn't exist on my macOS 26 installation.
There was a problem hiding this comment.
removed/simplified all of this in the latest changes
| # Remove the '-sdk macosx metallib' and other unsupported flags from the command arguments | ||
| ARGS=$(echo "$@" | sed 's/-sdk macosx metallib //' | sed 's/-mmacosx-version-min=[^ ]* //') | ||
| # Add platform version for macOS 15+ to support Metal 3.2 / AIR 2.7 | ||
| $METALLIB_BIN -platform_version macos 15.0 15.0 $ARGS | ||
|
|
||
| elif [[ "$*" =~ "metal" ]]; then | ||
|
|
||
| # If metal is requested, find the metal compiler from the Metal toolchain | ||
| METAL_BIN=$(ls /var/run/com.apple.security.cryptexd/mnt/com.apple.MobileAsset.MetalToolchain*/Metal.xctoolchain/usr/bin/metal 2>/dev/null | head -n 1) | ||
| if [ -z "$METAL_BIN" ]; then | ||
| echo "Error: Metal compiler not found" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Remove the '-sdk macosx metal' from the command arguments | ||
| ARGS=$(echo "$@" | sed 's/-sdk macosx metal //') | ||
| $METAL_BIN $ARGS | ||
| else | ||
| # In all other cases, just use the host xcrun | ||
| unset DEVELOPER_DIR | ||
| /usr/bin/xcrun $@ | ||
| fi |
There was a problem hiding this comment.
I think most of this is not needed with #290 merged, we do not call xcrun anymore for metal and metallib.
There was a problem hiding this comment.
+1 no longer needed with latest updates. thanks!
|
closing in favor of a updated and more simple PR here #295 |
This PR supersedes #291 --- This PR adds support for a new `metal-cpp` kernel dependency. This is a follow up to the metal-cpp support in hf nix: huggingface/hf-nix#128 and enables kernels to use the cpp headers to drive metal kernels. Changes: - adds dep to build2cmake - adds new relu-metal-cpp example - builds example in CI example usage ```bash cd examples/relu-metal-cpp nix build -L . cd ... uv run test_relu_metal_cpp.py ``` `test_relu_metal_cpp.py` ```python # /// script # requires-python = ">=3.10" # dependencies = ["kernels", "torch", "numpy"] # /// from kernels import get_local_kernel import torch from pathlib import Path relu = get_local_kernel(Path("examples/relu-metal-cpp/result"), "relu").relu input = torch.tensor([-1.0, -1.5, 0.0, 2.0, 3.5], device="mps", dtype=torch.float16) out = relu(input) ref = torch.relu(input) assert torch.allclose(out, ref), f"Float16 failed: {out} != {ref}" print(out.cpu().numpy()) print(ref.cpu().numpy()) print("PASS") ``` output ``` [0. 0. 0. 2. 3.5] [0. 0. 0. 2. 3.5] PASS ```
This PR supersedes #291 --- This PR adds support for a new `metal-cpp` kernel dependency. This is a follow up to the metal-cpp support in hf nix: huggingface/hf-nix#128 and enables kernels to use the cpp headers to drive metal kernels. Changes: - adds dep to build2cmake - adds new relu-metal-cpp example - builds example in CI example usage ```bash cd examples/relu-metal-cpp nix build -L . cd ... uv run test_relu_metal_cpp.py ``` `test_relu_metal_cpp.py` ```python # /// script # requires-python = ">=3.10" # dependencies = ["kernels", "torch", "numpy"] # /// from kernels import get_local_kernel import torch from pathlib import Path relu = get_local_kernel(Path("examples/relu-metal-cpp/result"), "relu").relu input = torch.tensor([-1.0, -1.5, 0.0, 2.0, 3.5], device="mps", dtype=torch.float16) out = relu(input) ref = torch.relu(input) assert torch.allclose(out, ref), f"Float16 failed: {out} != {ref}" print(out.cpu().numpy()) print(ref.cpu().numpy()) print("PASS") ``` output ``` [0. 0. 0. 2. 3.5] [0. 0. 0. 2. 3.5] PASS ```
This PR adds support for a new
metal-cppkernel dependency. This is a follow up to the metal-cpp support in hf nix: huggingface/hf-nix#128 and enables kernels to use the cpp headers to drive metal kernels.Changes:
examples/relu/build.toml. since they both produce metal output we can only build one at a time..xcrunHostcommand to respect the cryptex location of the latest metal toolchain on the host. This may need to be improved in line with Metal 4 Update to macOS SDK 26 and Metal 4 #290example usage