Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem and root cause
metal.SetStreamallocates a newMetalRawStreamwrapper on every call and overwrites the previous pointer without freeing the wrapper. Its growth path also resizes the stream vector todevice_idbefore writing elementdevice_id, leaving that element outside the vector's size.Change
src/runtime/metal/metal_common.h: allow an existing borrowed-stream wrapper to update its command-buffer pointer.src/runtime/metal/metal_module.mm: own one wrapper per thread/device with a thread-local vector of unique_ptrs, reuse it on subsequent calls, and grow device slots todevice_id + 1.The wrapper borrows the command buffer; the change owns the wrapper rather than taking ownership of the external command buffer.
Tests and validation
A local native Objective-C++ regression harness linked against this branch's rebuilt TVM objects passes checks for:
On unmodified upstream, the harness fails the first slot-size assertion. The harness is local validation, not an automated regression test included in this branch. It uses sentinel command-buffer pointers without dispatching GPU work.
The existing Metal execution suite also has a baseline limitation:
test_metal_inf_nansegfaults on unmodified upstream. The native checks above validate wrapper bookkeeping, not end-to-end borrowed-stream GPU execution.Validation environment: Apple M4 Max, macOS/Darwin 24.5.0 arm64, Python 3.12.11. Each branch was rebuilt separately from upstream
907a88c87with:cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \ -DUSE_METAL=ON -DUSE_LLVM=OFF -DUSE_CUDA=OFF -DUSE_Z3=OFF cmake --build build --target tvm tvm_compiler tvm_runtime -j 8git diff --checkpasses. The applicable formatting and other pre-commit hooks pass. The repository-wide ASF-header hook reports 11 existing violations, reproduced on unmodified upstream; none is in the changed files.Related
No duplicate open PR was found. This runtime fix is independent of Metal target-property discovery in #71.