Introduce Model Parallel infrastructure and DTensor support for Keras Variables in torch core.py - #23402
Introduce Model Parallel infrastructure and DTensor support for Keras Variables in torch core.py#23402buildwithsuhana wants to merge 7 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for PyTorch DTensors and model parallelism in the PyTorch backend, including automatic tensor promotion, distributed variable initialization, and direct assignment. The review feedback focuses on improving robustness against device mismatch errors (e.g., CPU vs. CUDA) during tensor promotion, variable initialization, and direct assignment. It also suggests skipping device transfers for DTensors in convert_to_tensor to prevent runtime errors, and using a context manager for KerasDTensorPromotionMode in tests to avoid leaking the dispatch mode.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #23402 +/- ##
===========================================
- Coverage 85.03% 22.03% -63.00%
===========================================
Files 468 468
Lines 71147 71244 +97
Branches 11792 11819 +27
===========================================
- Hits 60499 15698 -44801
- Misses 7634 54565 +46931
+ Partials 3014 981 -2033
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
888e29e to
c8dad47
Compare
c8dad47 to
e1877e2
Compare
e1877e2 to
be70681
Compare
be70681 to
e8cf8d4
Compare
e8cf8d4 to
70c948e
Compare
1367f59 to
c30fef9
Compare
c30fef9 to
cb09d0a
Compare
| global _GLOBAL_DTENSOR_PROMOTION_MODE | ||
| if _GLOBAL_DTENSOR_PROMOTION_MODE is None: | ||
| _GLOBAL_DTENSOR_PROMOTION_MODE = KerasDTensorPromotionMode() | ||
| _GLOBAL_DTENSOR_PROMOTION_MODE.__enter__() |
There was a problem hiding this comment.
Ok. But that's a really weird place to enter this context manager.
Should it be open when we set the global distribution?
Should it be open always when we initialize keras with Torch as a backend?
| from keras.src.backend.torch import distribution_lib | ||
|
|
There was a problem hiding this comment.
Move this import to the top
There was a problem hiding this comment.
Actually, this was kept as a local import to prevent circular import dependency. The distribution library imports small utilities from this module (e.g. get_device,_parse_device_input). Importing keras.src.backend.torch.distribution_lib at module top would create a cycle during import
| from keras.src.distribution.distribution_lib import TensorLayout | ||
|
|
There was a problem hiding this comment.
Move this import to the top.
There was a problem hiding this comment.
This would also cause a circular dependency hence we import TensorLayout locally to avoid circular imports between the high-level distribution API and backend-specific modules.
4d8d844 to
b6577dd
Compare
01344b9 to
21b5186
Compare
| if hasattr(x, "to_local"): | ||
| x = x.to_local() |
There was a problem hiding this comment.
Isn't that covered by lines 394-395?
Under what other circumstances would this be needed? Also, the if isinstance(x, Variable) should definitely come first.
There was a problem hiding this comment.
I've updated the code to remove that redundant check
| torch_core._GLOBAL_DTENSOR_PROMOTION_MODE.__enter__() | ||
|
|
There was a problem hiding this comment.
Add comment about the fact that this context is intentionally never exited.
| else: | ||
| device = get_device() |
There was a problem hiding this comment.
Just for my understanding, why was the else added here?
There was a problem hiding this comment.
Removed the else block for consistency :)
… Variables in core.py
21b5186 to
50cbdf7
Compare
hertschuh
left a comment
There was a problem hiding this comment.
So it's crashing on Torch now. Can you look into it?
This PR implements distributed variable initialization and tensor promotion for the PyTorch backend using torch.distributed.tensor (DTensor). It enables ModelParallel distribution strategies to correctly shard variables and manage mixed-tensor operations.
Key Changes:
Description
Contributor Agreement
Please review our AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review:
Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.