# Path: flaxformer/activation_partitioning.py
# def global_mesh_defined():
# def with_sharding(x, partitioning_dims: int):
# def with_sharding_migration(
#     x: T,
#     activation_partitioning_dims: Optional[int],
#     logical_axis_names: Tuple[str, ...],
# ) -> T:
# T = TypeVar('T')


from unittest import mock
from absl.testing import absltest
from absl.testing import parameterized
from flax.linen import partitioning as flax_partitioning
from jax import numpy as jnp
from flaxformer import activation_partitioning
    x = jnp.array([1, 2, 3])
    with mock.patch.object(
        flax_partitioning, "get_axis_rules", return_value=["rule"]):
      with mock.patch.object(
          flax_partitioning,
          "with_sharding_constraint",
          autospec=True,
      ) as mock_wsc:
        activation_partitioning.with_sharding_migration(
            x=x, activation_partitioning_dims=1, logical_axis_names=("foo",))
      mock_wsc.assert_called_once_with(x, ("foo",))

  def test_with_sharding_migration_dims_2_errors(self):
    x = jnp.array([1, 2, 3])
    with mock.patch.object(
        flax_partitioning, "get_axis_rules", return_value=["rule"]):
      with self.assertRaisesRegex(ValueError, "rules.*dims.*present"):
        activation_partitioning.with_sharding_migration(
            x=x, activation_partitioning_dims=2, logical_axis_names=("foo",))

  @parameterized.parameters(1, 2)
  def test_with_sharding_migration_no_logical_axis_rules(self, dims):
    x = jnp.array([1, 2, 3])
    with mock.patch.object(
        flax_partitioning, "get_axis_rules", return_value=()):
      with mock.patch.object(
          activation_partitioning,
          "with_sharding",
          autospec=True,
      ) as mock_ws:

# Path: flaxformer/activation_partitioning.py
# def global_mesh_defined():
# def with_sharding(x, partitioning_dims: int):
# def with_sharding_migration(
#     x: T,
#     activation_partitioning_dims: Optional[int],
#     logical_axis_names: Tuple[str, ...],
# ) -> T:
# T = TypeVar('T')


# Path: flaxformer/activation_partitioning_test.py
from unittest import mock
from absl.testing import absltest
from absl.testing import parameterized
from flax.linen import partitioning as flax_partitioning
from jax import numpy as jnp
from flaxformer import activation_partitioning

    x = jnp.array([1, 2, 3])
    with mock.patch.object(
        flax_partitioning, "get_axis_rules", return_value=["rule"]):
      with mock.patch.object(
          flax_partitioning,
          "with_sharding_constraint",
          autospec=True,
      ) as mock_wsc:
        activation_partitioning.with_sharding_migration(
            x=x, activation_partitioning_dims=1, logical_axis_names=("foo",))
      mock_wsc.assert_called_once_with(x, ("foo",))

  def test_with_sharding_migration_dims_2_errors(self):
    x = jnp.array([1, 2, 3])
    with mock.patch.object(
        flax_partitioning, "get_axis_rules", return_value=["rule"]):
      with self.assertRaisesRegex(ValueError, "rules.*dims.*present"):
        activation_partitioning.with_sharding_migration(
            x=x, activation_partitioning_dims=2, logical_axis_names=("foo",))

  @parameterized.parameters(1, 2)
  def test_with_sharding_migration_no_logical_axis_rules(self, dims):
    x = jnp.array([1, 2, 3])
    with mock.patch.object(
        flax_partitioning, "get_axis_rules", return_value=()):
      with mock.patch.object(
          activation_partitioning,
          "with_sharding",
          autospec=True,
      ) as mock_ws:

        activation_partitioning.with_sharding_migration(