Repository navigation
mps: clone() copies instead of aliasing its source (#97) - #98
Merged
Merged
Conversation
…ffer (#97) On mps, x.clone() (and clone(contiguous), detach().clone()) returned a tensor sharing x's Metal buffer, so y = x.clone(); y.add_(1) changed x; fill_, mul_ and setitem too. cpu was correct. Cause: clone_default calls candle Tensor::copy(), whose MetalStorage::try_clone was upstream's Ok(self.clone()), an Arc<Buffer> share; candle never writes in place, so it was safe there, but torch._C does. try_clone now allocates a buffer of the same element count and fills it with one blit, so the layout stays valid. Patch regenerated; vendor_candle.sh --check passes. tests/devices/mps/test_mpsclone.py (22): five clone spellings x four in-place writes x f32/f16/bf16/i64 leave the source unchanged and give upstream's values, with an encoder counted during the clone; detach and aten.alias still share (negative controls). Red before: 20 of 22 failed ("changed the source"). Green after: 15 ok, 7 skipped by name: strided clones written in place need PR #89's strided writes on mps. Gate (worktree): suites=134/134 ok=2111 FAIL=2, worktree environment only.
Contributor
Gate on CI (issue #24)Full gate: 134 suites = 126 run on at least one CI runner + 8 local-only. The local gate (
Local-only: verified by no CI runner (8)
Suites that verified nothing, or failed, on a runner
Every suite, every runner
A SKIP is unverified on that runner, never passed. UNVERIFIED means the suite ran and every one of its tests skipped by name. |
2 tasks
b-re-w
added a commit
that referenced
this pull request
Oct 16, 2026
Conflict: torchnative/rust/vendor/int8-candle-0.11.0-cpu.patch, a generated file. Both sides edited metal_backend/mod.rs (#98's try_clone copy, this branch's I32 arms), which merged cleanly; the patch was then regenerated from the pristine candle-core 0.11.0 crate against the merged tree (git diff --no-index --no-prefix), and vendor_candle.sh --check reports every tree byte for byte. test_mpsint32: the clone encoder-count skip (#97) is removed; it passes. Rebuilt: test_mpsint32 6/6, test_mpsclone 22/22 (no skips now: #89's strided writes and #98's copy are both in), test_mpscb, test_intmps, test_dtypedev, test_shim ok.
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.
Closes #97
Defect fixed: on mps,
y = x.clone(); y.add_(1)changedx(alsofill_,mul_, setitem;clone(memory_format=contiguous),detach().clone()). candle'sMetalStorage::try_cloneshared theArc<Buffer>; the fork now copies into a fresh buffer with one blit. Silent wrong answers on any mps code that clones then writes (KV caches, in-place updates). cpu was never affected.Test added:
tests/devices/mps/test_mpsclone.py(22): every clone spelling x in-place write x f32/f16/bf16/i64; encoder counted during clone;detachandaten.aliasmust still share (so making everything copy cannot pass).Evidence (local, this Mac): red before 20/22 FAIL ("changed the source"); green after 15 ok, 7 skipped by name (strided clones written in place need #89's strided mps writes; they run once #89 lands).
vendor_candle.sh --checkpasses. Gate: 134/134 suites, ok=2111, FAIL=2 (worktree environment only).