from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  torch.Tensor:






      '''simple docstring'''





      if not inplace:
            UpperCAmelCase    : str      =ma + ma
      else:
            ma += ma

      return ma





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase						)->  torch.Tensor:






      '''simple docstring'''





      UpperCAmelCase    : str      =-1 * len(__lowerCAmelCase						)
      UpperCAmelCase    : str      =list(range(len(tensor.shape[:zero_index]						)						)						)
      return tensor.permute(first_inds + [zero_index + i for i in inds]						)





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase						)->  torch.Tensor:






      '''simple docstring'''





      return t.reshape(t.shape[:-no_dims] + (-1,)						)





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase = 1e-4						)->  torch.Tensor:






      '''simple docstring'''





      UpperCAmelCase    : str      =mask.expand(*value.shape						)
      return torch.sum(mask * value		,					dim=__lowerCAmelCase						) / (eps + torch.sum(__lowerCAmelCase		,					dim=__lowerCAmelCase						))





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase = 2.3125		,					__lowerCAmelCase = 21.6875		,					__lowerCAmelCase = 64						)->  torch.Tensor:






      '''simple docstring'''





      UpperCAmelCase    : List[Any]      =torch.linspace(__lowerCAmelCase		,					__lowerCAmelCase		,					no_bins - 1		,					device=pts.device						)
      UpperCAmelCase    : Optional[int]      =torch.sqrt(torch.sum((pts.unsqueeze(-2						) - pts.unsqueeze(-3						)) ** 2		,					dim=-1						)						)
      return torch.bucketize(__lowerCAmelCase		,					__lowerCAmelCase						)





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase						)->  dict:






      '''simple docstring'''





      UpperCAmelCase    : List[Any]      =dicts[0]
      UpperCAmelCase    : Any      ={}
      for k, v in first.items():
            UpperCAmelCase    : Tuple      =[d[k] for d in dicts]
            if isinstance(__lowerCAmelCase		,					__lowerCAmelCase						):
                  UpperCAmelCase    : Optional[int]      =dict_multimap(__lowerCAmelCase		,					__lowerCAmelCase						)
            else:
                  UpperCAmelCase    : Optional[Any]      =fn(__lowerCAmelCase						)

      return new_dict





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase						)->  torch.Tensor:






      '''simple docstring'''





      UpperCAmelCase    : Tuple      =v_bins.view(((1,) * len(x.shape						)) + (len(__lowerCAmelCase						),)						)
      UpperCAmelCase    : List[str]      =x[..., None] - reshaped_bins
      UpperCAmelCase    : Any      =torch.argmin(torch.abs(__lowerCAmelCase						)		,					dim=-1						)
      return nn.functional.one_hot(__lowerCAmelCase		,					num_classes=len(__lowerCAmelCase						)						).float()





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase = 0		,					__lowerCAmelCase = 0						)->  torch.Tensor:






      '''simple docstring'''





      UpperCAmelCase    : List[Union[slice, torch.Tensor]]      =[]
      for i, s in enumerate(data.shape[:no_batch_dims]						):
            UpperCAmelCase    : str      =torch.arange(__lowerCAmelCase						)
            UpperCAmelCase    : List[Any]      =r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape						) - i - 1)))						)
            ranges.append(__lowerCAmelCase						)

      UpperCAmelCase    : List[Union[slice, torch.Tensor]]      =[slice(__lowerCAmelCase						) for _ in range(len(data.shape						) - no_batch_dims						)]
      UpperCAmelCase    : Any      =inds
      ranges.extend(__lowerCAmelCase						)
      # Matt note: Editing this to get around the behaviour of using a list as an array index changing
      # in recent Numpy versions
      return data[tuple(__lowerCAmelCase						)]


__snake_case			      =    TypeVar('''T''')





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  Dict[Any, Union[dict, list, tuple, Any]]:






      '''simple docstring'''





      UpperCAmelCase    : Dict[Any, Union[dict, list, tuple, Any]]      ={}
      for k, v in dic.items():
            if isinstance(__lowerCAmelCase		,					__lowerCAmelCase						):
                  UpperCAmelCase    : Tuple      =dict_map(__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)
            else:
                  UpperCAmelCase    : Optional[Any]      =tree_map(__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)

      return new_dict





@overload
def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  Any:






      '''simple docstring'''





      ...





@overload
def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  dict:






      '''simple docstring'''





      ...





@overload
def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  list:






      '''simple docstring'''





      ...





@overload
def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  tuple:






      '''simple docstring'''





      ...





def   lowerCAmelCase_ (				__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)->  Optional[int]:






      '''simple docstring'''





      if isinstance(__lowerCAmelCase		,					__lowerCAmelCase						):
            return dict_map(__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						)
      elif isinstance(__lowerCAmelCase		,					__lowerCAmelCase						):
            return [tree_map(__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						) for x in tree]
      elif isinstance(__lowerCAmelCase		,					__lowerCAmelCase						):
            return tuple(tree_map(__lowerCAmelCase		,					__lowerCAmelCase		,					__lowerCAmelCase						) for x in tree						)
      elif isinstance(__lowerCAmelCase		,					__lowerCAmelCase						):
            return fn(__lowerCAmelCase						)
      else:
            print(type(__lowerCAmelCase						)						)
            raise ValueError('''Not supported'''						)


__snake_case			      =    partial(tree_map, leaf_type=torch.Tensor)

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


__snake_case			      =    {
    '''configuration_esm''': ['''ESM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EsmConfig'''],
    '''tokenization_esm''': ['''EsmTokenizer'''],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __snake_case			      =    [
         '''ESM_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''EsmForMaskedLM''',
         '''EsmForSequenceClassification''',
         '''EsmForTokenClassification''',
         '''EsmModel''',
         '''EsmPreTrainedModel''',
     ]
     __snake_case			      =    ['''EsmForProteinFolding''', '''EsmFoldPreTrainedModel''']

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __snake_case			      =    [
         '''TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''TFEsmForMaskedLM''',
         '''TFEsmForSequenceClassification''',
         '''TFEsmForTokenClassification''',
         '''TFEsmModel''',
         '''TFEsmPreTrainedModel''',
     ]

if TYPE_CHECKING:
     from .configuration_esm import ESM_PRETRAINED_CONFIG_ARCHIVE_MAP, EsmConfig
     from .tokenization_esm import EsmTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_esm import (
              ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
              EsmForMaskedLM,
              EsmForSequenceClassification,
              EsmForTokenClassification,
              EsmModel,
              EsmPreTrainedModel,
          )
          from .modeling_esmfold import EsmFoldPreTrainedModel, EsmForProteinFolding

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_esm import (
              TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFEsmForMaskedLM,
              TFEsmForSequenceClassification,
              TFEsmForTokenClassification,
              TFEsmModel,
              TFEsmPreTrainedModel,
          )


else:
     import sys

     __snake_case			      =    _LazyModule(__name__, globals()['''__file__'''], _import_structure)
