-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
204 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,76 @@ | ||
# Copyright 2024 The kauldron Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from unittest import mock | ||
|
||
import jax | ||
from kauldron.utils import _jax | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'axes, process_count, in_shape, expected', | ||
[ | ||
((), 1, (3, 4), (3, 4)), | ||
((), 4, (3, 4), (3, 4)), | ||
(('batch',), 1, (3, 4), (3, 4)), | ||
(('batch',), 4, (3, 4), (12, 4)), | ||
# One dim can match to 2 mesh axes (`spec[0] == ('batch', 'replica')`) | ||
# by using nested tuple: `(('batch', 'replica'),)` | ||
((('batch', 'replica'),), 1, (3, 4), (3, 4)), | ||
((('batch', 'replica'),), 4, (3, 4), (12, 4)), | ||
], | ||
) | ||
def test_local_to_global_shape( | ||
axes: tuple[str, ...], | ||
process_count: int, | ||
in_shape: tuple[int, ...], | ||
expected: tuple[int, ...], | ||
): | ||
devices = np.array(jax.devices()) | ||
devices = devices.reshape((1, 1, 1, 1)) | ||
|
||
mesh = jax.sharding.Mesh( | ||
devices, | ||
axis_names=('replica', 'batch', 'seq', 'model'), | ||
) | ||
|
||
sharding = jax.sharding.NamedSharding( | ||
mesh, | ||
spec=jax.sharding.PartitionSpec(*axes), | ||
) | ||
|
||
with mock.patch.object(jax, 'process_count', return_value=process_count): | ||
out_shape = _jax.local_to_global_shape(in_shape, sharding=sharding) | ||
assert out_shape == expected | ||
|
||
|
||
def test_local_to_global_shape_fail(): | ||
|
||
devices = np.array(jax.devices()) | ||
devices = devices.reshape((1, 1, 1, 1)) | ||
|
||
mesh = jax.sharding.Mesh( | ||
devices, | ||
axis_names=('replica', 'batch', 'seq', 'model'), | ||
) | ||
|
||
sharding = jax.sharding.NamedSharding( | ||
mesh, | ||
spec=jax.sharding.PartitionSpec('batch', 'seq'), | ||
) | ||
|
||
with pytest.raises(ValueError, match='Data can only be sharded on the first'): | ||
_jax.local_to_global_shape((3, 4), sharding=sharding) |
Oops, something went wrong.