|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from collections.abc import Mapping |
| 16 | +import json |
| 17 | +from typing import Any |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from absl.testing import absltest |
| 21 | +from absl.testing import parameterized |
| 22 | +import jax |
| 23 | +import jax.numpy as jnp |
| 24 | +from pathwaysutils import jax as pw_jax |
| 25 | +from pathwaysutils import plugin_executable |
| 26 | +from pathwaysutils.experimental import reshard |
| 27 | + |
| 28 | + |
| 29 | +class ReshardTest(parameterized.TestCase): |
| 30 | + |
| 31 | + @parameterized.parameters( |
| 32 | + dict(reshard_kwargs={"donate": True}, expected_donate=True), |
| 33 | + dict(reshard_kwargs={"donate": False}, expected_donate=False), |
| 34 | + dict(reshard_kwargs={}, expected_donate=False), |
| 35 | + ) |
| 36 | + def test_ifrt_reshard_donate( |
| 37 | + self, reshard_kwargs: Mapping[str, Any], expected_donate: bool |
| 38 | + ): |
| 39 | + x = jnp.array([1, 2]) |
| 40 | + devices = jax.devices() |
| 41 | + sharding = jax.sharding.SingleDeviceSharding(devices[0]) |
| 42 | + |
| 43 | + mock_transfer = self.enter_context( |
| 44 | + mock.patch.object(pw_jax, "transfer_to_shardings", autospec=True) |
| 45 | + ) |
| 46 | + self.enter_context( |
| 47 | + mock.patch.object( |
| 48 | + pw_jax, "ifrt_reshard_available", return_value=True, autospec=True |
| 49 | + ) |
| 50 | + ) |
| 51 | + |
| 52 | + reshard.reshard(x, sharding, **reshard_kwargs) |
| 53 | + |
| 54 | + # Signature: transfer_to_shardings(arrays, shardings, donate) |
| 55 | + mock_transfer.assert_called_with(mock.ANY, mock.ANY, expected_donate) |
| 56 | + |
| 57 | + @parameterized.parameters( |
| 58 | + dict(reshard_kwargs={"donate": True}, expected_donate=True), |
| 59 | + dict(reshard_kwargs={"donate": False}, expected_donate=False), |
| 60 | + dict(reshard_kwargs={}, expected_donate=False), |
| 61 | + ) |
| 62 | + def test_sidechannel_reshard_donate( |
| 63 | + self, reshard_kwargs: Mapping[str, Any], expected_donate: bool |
| 64 | + ): |
| 65 | + x = jnp.array([1, 2]) |
| 66 | + devices = jax.devices() |
| 67 | + sharding = jax.sharding.SingleDeviceSharding(devices[0]) |
| 68 | + |
| 69 | + self.enter_context( |
| 70 | + mock.patch.object( |
| 71 | + pw_jax, "ifrt_reshard_available", return_value=False, autospec=True |
| 72 | + ) |
| 73 | + ) |
| 74 | + mock_pe = self.enter_context( |
| 75 | + mock.patch.object(plugin_executable, "PluginExecutable", autospec=True) |
| 76 | + ) |
| 77 | + mock_pe.return_value.call.return_value = ([mock.Mock()], mock.Mock()) |
| 78 | + |
| 79 | + reshard.reshard(x, sharding, **reshard_kwargs) |
| 80 | + |
| 81 | + mock_pe.assert_called() |
| 82 | + (json_request,), _ = mock_pe.call_args |
| 83 | + request = json.loads(json_request) |
| 84 | + self.assertEqual(request["reshardRequest"]["donateInput"], expected_donate) |
| 85 | + |
| 86 | + @parameterized.parameters(True, False, None) |
| 87 | + def test_ifrt_reshard_cache_resharding_plans(self, cache: bool | None): |
| 88 | + x = jnp.array([1, 2]) |
| 89 | + devices = jax.devices() |
| 90 | + sharding = jax.sharding.SingleDeviceSharding(devices[0]) |
| 91 | + |
| 92 | + mock_transfer = self.enter_context( |
| 93 | + mock.patch.object(pw_jax, "transfer_to_shardings") |
| 94 | + ) |
| 95 | + self.enter_context( |
| 96 | + mock.patch.object(pw_jax, "ifrt_reshard_available", return_value=True) |
| 97 | + ) |
| 98 | + |
| 99 | + if cache is None: |
| 100 | + reshard.reshard(x, sharding) |
| 101 | + elif cache: |
| 102 | + with self.assertWarnsRegex( |
| 103 | + UserWarning, "cache_resharding_plans` is only applicable" |
| 104 | + ): |
| 105 | + reshard.reshard(x, sharding, cache_resharding_plans=cache) |
| 106 | + else: |
| 107 | + reshard.reshard(x, sharding, cache_resharding_plans=cache) |
| 108 | + |
| 109 | + mock_transfer.assert_called_once() |
| 110 | + |
| 111 | + @parameterized.parameters( |
| 112 | + dict(cache=True, expected_cache=True), |
| 113 | + dict(cache=False, expected_cache=False), |
| 114 | + dict(cache=None, expected_cache=False), |
| 115 | + ) |
| 116 | + def test_sidechannel_reshard_cache_resharding_plans( |
| 117 | + self, cache, expected_cache |
| 118 | + ): |
| 119 | + x = jnp.array([1, 2]) |
| 120 | + devices = jax.devices() |
| 121 | + sharding = jax.sharding.SingleDeviceSharding(devices[0]) |
| 122 | + |
| 123 | + self.enter_context( |
| 124 | + mock.patch.object(pw_jax, "ifrt_reshard_available", return_value=False) |
| 125 | + ) |
| 126 | + mock_pe = self.enter_context( |
| 127 | + mock.patch.object(plugin_executable, "PluginExecutable") |
| 128 | + ) |
| 129 | + mock_pe.return_value.call.return_value = ([mock.Mock()], mock.Mock()) |
| 130 | + |
| 131 | + mock_get_resharding_plan_cached = self.enter_context( |
| 132 | + mock.patch.object(reshard, "_get_resharding_plan_cached") |
| 133 | + ) |
| 134 | + |
| 135 | + if cache is None: |
| 136 | + reshard.reshard(x, sharding) |
| 137 | + else: |
| 138 | + reshard.reshard(x, sharding, cache_resharding_plans=cache) |
| 139 | + |
| 140 | + self.assertEqual(mock_pe.call_count, 0 if expected_cache else 1) |
| 141 | + |
| 142 | + self.assertEqual( |
| 143 | + mock_get_resharding_plan_cached.call_count, |
| 144 | + 1 if expected_cache else 0, |
| 145 | + ) |
| 146 | + |
| 147 | +if __name__ == "__main__": absltest.main() |
0 commit comments