Commit bb2683b
authored
Metal backend: stop tracking blob memory once a model is destroyed (pytorch#22954)
### Summary
Fixes pytorch#22953
Tensors created from a model's constants blob are tracked in
`memory_to_n_tensor` as `NOT_OWN`, and `aoti_torch_delete_tensor_object`
leaves those entries in place when it deletes such a tensor.
`cleanup_memory()` cleared `tensors` but not that map, so the addresses
stayed tracked after the model was gone. The next model whose constants
landed on a reused address then failed to load with "Memory address ...
is already being tracked by another tensor". Loading six small Metal
models twice in one process failed 7 of 12 loads.
`cleanup_memory()` now clears `memory_to_n_tensor` once every tensor has
been deleted. The entry is not erased in the `NOT_OWN` branch of
`aoti_torch_delete_tensor_object` because several tensors can alias one
`NOT_OWN` address without a count, and erasing on the first delete would
make the later ones fail with "memory not found during deletion".
### Test plan
New `backends/apple/metal/runtime/test/test_memory.cpp`, wired through
`et_cxx_test` under `BUILD_TESTING` the same way `backends/cuda` does
it:
```
cmake -S . -B cmake-out-test -DEXECUTORCH_BUILD_METAL=ON -DAOTI_METAL=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON -DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP=ON -DEXECUTORCH_BUILD_TESTS=ON
cmake --build cmake-out-test --target test_metal_memory
cmake-out-test/backends/apple/metal/test_metal_memory
```
- Before the change: `BlobAddressCanBeReusedAfterCleanup` and
`CleanupLeavesNoTrackedMemory` fail;
`BlobAddressIsTrackedWhileTensorIsAlive` (the control) passes.
- After: `[ PASSED ] 3 tests.`
- End to end, the load/destroy/load loop from the issue went from `12
loads, 7 failures` to `12 loads, 0 failures` in three consecutive runs.
- `python -m unittest
backends.apple.metal.tests.test_modules.TestMetalBackendModules`: `Ran
134 tests ... OK`.
- `lintrunner` clean on the touched files.1 parent 32fb206 commit bb2683b
3 files changed
Lines changed: 109 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
706 | 706 | | |
707 | 707 | | |
708 | 708 | | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
709 | 716 | | |
710 | 717 | | |
711 | 718 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
0 commit comments