




'''simple docstring'''



import unittest

from transformers import 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
          OpenAIGPTConfig,
          OpenAIGPTDoubleHeadsModel,
          OpenAIGPTForSequenceClassification,
          OpenAIGPTLMHeadModel,
          OpenAIGPTModel,
      )






class 							_a				:





       '''simple docstring'''






       def __init__(      self,							A,							A=13,							A=7,							A=True,							A=True,							A=True,							A=99,							A=32,							A=5,							A=4,							A=37,							A="gelu",							A=0.1,							A=0.1,							A=512,							A=16,							A=2,							A=0.02,							A=3,							A=4,							A=None,							):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						parent
             SCREAMING_SNAKE_CASE   :      str    					=						batch_size
             SCREAMING_SNAKE_CASE   :      Dict    					=						seq_length
             SCREAMING_SNAKE_CASE   :      Tuple    					=						is_training
             SCREAMING_SNAKE_CASE   :      Dict    					=						use_token_type_ids
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						use_labels
             SCREAMING_SNAKE_CASE   :      Any    					=						vocab_size
             SCREAMING_SNAKE_CASE   :      List[str]    					=						hidden_size
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						num_hidden_layers
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						num_attention_heads
             SCREAMING_SNAKE_CASE   :      Tuple    					=						intermediate_size
             SCREAMING_SNAKE_CASE   :      Any    					=						hidden_act
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						hidden_dropout_prob
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						attention_probs_dropout_prob
             SCREAMING_SNAKE_CASE   :      Tuple    					=						max_position_embeddings
             SCREAMING_SNAKE_CASE   :      Dict    					=						type_vocab_size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						type_sequence_label_size
             SCREAMING_SNAKE_CASE   :      Any    					=						initializer_range
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						num_labels
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						num_choices
             SCREAMING_SNAKE_CASE   :      Dict    					=						scope
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.vocab_size - 1







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						ids_tensor([self.batch_size, self.seq_length],							self.vocab_size  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						None
             if self.use_token_type_ids:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						ids_tensor([self.batch_size, self.seq_length],							self.type_vocab_size  )

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

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						OpenAIGPTConfig(
                 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,							)

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						ids_tensor([self.num_hidden_layers, self.num_attention_heads],							2  )

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







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							*A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						OpenAIGPTModel(config=A  )
             model.to(A  )
             model.eval()

             SCREAMING_SNAKE_CASE   :      str    					=						model(A,							token_type_ids=A,							head_mask=A  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						model(A,							token_type_ids=A  )
             SCREAMING_SNAKE_CASE   :      str    					=						model(A  )

             self.parent.assertEqual(result.last_hidden_state.shape,							(self.batch_size, self.seq_length, self.hidden_size)  )







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							*A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						OpenAIGPTLMHeadModel(A  )
             model.to(A  )
             model.eval()

             SCREAMING_SNAKE_CASE   :      Union[str, 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,							A,							A,							A,							A,							*A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						OpenAIGPTDoubleHeadsModel(A  )
             model.to(A  )
             model.eval()

             SCREAMING_SNAKE_CASE   :      int    					=						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,							A,							A,							A,							A,							*A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						self.num_labels
             SCREAMING_SNAKE_CASE   :      int    					=						OpenAIGPTForSequenceClassification(A  )
             model.to(A  )
             model.eval()

             SCREAMING_SNAKE_CASE   :      Tuple    					=						ids_tensor([self.batch_size],							self.type_sequence_label_size  )
             SCREAMING_SNAKE_CASE   :      Tuple    					=						model(A,							token_type_ids=A,							labels=A  )
             self.parent.assertEqual(result.logits.shape,							(self.batch_size, self.num_labels)  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self.prepare_config_and_inputs()
             (
                 (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    (
                 SCREAMING_SNAKE_CASE
             )					,    
             )   :      Optional[Any]    					=						config_and_inputs
             SCREAMING_SNAKE_CASE   :      Dict    					=						{
                 'input_ids': input_ids,
                 'token_type_ids': token_type_ids,
                 'head_mask': head_mask,
             }

             return config, inputs_dict






@require_torch
class 							_a				(			SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      unittest.TestCase ):





       '''simple docstring'''






       A						:				Tuple        =						(
           (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
           if is_torch_available()
           else ()
       )
       A						:				Dict        =						(
           (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       A						:				Tuple        =						(
           {
               '''feature-extraction''': OpenAIGPTModel,
               '''text-classification''': OpenAIGPTForSequenceClassification,
               '''text-generation''': OpenAIGPTLMHeadModel,
               '''zero-shot''': OpenAIGPTForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							A  ):







             '''simple docstring'''







             if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
                   # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
                   # `OpenAIGPTConfig` 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,							A,							A,							A=False  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						super()._prepare_for_class(A,							A,							return_labels=A  )

             if return_labels:
                   if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                         SCREAMING_SNAKE_CASE   :      List[Any]    					=						torch.zeros(
                             (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length),							dtype=torch.long,							device=A,							)
                         SCREAMING_SNAKE_CASE   :      List[Any]    					=						inputs_dict['labels']
                         SCREAMING_SNAKE_CASE   :      List[str]    					=						inputs_dict['labels']
                         SCREAMING_SNAKE_CASE   :      int    					=						torch.zeros(
                             (self.model_tester.batch_size, self.model_tester.num_choices),							dtype=torch.long,							device=A,							)
                         SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.zeros(
                             self.model_tester.batch_size,							dtype=torch.long,							device=A  )
             return inputs_dict







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						OpenAIGPTModelTester(self  )
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						ConfigTester(self,							config_class=A,							n_embd=37  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             self.config_tester.run_common_tests()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_model(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_lm_head_model(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_double_lm_head_model(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						OpenAIGPTModel.from_pretrained(A  )
                   self.assertIsNotNone(A  )






@require_torch
class 							_a				(			unittest.TestCase ):





       '''simple docstring'''






       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'  )
             model.to(A  )
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.tensor([[481, 4_735, 544]],							dtype=torch.long,							device=A  )  # the president is
             SCREAMING_SNAKE_CASE   :      Dict    					=						[
                 481,
                 4_735,
                 544,
                 246,
                 963,
                 870,
                 762,
                 239,
                 244,
                 40_477,
                 244,
                 249,
                 719,
                 881,
                 487,
                 544,
                 240,
                 244,
                 603,
                 481,
             ]  # the president is a very good man. " \n " i\'m sure he is, " said the

             SCREAMING_SNAKE_CASE   :      Dict    					=						model.generate(A,							do_sample=A  )
             self.assertListEqual(output_ids[0].tolist(),							A  )








'''simple docstring'''



import os
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import torch
from torch import nn

from ...models.controlnet import ControlNetModel, ControlNetOutput
from ...models.modeling_utils import ModelMixin
from ...utils import logging


UpperCamelCase_    	=				logging.get_logger(__name__)






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       def __init__(      self,							A  ):







             '''simple docstring'''







             super().__init__()
             SCREAMING_SNAKE_CASE   :      Dict    					=						nn.ModuleList(A  )







       def        UpperCamelCase_							(      self,							A,							A,							A,							A,							A,							A = None,							A = None,							A = None,							A = None,							A = False,							A = True,							):







             '''simple docstring'''







             for i, (image, scale, controlnet) in enumerate(zip(A,							A,							self.nets  )  ):
                   SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						controlnet(
                       A,							A,							A,							A,							A,							A,							A,							A,							A,							A,							A,							)

                   # merge samples
                   if i == 0:
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      int    					=						down_samples, mid_sample
                   else:
                         SCREAMING_SNAKE_CASE   :      str    					=						[
                             samples_prev + samples_curr
                             for samples_prev, samples_curr in zip(A,							A  )
                         ]
                         mid_block_res_sample += mid_sample

             return down_block_res_samples, mid_block_res_sample







       def        UpperCamelCase_							(      self,							A,							A = True,							A = None,							A = False,							A = None,							):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						0
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						save_directory
             for controlnet in self.nets:
                   controlnet.save_pretrained(
                       A,							is_main_process=A,							save_function=A,							safe_serialization=A,							variant=A,							)

                   idx += 1
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						model_path_to_save + F"_{idx}"







       @classmethod
       def        UpperCamelCase_							(      cls,							A,							**A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						0
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						[]

             # load controlnet and append to list until no controlnet directory exists anymore
             # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained`
             # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ...
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						pretrained_model_path
             while os.path.isdir(A  ):
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						ControlNetModel.from_pretrained(A,							**A  )
                   controlnets.append(A  )

                   idx += 1
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						pretrained_model_path + F"_{idx}"

             logger.info(F"{len(A  )} controlnets loaded from {pretrained_model_path}."  )

             if len(A  ) == 0:
                   raise ValueError(
                       F"No ControlNets found under {os.path.dirname(A  )}. Expected at least {pretrained_model_path + '_0'}."  )

             return cls(A  )


