# Path: t5x/train_state.py
# EMPTY_DICT = flax.core.freeze({})
# class TrainState(typing_extensions.Protocol):
# class FlaxOptimTrainState(flax.struct.PyTreeNode):
# class InferenceState(flax.struct.PyTreeNode):
#   def step(self) -> jnp.ndarray:
#   def params(self) -> FrozenVariableDict:
#   def param_states(self) -> FrozenVariableDict:
#   def flax_mutables(self) -> FrozenVariableDict:
#   def state_dict(self) -> MutableVariableDict:
#   def restore_state(self, state_dict: Mapping[str, Any]) -> 'TrainState':
#   def replace_params(self, params: VariableDict) -> 'TrainState':
#   def replace_step(self, step: jnp.ndarray) -> 'TrainState':
#   def apply_gradient(self,
#                      grads,
#                      learning_rate,
#                      flax_mutables=EMPTY_DICT) -> 'TrainState':
#   def as_logical_axes(self) -> 'TrainState':
# def _validate_params_axes(params_axes, params):
#   def create(cls, optimizer_def: optim.OptimizerDef,
#              model_variables: FrozenVariableDict) -> 'FlaxOptimTrainState':
#   def step(self) -> jnp.ndarray:
#   def params(self) -> FrozenVariableDict:
#   def param_states(self) -> FrozenVariableDict:
#   def state_dict(self) -> MutableVariableDict:
#   def apply_gradient(self,
#                      grads,
#                      learning_rate,
#                      flax_mutables=EMPTY_DICT) -> 'FlaxOptimTrainState':
#   def replace_params(self, params: VariableDict) -> 'FlaxOptimTrainState':
#   def replace_step(self, step: jnp.ndarray) -> 'FlaxOptimTrainState':
#   def restore_state(self, state_dict: VariableDict) -> 'FlaxOptimTrainState':
#   def as_logical_axes(self) -> 'FlaxOptimTrainState':
#   def create(cls, model_variables: FrozenVariableDict) -> 'InferenceState':
#   def param_states(self) -> FrozenVariableDict:
#   def apply_gradient(self, *args, **kwargs) -> 'InferenceState':
#   def state_dict(self) -> MutableMapping[str, Any]:
#   def replace_step(self, step: jnp.ndarray) -> 'InferenceState':
#   def replace_params(self, params: FrozenVariableDict) -> 'InferenceState':
#   def restore_state(self, state_dict: Mapping[str, Any]) -> 'InferenceState':
#   def as_logical_axes(self) -> 'InferenceState':


import abc
import collections
import dataclasses
import typing
import cached_property
import jax
import numpy as np
from typing import Any, Callable, Optional, Sequence, Tuple, Union
from absl import logging
from flax import traverse_util
from flax.linen import partitioning as flax_partitioning
from jax import numpy as jnp
from jax import random
from jax.experimental.maps import Mesh
from jax.experimental.pjit import pjit as jax_pjit
from jax.interpreters.sharded_jit import PartitionSpec  # pylint:disable=unused-import
from t5x import train_state as train_state_lib
# Copyright 2022 The T5X 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.

"""Utilities for partitioning."""



JaxDevice = jax.lib.xla_client.Device
TpuMesh = Tuple[int, int, int, int]  # (x, y, z, num_cores).
OtherMesh = Tuple[int, int]
HardwareMesh = Union[TpuMesh, OtherMesh]
PyTreeDef = type(jax.tree_structure(None))

# Path: t5x/train_state.py
# EMPTY_DICT = flax.core.freeze({})
# class TrainState(typing_extensions.Protocol):
# class FlaxOptimTrainState(flax.struct.PyTreeNode):
# class InferenceState(flax.struct.PyTreeNode):
#   def step(self) -> jnp.ndarray:
#   def params(self) -> FrozenVariableDict:
#   def param_states(self) -> FrozenVariableDict:
#   def flax_mutables(self) -> FrozenVariableDict:
#   def state_dict(self) -> MutableVariableDict:
#   def restore_state(self, state_dict: Mapping[str, Any]) -> 'TrainState':
#   def replace_params(self, params: VariableDict) -> 'TrainState':
#   def replace_step(self, step: jnp.ndarray) -> 'TrainState':
#   def apply_gradient(self,
#                      grads,
#                      learning_rate,
#                      flax_mutables=EMPTY_DICT) -> 'TrainState':
#   def as_logical_axes(self) -> 'TrainState':
# def _validate_params_axes(params_axes, params):
#   def create(cls, optimizer_def: optim.OptimizerDef,
#              model_variables: FrozenVariableDict) -> 'FlaxOptimTrainState':
#   def step(self) -> jnp.ndarray:
#   def params(self) -> FrozenVariableDict:
#   def param_states(self) -> FrozenVariableDict:
#   def state_dict(self) -> MutableVariableDict:
#   def apply_gradient(self,
#                      grads,
#                      learning_rate,
#                      flax_mutables=EMPTY_DICT) -> 'FlaxOptimTrainState':
#   def replace_params(self, params: VariableDict) -> 'FlaxOptimTrainState':
#   def replace_step(self, step: jnp.ndarray) -> 'FlaxOptimTrainState':
#   def restore_state(self, state_dict: VariableDict) -> 'FlaxOptimTrainState':
#   def as_logical_axes(self) -> 'FlaxOptimTrainState':
#   def create(cls, model_variables: FrozenVariableDict) -> 'InferenceState':
#   def param_states(self) -> FrozenVariableDict:
#   def apply_gradient(self, *args, **kwargs) -> 'InferenceState':
#   def state_dict(self) -> MutableMapping[str, Any]:
#   def replace_step(self, step: jnp.ndarray) -> 'InferenceState':
#   def replace_params(self, params: FrozenVariableDict) -> 'InferenceState':
#   def restore_state(self, state_dict: Mapping[str, Any]) -> 'InferenceState':
#   def as_logical_axes(self) -> 'InferenceState':


# Path: t5x/partitioning.py
import abc
import collections
import dataclasses
import typing
import cached_property
import jax
import numpy as np
from typing import Any, Callable, Optional, Sequence, Tuple, Union
from absl import logging
from flax import traverse_util
from flax.linen import partitioning as flax_partitioning
from jax import numpy as jnp
from jax import random
from jax.experimental.maps import Mesh
from jax.experimental.pjit import pjit as jax_pjit
from jax.interpreters.sharded_jit import PartitionSpec  # pylint:disable=unused-import
from t5x import train_state as train_state_lib

# Copyright 2022 The T5X 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.

"""Utilities for partitioning."""



JaxDevice = jax.lib.xla_client.Device
TpuMesh = Tuple[int, int, int, int]  # (x, y, z, num_cores).
OtherMesh = Tuple[int, int]
HardwareMesh = Union[TpuMesh, OtherMesh]
PyTreeDef = type(jax.tree_structure(None))

TrainState = train_state_lib.TrainState