-
Notifications
You must be signed in to change notification settings - Fork 559
Cache OpSharding by mesh and partition_spec #5593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,10 +199,12 @@ void XlaNode::UpdateShardingHash() { | |
| sharding_hash_ = torch::lazy::HashCombine( | ||
| sharding_hash_, (uint32_t)tile_assignment_dimension); | ||
| } | ||
| for (const auto& tile_assignment_device : | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this for loop is expansive as well. |
||
| sharding->tile_assignment_devices()) { | ||
| sharding_hash_ = torch::lazy::HashCombine( | ||
| sharding_hash_, (uint32_t)tile_assignment_device); | ||
| { | ||
| const int64_t* data = sharding->tile_assignment_devices().data(); | ||
| const size_t size_in_bytes = | ||
| sharding->tile_assignment_devices().size() * sizeof(*data); | ||
| sharding_hash_ = | ||
| torch::lazy::HashBlock(data, size_in_bytes, sharding_hash_); | ||
| } | ||
| for (const auto& last_tile_dim : sharding->last_tile_dims()) { | ||
| sharding_hash_ = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import torch_xla.runtime as xr | ||
|
|
||
| import numpy as np | ||
| import functools | ||
| import itertools | ||
| from typing import Tuple, Union, List, Sequence, Any, Optional, Set | ||
| from enum import IntEnum | ||
|
|
@@ -79,6 +80,26 @@ def get_axis_name_idx(self, name: str) -> int: | |
| return None | ||
| return self.axis_names.index(name) | ||
|
|
||
| @functools.lru_cache(maxsize=None) | ||
| def get_op_sharding(self, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't think about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I set |
||
| partition_spec: Tuple) -> torch_xla._XLAC.OpSharding: | ||
| """ | ||
| Return the OpSharding for the given partition spec. This is an expensive | ||
| operation as the mesh grows, so the value is cached for reuse. | ||
| """ | ||
| tile_assignment = _get_tile_assignment(self, partition_spec) | ||
| if len(tile_assignment.shape) > len(partition_spec): | ||
| # Use partial replication for sharding a tensor over a higher-rank mesh | ||
| sharding_type = ShardingType.PARTIAL | ||
| else: | ||
| sharding_type = _get_sharding_type(partition_spec, self.size()) | ||
| replicate_dims = {i for i, d in enumerate(partition_spec) if d is None} | ||
| group_assignment, replication_groups = _get_group_assignment( | ||
| sharding_type, tile_assignment, len(partition_spec), replicate_dims) | ||
| return torch_xla._XLAC.OpSharding(tile_assignment.tolist(), | ||
| group_assignment, replication_groups, | ||
| int(sharding_type)) | ||
|
|
||
|
|
||
| # HybridDevice class has been inspired from jax's mesh_utils: https://github.com/google/jax/blob/fc5960f2b8b7a0ef74dbae4e27c5c08ff1564cff/jax/experimental/mesh_utils.py#L4 | ||
|
|
||
|
|
@@ -475,25 +496,12 @@ def mark_sharding( | |
| assert len(specs) == len(np.unique(specs)), \ | ||
| f"Each device mesh dimension should appear at most once in partition_spec {partition_spec}." | ||
|
|
||
| tile_assignment = _get_tile_assignment(mesh, partition_spec) | ||
| if len(tile_assignment.shape) > len(partition_spec): | ||
| # Use partial replication for sharding a tensor over a higher-rank mesh | ||
| sharding_type = ShardingType.PARTIAL | ||
| else: | ||
| sharding_type = _get_sharding_type(partition_spec, num_devices) | ||
| replicate_dims = {i for i, d in enumerate(partition_spec) if d is None} | ||
| group_assignment, replication_groups = _get_group_assignment( | ||
| sharding_type, tile_assignment, len(partition_spec), replicate_dims) | ||
| op_sharding = mesh.get_op_sharding(partition_spec) | ||
|
|
||
| if isinstance(t, XLAShardedTensor): | ||
| torch_xla._XLAC._xla_mark_sharding(t.global_tensor, | ||
| tile_assignment.tolist(), | ||
| group_assignment, replication_groups, | ||
| int(sharding_type)) | ||
| torch_xla._XLAC._xla_mark_sharding(t.global_tensor, op_sharding) | ||
| return t | ||
| torch_xla._XLAC._xla_mark_sharding(t, tile_assignment.tolist(), | ||
| group_assignment, replication_groups, | ||
| int(sharding_type)) | ||
| torch_xla._XLAC._xla_mark_sharding(t, op_sharding) | ||
| return XLAShardedTensor(t) | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's smart to use a counter here.