Skip to content

vk_pipeline_check: the large-module memory budget reads macOS's free memory - #5167

Closed
sovljanskii wants to merge 1 commit into
ProjectLeonida:mainfrom
sovljanskii:pipeline-check-macos-memory
Closed

sovljanskii wants to merge 1 commit into
ProjectLeonida:mainfrom
sovljanskii:pipeline-check-macos-memory

Conversation

@sovljanskii

Copy link
Copy Markdown
Contributor

Item: new, claimed here. Follow-up of #4278. Depends on: none.

Why

#4278 lets large modules (over 4 MiB) share a memory budget instead of compiling one at a time. The budget comes from available_memory_bytes() in runtime/test/large_module_gate.h, which reads MemAvailable on Linux and ullAvailPhys on Windows and returns 0 anywhere else. On a Mac it is always 0, so the checker always falls back to the old exclusive gate: each large module compiles alone while the other threads wait.

What

  • large_module_gate.h. An __APPLE__ branch returns hw.memsize × kern.memorystatus_level / 100. That is the kernel's own free percentage, the one memory_pressure prints as "System-wide memory free percentage"; it counts purgeable and compressible pages too. If either sysctl fails, or the level is outside 1..100, it returns 0 and the old gate stays.
  • large_module_gate_test.cpp. On macOS the value must be non-zero and at most hw.memsize. Against the previous header the test fails there (available_memory_bytes() > 0).
  • docs/pipeline_check.md. Names the macOS source and adds the M4 measurement.

Why the system-wide level. Metal compiles in its MTLCompilerService processes, not in the checker. One 4 MiB module took 11.7 s of wall time, but only 0.3 s of the checker's own CPU and 0.24 GB of its RSS. Meanwhile MTLCompilerService held about 2.3 GB for a large module. So the 512-bytes-per-SPIR-V-byte reservation is still about right on a Mac, but the memory lives outside the process.

Measured

Apple M4 (24 GB), macOS, MoltenVK 1.4.2, with modules translated from my own 1.0.2.1 copy (counts only). Metal keeps its own shader cache, so I used two disjoint sets with matched size distributions: 24 translated compute modules over 4 MiB each, 124.7 and 126.2 MiB in total. Each set ran once with no cache entries for its modules:

Large modules Threads Wall time Checker peak RSS Lowest system free level
one at a time (what a Mac did before; set A) 5 1,291 s 0.96 GB 80%
memory budget, 16 GB of 21.4 GB (set B) 5 701 s 1.12 GB 83%
  • 24/24 pipelines ok both ways, a 1.84× speedup, and the system never came under memory pressure.
  • Run again with the Metal cache holding the modules, both modes took about 10 s.

Tested

  • Whole runtime build on macOS (AppleClang): clean, no warnings in the changed files.
  • large_module_gate_test: ok. ctest -R "large_module|pipeline_check": 2/2.
  • vk_pipeline_check now prints large modules (over 4194304 bytes): up to 5 at once within 15.8 GB (3/4 of the 21.1 GB available) on the M4, instead of one at a time.
  • python -I tests/test_docs_*.py: all 10 OK. git diff --check is clean.
  • Linux and Windows code paths are unchanged.

GPU: Apple M4, MoltenVK 1.4.2. No game data in the change or here. AI-assisted.

@Kam471 Kam471 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.

Built and ran large_module_gate_test at f16628387 on Windows 11: large_module_gate: ok, exit 0; git diff --check clean.

The large-module memory budget now has a macOS branch that reads the host's free memory (the same gate that used cgroup limits on Linux and GlobalMemoryStatusEx on Windows), so a Mac host is no longer treated as unlimited; the docs row and the added checks cover the new source and the existing ones. I cannot run the macOS path here. No blocking issue.

@sovljanskii

Copy link
Copy Markdown
Contributor Author

@Kam471 thanks for building it on Windows; the unchanged paths matter too. One detail for whoever merges: before this, a Mac wasn't treated as unlimited. It was the opposite. available_memory_bytes() returned 0, which falls back to the old exclusive gate, so every large module compiled alone. This PR gives a Mac the budget Linux and Windows already have (1,291 s → 701 s on the M4 set). AI-assisted.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested f166283 merged onto main 3faac55 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 178/178.
  • large_module_gate: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

@DomasUrbonas

Copy link
Copy Markdown
Contributor

Independent check of f166283 on Apple M4 Pro, macOS 27.2.1, MoltenVK 1.4.2. large_module_gate passed. With two workers, vk_pipeline_check reported 15.5 GB available, set an 11.6 GB budget, and compiled both synthetic compute pipelines. When I forced the memory query to fail in the test process, it switched to one-at-a-time compilation and both still passed.

I lowered the size threshold to exercise both paths with small synthetic modules. Peak memory and throughput on multi-megabyte modules were outside this check. No game files used. AI-assisted.

@jgrizon

jgrizon commented Nov 23, 2026

Copy link
Copy Markdown

Built and ran f166283 on Apple M5 Pro (20-core GPU), MoltenVK 1.4.2, macOS 27.3 (arm64), LLVM 23.1.6.

large_module_gate_test: pass. Full run: 50 tests, 1 failed, 2 skipped.

The failure is vk_overlapping_occlusion, which this PR does not touch (docs/pipeline_check.md, runtime/test/large_module_gate.h, runtime/test/large_module_gate_test.cpp only). It passes on current main (37f2151), so it comes from this branch's base rather than from this change.

Heads-up: the branch is a long way behind main and GitHub reports it UNKNOWN mergeable, so it needs a rebase before its own result means much.

…memory

available_memory_bytes() returned 0 on macOS, so a Mac always took the old
exclusive gate: each module over 4 MiB compiled alone while the other
threads waited. It now returns hw.memsize times kern.memorystatus_level,
the kernel's free percentage (what memory_pressure prints as "System-wide
memory free percentage"). Metal compiles in MTLCompilerService rather than
in the checker, so the reservation is for system memory and the system-wide
level is the one to read.

Apple M4 (24 GB), MoltenVK 1.4.2, two disjoint sets of 24 translated compute
modules over 4 MiB, cold Metal cache: 1,291 s one at a time, 701 s within the
budget (16 GB, 5 threads); 24/24 pipelines ok both ways, system free level
never below 80%.

large_module_gate_test checks the value on macOS too (non-zero, at most
hw.memsize); against the previous header it fails there.
@sovljanskii

Copy link
Copy Markdown
Contributor Author

Thanks @jgrizon. The branch is now rebased onto main@867d922f0 as ea72feacf; the rebase was clean, and the change is the same three files.

I reran it on Apple M4, MoltenVK 1.4.2, macOS 27.2.1, with a native RelWithDebInfo build. ctest passed 64 of 64 (2 skipped: shader_library_corpus and vk_sync_validation), including large_module_gate and vk_overlapping_occlusion. So the vk_overlapping_occlusion failure you saw came from the old base, as you thought.

agent: yes

@alvaroaguileraweb

Copy link
Copy Markdown

Apple M2 16 GB validation of ea72fea: pass.

  • Device: Apple M2 (Mac14,2), macOS 26.6.2 (25G83), 16 GB unified memory, MoltenVK 1.4.2.
  • large_module_gate_test: ok.
  • ctest -R 'large_module|pipeline_check': 2/2 passed.
  • vk_pipeline_check now prints on this M2:
    with --jobs 5: "large modules (over 4194304 bytes): up to 5 at once within 9.8 GB (3/4 of the 13.1 GB available)"
    with --jobs 2: "up to 2 at once within 9.7 GB (3/4 of the 12.9 GB available)"
    instead of the old one-at-a-time gate.
  • A 2-shader smoke run on MoltenVK completed (cs: 2 modules ok, 0 failed); both pipelines skipped for no shaderDenormPreserveFloat16, unrelated to this change.
  • No game files; synthetic fixtures only.

AI-assisted.

@Cryserrrrr

Copy link
Copy Markdown

Tested ea72feacf merged onto main 867d922f0 (clean) on Windows 11 (fr-FR, cp1252), MSVC 14.51: pass. This is a Windows non-regression check: only the __APPLE__ branches change.

  • large_module_gate_test and vk_pipeline_check build with no warnings.
  • ctest large_module_gate: 1/1 pass before and after.

AI-assisted (Claude Code).

@Michele0303

Copy link
Copy Markdown

checked: #5167 at ea72feac | base: main@5d74c27 | on: macOS 27.0.1 arm64 (Apple M5), MoltenVK 1.4.2, Python 3.13 | result: pass; large_module_gate_test ok, ctest large_module_gate + vk_pipeline_check_cli 2/2; the new test built against main's header fails at line 109 (available_memory_bytes() > 0)
vk_pipeline_check on a one-module index: main prints one at a time, this head up to 5 at once within 4.6 GB (3/4 of the 6.2 GB available) (16 GB M5 under parallel build load).
AI-assisted (Claude Code). No game data.

@optinium51-bot

Copy link
Copy Markdown

Tested ea72feacf merged onto main 5d74c2723 (clean merge) on NVIDIA GeForce RTX 3060 (driver 616.92), Windows 11 26200, MSVC 14.52 (VS 2026), RelWithDebInfo + Ninja: pass.

No game files. AI-assisted (Claude Code).

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.

10 participants