




"""simple docstring"""



import gc
import random
import unittest

import numpy as np
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import (
    DiffusionPipeline,
    UnCLIPImageVariationPipeline,
    UnCLIPScheduler,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel
from diffusers.utils import floats_tensor, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps

from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()




class      UpperCAmelCase_  (						A_,       unittest.TestCase							):
    lowercase__						 =				UnCLIPImageVariationPipeline
    lowercase__						 =				IMAGE_VARIATION_PARAMS - {'''height''', '''width''', '''guidance_scale'''}
    lowercase__						 =				IMAGE_VARIATION_BATCH_PARAMS

    lowercase__						 =				[
        '''generator''',
        '''return_dict''',
        '''decoder_num_inference_steps''',
        '''super_res_num_inference_steps''',
    ]
    lowercase__						 =				False




    @property
    def 							__magic_name__							(   self						:	Dict				)				->							List[Any]:




     '''simple docstring'''

     return 32




    @property
    def 							__magic_name__							(   self						:	Optional[Any]				)				->							int:




     '''simple docstring'''

     return 32




    @property
    def 							__magic_name__							(   self						:	Dict				)				->							List[Any]:




     '''simple docstring'''

     return self.time_input_dim




    @property
    def 							__magic_name__							(   self						:	str				)				->							Dict:




     '''simple docstring'''

     return self.time_input_dim * 4




    @property
    def 							__magic_name__							(   self						:	List[Any]				)				->							Union[str, Any]:




     '''simple docstring'''

     return 100




    @property
    def 							__magic_name__							(   self						:	Tuple				)				->							Any:




     '''simple docstring'''

     A__	=    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"				)
     return tokenizer




    @property
    def 							__magic_name__							(   self						:	str				)				->							Tuple:




     '''simple docstring'''

     torch.manual_seed(0				)
     A__	=    CLIPTextConfig(
         bos_token_id=0     ,			eos_token_id=2     ,			hidden_size=self.text_embedder_hidden_size     ,			projection_dim=self.text_embedder_hidden_size     ,			intermediate_size=37     ,			layer_norm_eps=1e-05     ,			num_attention_heads=4     ,			num_hidden_layers=5     ,			pad_token_id=1     ,			vocab_size=1_000     ,			)
     return CLIPTextModelWithProjection(snake_case_				)




    @property
    def 							__magic_name__							(   self						:	int				)				->							Tuple:




     '''simple docstring'''

     torch.manual_seed(0				)
     A__	=    CLIPVisionConfig(
         hidden_size=self.text_embedder_hidden_size     ,			projection_dim=self.text_embedder_hidden_size     ,			num_hidden_layers=5     ,			num_attention_heads=4     ,			image_size=32     ,			intermediate_size=37     ,			patch_size=1     ,			)
     return CLIPVisionModelWithProjection(snake_case_				)




    @property
    def 							__magic_name__							(   self						:	List[str]				)				->							Union[str, Any]:




     '''simple docstring'''

     torch.manual_seed(0				)

     A__	=    {
         "clip_embeddings_dim": self.text_embedder_hidden_size,
         "time_embed_dim": self.time_embed_dim,
         "cross_attention_dim": self.cross_attention_dim,
     }

     A__	=    UnCLIPTextProjModel(**snake_case_				)
     return model




    @property
    def 							__magic_name__							(   self						:	Dict				)				->							Optional[Any]:




     '''simple docstring'''

     torch.manual_seed(0				)

     A__	=    {
         "sample_size": 32,
         # RGB in channels
         "in_channels": 3,
         # Out channels is double in channels because predicts mean and variance
         "out_channels": 6,
         "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"),
         "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"),
         "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
         "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
         "layers_per_block": 1,
         "cross_attention_dim": self.cross_attention_dim,
         "attention_head_dim": 4,
         "resnet_time_scale_shift": "scale_shift",
         "class_embed_type": "identity",
     }

     A__	=    UNetaDConditionModel(**snake_case_				)
     return model




    @property
    def 							__magic_name__							(   self						:	Optional[int]				)				->							Union[str, Any]:




     '''simple docstring'''

     return {
         "sample_size": 64,
         "layers_per_block": 1,
         "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"),
         "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"),
         "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
         "in_channels": 6,
         "out_channels": 3,
     }




    @property
    def 							__magic_name__							(   self						:	Optional[int]				)				->							Tuple:




     '''simple docstring'''

     torch.manual_seed(0				)

     A__	=    UNetaDModel(**self.dummy_super_res_kwargs				)
     return model




    @property
    def 							__magic_name__							(   self						:	Tuple				)				->							List[Any]:




     '''simple docstring'''

     torch.manual_seed(1				)

     A__	=    UNetaDModel(**self.dummy_super_res_kwargs				)
     return model




    def 							__magic_name__							(   self						:	str				)				->							str:




     '''simple docstring'''

     A__	=    self.dummy_decoder
     A__	=    self.dummy_text_proj
     A__	=    self.dummy_text_encoder
     A__	=    self.dummy_tokenizer
     A__	=    self.dummy_super_res_first
     A__	=    self.dummy_super_res_last

     A__	=    UnCLIPScheduler(
         variance_type="learned_range"     ,			prediction_type="epsilon"     ,			num_train_timesteps=1_000     ,			)

     A__	=    UnCLIPScheduler(
         variance_type="fixed_small_log"     ,			prediction_type="epsilon"     ,			num_train_timesteps=1_000     ,			)

     A__	=    CLIPImageProcessor(crop_size=32     ,			size=32				)

     A__	=    self.dummy_image_encoder

     return {
         "decoder": decoder,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
         "text_proj": text_proj,
         "feature_extractor": feature_extractor,
         "image_encoder": image_encoder,
         "super_res_first": super_res_first,
         "super_res_last": super_res_last,
         "decoder_scheduler": decoder_scheduler,
         "super_res_scheduler": super_res_scheduler,
     }




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	str     ,			snake_case_						:	Optional[int]=0     ,			snake_case_						:	Any=True				)				->							Dict:




     '''simple docstring'''

     A__	=    floats_tensor((1, 3, 32, 32)     ,			rng=random.Random(snake_case_				)				).to(snake_case_				)
     if str(snake_case_				).startswith("mps"				):
      A__	=    torch.manual_seed(snake_case_				)
     else:
      A__	=    torch.Generator(device=snake_case_				).manual_seed(snake_case_				)

     if pil_image:
      A__	=    input_image * 0.5 + 0.5
      A__	=    input_image.clamp(0     ,			1				)
      A__	=    input_image.cpu().permute(0     ,			2     ,			3     ,			1				).float().numpy()
      A__	=    DiffusionPipeline.numpy_to_pil(snake_case_				)[0]

     return {
         "image": input_image,
         "generator": generator,
         "decoder_num_inference_steps": 2,
         "super_res_num_inference_steps": 2,
         "output_type": "np",
     }




    def 							__magic_name__							(   self						:	Tuple				)				->							List[Any]:




     '''simple docstring'''

     A__	=    "cpu"

     A__	=    self.get_dummy_components()

     A__	=    self.pipeline_class(**snake_case_				)
     A__	=    pipe.to(snake_case_				)

     pipe.set_progress_bar_config(disable=snake_case_				)

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)

     A__	=    pipe(**snake_case_				)
     A__	=    output.images

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)

     A__	=    pipe(
         **snake_case_     ,			return_dict=snake_case_     ,			)[0]

     A__	=    image[0, -3:, -3:, -1]
     A__	=    image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)

     A__	=    np.array(
         [
             0.9997,
             0.0002,
             0.9997,
             0.9997,
             0.9969,
             0.0023,
             0.9997,
             0.9969,
             0.9970,
         ]				)

     assert np.abs(image_slice.flatten() - expected_slice				).max() < 1e-2
     assert np.abs(image_from_tuple_slice.flatten() - expected_slice				).max() < 1e-2




    def 							__magic_name__							(   self						:	Optional[int]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    "cpu"

     A__	=    self.get_dummy_components()

     A__	=    self.pipeline_class(**snake_case_				)
     A__	=    pipe.to(snake_case_				)

     pipe.set_progress_bar_config(disable=snake_case_				)

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)

     A__	=    pipe(**snake_case_				)
     A__	=    output.images

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)

     A__	=    pipe(
         **snake_case_     ,			return_dict=snake_case_     ,			)[0]

     A__	=    image[0, -3:, -3:, -1]
     A__	=    image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)

     A__	=    np.array([0.9997, 0.0003, 0.9997, 0.9997, 0.9970, 0.0024, 0.9997, 0.9971, 0.9971]				)

     assert np.abs(image_slice.flatten() - expected_slice				).max() < 1e-2
     assert np.abs(image_from_tuple_slice.flatten() - expected_slice				).max() < 1e-2




    def 							__magic_name__							(   self						:	Tuple				)				->							List[Any]:




     '''simple docstring'''

     A__	=    "cpu"

     A__	=    self.get_dummy_components()

     A__	=    self.pipeline_class(**snake_case_				)
     A__	=    pipe.to(snake_case_				)

     pipe.set_progress_bar_config(disable=snake_case_				)

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)
     A__	=    [
         pipeline_inputs["image"],
         pipeline_inputs["image"],
     ]

     A__	=    pipe(**snake_case_				)
     A__	=    output.images

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)
     A__	=    [
         tuple_pipeline_inputs["image"],
         tuple_pipeline_inputs["image"],
     ]

     A__	=    pipe(
         **snake_case_     ,			return_dict=snake_case_     ,			)[0]

     A__	=    image[0, -3:, -3:, -1]
     A__	=    image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (2, 64, 64, 3)

     A__	=    np.array(
         [
             0.9997,
             0.9989,
             0.0008,
             0.0021,
             0.9960,
             0.0018,
             0.0014,
             0.0002,
             0.9933,
         ]				)

     assert np.abs(image_slice.flatten() - expected_slice				).max() < 1e-2
     assert np.abs(image_from_tuple_slice.flatten() - expected_slice				).max() < 1e-2




    def 							__magic_name__							(   self						:	Any				)				->							str:




     '''simple docstring'''

     A__	=    torch.device("cpu"				)




     class      UpperCAmelCase_  :
         lowercase__						 =				1






     A__	=    self.get_dummy_components()

     A__	=    self.pipeline_class(**snake_case_				)
     A__	=    pipe.to(snake_case_				)

     pipe.set_progress_bar_config(disable=snake_case_				)

     A__	=    torch.Generator(device=snake_case_				).manual_seed(0				)
     A__	=    pipe.decoder.dtype
     A__	=    1

     A__	=    (
         batch_size,
         pipe.decoder.config.in_channels,
         pipe.decoder.config.sample_size,
         pipe.decoder.config.sample_size,
     )
     A__	=    pipe.prepare_latents(
         snake_case_     ,			dtype=snake_case_     ,			device=snake_case_     ,			generator=snake_case_     ,			latents=snake_case_     ,			scheduler=DummyScheduler()				)

     A__	=    (
         batch_size,
         pipe.super_res_first.config.in_channels // 2,
         pipe.super_res_first.config.sample_size,
         pipe.super_res_first.config.sample_size,
     )
     A__	=    pipe.prepare_latents(
         snake_case_     ,			dtype=snake_case_     ,			device=snake_case_     ,			generator=snake_case_     ,			latents=snake_case_     ,			scheduler=DummyScheduler()				)

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)

     A__	=    pipe(
         **snake_case_     ,			decoder_latents=snake_case_     ,			super_res_latents=snake_case_				).images

     A__	=    self.get_dummy_inputs(snake_case_     ,			pil_image=snake_case_				)
     # Don't pass image, instead pass embedding
     A__	=    pipeline_inputs.pop("image"				)
     A__	=    pipe.image_encoder(snake_case_				).image_embeds

     A__	=    pipe(
         **snake_case_     ,			decoder_latents=snake_case_     ,			super_res_latents=snake_case_     ,			image_embeddings=snake_case_     ,			).images

     # make sure passing text embeddings manually is identical
     assert np.abs(img_out_a - img_out_a				).max() < 1e-4




    @skip_mps
    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    torch_device == "cpu"

     # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor
     A__	=    1e-2

     self._test_attention_slicing_forward_pass(
         test_max_difference=snake_case_     ,			expected_max_diff=snake_case_				)




    @skip_mps
    def 							__magic_name__							(   self						:	List[Any]				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    torch_device == "cpu"
     A__	=    True
     A__	=    [
         "decoder_num_inference_steps",
         "super_res_num_inference_steps",
     ]

     self._test_inference_batch_single_identical(
         test_max_difference=snake_case_     ,			relax_max_difference=snake_case_     ,			additional_params_copy_to_batched_inputs=snake_case_     ,			)




    def 							__magic_name__							(   self						:	List[str]				)				->							Tuple:




     '''simple docstring'''

     A__	=    [
         "decoder_num_inference_steps",
         "super_res_num_inference_steps",
     ]

     if torch_device == "mps":
      # TODO: MPS errors with larger batch sizes
      A__	=    [2, 3]
      self._test_inference_batch_consistent(
          batch_sizes=snake_case_     ,			additional_params_copy_to_batched_inputs=snake_case_     ,			)
     else:
      self._test_inference_batch_consistent(
          additional_params_copy_to_batched_inputs=snake_case_				)




    @skip_mps
    def 							__magic_name__							(   self						:	Tuple				)				->							Any:




     '''simple docstring'''

     return super().test_dict_tuple_outputs_equivalent()




    @skip_mps
    def 							__magic_name__							(   self						:	str				)				->							List[Any]:




     '''simple docstring'''

     return super().test_save_load_local()




    @skip_mps
    def 							__magic_name__							(   self						:	Any				)				->							int:




     '''simple docstring'''

     return super().test_save_load_optional_components()






@slow
@require_torch_gpu
class      UpperCAmelCase_  (						unittest.TestCase							):




    def 							__magic_name__							(   self						:	Optional[int]				)				->							List[Any]:




     '''simple docstring'''

     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()




    def 							__magic_name__							(   self						:	str				)				->							List[str]:




     '''simple docstring'''

     A__	=    load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png"				)
     A__	=    load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
         "/unclip/karlo_v1_alpha_cat_variation_fp16.npy"				)

     A__	=    UnCLIPImageVariationPipeline.from_pretrained(
         "kakaobrain/karlo-v1-alpha-image-variations"     ,			torch_dtype=torch.floataa				)
     A__	=    pipeline.to(snake_case_				)
     pipeline.set_progress_bar_config(disable=snake_case_				)

     A__	=    torch.Generator(device="cpu"				).manual_seed(0				)
     A__	=    pipeline(
         snake_case_     ,			generator=snake_case_     ,			output_type="np"     ,			)

     A__	=    output.images[0]

     assert image.shape == (256, 256, 3)

     assert_mean_pixel_difference(snake_case_     ,			snake_case_     ,			15				)











"""simple docstring"""



import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 (
          MODEL_FOR_PRETRAINING_MAPPING,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForPreTraining,
          AlbertForQuestionAnswering,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertModel,
      )
      from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST




class      UpperCAmelCase_  :




    def __init__(   self						:	Tuple     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]=13     ,			snake_case_						:	Optional[int]=7     ,			snake_case_						:	Any=True     ,			snake_case_						:	List[Any]=True     ,			snake_case_						:	Dict=True     ,			snake_case_						:	Dict=True     ,			snake_case_						:	List[str]=99     ,			snake_case_						:	Union[str, Any]=16     ,			snake_case_						:	Any=36     ,			snake_case_						:	List[Any]=6     ,			snake_case_						:	Optional[Any]=6     ,			snake_case_						:	Optional[Any]=6     ,			snake_case_						:	Any=37     ,			snake_case_						:	int="gelu"     ,			snake_case_						:	Any=0.1     ,			snake_case_						:	int=0.1     ,			snake_case_						:	str=512     ,			snake_case_						:	Union[str, Any]=16     ,			snake_case_						:	Tuple=2     ,			snake_case_						:	Any=0.02     ,			snake_case_						:	Optional[Any]=3     ,			snake_case_						:	Union[str, Any]=4     ,			snake_case_						:	Any=None     ,			)				->							Tuple:




     '''simple docstring'''

     A__	=    parent
     A__	=    batch_size
     A__	=    seq_length
     A__	=    is_training
     A__	=    use_input_mask
     A__	=    use_token_type_ids
     A__	=    use_labels
     A__	=    vocab_size
     A__	=    embedding_size
     A__	=    hidden_size
     A__	=    num_hidden_layers
     A__	=    num_hidden_groups
     A__	=    num_attention_heads
     A__	=    intermediate_size
     A__	=    hidden_act
     A__	=    hidden_dropout_prob
     A__	=    attention_probs_dropout_prob
     A__	=    max_position_embeddings
     A__	=    type_vocab_size
     A__	=    type_sequence_label_size
     A__	=    initializer_range
     A__	=    num_labels
     A__	=    num_choices
     A__	=    scope




    def 							__magic_name__							(   self						:	Tuple				)				->							List[str]:




     '''simple docstring'''

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

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

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

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

     A__	=    self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




    def 							__magic_name__							(   self						:	int				)				->							Dict:




     '''simple docstring'''

     return AlbertConfig(
         vocab_size=self.vocab_size     ,			hidden_size=self.hidden_size     ,			num_hidden_layers=self.num_hidden_layers     ,			num_attention_heads=self.num_attention_heads     ,			intermediate_size=self.intermediate_size     ,			hidden_act=self.hidden_act     ,			hidden_dropout_prob=self.hidden_dropout_prob     ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,			max_position_embeddings=self.max_position_embeddings     ,			type_vocab_size=self.type_vocab_size     ,			initializer_range=self.initializer_range     ,			num_hidden_groups=self.num_hidden_groups     ,			)




    def 							__magic_name__							(   self						:	Optional[int]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[str]     ,			snake_case_						:	List[str]     ,			snake_case_						:	Any     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Union[str, Any]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    AlbertModel(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_				)
     A__	=    model(snake_case_     ,			token_type_ids=snake_case_				)
     A__	=    model(snake_case_				)
     self.parent.assertEqual(result.last_hidden_state.shape     ,			(self.batch_size, self.seq_length, self.hidden_size)				)
     self.parent.assertEqual(result.pooler_output.shape     ,			(self.batch_size, self.hidden_size)				)




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	int     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Union[str, Any]				)				->							int:




     '''simple docstring'''

     A__	=    AlbertForPreTraining(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(
         snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_     ,			sentence_order_label=snake_case_     ,			)
     self.parent.assertEqual(result.prediction_logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)				)
     self.parent.assertEqual(result.sop_logits.shape     ,			(self.batch_size, config.num_labels)				)




    def 							__magic_name__							(   self						:	str     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Dict     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[str]				)				->							Dict:




     '''simple docstring'''

     A__	=    AlbertForMaskedLM(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_				)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)				)




    def 							__magic_name__							(   self						:	int     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	List[str]     ,			snake_case_						:	List[str]				)				->							Tuple:




     '''simple docstring'''

     A__	=    AlbertForQuestionAnswering(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(
         snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			start_positions=snake_case_     ,			end_positions=snake_case_     ,			)
     self.parent.assertEqual(result.start_logits.shape     ,			(self.batch_size, self.seq_length)				)
     self.parent.assertEqual(result.end_logits.shape     ,			(self.batch_size, self.seq_length)				)




    def 							__magic_name__							(   self						:	Any     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	str     ,			snake_case_						:	Any				)				->							Any:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    AlbertForSequenceClassification(snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_				)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)				)




    def 							__magic_name__							(   self						:	Tuple     ,			snake_case_						:	Dict     ,			snake_case_						:	int     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	int     ,			snake_case_						:	str     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    AlbertForTokenClassification(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    model(snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_				)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.num_labels)				)




    def 							__magic_name__							(   self						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any     ,			snake_case_						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any     ,			snake_case_						:	Tuple     ,			snake_case_						:	Any				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.num_choices
     A__	=    AlbertForMultipleChoice(config=snake_case_				)
     model.to(snake_case_				)
     model.eval()
     A__	=    input_ids.unsqueeze(1				).expand(-1     ,			self.num_choices     ,			-1				).contiguous()
     A__	=    token_type_ids.unsqueeze(1				).expand(-1     ,			self.num_choices     ,			-1				).contiguous()
     A__	=    input_mask.unsqueeze(1				).expand(-1     ,			self.num_choices     ,			-1				).contiguous()
     A__	=    model(
         snake_case_     ,			attention_mask=snake_case_     ,			token_type_ids=snake_case_     ,			labels=snake_case_     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_choices)				)




    def 							__magic_name__							(   self						:	Any				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.prepare_config_and_inputs()
     (
         (
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				(
         A__
     ),				
     )	=    config_and_inputs
     A__	=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict






@require_torch
class      UpperCAmelCase_  (						A_,       A_,       unittest.TestCase							):
    lowercase__						 =				(
        (
            AlbertModel,
            AlbertForPreTraining,
            AlbertForMaskedLM,
            AlbertForMultipleChoice,
            AlbertForSequenceClassification,
            AlbertForTokenClassification,
            AlbertForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    lowercase__						 =				(
        {
            '''feature-extraction''': AlbertModel,
            '''fill-mask''': AlbertForMaskedLM,
            '''question-answering''': AlbertForQuestionAnswering,
            '''text-classification''': AlbertForSequenceClassification,
            '''token-classification''': AlbertForTokenClassification,
            '''zero-shot''': AlbertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowercase__						 =				True




    def 							__magic_name__							(   self						:	Tuple     ,			snake_case_						:	Dict     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[Any]=False				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    super()._prepare_for_class(snake_case_     ,			snake_case_     ,			return_labels=snake_case_				)

     if return_labels:
      if model_class in get_values(snake_case_				):
       A__	=    torch.zeros(
           (self.model_tester.batch_size, self.model_tester.seq_length)     ,			dtype=torch.long     ,			device=snake_case_				)
       A__	=    torch.zeros(
           self.model_tester.batch_size     ,			dtype=torch.long     ,			device=snake_case_				)
     return inputs_dict




    def 							__magic_name__							(   self						:	Dict				)				->							List[Any]:




     '''simple docstring'''

     A__	=    AlbertModelTester(self				)
     A__	=    ConfigTester(self     ,			config_class=snake_case_     ,			hidden_size=37				)




    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Optional[int]:




     '''simple docstring'''

     self.config_tester.run_common_tests()




    def 							__magic_name__							(   self						:	Any				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*snake_case_				)




    def 							__magic_name__							(   self						:	int				)				->							str:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*snake_case_				)




    def 							__magic_name__							(   self						:	Any				)				->							str:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*snake_case_				)




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Any:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*snake_case_				)




    def 							__magic_name__							(   self						:	List[str]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
      A__	=    type
      self.model_tester.create_and_check_model(*snake_case_				)




    @slow
    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Dict:




     '''simple docstring'''

     for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      A__	=    AlbertModel.from_pretrained(snake_case_				)
      self.assertIsNotNone(snake_case_				)






@require_torch
class      UpperCAmelCase_  (						unittest.TestCase							):




    @slow
    def 							__magic_name__							(   self						:	int				)				->							Tuple:




     '''simple docstring'''

     A__	=    AlbertModel.from_pretrained("albert-base-v2"				)
     A__	=    torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
     A__	=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
     with torch.no_grad():
      A__	=    model(snake_case_     ,			attention_mask=snake_case_				)[0]
     A__	=    torch.Size((1, 11, 768)				)
     self.assertEqual(output.shape     ,			snake_case_				)
     A__	=    torch.tensor(
         [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]				)

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,			snake_case_     ,			atol=1e-4				)				)





