






"""simple docstring"""



# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# 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 typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


A__       :				int 					=					{
    'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'],
}
try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__       :				int 					=					['VivitImageProcessor']


try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__       :				Tuple 					=					[
						    'VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'VivitModel',
						    'VivitPreTrainedModel',
						    'VivitForVideoClassification',
						]


if TYPE_CHECKING:
						from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .image_processing_vivit import VivitImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_vivit import (
												    VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    VivitForVideoClassification,
												    VivitModel,
												    VivitPreTrainedModel,
												)


else:
						import sys

						A__       :				Tuple 					=					_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)







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
lowerCAmelCase__      		= object()

# For specifying empty leaf dict `{}`
lowerCAmelCase__      		= object()
def 							_UpperCAmelCase				(UpperCamelCase__     :  str				,							UpperCamelCase__     :  Union[str, Any]						):
       _A      :       str				    = tuple((re.compile(x + "$"						) for x in qs)						)
       for i in range(len(UpperCamelCase__						) - len(UpperCamelCase__						) + 1						):
              _A      :       Tuple				    = [x.match(UpperCamelCase__						) for x, y in zip(UpperCamelCase__				,							ks[i:]						)]
              if matches and all(UpperCamelCase__						):
                     return True
       return False
def 							_UpperCAmelCase				(UpperCamelCase__     :  str						):
       def replace(UpperCamelCase__     :  List[str]				,							UpperCamelCase__     :  Optional[int]						):
              for rule, replacement in rules:
                     if _match(UpperCamelCase__				,							UpperCamelCase__						):
                            return replacement
              return val

       return replace
def 							_UpperCAmelCase				():
       return [
           # embeddings
           (("transformer", "wpe", "embedding"), P("mp"				,							UpperCamelCase__						)),
           (("transformer", "wte", "embedding"), P("mp"				,							UpperCamelCase__						)),
           # atention
           (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("attention", "out_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("attention", "out_proj", "bias"), None),
           # mlp
           (("mlp", "c_fc", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("mlp", "c_fc", "bias"), P("mp"						)),
           (("mlp", "c_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("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 							_UpperCAmelCase				(UpperCamelCase__     :  List[str]						):
       _A      :       int				    = _get_partition_rules()
       _A      :       Optional[int]				    = _replacement_rules(UpperCamelCase__						)
       _A      :       Optional[int]				    = {k: _unmatched for k in flatten_dict(UpperCamelCase__						)}
       _A      :       List[str]				    = {k: replace(UpperCamelCase__				,							UpperCamelCase__						) for k, v in initd.items()}
       assert _unmatched not in result.values(), "Incomplete partition spec."
       return freeze(unflatten_dict(UpperCamelCase__						)						)
