


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      :     Tuple      ,							lowerCamelCase_      :     Any      ,							lowerCamelCase_      :     List[Any]=1_4      ,							lowerCamelCase_      :     Dict=7      ,							lowerCamelCase_      :     List[str]=True      ,							lowerCamelCase_      :     Tuple=True      ,							lowerCamelCase_      :     Union[str, Any]=True      ,							lowerCamelCase_      :     List[Any]=True      ,							lowerCamelCase_      :     Optional[int]=True      ,							lowerCamelCase_      :     Tuple=9_9      ,							lowerCamelCase_      :     Optional[Any]=3_2      ,							lowerCamelCase_      :     List[str]=5      ,							lowerCamelCase_      :     Dict=4      ,							lowerCamelCase_      :     str=3_7      ,							lowerCamelCase_      :     Dict="gelu"      ,							lowerCamelCase_      :     List[str]=0.1      ,							lowerCamelCase_      :     str=0.1      ,							lowerCamelCase_      :     Any=5_1_2      ,							lowerCamelCase_      :     Union[str, Any]=1_6      ,							lowerCamelCase_      :     List[Any]=2      ,							lowerCamelCase_      :     Tuple=0.0_2      ,							lowerCamelCase_      :     Tuple=3      ,							lowerCamelCase_      :     Union[str, Any]=4      ,							lowerCamelCase_      :     Any=None      ,							):
            a_    :     Tuple										=    parent
            a_    :     List[Any]										=    batch_size
            a_    :     Optional[Any]										=    seq_length
            a_    :     Dict										=    is_training
            a_    :     Tuple										=    use_token_type_ids
            a_    :     Tuple										=    use_input_mask
            a_    :     int										=    use_labels
            a_    :     Dict										=    use_mc_token_ids
            a_    :     List[str]										=    vocab_size
            a_    :     List[Any]										=    hidden_size
            a_    :     List[str]										=    num_hidden_layers
            a_    :     List[Any]										=    num_attention_heads
            a_    :     Dict										=    intermediate_size
            a_    :     Union[str, Any]										=    hidden_act
            a_    :     List[str]										=    hidden_dropout_prob
            a_    :     List[str]										=    attention_probs_dropout_prob
            a_    :     Tuple										=    max_position_embeddings
            a_    :     Tuple										=    type_vocab_size
            a_    :     Dict										=    type_sequence_label_size
            a_    :     int										=    initializer_range
            a_    :     List[str]										=    num_labels
            a_    :     Optional[Any]										=    num_choices
            a_    :     str										=    scope
            a_    :     Optional[Any]										=    self.vocab_size - 1

       def 						UpperCAmelCase(   self      :     Optional[int]    ):
            a_    :     int										=    ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size    )

            a_    :     Union[str, Any]										=    None
            if self.use_input_mask:
                 a_    :     Union[str, Any]										=    random_attention_mask([self.batch_size, self.seq_length]    )

            a_    :     Optional[int]										=    None
            if self.use_token_type_ids:
                 a_    :     Optional[Any]										=    ids_tensor([self.batch_size, self.seq_length]      ,							self.type_vocab_size    )

            a_    :     Optional[Any]										=    None
            if self.use_mc_token_ids:
                 a_    :     Dict										=    ids_tensor([self.batch_size, self.num_choices]      ,							self.seq_length    )

            a_    :     Optional[Any]										=    None
            a_    :     Union[str, Any]										=    None
            a_    :     List[Any]										=    None
            if self.use_labels:
                 a_    :     Optional[Any]										=    ids_tensor([self.batch_size]      ,							self.type_sequence_label_size    )
                 a_    :     Dict										=    ids_tensor([self.batch_size, self.seq_length]      ,							self.num_labels    )
                 a_    :     str										=    ids_tensor([self.batch_size]      ,							self.num_choices    )

            a_    :     str										=    self.get_config()

            a_    :     Optional[int]										=    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 						UpperCAmelCase(   self      :     Optional[int]    ):
            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 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     Optional[int]      ,							lowerCamelCase_      :     List[str]      ,							lowerCamelCase_      :     List[str]      ,							*lowerCamelCase_      :     Optional[int]    ):
            a_    :     Any										=    CTRLModel(config=__A    )
            model.to(__A    )
            model.eval()

            model(__A      ,							token_type_ids=__A      ,							head_mask=__A    )
            model(__A      ,							token_type_ids=__A    )
            a_    :     Optional[int]										=    model(__A    )
            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 						UpperCAmelCase(   self      :     Optional[Any]      ,							lowerCamelCase_      :     List[str]      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     Any      ,							*lowerCamelCase_      :     Any    ):
            a_    :     Tuple										=    CTRLLMHeadModel(__A    )
            model.to(__A    )
            model.eval()

            a_    :     List[Any]										=    model(__A      ,							token_type_ids=__A      ,							labels=__A    )
            self.parent.assertEqual(result.loss.shape      ,							()    )
            self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)    )

       def 						UpperCAmelCase(   self      :     Optional[int]    ):
            a_    :     Union[str, Any]										=    self.prepare_config_and_inputs()

            (
                (
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			(
                a_
            )			,			
            )    :     str										=    config_and_inputs

            a_    :     List[Any]										=    {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

            return config, inputs_dict

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     Dict      ,							lowerCamelCase_      :     Tuple      ,							lowerCamelCase_      :     List[Any]      ,							*lowerCamelCase_      :     Optional[int]    ):
            a_    :     str										=    self.num_labels
            a_    :     Tuple										=    CTRLForSequenceClassification(__A    )
            model.to(__A    )
            model.eval()
            a_    :     Optional[int]										=    ids_tensor([self.batch_size]      ,							self.type_sequence_label_size    )
            a_    :     Tuple										=    model(__A      ,							token_type_ids=__A      ,							labels=__A    )
            self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels)    )


@require_torch
class   a__	(						lowerCAmelCase_						,       lowerCAmelCase_						,       lowerCAmelCase_						,       unittest.TestCase    ):
       lowerCamelCase__:     int							    =     (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       lowerCamelCase__:     Optional[int]							    =     (CTRLLMHeadModel,) if is_torch_available() else ()
       lowerCamelCase__:     Dict							    =     (
           {
               """feature-extraction""": CTRLModel,
               """text-classification""": CTRLForSequenceClassification,
               """text-generation""": CTRLLMHeadModel,
               """zero-shot""": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCamelCase__:     Optional[Any]							    =     True
       lowerCamelCase__:     Tuple							    =     False
       lowerCamelCase__:     str							    =     False

       def 						UpperCAmelCase(   self      :     Any      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     Optional[Any]      ,							lowerCamelCase_      :     Optional[int]      ,							lowerCamelCase_      :     List[Any]    ):
            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 						UpperCAmelCase(   self      :     Any    ):
            a_    :     Optional[int]										=    CTRLModelTester(self    )
            a_    :     int										=    ConfigTester(self      ,							config_class=__A      ,							n_embd=3_7    )

       def 						UpperCAmelCase(   self      :     Optional[int]    ):
            super().tearDown()
            # clean-up as much as possible GPU memory occupied by PyTorch
            gc.collect()
            torch.cuda.empty_cache()

       def 						UpperCAmelCase(   self      :     Dict    ):
            self.config_tester.run_common_tests()

       def 						UpperCAmelCase(   self      :     str    ):
            a_    :     int										=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_ctrl_model(*__A    )

       def 						UpperCAmelCase(   self      :     List[str]    ):
            a_    :     Tuple										=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_lm_head_model(*__A    )

       @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""    )
       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            pass

       @slow
       def 						UpperCAmelCase(   self      :     Tuple    ):
            for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a_    :     Dict										=    CTRLModel.from_pretrained(__A    )
                 self.assertIsNotNone(__A    )

       @unittest.skip("""The model doesn\'t support left padding"""    )  # and it's not used enough to be worth fixing :)
       def 						UpperCAmelCase(   self      :     Any    ):
            pass


@require_torch
class   a__	(						unittest.TestCase    ):

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

       @slow
       def 						UpperCAmelCase(   self      :     Any    ):
            a_    :     Union[str, Any]										=    CTRLLMHeadModel.from_pretrained("""ctrl"""    )
            model.to(__A    )
            a_    :     Union[str, Any]										=    torch.tensor(
                [[1_1_8_5_9, 0, 1_6_1_1, 8]]      ,							dtype=torch.long      ,							device=__A    )  # Legal the president is
            a_    :     Optional[Any]										=    [
                1_1_8_5_9,
                0,
                1_6_1_1,
                8,
                5,
                1_5_0,
                2_6_4_4_9,
                2,
                1_9,
                3_4_8,
                4_6_9,
                3,
                2_5_9_5,
                4_8,
                2_0_7_4_0,
                2_4_6_5_3_3,
                2_4_6_5_3_3,
                1_9,
                3_0,
                5,
            ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

            a_    :     int										=    model.generate(__A      ,							do_sample=__A    )
            self.assertListEqual(output_ids[0].tolist()      ,							__A    )







from heapq import heappop, heappush

import numpy as np



def  _a			(						__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase  ,  ):
     a_			,			a_    :     Any										=    grid.shape
     a_    :     Dict										=    [-1, 1, 0, 0]
     a_    :     List[Any]										=    [0, 0, -1, 1]
     if allow_diagonal:
          dx += [-1, -1, 1, 1]
          dy += [-1, 1, -1, 1]

     a_			,			a_    :     int										=    [(0, source)], set()
     a_    :     Optional[Any]										=    np.full((rows, cols)  ,  np.inf )
     a_    :     Tuple										=    0
     a_    :     Optional[Any]										=    np.empty((rows, cols)  ,  dtype=__UpperCamelCase )
     a_    :     Optional[Any]										=    None

     while queue:
          ((a_)			,			(a_))    :     Any										=    heappop(__UpperCamelCase )
          if (x, y) in visited:
               continue
          visited.add((x, y) )

          if (x, y) == destination:
               a_    :     Tuple										=    []
               while (x, y) != source:
                    path.append((x, y) )
                    a_			,			a_    :     Any										=    predecessors[x, y]
               path.append(__UpperCamelCase )  # add the source manually
               path.reverse()
               return matrix[destination], path

          for i in range(len(__UpperCamelCase ) ):
               a_			,			a_    :     Dict										=    x + dx[i], y + dy[i]
               if 0 <= nx < rows and 0 <= ny < cols:
                    a_    :     List[Any]										=    grid[nx][ny]
                    if next_node == 1 and matrix[nx, ny] > dist + 1:
                         heappush(__UpperCamelCase  ,  (dist + 1, (nx, ny)) )
                         a_    :     List[str]										=    dist + 1
                         a_    :     Union[str, Any]										=    (x, y)

     return np.inf, []


if __name__ == "__main__":
       import doctest

       doctest.testmod()



