






import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
__UpperCAmelCase								=						object()

# For specifying empty leaf dict `{}`
__UpperCAmelCase								=						object()




def      lowercase__      (     __snake_case    :							List[Any]     ,       __snake_case    :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_    :      Tuple            =						tuple((re.compile(x + '$'  ) for x in qs)  )
					for i in range(len(__snake_case  ) - len(__snake_case  ) + 1  ):
										UpperCAmelCase_    :      Any            =						[x.match(__snake_case  ) for x, y in zip(__snake_case     ,       ks[i:]  )]
										if matches and all(__snake_case  ):
															return True
					return False




def      lowercase__      (     __snake_case    :							Optional[Any]  ):


					'''simple docstring'''


					def replace(__snake_case    :							List[Any]     ,       __snake_case    :							Optional[Any]  ):
										for rule, replacement in rules:
															if _match(__snake_case     ,       __snake_case  ):
																				return replacement
										return val

					return replace




def      lowercase__      (     ):


					'''simple docstring'''


					return [
					    # embeddings
					    (("transformer", "wpe", "embedding"), P('mp'     ,       __snake_case  )),
					    (("transformer", "wte", "embedding"), P('mp'     ,       __snake_case  )),
					    # atention
					    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__snake_case     ,       'mp'  )),
					    (("attention", "out_proj", "kernel"), P('mp'     ,       __snake_case  )),
					    (("attention", "out_proj", "bias"), None),
					    # mlp
					    (("mlp", "c_fc", "kernel"), P(__snake_case     ,       'mp'  )),
					    (("mlp", "c_fc", "bias"), P('mp'  )),
					    (("mlp", "c_proj", "kernel"), P('mp'     ,       __snake_case  )),
					    (("mlp", "c_proj", "bias"), None),
					    # layer norms
					    ((r"ln_\d+", "bias"), None),
					    ((r"\d+", r"ln_\d+", "scale"), None),
					    (("ln_f", "bias"), None),
					    (("ln_f", "scale"), None),
					]






def      lowercase__      (     __snake_case    :							List[Any]  ):


					'''simple docstring'''


					UpperCAmelCase_    :      Dict            =						_get_partition_rules()
					UpperCAmelCase_    :      Optional[Any]            =						_replacement_rules(__snake_case  )
					UpperCAmelCase_    :      Dict            =						{k: _unmatched for k in flatten_dict(__snake_case  )}
					UpperCAmelCase_    :      Optional[Any]            =						{k: replace(__snake_case     ,       __snake_case  ) for k, v in initd.items()}
					assert _unmatched not in result.values(), "Incomplete partition spec."
					return freeze(unflatten_dict(__snake_case  )  )














from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__UpperCAmelCase								=						{
    'configuration_time_series_transformer': [
        'TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'TimeSeriesTransformerConfig',
    ],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase								=						[
		    'TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TimeSeriesTransformerForPrediction',
		    'TimeSeriesTransformerModel',
		    'TimeSeriesTransformerPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_time_series_transformer import (
		    TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    TimeSeriesTransformerConfig,
		)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_time_series_transformer import (
				    TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TimeSeriesTransformerForPrediction,
				    TimeSeriesTransformerModel,
				    TimeSeriesTransformerPreTrainedModel,
				)

else:
		import sys

		__UpperCAmelCase								=						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






