




import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_url
from PIL import Image

from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
a_     :   List[Any]   		=     logging.get_logger(__name__)




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	str:

    """simple docstring"""


    __UpperCamelCase  		=						DPTConfig(embedding_type='''hybrid'''      )

    if "large" in checkpoint_url:
        __UpperCamelCase  		=						10_24
        __UpperCamelCase  		=						40_96
        __UpperCamelCase  		=						24
        __UpperCamelCase  		=						16
        __UpperCamelCase  		=						[5, 11, 17, 23]
        __UpperCamelCase  		=						[2_56, 5_12, 10_24, 10_24]
        __UpperCamelCase  		=						(1, 3_84, 3_84)

    if "nyu" or "midas" in checkpoint_url:
        __UpperCamelCase  		=						7_68
        __UpperCamelCase  		=						[1, 1, 1, 0.5]
        __UpperCamelCase  		=						[2_56, 5_12, 7_68, 7_68]
        __UpperCamelCase  		=						1_50
        __UpperCamelCase  		=						16
        __UpperCamelCase  		=						(1, 3_84, 3_84)
        __UpperCamelCase  		=						False
        __UpperCamelCase  		=						'project'

    if "ade" in checkpoint_url:
        __UpperCamelCase  		=						True
        __UpperCamelCase  		=						7_68
        __UpperCamelCase  		=						[1, 1, 1, 0.5]
        __UpperCamelCase  		=						1_50
        __UpperCamelCase  		=						16
        __UpperCamelCase  		=						'huggingface/label-files'
        __UpperCamelCase  		=						'ade20k-id2label.json'
        __UpperCamelCase  		=						json.load(open(cached_download(hf_hub_url(__lowercase					,     __lowercase					,     repo_type='''dataset'''      )      )					,     '''r'''      )      )
        __UpperCamelCase  		=						{int(__lowercase      ): v for k, v in idalabel.items()}
        __UpperCamelCase  		=						idalabel
        __UpperCamelCase  		=						{v: k for k, v in idalabel.items()}
        __UpperCamelCase  		=						[1, 1_50, 4_80, 4_80]

    return config, expected_shape




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Tuple:

    """simple docstring"""


    __UpperCamelCase  		=						['pretrained.model.head.weight', 'pretrained.model.head.bias']
    for k in ignore_keys:
        state_dict.pop(__lowercase					,     __lowercase      )




def 					__SCREAMING_SNAKE_CASE   (  lowercase_      )       ->	Union[str, Any]:

    """simple docstring"""


    if (
        "pretrained.model" in name
        and "cls_token" not in name
        and "pos_embed" not in name
        and "patch_embed" not in name
    ):
        __UpperCamelCase  		=						name.replace('''pretrained.model'''					,     '''dpt.encoder'''      )
    if "pretrained.model" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.model'''					,     '''dpt.embeddings'''      )
    if "patch_embed" in name:
        __UpperCamelCase  		=						name.replace('''patch_embed'''					,     ''''''      )
    if "pos_embed" in name:
        __UpperCamelCase  		=						name.replace('''pos_embed'''					,     '''position_embeddings'''      )
    if "attn.proj" in name:
        __UpperCamelCase  		=						name.replace('''attn.proj'''					,     '''attention.output.dense'''      )
    if "proj" in name and "project" not in name:
        __UpperCamelCase  		=						name.replace('''proj'''					,     '''projection'''      )
    if "blocks" in name:
        __UpperCamelCase  		=						name.replace('''blocks'''					,     '''layer'''      )
    if "mlp.fc1" in name:
        __UpperCamelCase  		=						name.replace('''mlp.fc1'''					,     '''intermediate.dense'''      )
    if "mlp.fc2" in name:
        __UpperCamelCase  		=						name.replace('''mlp.fc2'''					,     '''output.dense'''      )
    if "norm1" in name and "backbone" not in name:
        __UpperCamelCase  		=						name.replace('''norm1'''					,     '''layernorm_before'''      )
    if "norm2" in name and "backbone" not in name:
        __UpperCamelCase  		=						name.replace('''norm2'''					,     '''layernorm_after'''      )
    if "scratch.output_conv" in name:
        __UpperCamelCase  		=						name.replace('''scratch.output_conv'''					,     '''head'''      )
    if "scratch" in name:
        __UpperCamelCase  		=						name.replace('''scratch'''					,     '''neck'''      )
    if "layer1_rn" in name:
        __UpperCamelCase  		=						name.replace('''layer1_rn'''					,     '''convs.0'''      )
    if "layer2_rn" in name:
        __UpperCamelCase  		=						name.replace('''layer2_rn'''					,     '''convs.1'''      )
    if "layer3_rn" in name:
        __UpperCamelCase  		=						name.replace('''layer3_rn'''					,     '''convs.2'''      )
    if "layer4_rn" in name:
        __UpperCamelCase  		=						name.replace('''layer4_rn'''					,     '''convs.3'''      )
    if "refinenet" in name:
        __UpperCamelCase  		=						int(name[len('''neck.refinenet'''      ) : len('''neck.refinenet'''      ) + 1]      )
        # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3
        __UpperCamelCase  		=						name.replace(F"refinenet{layer_idx}"					,     F"fusion_stage.layers.{abs(layer_idx-4      )}"      )
    if "out_conv" in name:
        __UpperCamelCase  		=						name.replace('''out_conv'''					,     '''projection'''      )
    if "resConfUnit1" in name:
        __UpperCamelCase  		=						name.replace('''resConfUnit1'''					,     '''residual_layer1'''      )
    if "resConfUnit2" in name:
        __UpperCamelCase  		=						name.replace('''resConfUnit2'''					,     '''residual_layer2'''      )
    if "conv1" in name:
        __UpperCamelCase  		=						name.replace('''conv1'''					,     '''convolution1'''      )
    if "conv2" in name:
        __UpperCamelCase  		=						name.replace('''conv2'''					,     '''convolution2'''      )
    # readout blocks
    if "pretrained.act_postprocess1.0.project.0" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess1.0.project.0'''					,     '''neck.reassemble_stage.readout_projects.0.0'''      )
    if "pretrained.act_postprocess2.0.project.0" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess2.0.project.0'''					,     '''neck.reassemble_stage.readout_projects.1.0'''      )
    if "pretrained.act_postprocess3.0.project.0" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess3.0.project.0'''					,     '''neck.reassemble_stage.readout_projects.2.0'''      )
    if "pretrained.act_postprocess4.0.project.0" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess4.0.project.0'''					,     '''neck.reassemble_stage.readout_projects.3.0'''      )

    # resize blocks
    if "pretrained.act_postprocess1.3" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess1.3'''					,     '''neck.reassemble_stage.layers.0.projection'''      )
    if "pretrained.act_postprocess1.4" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess1.4'''					,     '''neck.reassemble_stage.layers.0.resize'''      )
    if "pretrained.act_postprocess2.3" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess2.3'''					,     '''neck.reassemble_stage.layers.1.projection'''      )
    if "pretrained.act_postprocess2.4" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess2.4'''					,     '''neck.reassemble_stage.layers.1.resize'''      )
    if "pretrained.act_postprocess3.3" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess3.3'''					,     '''neck.reassemble_stage.layers.2.projection'''      )
    if "pretrained.act_postprocess4.3" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess4.3'''					,     '''neck.reassemble_stage.layers.3.projection'''      )
    if "pretrained.act_postprocess4.4" in name:
        __UpperCamelCase  		=						name.replace('''pretrained.act_postprocess4.4'''					,     '''neck.reassemble_stage.layers.3.resize'''      )
    if "pretrained" in name:
        __UpperCamelCase  		=						name.replace('''pretrained'''					,     '''dpt'''      )
    if "bn" in name:
        __UpperCamelCase  		=						name.replace('''bn'''					,     '''batch_norm'''      )
    if "head" in name:
        __UpperCamelCase  		=						name.replace('''head'''					,     '''head.head'''      )
    if "encoder.norm" in name:
        __UpperCamelCase  		=						name.replace('''encoder.norm'''					,     '''layernorm'''      )
    if "auxlayer" in name:
        __UpperCamelCase  		=						name.replace('''auxlayer'''					,     '''auxiliary_head.head'''      )
    if "backbone" in name:
        __UpperCamelCase  		=						name.replace('''backbone'''					,     '''backbone.bit.encoder'''      )

    if ".." in name:
        __UpperCamelCase  		=						name.replace('''..'''					,     '''.'''      )

    if "stem.conv" in name:
        __UpperCamelCase  		=						name.replace('''stem.conv'''					,     '''bit.embedder.convolution'''      )
    if "blocks" in name:
        __UpperCamelCase  		=						name.replace('''blocks'''					,     '''layers'''      )
    if "convolution" in name and "backbone" in name:
        __UpperCamelCase  		=						name.replace('''convolution'''					,     '''conv'''      )
    if "layer" in name and "backbone" in name:
        __UpperCamelCase  		=						name.replace('''layer'''					,     '''layers'''      )
    if "backbone.bit.encoder.bit" in name:
        __UpperCamelCase  		=						name.replace('''backbone.bit.encoder.bit'''					,     '''backbone.bit'''      )
    if "embedder.conv" in name:
        __UpperCamelCase  		=						name.replace('''embedder.conv'''					,     '''embedder.convolution'''      )
    if "backbone.bit.encoder.stem.norm" in name:
        __UpperCamelCase  		=						name.replace('''backbone.bit.encoder.stem.norm'''					,     '''backbone.bit.embedder.norm'''      )
    return name




def 					__SCREAMING_SNAKE_CASE   (  lowercase_					,     lowercase_      )       ->	Any:

    """simple docstring"""


    for i in range(config.num_hidden_layers      ):
        # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
        __UpperCamelCase  		=						state_dict.pop(F"dpt.encoder.layer.{i}.attn.qkv.weight"      )
        __UpperCamelCase  		=						state_dict.pop(F"dpt.encoder.layer.{i}.attn.qkv.bias"      )
        # next, add query, keys and values (in that order) to the state dict
        __UpperCamelCase  		=						in_proj_weight[: config.hidden_size, :]
        __UpperCamelCase  		=						in_proj_bias[: config.hidden_size]
        __UpperCamelCase  		=						in_proj_weight[
            config.hidden_size : config.hidden_size * 2, :
        ]
        __UpperCamelCase  		=						in_proj_bias[
            config.hidden_size : config.hidden_size * 2
        ]
        __UpperCamelCase  		=						in_proj_weight[
            -config.hidden_size :, :
        ]
        __UpperCamelCase  		=						in_proj_bias[-config.hidden_size :]




def 					__SCREAMING_SNAKE_CASE   (  )       ->	Any:

    """simple docstring"""


    __UpperCamelCase  		=						'http://images.cocodataset.org/val2017/000000039769.jpg'
    __UpperCamelCase  		=						Image.open(requests.get(__lowercase					,     stream=__lowercase      ).raw      )
    return im




@torch.no_grad()
def 					__SCREAMING_SNAKE_CASE   (  lowercase_					,     lowercase_					,     lowercase_					,     lowercase_					,     lowercase_      )       ->	Optional[int]:

    """simple docstring"""


    __UpperCamelCase  		=						get_dpt_config(__lowercase      )
    # load original state_dict from URL
    # state_dict = torch.hub.load_state_dict_from_url(checkpoint_url, map_location="cpu")
    __UpperCamelCase  		=						torch.load(__lowercase					,     map_location='''cpu'''      )
    # remove certain keys
    remove_ignore_keys_(__lowercase      )
    # rename keys
    for key in state_dict.copy().keys():
        __UpperCamelCase  		=						state_dict.pop(__lowercase      )
        __UpperCamelCase  		=						val
    # read in qkv matrices
    read_in_q_k_v(__lowercase					,     __lowercase      )

    # load HuggingFace model
    __UpperCamelCase  		=						DPTForSemanticSegmentation(__lowercase      ) if 'ade' in checkpoint_url else DPTForDepthEstimation(__lowercase      )
    model.load_state_dict(__lowercase      )
    model.eval()

    # Check outputs on an image
    __UpperCamelCase  		=						4_80 if 'ade' in checkpoint_url else 3_84
    __UpperCamelCase  		=						DPTImageProcessor(size=__lowercase      )

    __UpperCamelCase  		=						prepare_img()
    __UpperCamelCase  		=						image_processor(__lowercase					,     return_tensors='''pt'''      )

    # forward pass
    __UpperCamelCase  		=						model(**__lowercase      ).logits if 'ade' in checkpoint_url else model(**__lowercase      ).predicted_depth

    if show_prediction:
        __UpperCamelCase  		=						(
            torch.nn.functional.interpolate(
                outputs.unsqueeze(1      )					,     size=(image.size[1], image.size[0])					,     mode='''bicubic'''					,     align_corners=__lowercase					,     )
            .squeeze()
            .cpu()
            .numpy()
        )

        Image.fromarray((prediction / prediction.max()) * 2_55      ).show()

    if pytorch_dump_folder_path is not None:
        Path(__lowercase      ).mkdir(exist_ok=__lowercase      )
        print(F"Saving model to {pytorch_dump_folder_path}"      )
        model.save_pretrained(__lowercase      )
        print(F"Saving image processor to {pytorch_dump_folder_path}"      )
        image_processor.save_pretrained(__lowercase      )

    if push_to_hub:
        model.push_to_hub('''ybelkada/dpt-hybrid-midas'''      )
        image_processor.push_to_hub('''ybelkada/dpt-hybrid-midas'''      )


if __name__ == "__main__":
 a_     :   Optional[int]   		=     argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--checkpoint_url",
     default="https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt",
     type=str,
     help="URL of the original DPT checkpoint you'd like to convert.",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path",
     default=None,
     type=str,
     required=False,
     help="Path to the output PyTorch model directory.",
 )
 parser.add_argument(
     "--push_to_hub",
     action="store_true",
 )
 parser.add_argument(
     "--model_name",
     default="dpt-large",
     type=str,
     help="Name of the model, in case you're pushing to the hub.",
 )
 parser.add_argument(
     "--show_prediction",
     action="store_true",
 )

 a_     :   Optional[int]   		=     parser.parse_args()
 convert_dpt_checkpoint(
     args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name, args.show_prediction
 )












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

      """simple docstring"""

      def __init__(     self					: str ,       snake_case					: Any ,       snake_case					: str=14 ,       snake_case					: Dict=7 ,       snake_case					: Any=True ,       snake_case					: Any=True ,       snake_case					: str=True ,       snake_case					: List[str]=True ,       snake_case					: int=True ,       snake_case					: List[Any]=99 ,       snake_case					: Optional[int]=32 ,       snake_case					: str=5 ,       snake_case					: int=4 ,       snake_case					: str=37 ,       snake_case					: Union[str, Any]="gelu" ,       snake_case					: List[str]=0.1 ,       snake_case					: Optional[int]=0.1 ,       snake_case					: Tuple=512 ,       snake_case					: int=16 ,       snake_case					: Any=2 ,       snake_case					: List[str]=0.02 ,       snake_case					: List[Any]=3 ,       snake_case					: str=4 ,       snake_case					: Tuple=None ,       ):
          __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       snake_case	(     self					: Optional[Any]				):
          __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       snake_case	(     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       snake_case	(     self					: Any ,       snake_case					: Optional[int] ,       snake_case					: Any ,       snake_case					: Union[str, Any] ,       snake_case					: Dict ,       snake_case					: List[Any] ,       *snake_case					: Union[str, Any]				):
          __UpperCamelCase  		=						CTRLModel(config=snake_case				)
          model.to(snake_case				)
          model.eval()

          model(snake_case ,       token_type_ids=snake_case ,       head_mask=snake_case				)
          model(snake_case ,       token_type_ids=snake_case				)
          __UpperCamelCase  		=						model(snake_case				)
          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       snake_case	(     self					: Any ,       snake_case					: Tuple ,       snake_case					: Tuple ,       snake_case					: Union[str, Any] ,       snake_case					: Dict ,       snake_case					: List[str] ,       *snake_case					: Union[str, Any]				):
          __UpperCamelCase  		=						CTRLLMHeadModel(snake_case				)
          model.to(snake_case				)
          model.eval()

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







      def       snake_case	(     self					: Any				):
          __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       snake_case	(     self					: List[Any] ,       snake_case					: int ,       snake_case					: Dict ,       snake_case					: Optional[Any] ,       snake_case					: Any ,       *snake_case					: str				):
          __UpperCamelCase  		=						self.num_labels
          __UpperCamelCase  		=						CTRLForSequenceClassification(snake_case				)
          model.to(snake_case				)
          model.eval()
          __UpperCamelCase  		=						ids_tensor([self.batch_size] ,       self.type_sequence_label_size				)
          __UpperCamelCase  		=						model(snake_case ,       token_type_ids=snake_case ,       labels=snake_case				)
          self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)				)







@require_torch
class  _lowerCamelCase						(			_SCREAMING_SNAKE_CASE							,			_SCREAMING_SNAKE_CASE							,			_SCREAMING_SNAKE_CASE							,			unittest.TestCase   ):

      """simple docstring"""

      lowerCAmelCase__			: int    =					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
      lowerCAmelCase__			: List[str]    =					(CTRLLMHeadModel,) if is_torch_available() else ()
      lowerCAmelCase__			: str    =					(
          {
              "feature-extraction": CTRLModel,
              "text-classification": CTRLForSequenceClassification,
              "text-generation": CTRLLMHeadModel,
              "zero-shot": CTRLForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      lowerCAmelCase__			: Dict    =					True
      lowerCAmelCase__			: List[Any]    =					False
      lowerCAmelCase__			: str    =					False







      def       snake_case	(     self					: str ,       snake_case					: Optional[int] ,       snake_case					: List[str] ,       snake_case					: Tuple ,       snake_case					: Dict ,       snake_case					: List[str]				):
          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       snake_case	(     self					: Optional[int]				):
          __UpperCamelCase  		=						CTRLModelTester(self				)
          __UpperCamelCase  		=						ConfigTester(self ,       config_class=snake_case ,       n_embd=37				)







      def       snake_case	(     self					: Union[str, Any]				):
          super().tearDown()
          # clean-up as much as possible GPU memory occupied by PyTorch
          gc.collect()
          torch.cuda.empty_cache()







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







      def       snake_case	(     self					: Optional[int]				):
          __UpperCamelCase  		=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_model(*snake_case				)







      def       snake_case	(     self					: Tuple				):
          __UpperCamelCase  		=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*snake_case				)







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







      @slow
      def       snake_case	(     self					: Optional[Any]				):
          for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __UpperCamelCase  		=						CTRLModel.from_pretrained(snake_case				)
              self.assertIsNotNone(snake_case				)







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







@require_torch
class  _lowerCamelCase						(			unittest.TestCase   ):

      """simple docstring"""

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







      @slow
      def       snake_case	(     self					: Optional[int]				):
          __UpperCamelCase  		=						CTRLLMHeadModel.from_pretrained('''ctrl'''				)
          model.to(snake_case				)
          __UpperCamelCase  		=						torch.tensor(
              [[11859, 0, 1611, 8]] ,       dtype=torch.long ,       device=snake_case				)  # Legal the president is
          __UpperCamelCase  		=						[
              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

          __UpperCamelCase  		=						model.generate(snake_case ,       do_sample=snake_case				)
          self.assertListEqual(output_ids[0].tolist() ,       snake_case				)






