-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Adding Tensor_layout for Tensor parallelism for Autosharding #21792
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
Open
buildwithsuhana
wants to merge
5
commits into
keras-team:master
Choose a base branch
from
buildwithsuhana:tensor_parallel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+331
−0
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06bb3bb
Adding tensor layout for TP autosharding
buildwithsuhana 41f8025
formatting files
buildwithsuhana e74eab2
Updating the docstring
buildwithsuhana 2cddf39
refactoring the code
buildwithsuhana fee036e
Merge branch 'tensor_parallel' of https://github.com/buildwithsuhana/…
buildwithsuhana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import collections | ||
|
|
||
| from keras.src import ops | ||
|
|
||
|
|
||
| def split_tensor_for_parallelism(tensor, index, device_count, dim): | ||
| """Calculates a slice of a tensor along a specified dimension for a | ||
| given index. | ||
| This utility is used in tensor parallelism API to distribute a | ||
| tensor across multiple devices. | ||
| Args: | ||
| tensor: The full tensor to be sharded. | ||
| index: The index of the device/shard to return (e.g., 0, 1, 2...). | ||
| device_count: The total number of parallel devices or splits. | ||
| dim: The dimension along which to split the tensor. If -1, the | ||
| last dimension is used. | ||
| Returns: | ||
| A tensor slice corresponding to the given `index`. | ||
| """ | ||
buildwithsuhana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if dim == -1: | ||
| static_shape = getattr(tensor, "shape", None) | ||
| if static_shape is not None: | ||
| rank = len(static_shape) | ||
| else: | ||
| rank = None | ||
|
|
||
| if rank is not None: | ||
| split_dim = rank - 1 | ||
| else: | ||
| split_dim = ops.ndim(tensor) - 1 | ||
| else: | ||
| split_dim = dim | ||
buildwithsuhana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| splits = ops.array_split( | ||
| tensor, indices_or_sections=device_count, axis=split_dim | ||
| ) | ||
| return splits[index] | ||
|
|
||
|
|
||
| LayoutMap = collections.namedtuple("LayoutMap", ["state_rules", "output_rules"]) | ||
163 changes: 163 additions & 0 deletions
163
keras/src/distribution/tensor_parallel/tensor_layout_test.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| from keras.src import ops | ||
| from keras.src import testing | ||
| from keras.src.distribution.tensor_parallel.tensor_layout import LayoutMap | ||
| from keras.src.distribution.tensor_parallel.tensor_layout import ( | ||
| split_tensor_for_parallelism, | ||
| ) | ||
|
|
||
|
|
||
| class LayoutTest(testing.TestCase): | ||
| """Test suite for tensor layout actions and mappings.""" | ||
|
|
||
| def test_split_with_even_division(self): | ||
| """Tests splitting a tensor that divides evenly among workers.""" | ||
| device_count = 4 | ||
| dim = 0 | ||
| tensor = ops.reshape(ops.arange(16, dtype="float32"), (8, 2)) | ||
|
|
||
| expected_shard_0 = ops.array([[0.0, 1.0], [2.0, 3.0]]) | ||
| expected_shard_2 = ops.array([[8.0, 9.0], [10.0, 11.0]]) | ||
|
|
||
| shard_0 = split_tensor_for_parallelism( | ||
| tensor, index=0, device_count=device_count, dim=dim | ||
| ) | ||
| shard_2 = split_tensor_for_parallelism( | ||
| tensor, index=2, device_count=device_count, dim=dim | ||
| ) | ||
|
|
||
| self.assertAllClose(shard_0, expected_shard_0) | ||
| self.assertAllClose(shard_2, expected_shard_2) | ||
| self.assertEqual(shard_0.shape, (2, 2)) | ||
|
|
||
| def test_split_with_uneven_division(self): | ||
| """Tests splitting tensor where remainder is distributed correctly.""" | ||
| device_count = 3 | ||
| dim = 0 | ||
| tensor = ops.reshape(ops.arange(10, dtype="float32"), (10, 1)) | ||
|
|
||
| shard_0 = split_tensor_for_parallelism( | ||
| tensor, index=0, device_count=device_count, dim=dim | ||
| ) | ||
| self.assertEqual(shard_0.shape, (4, 1)) | ||
| self.assertAllClose(shard_0, ops.array([[0.0], [1.0], [2.0], [3.0]])) | ||
|
|
||
| shard_1 = split_tensor_for_parallelism( | ||
| tensor, index=1, device_count=device_count, dim=dim | ||
| ) | ||
| self.assertEqual(shard_1.shape, (3, 1)) | ||
| self.assertAllClose(shard_1, ops.array([[4.0], [5.0], [6.0]])) | ||
|
|
||
| shard_2 = split_tensor_for_parallelism( | ||
| tensor, index=2, device_count=device_count, dim=dim | ||
| ) | ||
| self.assertEqual(shard_2.shape, (3, 1)) | ||
| self.assertAllClose(shard_2, ops.array([[7.0], [8.0], [9.0]])) | ||
|
|
||
| def test_split_and_undo_cycle_even_removed(self): | ||
| """ | ||
| Confirms that the original tensor can be reconstructed. | ||
| """ | ||
| device_count = 2 | ||
| dim = 0 | ||
| original_tensor = ops.reshape(ops.arange(12, dtype="float32"), (6, 2)) | ||
|
|
||
| shards = [ | ||
| split_tensor_for_parallelism( | ||
| original_tensor, index=i, device_count=device_count, dim=dim | ||
| ) | ||
| for i in range(device_count) | ||
| ] | ||
|
|
||
| reconstructed_tensor = ops.concatenate(shards, axis=dim) | ||
|
|
||
| self.assertAllClose(original_tensor, reconstructed_tensor) | ||
|
|
||
| def test_split_and_undo_cycle_uneven_removed(self): | ||
| """ | ||
| Confirms that original tensor can be reconstructed with uneven split. | ||
| """ | ||
| device_count = 4 | ||
| dim = 0 | ||
| original_tensor = ops.reshape(ops.arange(22, dtype="float32"), (11, 2)) | ||
|
|
||
| shards = [ | ||
| split_tensor_for_parallelism( | ||
| original_tensor, index=i, device_count=device_count, dim=dim | ||
| ) | ||
| for i in range(device_count) | ||
| ] | ||
|
|
||
| self.assertEqual(shards[0].shape, (3, 2)) | ||
| self.assertEqual(shards[1].shape, (3, 2)) | ||
| self.assertEqual(shards[2].shape, (3, 2)) | ||
| self.assertEqual(shards[3].shape, (2, 2)) | ||
|
|
||
| reconstructed_tensor = ops.concatenate(shards, axis=dim) | ||
| self.assertAllClose(original_tensor, reconstructed_tensor) | ||
|
|
||
| def test_split_last_dimension(self): | ||
| """Tests splitting on the last dimension using dim=-1.""" | ||
| device_count = 3 | ||
| dim = -1 | ||
| original_tensor = ops.reshape( | ||
| ops.arange(30, dtype="float32"), (2, 5, 3) | ||
| ) | ||
|
|
||
| shards = [ | ||
| split_tensor_for_parallelism( | ||
| original_tensor, index=i, device_count=device_count, dim=dim | ||
| ) | ||
| for i in range(device_count) | ||
| ] | ||
|
|
||
| self.assertEqual(shards[0].shape, (2, 5, 1)) | ||
| self.assertEqual(shards[1].shape, (2, 5, 1)) | ||
| self.assertEqual(shards[2].shape, (2, 5, 1)) | ||
|
|
||
| def test_split_with_sharding_type_hint(self): | ||
| """Tests using 'row' and 'column' sharding hints for 2D tensors.""" | ||
| device_count = 2 | ||
| tensor = ops.reshape(ops.arange(16, dtype="float32"), (4, 4)) | ||
|
|
||
| row_dim = 0 | ||
| shard_row_0 = split_tensor_for_parallelism( | ||
| tensor, index=0, device_count=device_count, dim=row_dim | ||
| ) | ||
| self.assertAllClose(shard_row_0, tensor[:2, :]) | ||
|
|
||
| col_dim = 1 | ||
| shard_col_0 = split_tensor_for_parallelism( | ||
| tensor, index=0, device_count=device_count, dim=col_dim | ||
| ) | ||
| self.assertAllClose(shard_col_0, tensor[:, :2]) | ||
|
|
||
| def test_layout_map_namedtuple_behavior(self): | ||
| """Tests basic behavior of the LayoutMap namedtuple.""" | ||
|
|
||
| def rule_kernel(tensor, index): | ||
| return split_tensor_for_parallelism( | ||
| tensor, index=index, device_count=2, dim=0 | ||
| ) | ||
|
|
||
| def rule_output(tensor, index): | ||
| return split_tensor_for_parallelism( | ||
| tensor, index=index, device_count=2, dim=-1 | ||
| ) | ||
|
|
||
| state_rules = {"kernel": rule_kernel} | ||
| output_rules = {"output": rule_output} | ||
|
|
||
| layout_map = LayoutMap( | ||
| state_rules=state_rules, output_rules=output_rules | ||
| ) | ||
|
|
||
| self.assertIs(layout_map.state_rules, state_rules) | ||
| self.assertIs(layout_map.output_rules, output_rules) | ||
|
|
||
| self.assertIs(layout_map[0], state_rules) | ||
| self.assertIs(layout_map[1], output_rules) | ||
|
|
||
| with self.assertRaises(AttributeError): | ||
| layout_map.state_rules = {} | ||
|
|
||
| self.assertTrue(callable(layout_map.state_rules["kernel"])) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.