macOS platform guards in the host files (C2): every host source under src/ now compiles for Metal

All under __APPLE__ / STRATA_GPU_METAL, so the Linux and Windows code is unchanged:
- direct_file.cpp: no O_DIRECT on macOS; F_NOCACHE after open keeps the reads out of the buffer cache
- memory.cpp: F_RDADVISE is the read-ahead hint where posix_fadvise(WILLNEED) is used
- expert_source.cpp: available memory from host_statistics64 (free + inactive + purgeable); the posix_fadvise of a
  released expert layer is skipped (the madvise before it already released the mapping)
- generate.cpp: <sys/sysmacros.h> is not on macOS (major/minor come from <sys/types.h>); the CUDA toolkit mismatch
  check is CUDA's
- vmm.cpp: the cuMem* virtual memory stays CUDA's (Metal takes the HIP branch: elastic K/V off)
- prefill.cpp: std::max<size_t> where size_t and uint64_t meet (the same type on Linux, distinct on macOS)
This commit is contained in:
Niko1221 committed 2026-10-12 12:29:27 +02:00
1 parent bf4b79abda
commit f1c5a6772c
6 files changed
+33 -4

No files matched your search

+15
View File
@@ -45,6 +45,9 @@
#include <windows.h>
#else
#include <fcntl.h>
#if defined(__APPLE__)
#include <mach/mach.h> // host_statistics64: the available memory
#endif
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -411,6 +414,16 @@ bool host_available_memory(HostMemory& m, const std::string& meminfo, const std:
}
m.available = bytes;
return true;
#elif defined(__APPLE__)
// macOS: free + inactive + purgeable pages (what the OS can hand out without swapping), as vm_stat reports them
(void) meminfo; (void) self_cgroup; (void) cgroup_root;
vm_statistics64_data_t vs{};
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
vm_size_t page_bytes = 0;
if (host_page_size(mach_host_self(), &page_bytes) != KERN_SUCCESS ||
host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) &vs, &count) != KERN_SUCCESS) return false;
m.available = ((uint64_t) vs.free_count + vs.inactive_count + vs.purgeable_count) * (uint64_t) page_bytes;
return m.available > 0;
#else
(void) meminfo; (void) self_cgroup; (void) cgroup_root;
const long pages = sysconf(_SC_AVPHYS_PAGES);
@@ -3107,10 +3120,12 @@ bool FileExpertSource::pin_cache_complement(
fail("FileExpertSource: madvise could not release mapped expert layer " + std::to_string(layer));
return;
}
#if !defined(__APPLE__) // macOS has no posix_fadvise; the madvise above already released the mapping
if (posix_fadvise(fd_, (off_t) layer_offset, (off_t) layer_bytes, POSIX_FADV_DONTNEED) != 0) {
fail("FileExpertSource: posix_fadvise could not release expert layer " + std::to_string(layer));
return;
}
#endif
}
#endif
const int64_t done = layers_done.fetch_add(1) + 1;
+1 -1
View File
@@ -1,6 +1,6 @@
#include "strata/core/vmm.hpp"
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) && !defined(STRATA_GPU_METAL)
#include <cuda.h>
#include <cuda_runtime.h>
+5
View File
@@ -306,7 +306,12 @@ DirectFile::~DirectFile() { close(); delete impl_; }
bool DirectFile::open(const std::string& path, std::string& err) {
close();
#if defined(__APPLE__) // macOS has no O_DIRECT: F_NOCACHE keeps the reads out of the unified buffer cache
impl_->fd = ::open(path.c_str(), O_RDONLY);
if (impl_->fd >= 0 && fcntl(impl_->fd, F_NOCACHE, 1) != 0) { ::close(impl_->fd); impl_->fd = -1; }
#else
impl_->fd = ::open(path.c_str(), O_RDONLY | O_DIRECT);
#endif
if (impl_->fd < 0) { err = "DirectFile: cannot open " + path; return false; }
struct stat st;
if (fstat(impl_->fd, &st) != 0) { err = "DirectFile: cannot size " + path; close(); return false; }
+7
View File
@@ -166,7 +166,14 @@ void advise_willneed(const void* p, uint64_t bytes) {
void advise_willneed(int fd, uint64_t offset, uint64_t bytes) {
if (fd < 0 || !read_ahead_enabled()) return;
for (uint64_t at = 0; at < bytes; at += kAdviseStep)
#if defined(__APPLE__) // macOS: F_RDADVISE is the read-ahead hint
{
radvisory ra{(off_t) (offset + at), (int) std::min(kAdviseStep, bytes - at)};
(void) fcntl(fd, F_RDADVISE, &ra);
}
#else
(void) posix_fadvise(fd, (off_t) (offset + at), (off_t) std::min(kAdviseStep, bytes - at), POSIX_FADV_WILLNEED);
#endif
}
#endif
+2 -2
View File
@@ -1033,8 +1033,8 @@ struct MoeBufs { size_t gu, h, xq, hq; };
MoeBufs moe_bufs(size_t T, int64_t n_expert, bool fused) {
if (!fused) return {T * K * 1280, T * K * 640, mmq::q8_bytes((int64_t) (T * K), N), mmq::q8_bytes((int64_t) (T * K), 640)};
const size_t ts = (size_t) std::min<int64_t>((int64_t) T, stream_all_min() - 1); // MMQ's last small chunk
return {std::max(ts * K * 1280, (fused::group_bytes((int64_t) (T * K), (int) n_expert) + 3) / 4),
std::max(ts * K * 640, (fused::act_bytes((int64_t) (T * K), 640) + 3) / 4),
return {std::max<size_t>(ts * K * 1280, (fused::group_bytes((int64_t) (T * K), (int) n_expert) + 3) / 4),
std::max<size_t>(ts * K * 640, (fused::act_bytes((int64_t) (T * K), 640) + 3) / 4),
std::max(mmq::q8_bytes((int64_t) (ts * K), N), fused::act_bytes((int64_t) T, N)),
mmq::q8_bytes((int64_t) (ts * K), 640)};
}
+3 -1
View File
@@ -92,8 +92,10 @@
#include <unistd.h>
#include <cerrno>
#include <sys/stat.h>
#if !defined(__APPLE__) // major() / minor(): <sys/types.h> on macOS
#include <sys/sysmacros.h>
#endif
#endif
#include <cuda_runtime.h>
@@ -4651,7 +4653,7 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: GPU %d: %s, compute capability %d.%d%s\n", dev, name,
strata::cc_major_of(p.major), strata::cc_minor_of(p.minor),
strata::emulated_cc() ? " (STRATA_EMULATE_CC: a test mode, the card is emulated)" : "");
#if !defined(STRATA_HIP_GFX906) // a CUDA toolkit mismatch; the gfx906 HIP build has no CUDART_VERSION
#if !defined(STRATA_HIP_GFX906) && !defined(STRATA_GPU_METAL) // a CUDA toolkit mismatch; the gfx906 HIP build has no CUDART_VERSION
{ // #542: a build whose libcudart is older than its headers (a CUDA 13 kit with a dangling libcudart.so that
// CMake resolved to the system's CUDA 12 one) reads cudaDeviceProp shifted - silently, and slowly
int rt = 0;