



import torch

from transformers import AutoModel





class 		UpperCAmelCase						(						torch.nn.Module				):





   def __init__(	self       ,     _lowerCAmelCase="sayef/fsner-bert-base-uncased"			):
         super(_lowerCAmelCase       ,     self			).__init__()

         _lowerCAmelCase 			=  AutoModel.from_pretrained(_lowerCAmelCase       ,     return_dict=_lowerCAmelCase			)
         _lowerCAmelCase 			=  torch.nn.CosineSimilarity(3       ,     1E-08			)
         _lowerCAmelCase 			=  torch.nn.Softmax(dim=1			)





   def 	__lowerCAmelCase							(	self       ,     **_lowerCAmelCase			):
         return self.bert(**_lowerCAmelCase			).last_hidden_state





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         return token_embeddings.sum(2       ,     keepdim=_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase       ,     _lowerCAmelCase=1			):
         return self.softmax(T * self.cos(_lowerCAmelCase       ,     _lowerCAmelCase			)			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  W_supports['''sizes'''].tolist()
         _lowerCAmelCase 			=  W_supports['''start_token_id'''].item()
         _lowerCAmelCase 			=  W_supports['''end_token_id'''].item()

         del W_supports["sizes"]
         del W_supports["start_token_id"]
         del W_supports["end_token_id"]

         _lowerCAmelCase 			=  self.BERT(**_lowerCAmelCase			)
         _lowerCAmelCase 			=  self.BERT(**_lowerCAmelCase			)

         _lowerCAmelCase 			=  None
         _lowerCAmelCase 			=  None

         _lowerCAmelCase 			=  W_supports['''input_ids'''] == start_token_id
         _lowerCAmelCase 			=  W_supports['''input_ids'''] == end_token_id

         for i, size in enumerate(_lowerCAmelCase			):
               if i == 0:
                     _lowerCAmelCase 			=  0
               else:
                     _lowerCAmelCase 			=  support_sizes[i - 1]

               _lowerCAmelCase 			=  S[s : s + size][start_token_masks[s : s + size]]
               _lowerCAmelCase 			=  S[s : s + size][end_token_masks[s : s + size]]

               _lowerCAmelCase 			=  torch.matmul(q[i]       ,     s_start.T			).sum(1			).softmax(0			)
               _lowerCAmelCase 			=  torch.matmul(q[i]       ,     s_end.T			).sum(1			).softmax(0			)

               if p_starts is not None:
                     _lowerCAmelCase 			=  torch.vstack((p_starts, p_start)			)
                     _lowerCAmelCase 			=  torch.vstack((p_ends, p_end)			)
               else:
                     _lowerCAmelCase 			=  p_start
                     _lowerCAmelCase 			=  p_end

         return p_starts, p_ends




import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Optional[Any]     ,				_SCREAMING_SNAKE_CASE   :	List[Any]     ,				_SCREAMING_SNAKE_CASE   :	List[Any]				)->Dict:
      # Initialise PyTorch model
      _lowerCAmelCase 			=  AlbertConfig.from_json_file(_SCREAMING_SNAKE_CASE				)
      print(f'''Building PyTorch model from configuration: {config}'''				)
      _lowerCAmelCase 			=  AlbertForPreTraining(_SCREAMING_SNAKE_CASE				)

      # Load weights from tf checkpoint
      load_tf_weights_in_albert(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)

      # Save pytorch-model
      print(f'''Save PyTorch model to {pytorch_dump_path}'''				)
      torch.save(model.state_dict()     ,				_SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
    UpperCAmelCase_									=							argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
    )
    parser.add_argument(
        "--albert_config_file",
        default=None,
        type=str,
        required=True,
        help=(
            "The config json file corresponding to the pre-trained ALBERT model. \n"
            "This specifies the model architecture."
        ),
    )
    parser.add_argument(
        "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
    )
    UpperCAmelCase_									=							parser.parse_args()
    convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)