diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index ad28f56f..b11899f5 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -45,6 +45,9 @@ #include #else #include +#if defined(__APPLE__) +#include // host_statistics64: the available memory +#endif #include #include #include @@ -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; diff --git a/src/core/vmm.cpp b/src/core/vmm.cpp index e69d2b88..2ae5af6a 100644 --- a/src/core/vmm.cpp +++ b/src/core/vmm.cpp @@ -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 #include diff --git a/src/platform/direct_file.cpp b/src/platform/direct_file.cpp index 5f8d9962..b09536c2 100644 --- a/src/platform/direct_file.cpp +++ b/src/platform/direct_file.cpp @@ -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; } diff --git a/src/platform/memory.cpp b/src/platform/memory.cpp index c9f2a512..4180bdf0 100644 --- a/src/platform/memory.cpp +++ b/src/platform/memory.cpp @@ -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 diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index d13fdd19..6c867253 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -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) 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(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), 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)}; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index b1ffd943..649cdbdf 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -92,8 +92,10 @@ #include #include #include +#if !defined(__APPLE__) // major() / minor(): on macOS #include #endif +#endif #include @@ -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;