Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/184534
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ✅ No FailuresAs of commit e01c529 with merge base cd1956a ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
3bbdedb to
013923a
Compare
013923a to
6b099a4
Compare
…lTensorMode (pytorch#173836)" Remove coordinate cache in LocalDeviceMesh The cache uses the `id` of the Mesh as the key which is not unique over the lifetime of the `LocalTensorMode` instance: When the mesh is destroyed its slot may be reused leading to silently wrong, indeterministic results. E.g.: mesh_dim0_local_rank = mesh_2d["dim0"].get_local_rank(mesh_dim=0) mesh_dim1_local_rank = mesh_2d["dim1"].get_local_rank(mesh_dim=0) The values for both might be the same as the 2nd could use the cached coords from the first submesh, which is a temporary that gets immediately destroyed. Fixes pytorch#184526 This reverts commit 8e65dfa.
6b099a4 to
e01c529
Compare
|
Claude finished @aditvenk's task in 4m 25s —— View job Reviewing PR #184534
LGTM - This is a correct and well-motivated fix. The bug is clear: mesh_dim0_local_rank = mesh_2d["dim0"].get_local_rank(mesh_dim=0)
mesh_dim1_local_rank = mesh_2d["dim1"].get_local_rank(mesh_dim=0)The temporary submesh from The removal is clean:
No concerns with this change. The original caching was a performance optimization, but correctness must come first. If caching is needed in the future, it would need a key that's tied to the mesh's identity/content rather than its memory address (e.g., using |
|
@claude suggest a way to fix the correctness issue but retain the performance benefits of the caching approach. |
|
Claude finished @aditvenk's task in 1m 23s —— View job Suggesting a Correct Caching Approach for LocalDeviceMesh
The root problem with the removed cache was using Here are three approaches to retain caching correctly, ordered by my preference: Option 1: Use the mesh itself as the dict key (simplest, recommended)
# In LocalTensorMode.__init__:
self._coordinate_cache: dict[DeviceMesh, list[SymInt]] = {}
# In _LocalDeviceMesh.get_coordinate:
if self in lm._coordinate_cache:
return lm._coordinate_cache[self]
# ... compute coords ...
lm._coordinate_cache[self] = out
return outPros:
Cons:
Option 2: Use
|
|
I was thinking about using Option 3 already: The coordinates is a property of the mesh, so why not store it there instead of in the LocalTensor context? |
|
What's wrong with option 1? |
|
Besides of that: Why cache a class instance methods result in another class instead of in the same class? Isn't that non-locality an issue? That looks much cleaner to me than having a cache in a class that need to be retrieved first, which may even fail. |
|
I think it does make sense to move some of this logic to a cached property on DeviceMesh, with caveats:
I feel like this API probably still could use some discussion as far as efficiency. Do we really want to create a fully-enumerated rank mapping dict here? If we can avoid that and do cute algebra to get the local value directly it seems worth it. cc @fegin |
|
Storing of the coordinates was done as an optimization. So I'd say it is fine to have this as an implementation detail of would be fine. |
Why do we want a cached property on DeviceMesh? I thought we want to move away from that idea -- killing the flatten DeivceMesh being recorded in the global data. In the real workload, we want to ask users to explicitly cache the DeviceMesh rather than relying on DeviceMesh itself do the work. |
|
After discussing offline with @fegin, I want to propose we keep the cache in LocalTensor but we change the cache key from id() to some function of DeviceMesh's rank_tensor and layout.
The proposal to move the cache into DeviceMesh was rejected on the basis that DeviceMesh shouldn't itself know about or cache rank mappings of the non-local rank. LocalTensor itself is the place where non-local ranks (all world ranks) need to be brought together in one process @Flamefire would you be open to doing it this way? In any case, I am approving this PR to land now, to unblock others. We can take the perf regression on LocalTensor in the mean time. The caching was added to speed up DTensor sharding validator, and I don't think the regression would be an absolute blocker. cc @pianpwk @anshul-si |
Sorry, I don't fully understand the reasoning. Here we have a property-like method that depends mostly on properties of the class it belongs to: Is it possible that In that case I assume replacing the original Semi-related: for performance, |
|
My previous comment was targeting something different. Sorry for the confusion. But one main idea is the same, the fix shouldn't go to DeviceMesh but should keep in LocalMesh. DeviceMesh already cache coordinates. It is LocalMesh, which performs something differently, so any optimization should be done in LocalMesh. |
|
Ah, so not in Can you take a look at #187021 if that is what you imagined? I documented an alternative there too. |
|
@Flamefire May I ask which one is the final proposal, there seem to be several open PRs now. |
I guess the last one is the way to go |
Alternative to pytorch#184534 after discussion and pytorch#187021. The cache used the `id` of the Mesh as the key which is not unique over the lifetime of the `LocalTensorMode` instance: When the mesh is destroyed its slot may be reused leading to silently wrong, indeterministic results. E.g.: mesh_dim0_local_rank = mesh_2d["dim0"].get_local_rank(mesh_dim=0) mesh_dim1_local_rank = mesh_2d["dim1"].get_local_rank(mesh_dim=0) The values for both might be the same as the 2nd could use the cached coords from the first submesh, which is a temporary that gets immediately destroyed. Use the attributes used for the calculation as the cache key instead. Those are all tuples or data classes which works fine. Fixes pytorch#184526 Fixes 8e65dfa. Closes pytorch#184534 Closes pytorch#187021 Pull Request resolved: pytorch#187052 Approved by: https://github.com/fegin
Alternative to pytorch#184534 after discussion and pytorch#187021. The cache used the `id` of the Mesh as the key which is not unique over the lifetime of the `LocalTensorMode` instance: When the mesh is destroyed its slot may be reused leading to silently wrong, indeterministic results. E.g.: mesh_dim0_local_rank = mesh_2d["dim0"].get_local_rank(mesh_dim=0) mesh_dim1_local_rank = mesh_2d["dim1"].get_local_rank(mesh_dim=0) The values for both might be the same as the 2nd could use the cached coords from the first submesh, which is a temporary that gets immediately destroyed. Use the attributes used for the calculation as the cache key instead. Those are all tuples or data classes which works fine. Fixes pytorch#184526 Fixes 8e65dfa. Closes pytorch#184534 Closes pytorch#187021 Pull Request resolved: pytorch#187052 Approved by: https://github.com/fegin
The cache uses the
idof the Mesh as the key which is not unique over the lifetime of theLocalTensorModeinstance:When the mesh is destroyed its slot may be reused leading to silently wrong, indeterministic results.
E.g.:
The values for both might be the same as the 2nd could use the cached coords from the first submesh, which is a temporary that gets immediately destroyed.
Mostly a revert of #173836
Fixes #184526