'''simple docstring'''

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       UpperCAmelCase  :





       '''simple docstring'''



       def __init__(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase=14     ,       __lowerCAmelCase=7     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=99     ,       __lowerCAmelCase=32     ,       __lowerCAmelCase=5     ,       __lowerCAmelCase=4     ,       __lowerCAmelCase=37     ,       __lowerCAmelCase="gelu"     ,       __lowerCAmelCase=0.1     ,       __lowerCAmelCase=0.1     ,       __lowerCAmelCase=512     ,       __lowerCAmelCase=16     ,       __lowerCAmelCase=2     ,       __lowerCAmelCase=0.0_2     ,       __lowerCAmelCase=3     ,       __lowerCAmelCase=4     ,       __lowerCAmelCase=None     ,       )  ->  Optional[int]:
        lowercase__    :   List[Any]						= parent
        lowercase__    :   Union[str, Any]						= batch_size
        lowercase__    :   List[Any]						= seq_length
        lowercase__    :   Optional[Any]						= is_training
        lowercase__    :   Union[str, Any]						= use_token_type_ids
        lowercase__    :   str						= use_input_mask
        lowercase__    :   Any						= use_labels
        lowercase__    :   Optional[Any]						= use_mc_token_ids
        lowercase__    :   List[Any]						= vocab_size
        lowercase__    :   Tuple						= hidden_size
        lowercase__    :   Any						= num_hidden_layers
        lowercase__    :   Optional[Any]						= num_attention_heads
        lowercase__    :   str						= intermediate_size
        lowercase__    :   Optional[int]						= hidden_act
        lowercase__    :   Optional[Any]						= hidden_dropout_prob
        lowercase__    :   Union[str, Any]						= attention_probs_dropout_prob
        lowercase__    :   Tuple						= max_position_embeddings
        lowercase__    :   List[str]						= type_vocab_size
        lowercase__    :   Dict						= type_sequence_label_size
        lowercase__    :   List[Any]						= initializer_range
        lowercase__    :   Any						= num_labels
        lowercase__    :   Optional[Any]						= num_choices
        lowercase__    :   Tuple						= scope
        lowercase__    :   Dict						= self.vocab_size - 1

       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        lowercase__    :   Dict						= ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size			)

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

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

        lowercase__    :   Union[str, Any]						= None
        if self.use_mc_token_ids:
         lowercase__    :   List[str]						= ids_tensor([self.batch_size, self.num_choices]     ,       self.seq_length			)

        lowercase__    :   Tuple						= None
        lowercase__    :   str						= None
        lowercase__    :   int						= None
        if self.use_labels:
         lowercase__    :   Any						= ids_tensor([self.batch_size]     ,       self.type_sequence_label_size			)
         lowercase__    :   Optional[int]						= ids_tensor([self.batch_size, self.seq_length]     ,       self.num_labels			)
         lowercase__    :   List[Any]						= ids_tensor([self.batch_size]     ,       self.num_choices			)

        lowercase__    :   List[str]						= self.get_config()

        lowercase__    :   List[str]						= 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			)  ->  List[Any]:
        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     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       *__lowerCAmelCase			)  ->  Tuple:
        lowercase__    :   List[str]						= CTRLModel(config=__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()

        model(__lowerCAmelCase     ,       token_type_ids=__lowerCAmelCase     ,       head_mask=__lowerCAmelCase			)
        model(__lowerCAmelCase     ,       token_type_ids=__lowerCAmelCase			)
        lowercase__    :   str						= 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     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       *__lowerCAmelCase			)  ->  Dict:
        lowercase__    :   Union[str, Any]						= CTRLLMHeadModel(__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()

        lowercase__    :   List[str]						= 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			)  ->  str:
        lowercase__    :   Any						= self.prepare_config_and_inputs()

        (
            (
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							(
            lowercase__
        )			,							
        )    :   Dict						= config_and_inputs

        lowercase__    :   str						= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

        return config, inputs_dict


       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       *__lowerCAmelCase			)  ->  Dict:
        lowercase__    :   str						= self.num_labels
        lowercase__    :   str						= CTRLForSequenceClassification(__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()
        lowercase__    :   Optional[int]						= ids_tensor([self.batch_size]     ,       self.type_sequence_label_size			)
        lowercase__    :   Tuple						= model(__lowerCAmelCase     ,       token_type_ids=__lowerCAmelCase     ,       labels=__lowerCAmelCase			)
        self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)			)



@require_torch
class       UpperCAmelCase  (						a__   ,						a__   ,						a__   ,						unittest.TestCase ):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE 		=   (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE 		=   (CTRLLMHeadModel,) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE 		=   (
           {
               "feature-extraction": CTRLModel,
               "text-classification": CTRLForSequenceClassification,
               "text-generation": CTRLLMHeadModel,
               "zero-shot": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       SCREAMING_SNAKE_CASE 		=   True
       SCREAMING_SNAKE_CASE 		=   False
       SCREAMING_SNAKE_CASE 		=   False

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  int:
        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			)  ->  int:
        lowercase__    :   int						= CTRLModelTester(self			)
        lowercase__    :   List[str]						= ConfigTester(self     ,       config_class=__lowerCAmelCase     ,       n_embd=37			)

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

       def _lowerCAmelCase(						self			)  ->  Tuple:
        self.config_tester.run_common_tests()

       def _lowerCAmelCase(						self			)  ->  Optional[int]:
        lowercase__    :   Dict						= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*__lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  List[str]:
        lowercase__    :   str						= 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			)  ->  Tuple:
        pass

       @slow
       def _lowerCAmelCase(						self			)  ->  Any:
        for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowercase__    :   Tuple						= 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			)  ->  Union[str, Any]:
        pass



@require_torch
class       UpperCAmelCase  (						unittest.TestCase ):





       '''simple docstring'''



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


       @slow
       def _lowerCAmelCase(						self			)  ->  Optional[int]:
        lowercase__    :   List[Any]						= CTRLLMHeadModel.from_pretrained('''ctrl'''			)
        model.to(__lowerCAmelCase			)
        lowercase__    :   int						= torch.tensor(
            [[11859, 0, 1611, 8]]     ,       dtype=torch.long     ,       device=__lowerCAmelCase			)  # Legal the president is
        lowercase__    :   Dict						= [
            11859,
            0,
            1611,
            8,
            5,
            150,
            26449,
            2,
            19,
            348,
            469,
            3,
            2595,
            48,
            20740,
            246533,
            246533,
            19,
            30,
            5,
        ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

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


'''simple docstring'''

from manim import *



class       UpperCAmelCase  (						a__ ):





       '''simple docstring'''



       def _lowerCAmelCase(						self			)  ->  List[Any]:
        lowercase__    :   int						= Rectangle(height=0.5     ,       width=0.5			)
        lowercase__    :   Optional[int]						= Rectangle(height=0.2_5     ,       width=0.2_5			)
        lowercase__    :   Tuple						= Rectangle(height=0.4_6     ,       width=0.4_6			).set_stroke(width=0			)

        lowercase__    :   str						= [mem.copy() for i in range(6			)]
        lowercase__    :   Dict						= [mem.copy() for i in range(6			)]
        lowercase__    :   Tuple						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   List[str]						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   str						= VGroup(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   str						= Text('''CPU'''     ,       font_size=24			)
        lowercase__    :   List[Any]						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        cpu.move_to([-2.5, -0.5, 0]			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   Any						= [mem.copy() for i in range(4			)]
        lowercase__    :   int						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Optional[int]						= Text('''GPU'''     ,       font_size=24			)
        lowercase__    :   Tuple						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        gpu.move_to([-1, -1, 0]			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   int						= [mem.copy() for i in range(6			)]
        lowercase__    :   Optional[int]						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)

        lowercase__    :   Any						= Text('''Model'''     ,       font_size=24			)
        lowercase__    :   Optional[Any]						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        model.move_to([3, -1.0, 0]			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   int						= []
        lowercase__    :   int						= []
        lowercase__    :   Any						= []

        for i, rect in enumerate(__lowerCAmelCase			):
         rect.set_stroke(__lowerCAmelCase			)

         lowercase__    :   Optional[int]						= Rectangle(height=0.4_6 / 4     ,       width=0.4_6 / 3			).set_stroke(width=0.0			).set_fill(__lowerCAmelCase     ,       opacity=0.7			)

         if i == 0:
          cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT			)     ,       buff=0.0_2     ,       direction=__lowerCAmelCase			)
          cpu_target.set_x(cpu_target.get_x() + 0.1			)
         elif i == 3:
          cpu_target.next_to(model_cpu_arr[0]     ,       direction=__lowerCAmelCase     ,       buff=0.0			)
         else:
          cpu_target.next_to(model_cpu_arr[i - 1]     ,       direction=__lowerCAmelCase     ,       buff=0.0			)
         self.add(__lowerCAmelCase			)
         model_cpu_arr.append(__lowerCAmelCase			)

        self.add(*__lowerCAmelCase     ,       *__lowerCAmelCase     ,       *__lowerCAmelCase			)

        lowercase__    :   Optional[int]						= [mem.copy() for i in range(6			)]
        lowercase__    :   List[Any]						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)

        lowercase__    :   Optional[Any]						= Text('''Loaded Checkpoint'''     ,       font_size=24			)
        lowercase__    :   Dict						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        checkpoint.move_to([3, 0.5, 0]			)

        self.add(__lowerCAmelCase			)

        lowercase__    :   str						= []
        lowercase__    :   List[str]						= []

        for i, rect in enumerate(__lowerCAmelCase			):
         lowercase__    :   List[str]						= fill.copy().set_fill(__lowerCAmelCase     ,       opacity=0.7			)
         target.move_to(__lowerCAmelCase			)
         ckpt_arr.append(__lowerCAmelCase			)

         lowercase__    :   Any						= target.copy()
         if i < 5:
          cpu_target.move_to(cpu_left_col_base[i + 1]			)
         else:
          cpu_target.move_to(cpu_right_col_base[i - 5]			)
         ckpt_cpu_arr.append(__lowerCAmelCase			)
        self.add(*__lowerCAmelCase     ,       *__lowerCAmelCase			)

        lowercase__    :   Optional[int]						= Square(side_length=2.2			)
        key.move_to([-5, 2, 0]			)

        lowercase__    :   Optional[Any]						= MarkupText(
            F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""     ,       font_size=18     ,       )

        key_text.move_to([-5, 2.4, 0]			)

        self.add(__lowerCAmelCase     ,       __lowerCAmelCase			)

        lowercase__    :   str						= MarkupText(
            F"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""     ,       font_size=18     ,       )

        blue_text.next_to(__lowerCAmelCase     ,       DOWN * 2.4     ,       aligned_edge=key_text.get_left()			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   Union[str, Any]						= MarkupText(
            F"""Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device."""     ,       font_size=24     ,       )
        step_a.move_to([2, 2, 0]			)

        lowercase__    :   Tuple						= [meta_mem.copy() for i in range(6			)]
        lowercase__    :   Any						= [meta_mem.copy() for i in range(6			)]
        lowercase__    :   str						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Any						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Union[str, Any]						= VGroup(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Union[str, Any]						= Text('''Disk'''     ,       font_size=24			)
        lowercase__    :   Tuple						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        disk.move_to([-4.0, -1.2_5, 0]			)
        self.play(Write(__lowerCAmelCase     ,       run_time=3			)     ,       Write(__lowerCAmelCase     ,       run_time=1			)     ,       Create(__lowerCAmelCase     ,       run_time=1			)			)

        lowercase__    :   Tuple						= []
        for i, rect in enumerate(__lowerCAmelCase			):
         lowercase__    :   Dict						= rect.copy()
         target.generate_target()
         target.target.move_to(disk_left_col_base[i]			).scale(0.5			)
         animations.append(MoveToTarget(__lowerCAmelCase     ,       run_time=1.5			)			)
        self.play(*__lowerCAmelCase			)

        self.play(FadeOut(__lowerCAmelCase			)			)

        lowercase__    :   Dict						= MarkupText(F"""Then, the checkpoint is removed from memory\nthrough garbage collection."""     ,       font_size=24			)
        step_a.move_to([2, 2, 0]			)

        self.play(Write(__lowerCAmelCase     ,       run_time=3			)			)

        self.play(
            FadeOut(__lowerCAmelCase     ,       __lowerCAmelCase     ,       *__lowerCAmelCase     ,       *__lowerCAmelCase			)     ,       )

        self.wait()

