

from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map





def      snake_case_				(__A					:						Dict[str, torch.Tensor]					)      ->     Dict[str, torch.Tensor]:
   __lowerCAmelCase  :      Any    =						[]
   __lowerCAmelCase  :      List[str]    =						[]
   __lowerCAmelCase  :      Optional[int]    =						[]

   for rt in rc.restypes:
      __lowerCAmelCase  :      int    =						rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
      restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names]					)
      __lowerCAmelCase  :      Optional[int]    =						{name: i for i, name in enumerate(_UpperCAmelCase					)}
      restype_atomaa_to_atomaa_list.append(
          [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types]					)

      restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names]					)

   # Add dummy mapping for restype 'UNK'
   restype_atomaa_to_atomaa_list.append([0] * 1_4					)
   restype_atomaa_to_atomaa_list.append([0] * 3_7					)
   restype_atomaa_mask_list.append([0.0] * 1_4					)

   __lowerCAmelCase  :      Optional[Any]    =						torch.tensor(
       _UpperCAmelCase ,       dtype=torch.intaa ,       device=protein["""aatype"""].device ,       )
   __lowerCAmelCase  :      Union[str, Any]    =						torch.tensor(
       _UpperCAmelCase ,       dtype=torch.intaa ,       device=protein["""aatype"""].device ,       )
   __lowerCAmelCase  :      Optional[Any]    =						torch.tensor(
       _UpperCAmelCase ,       dtype=torch.floataa ,       device=protein["""aatype"""].device ,       )
   __lowerCAmelCase  :      Dict    =						protein['aatype'].to(torch.long					)

   # create the mapping for (residx, atom14) --> atom37, i.e. an array
   # with shape (num_res, 14) containing the atom37 indices for this protein
   __lowerCAmelCase  :      Any    =						restype_atomaa_to_atomaa[protein_aatype]
   __lowerCAmelCase  :      List[str]    =						restype_atomaa_mask[protein_aatype]

   __lowerCAmelCase  :      Tuple    =						residx_atomaa_mask
   __lowerCAmelCase  :      Union[str, Any]    =						residx_atomaa_to_atomaa.long()

   # create the gather indices for mapping back
   __lowerCAmelCase  :      Optional[Any]    =						restype_atomaa_to_atomaa[protein_aatype]
   __lowerCAmelCase  :      Any    =						residx_atomaa_to_atomaa.long()

   # create the corresponding mask
   __lowerCAmelCase  :      Any    =						torch.zeros([2_1, 3_7] ,       dtype=torch.floataa ,       device=protein["""aatype"""].device					)
   for restype, restype_letter in enumerate(rc.restypes					):
      __lowerCAmelCase  :      Optional[Any]    =						rc.restype_atoa[restype_letter]
      __lowerCAmelCase  :      Optional[int]    =						rc.residue_atoms[restype_name]
      for atom_name in atom_names:
         __lowerCAmelCase  :      Tuple    =						rc.atom_order[atom_name]
         __lowerCAmelCase  :      List[str]    =						1

   __lowerCAmelCase  :      str    =						restype_atomaa_mask[protein_aatype]
   __lowerCAmelCase  :      List[str]    =						residx_atomaa_mask

   return protein





def      snake_case_				(__A					:						Dict[str, torch.Tensor]					)      ->     Dict[str, np.ndarray]:
   __lowerCAmelCase  :      Union[str, Any]    =						tree_map(lambda __A					: torch.tensor(_UpperCAmelCase ,       device=batch["""aatype"""].device					) ,       _UpperCAmelCase ,       np.ndarray					)
   __lowerCAmelCase  :      List[Any]    =						tensor_tree_map(lambda __A					: np.array(_UpperCAmelCase					) ,       make_atomaa_masks(_UpperCAmelCase					)					)
   return out





import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
   from transformers.models.bert.modeling_flax_bert import (
       FlaxBertForMaskedLM,
       FlaxBertForMultipleChoice,
       FlaxBertForNextSentencePrediction,
       FlaxBertForPreTraining,
       FlaxBertForQuestionAnswering,
       FlaxBertForSequenceClassification,
       FlaxBertForTokenClassification,
       FlaxBertModel,
   )



class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






  def __init__(      self			:   Tuple	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   str=13	,							lowerCAmelCase			:   Optional[Any]=7	,							lowerCAmelCase			:   Optional[Any]=True	,							lowerCAmelCase			:   Any=True	,							lowerCAmelCase			:   str=True	,							lowerCAmelCase			:   Optional[Any]=True	,							lowerCAmelCase			:   Dict=99	,							lowerCAmelCase			:   Any=32	,							lowerCAmelCase			:   int=5	,							lowerCAmelCase			:   Tuple=4	,							lowerCAmelCase			:   Optional[Any]=37	,							lowerCAmelCase			:   str="gelu"	,							lowerCAmelCase			:   str=0.1	,							lowerCAmelCase			:   str=0.1	,							lowerCAmelCase			:   Any=5_12	,							lowerCAmelCase			:   Optional[Any]=16	,							lowerCAmelCase			:   Dict=2	,							lowerCAmelCase			:   Tuple=0.02	,							lowerCAmelCase			:   Optional[int]=4	,							)							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						parent
     __lowerCAmelCase  :      Dict    =						batch_size
     __lowerCAmelCase  :      Dict    =						seq_length
     __lowerCAmelCase  :      Union[str, Any]    =						is_training
     __lowerCAmelCase  :      List[Any]    =						use_attention_mask
     __lowerCAmelCase  :      List[Any]    =						use_token_type_ids
     __lowerCAmelCase  :      Optional[int]    =						use_labels
     __lowerCAmelCase  :      str    =						vocab_size
     __lowerCAmelCase  :      Any    =						hidden_size
     __lowerCAmelCase  :      Optional[int]    =						num_hidden_layers
     __lowerCAmelCase  :      Optional[int]    =						num_attention_heads
     __lowerCAmelCase  :      Dict    =						intermediate_size
     __lowerCAmelCase  :      Tuple    =						hidden_act
     __lowerCAmelCase  :      Dict    =						hidden_dropout_prob
     __lowerCAmelCase  :      Any    =						attention_probs_dropout_prob
     __lowerCAmelCase  :      Union[str, Any]    =						max_position_embeddings
     __lowerCAmelCase  :      int    =						type_vocab_size
     __lowerCAmelCase  :      Tuple    =						type_sequence_label_size
     __lowerCAmelCase  :      int    =						initializer_range
     __lowerCAmelCase  :      Optional[int]    =						num_choices







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size   )

     __lowerCAmelCase  :      Dict    =						None
     if self.use_attention_mask:
        __lowerCAmelCase  :      Optional[int]    =						random_attention_mask([self.batch_size, self.seq_length]   )

     __lowerCAmelCase  :      Union[str, Any]    =						None
     if self.use_token_type_ids:
        __lowerCAmelCase  :      str    =						ids_tensor([self.batch_size, self.seq_length]	,							self.type_vocab_size   )

     __lowerCAmelCase  :      List[str]    =						BertConfig(
         vocab_size=self.vocab_size	,							hidden_size=self.hidden_size	,							num_hidden_layers=self.num_hidden_layers	,							num_attention_heads=self.num_attention_heads	,							intermediate_size=self.intermediate_size	,							hidden_act=self.hidden_act	,							hidden_dropout_prob=self.hidden_dropout_prob	,							attention_probs_dropout_prob=self.attention_probs_dropout_prob	,							max_position_embeddings=self.max_position_embeddings	,							type_vocab_size=self.type_vocab_size	,							is_decoder=lowerCAmelCase	,							initializer_range=self.initializer_range	,							)

     return config, input_ids, token_type_ids, attention_mask







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Any    =						self.prepare_config_and_inputs()
     __lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase  :      List[str]    =						config_and_inputs
     __lowerCAmelCase  :      Any    =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
     return config, inputs_dict







  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						self.prepare_config_and_inputs()
     __lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase  :      Dict    =						config_and_inputs

     __lowerCAmelCase  :      Any    =						True
     __lowerCAmelCase  :      str    =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
     __lowerCAmelCase  :      int    =						ids_tensor([self.batch_size, self.seq_length]	,							vocab_size=2   )

     return (
         config,
         input_ids,
         attention_mask,
         encoder_hidden_states,
         encoder_attention_mask,
     )






@require_flax
class SCREAMING_SNAKE_CASE     (  a_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      int										=True

  lowerCamelCase   :      Any										=(
      (
          FlaxBertModel,
          FlaxBertForPreTraining,
          FlaxBertForMaskedLM,
          FlaxBertForMultipleChoice,
          FlaxBertForQuestionAnswering,
          FlaxBertForNextSentencePrediction,
          FlaxBertForSequenceClassification,
          FlaxBertForTokenClassification,
          FlaxBertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str   )							->     Union[str, Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						FlaxBertModelTester(self   )







  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						FlaxBertModel.from_pretrained("""bert-base-cased"""   )
     __lowerCAmelCase  :      str    =						model(np.ones((1, 1)   )   )
     self.assertIsNotNone(lowerCAmelCase   )


