





import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
         CTRLForSequenceClassification,
         CTRLLMHeadModel,
         CTRLModel,
     )

class      A       :



      def __init__(	self:						str		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Dict=14		,  _lowerCAmelCase:						Any=7		,  _lowerCAmelCase:						List[Any]=True		,  _lowerCAmelCase:						Union[str, Any]=True		,  _lowerCAmelCase:						Optional[int]=True		,  _lowerCAmelCase:						Optional[int]=True		,  _lowerCAmelCase:						Any=True		,  _lowerCAmelCase:						Tuple=99		,  _lowerCAmelCase:						Optional[int]=32		,  _lowerCAmelCase:						Any=5		,  _lowerCAmelCase:						Union[str, Any]=4		,  _lowerCAmelCase:						Optional[int]=37		,  _lowerCAmelCase:						int="gelu"		,  _lowerCAmelCase:						Union[str, Any]=0.1		,  _lowerCAmelCase:						List[str]=0.1		,  _lowerCAmelCase:						int=512		,  _lowerCAmelCase:						Dict=16		,  _lowerCAmelCase:						Optional[Any]=2		,  _lowerCAmelCase:						Union[str, Any]=0.02		,  _lowerCAmelCase:						str=3		,  _lowerCAmelCase:						Any=4		,  _lowerCAmelCase:						Optional[Any]=None		,  )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=parent
         UpperCAmelCase_											=batch_size
         UpperCAmelCase_											=seq_length
         UpperCAmelCase_											=is_training
         UpperCAmelCase_											=use_token_type_ids
         UpperCAmelCase_											=use_input_mask
         UpperCAmelCase_											=use_labels
         UpperCAmelCase_											=use_mc_token_ids
         UpperCAmelCase_											=vocab_size
         UpperCAmelCase_											=hidden_size
         UpperCAmelCase_											=num_hidden_layers
         UpperCAmelCase_											=num_attention_heads
         UpperCAmelCase_											=intermediate_size
         UpperCAmelCase_											=hidden_act
         UpperCAmelCase_											=hidden_dropout_prob
         UpperCAmelCase_											=attention_probs_dropout_prob
         UpperCAmelCase_											=max_position_embeddings
         UpperCAmelCase_											=type_vocab_size
         UpperCAmelCase_											=type_sequence_label_size
         UpperCAmelCase_											=initializer_range
         UpperCAmelCase_											=num_labels
         UpperCAmelCase_											=num_choices
         UpperCAmelCase_											=scope
         UpperCAmelCase_											=self.vocab_size - 1



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.vocab_size       )

         UpperCAmelCase_											=None
         if self.use_input_mask:
            UpperCAmelCase_											=random_attention_mask([self.batch_size, self.seq_length]       )

         UpperCAmelCase_											=None
         if self.use_token_type_ids:
            UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.type_vocab_size       )

         UpperCAmelCase_											=None
         if self.use_mc_token_ids:
            UpperCAmelCase_											=ids_tensor([self.batch_size, self.num_choices]		,  self.seq_length       )

         UpperCAmelCase_											=None
         UpperCAmelCase_											=None
         UpperCAmelCase_											=None
         if self.use_labels:
            UpperCAmelCase_											=ids_tensor([self.batch_size]		,  self.type_sequence_label_size       )
            UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.num_labels       )
            UpperCAmelCase_											=ids_tensor([self.batch_size]		,  self.num_choices       )

         UpperCAmelCase_											=self.get_config()

         UpperCAmelCase_											=ids_tensor([self.num_hidden_layers, self.num_attention_heads]		,  2       )

         return (
             config,
             input_ids,
             input_mask,
             head_mask,
             token_type_ids,
             mc_token_ids,
             sequence_labels,
             token_labels,
             choice_labels,
         )



      def 	lowerCAmelCase__    (	self:						str       )						->    str:
         '''simple docstring'''

         return CTRLConfig(
             vocab_size=self.vocab_size		,  n_embd=self.hidden_size		,  n_layer=self.num_hidden_layers		,  n_head=self.num_attention_heads		,  n_positions=self.max_position_embeddings		,  pad_token_id=self.pad_token_id		,  )



      def 	lowerCAmelCase__    (	self:						int		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Any		,  *_lowerCAmelCase:						List[str]       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=CTRLModel(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()

         model(_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase		,  head_mask=_lowerCAmelCase       )
         model(_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )
         UpperCAmelCase_											=model(_lowerCAmelCase       )
         self.parent.assertEqual(result.last_hidden_state.shape		,  (self.batch_size, self.seq_length, self.hidden_size)       )
         self.parent.assertEqual(len(result.past_key_values       )		,  config.n_layer       )



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						Any		,  *_lowerCAmelCase:						int       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=CTRLLMHeadModel(_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()

         UpperCAmelCase_											=model(_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase		,  labels=_lowerCAmelCase       )
         self.parent.assertEqual(result.loss.shape		,  ()       )
         self.parent.assertEqual(result.logits.shape		,  (self.batch_size, self.seq_length, self.vocab_size)       )



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=self.prepare_config_and_inputs()

         (
             (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       
         )											=config_and_inputs

         UpperCAmelCase_											={"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

         return config, inputs_dict






      def 	lowerCAmelCase__    (	self:						List[str]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Optional[int]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						List[Any]		,  *_lowerCAmelCase:						Optional[int]       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.num_labels
         UpperCAmelCase_											=CTRLForSequenceClassification(_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=ids_tensor([self.batch_size]		,  self.type_sequence_label_size       )
         UpperCAmelCase_											=model(_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase		,  labels=_lowerCAmelCase       )
         self.parent.assertEqual(result.logits.shape		,  (self.batch_size, self.num_labels)       )

@require_torch
class      A       (	__lowercase		,      __lowercase		,      __lowercase		,      unittest.TestCase						):
      _snake_case							     =(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
      _snake_case							     =(CTRLLMHeadModel,) if is_torch_available() else ()
      _snake_case							     =(
          {
              '''feature-extraction''': CTRLModel,
              '''text-classification''': CTRLForSequenceClassification,
              '''text-generation''': CTRLLMHeadModel,
              '''zero-shot''': CTRLForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _snake_case							     =True
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Union[str, Any]		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Tuple       )						->    List[str]:
         '''simple docstring'''

         if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
            # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
            # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
            # config could not be created.
            return True

         return False



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=CTRLModelTester(self       )
         UpperCAmelCase_											=ConfigTester(self		,  config_class=_lowerCAmelCase		,  n_embd=37       )



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    int:
         '''simple docstring'''

         super().tearDown()
         # clean-up as much as possible GPU memory occupied by PyTorch
         gc.collect()
         torch.cuda.empty_cache()



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    List[str]:
         '''simple docstring'''

         self.config_tester.run_common_tests()



      def 	lowerCAmelCase__    (	self:						str       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_model(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						str       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase       )



      @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."       )
      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Dict:
         '''simple docstring'''

         pass



      @slow
      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Optional[Any]:
         '''simple docstring'''

         for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_											=CTRLModel.from_pretrained(_lowerCAmelCase       )
            self.assertIsNotNone(_lowerCAmelCase       )






      @unittest.skip("The model doesn't support left padding"       )  # and it's not used enough to be worth fixing :)
      def 	lowerCAmelCase__    (	self:						Any       )						->    List[str]:
         '''simple docstring'''

         pass

@require_torch
class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Union[str, Any]:
         '''simple docstring'''

         super().tearDown()
         # clean-up as much as possible GPU memory occupied by PyTorch
         gc.collect()
         torch.cuda.empty_cache()






      @slow
      def 	lowerCAmelCase__    (	self:						Any       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=CTRLLMHeadModel.from_pretrained("ctrl"       )
         model.to(_lowerCAmelCase       )
         UpperCAmelCase_											=torch.tensor(
             [[1_1859, 0, 1611, 8]]		,  dtype=torch.long		,  device=_lowerCAmelCase       )  # Legal the president is
         UpperCAmelCase_											=[
             1_1859,
             0,
             1611,
             8,
             5,
             150,
             2_6449,
             2,
             19,
             348,
             469,
             3,
             2595,
             48,
             2_0740,
             24_6533,
             24_6533,
             19,
             30,
             5,
         ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

         UpperCAmelCase_											=model.generate(_lowerCAmelCase		,  do_sample=_lowerCAmelCase       )
         self.assertListEqual(output_ids[0].tolist()		,  _lowerCAmelCase       )











from __future__ import annotations

from sys import maxsize
from typing import Generic, TypeVar

__lowercase  :   Optional[int]						      =TypeVar("""T""")




def     a__ (					lowercase__  ):






   '''simple docstring'''






   return (position - 1) // 2




def     a__ (					lowercase__  ):






   '''simple docstring'''






   return (2 * position) + 1




def     a__ (					lowercase__  ):






   '''simple docstring'''






   return (2 * position) + 2

class      A       (	Generic[T]						):



      def __init__(	self:						List[str]       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=[]
         UpperCAmelCase_											={}
         UpperCAmelCase_											=0



      def __len__(	self:						Union[str, Any]       )						->    int:
         '''simple docstring'''

         return self.elements



      def __repr__(	self:						Dict       )						->    str:
         '''simple docstring'''

         return str(self.heap       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    bool:
         '''simple docstring'''

         return self.elements == 0



      def 	lowerCAmelCase__    (	self:						Optional[int]		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         self.heap.append((elem, weight)       )
         UpperCAmelCase_											=self.elements
         self.elements += 1
         self._bubble_up(_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    T:
         '''simple docstring'''

         if self.elements > 1:
            self._swap_nodes(0		,  self.elements - 1       )
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap.pop()
         del self.position_map[elem]
         self.elements -= 1
         if self.elements > 0:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[0]
            self._bubble_down(_lowerCAmelCase       )
         return elem



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.position_map[elem]
         UpperCAmelCase_											=(elem, weight)
         if position > 0:
            UpperCAmelCase_											=get_parent_position(_lowerCAmelCase       )
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[parent_position]
            if parent_weight > weight:
               self._bubble_up(_lowerCAmelCase       )
            else:
               self._bubble_down(_lowerCAmelCase       )
         else:
            self._bubble_down(_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						T       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.position_map[elem]
         if curr_pos == 0:
            return None
         UpperCAmelCase_											=get_parent_position(_lowerCAmelCase       )
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[curr_pos]
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[parent_position]
         if parent_weight > weight:
            self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
            return self._bubble_up(_lowerCAmelCase       )
         return None



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						T       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.position_map[elem]
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[curr_pos]
         UpperCAmelCase_											=get_child_left_position(_lowerCAmelCase       )
         UpperCAmelCase_											=get_child_right_position(_lowerCAmelCase       )
         if child_left_position < self.elements and child_right_position < self.elements:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_left_position]
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_right_position]
            if child_right_weight < child_left_weight and child_right_weight < weight:
               self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
               return self._bubble_down(_lowerCAmelCase       )
         if child_left_position < self.elements:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_left_position]
            if child_left_weight < weight:
               self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
               return self._bubble_down(_lowerCAmelCase       )
         else:
            return None
         if child_right_position < self.elements:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_right_position]
            if child_right_weight < weight:
               self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
               return self._bubble_down(_lowerCAmelCase       )
         return None






      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.heap[nodea_pos][0]
         UpperCAmelCase_											=self.heap[nodea_pos][0]
         UpperCAmelCase_						,       UpperCAmelCase_											=(
             self.heap[nodea_pos],
             self.heap[nodea_pos],
         )
         UpperCAmelCase_											=nodea_pos
         UpperCAmelCase_											=nodea_pos

class      A       (	Generic[T]						):



      def __init__(	self:						Tuple       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											={}
         UpperCAmelCase_											=0



      def __repr__(	self:						List[str]       )						->    str:
         '''simple docstring'''

         return str(self.connections       )



      def __len__(	self:						Optional[Any]       )						->    int:
         '''simple docstring'''

         return self.nodes



      def 	lowerCAmelCase__    (	self:						Union[str, Any]		,  _lowerCAmelCase:						T       )						->    None:
         '''simple docstring'''

         if node not in self.connections:
            UpperCAmelCase_											={}
            self.nodes += 1






      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         self.add_node(_lowerCAmelCase       )
         self.add_node(_lowerCAmelCase       )
         UpperCAmelCase_											=weight
         UpperCAmelCase_											=weight




def     a__ (					lowercase__  ,    ):






   '''simple docstring'''






   UpperCAmelCase_											={node: maxsize for node in graph.connections}
   UpperCAmelCase_											={node: None for node in graph.connections}

   UpperCAmelCase_											=MinPriorityQueue()
   for node, weight in dist.items():
      priority_queue.push(lowercase__  ,    lowercase__  )

   if priority_queue.is_empty():
      return dist, parent

   # initialization
   UpperCAmelCase_											=priority_queue.extract_min()
   UpperCAmelCase_											=0
   for neighbour in graph.connections[node]:
      if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
         UpperCAmelCase_											=dist[node] + graph.connections[node][neighbour]
         priority_queue.update_key(lowercase__  ,    dist[neighbour]  )
         UpperCAmelCase_											=node

    # running prim's algorithm
   while not priority_queue.is_empty():
      UpperCAmelCase_											=priority_queue.extract_min()
      for neighbour in graph.connections[node]:
         if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
            UpperCAmelCase_											=dist[node] + graph.connections[node][neighbour]
            priority_queue.update_key(lowercase__  ,    dist[neighbour]  )
            UpperCAmelCase_											=node
   return dist, parent




