






from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


a						:			int     	=					logging.get_logger(__name__)  # pylint: disable=invalid-name

a						:			List[Any]     	=					"\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n        >>> from diffusers.utils import load_image\n        >>> import torch\n\n        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n        ...     \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16\n        ... )\n        >>> pipe_prior.to(\"cuda\")\n\n        >>> prompt = \"A red cartoon frog, 4k\"\n        >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n        >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n        ...     \"kandinsky-community/kandinsky-2-2-decoder\", torch_dtype=torch.float16\n        ... )\n        >>> pipe.to(\"cuda\")\n\n        >>> init_image = load_image(\n        ...     \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"\n        ...     \"/kandinsky/frog.png\"\n        ... )\n\n        >>> image = pipe(\n        ...     image=init_image,\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=zero_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=100,\n        ...     strength=0.2,\n        ... ).images\n\n        >>> image[0].save(\"red_frog.png\")\n        ```\n"






def 							lowerCamelCase__ (       __lowerCamelCase     :	int    ,						__lowerCamelCase     :	List[Any]    ,						__lowerCamelCase     :	List[str]=8						):
  __UpperCAmelCase				:      Tuple	 =    height // scale_factor**2
  if height % scale_factor**2 != 0:
    new_height += 1
  __UpperCAmelCase				:      List[str]	 =    width // scale_factor**2
  if width % scale_factor**2 != 0:
    new_width += 1
  return new_height * scale_factor, new_width * scale_factor






def 							lowerCamelCase__ (       __lowerCamelCase     :	List[Any]    ,						__lowerCamelCase     :	str=512    ,						__lowerCamelCase     :	Optional[Any]=512						):
  __UpperCAmelCase				:      int	 =    pil_image.resize((w, h)    ,						resample=Image.BICUBIC    ,						reducing_gap=1						)
  __UpperCAmelCase				:      int	 =    np.array(pil_image.convert("""RGB"""						)						)
  __UpperCAmelCase				:      Optional[Any]	 =    arr.astype(np.floataa						) / 1_2_7.5 - 1
  __UpperCAmelCase				:      Optional[int]	 =    np.transpose(__lowerCamelCase    ,						[2, 0, 1]						)
  __UpperCAmelCase				:      List[str]	 =    torch.from_numpy(__lowerCamelCase						).unsqueeze(0						)
  return image







class        a       (					lowercase__  ):



      """simple docstring"""


      def __init__( self   :     int						, __lowercase   :     UNetaDConditionModel						, __lowercase   :     DDPMScheduler						, __lowercase   :     VQModel						, )				-> str:
        super().__init__()

        self.register_modules(
            unet=__lowercase						, scheduler=__lowercase						, movq=__lowercase						, )
        __UpperCAmelCase				:      int	 =    2 ** (len(self.movq.config.block_out_channels   ) - 1)
      def       UpperCAmelCase ( self   :     List[Any]						, __lowercase   :     Tuple						, __lowercase   :     Optional[int]						, __lowercase   :     Optional[int]   )				-> Any:
        # get the original timestep using init_timestep
        __UpperCAmelCase				:      int	 =    min(int(num_inference_steps * strength   )						, __lowercase   )

        __UpperCAmelCase				:      Optional[int]	 =    max(num_inference_steps - init_timestep						, 0   )
        __UpperCAmelCase				:      Tuple	 =    self.scheduler.timesteps[t_start:]

        return timesteps, num_inference_steps - t_start
      def       UpperCAmelCase ( self   :     Union[str, Any]						, __lowercase   :     Optional[int]						, __lowercase   :     Optional[int]						, __lowercase   :     Optional[Any]						, __lowercase   :     int						, __lowercase   :     List[Any]						, __lowercase   :     int						, __lowercase   :     Tuple=None   )				-> List[str]:
        if not isinstance(__lowercase						, (torch.Tensor, PIL.Image.Image, list)   ):
          raise ValueError(
              f"""`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(__lowercase   )}"""   )

        __UpperCAmelCase				:      List[Any]	 =    image.to(device=__lowercase						, dtype=__lowercase   )

        __UpperCAmelCase				:      List[Any]	 =    batch_size * num_images_per_prompt

        if image.shape[1] == 4:
          __UpperCAmelCase				:      str	 =    image

        else:
          if isinstance(__lowercase						, __lowercase   ) and len(__lowercase   ) != batch_size:
            raise ValueError(
                f"""You have passed a list of generators of length {len(__lowercase   )}, but requested an effective batch"""
                f""" size of {batch_size}. Make sure the batch size matches the length of the generators."""   )

          elif isinstance(__lowercase						, __lowercase   ):
            __UpperCAmelCase				:      Union[str, Any]	 =    [
                self.movq.encode(image[i : i + 1]   ).latent_dist.sample(generator[i]   ) for i in range(__lowercase   )
            ]
            __UpperCAmelCase				:      List[str]	 =    torch.cat(__lowercase						, dim=0   )
          else:
            __UpperCAmelCase				:      Optional[Any]	 =    self.movq.encode(__lowercase   ).latent_dist.sample(__lowercase   )

          __UpperCAmelCase				:      Dict	 =    self.movq.config.scaling_factor * init_latents

        __UpperCAmelCase				:      List[Any]	 =    torch.cat([init_latents]						, dim=0   )

        __UpperCAmelCase				:      Dict	 =    init_latents.shape
        __UpperCAmelCase				:      int	 =    randn_tensor(__lowercase						, generator=__lowercase						, device=__lowercase						, dtype=__lowercase   )

        # get latents
        __UpperCAmelCase				:      Union[str, Any]	 =    self.scheduler.add_noise(__lowercase						, __lowercase						, __lowercase   )

        __UpperCAmelCase				:      Dict	 =    init_latents

        return latents
      def       UpperCAmelCase ( self   :     Union[str, Any]						, __lowercase   :     int=0   )				-> Dict:
        if is_accelerate_available():
          from accelerate import cpu_offload
        else:
          raise ImportError("""Please install accelerate via `pip install accelerate`"""   )

        __UpperCAmelCase				:      int	 =    torch.device(f"""cuda:{gpu_id}"""   )

        __UpperCAmelCase				:      Tuple	 =    [
            self.unet,
            self.movq,
        ]
        for cpu_offloaded_model in models:
          if cpu_offloaded_model is not None:
            cpu_offload(__lowercase						, __lowercase   )
      def       UpperCAmelCase ( self   :     List[Any]						, __lowercase   :     List[Any]=0   )				-> Union[str, Any]:
        if is_accelerate_available() and is_accelerate_version(""">="""						, """0.17.0.dev0"""   ):
          from accelerate import cpu_offload_with_hook
        else:
          raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."""   )

        __UpperCAmelCase				:      Tuple	 =    torch.device(f"""cuda:{gpu_id}"""   )

        if self.device.type != "cpu":
          self.to("""cpu"""						, silence_dtype_warnings=__lowercase   )
          torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

        __UpperCAmelCase				:      List[str]	 =    None
        for cpu_offloaded_model in [self.unet, self.movq]:
          __UpperCAmelCase ,					__UpperCAmelCase				:      int	 =    cpu_offload_with_hook(__lowercase						, __lowercase						, prev_module_hook=__lowercase   )

        # We'll offload the last model manually.
        __UpperCAmelCase				:      List[Any]	 =    hook
      @property
      # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
      def       UpperCAmelCase ( self   :     str   )				-> Optional[int]:
        if not hasattr(self.unet						, """_hf_hook"""   ):
          return self.device
        for module in self.unet.modules():
          if (
              hasattr(__lowercase						, """_hf_hook"""   )
              and hasattr(module._hf_hook						, """execution_device"""   )
              and module._hf_hook.execution_device is not None
          ):
            return torch.device(module._hf_hook.execution_device   )
        return self.device




      @torch.no_grad()
      @replace_example_docstring(__lowercase   )
      def __call__( self   :     Tuple						, __lowercase   :     Union[torch.FloatTensor, List[torch.FloatTensor]]						, __lowercase   :     Union[torch.FloatTensor, PIL.Image.Image, List[torch.FloatTensor], List[PIL.Image.Image]]						, __lowercase   :     Union[torch.FloatTensor, List[torch.FloatTensor]]						, __lowercase   :     int = 512						, __lowercase   :     int = 512						, __lowercase   :     int = 100						, __lowercase   :     float = 4.0						, __lowercase   :     float = 0.3						, __lowercase   :     int = 1						, __lowercase   :     Optional[Union[torch.Generator, List[torch.Generator]]] = None						, __lowercase   :     Optional[str] = "pil"						, __lowercase   :     bool = True						, )				-> Dict:
        __UpperCAmelCase				:      str	 =    self._execution_device

        __UpperCAmelCase				:      Optional[Any]	 =    guidance_scale > 1.0

        if isinstance(__lowercase						, __lowercase   ):
          __UpperCAmelCase				:      Tuple	 =    torch.cat(__lowercase						, dim=0   )
        __UpperCAmelCase				:      List[Any]	 =    image_embeds.shape[0]
        if isinstance(__lowercase						, __lowercase   ):
          __UpperCAmelCase				:      List[str]	 =    torch.cat(__lowercase						, dim=0   )

        if do_classifier_free_guidance:
          __UpperCAmelCase				:      List[Any]	 =    image_embeds.repeat_interleave(__lowercase						, dim=0   )
          __UpperCAmelCase				:      List[str]	 =    negative_image_embeds.repeat_interleave(__lowercase						, dim=0   )

        __UpperCAmelCase				:      Union[str, Any]	 =    torch.cat([negative_image_embeds, image_embeds]						, dim=0   ).to(dtype=self.unet.dtype						, device=__lowercase   )

        if not isinstance(__lowercase						, __lowercase   ):
          __UpperCAmelCase				:      Dict	 =    [image]
        if not all(isinstance(__lowercase						, (PIL.Image.Image, torch.Tensor)   ) for i in image   ):
          raise ValueError(
              f"""Input is in incorrect format: {[type(__lowercase   ) for i in image]}. Currently, we only support  PIL image and pytorch tensor"""   )

        __UpperCAmelCase				:      str	 =    torch.cat([prepare_image(__lowercase						, __lowercase						, __lowercase   ) for i in image]						, dim=0   )
        __UpperCAmelCase				:      str	 =    image.to(dtype=image_embeds.dtype						, device=__lowercase   )

        __UpperCAmelCase				:      List[str]	 =    self.movq.encode(__lowercase   )["""latents"""]
        __UpperCAmelCase				:      Any	 =    latents.repeat_interleave(__lowercase						, dim=0   )
        self.scheduler.set_timesteps(__lowercase						, device=__lowercase   )
        __UpperCAmelCase ,					__UpperCAmelCase				:      Optional[Any]	 =    self.get_timesteps(__lowercase						, __lowercase						, __lowercase   )
        __UpperCAmelCase				:      int	 =    timesteps[:1].repeat(batch_size * num_images_per_prompt   )
        __UpperCAmelCase ,					__UpperCAmelCase				:      Dict	 =    downscale_height_and_width(__lowercase						, __lowercase						, self.movq_scale_factor   )
        __UpperCAmelCase				:      Union[str, Any]	 =    self.prepare_latents(
            __lowercase						, __lowercase						, __lowercase						, __lowercase						, image_embeds.dtype						, __lowercase						, __lowercase   )
        for i, t in enumerate(self.progress_bar(__lowercase   )   ):
          # expand the latents if we are doing classifier free guidance
          __UpperCAmelCase				:      Any	 =    torch.cat([latents] * 2   ) if do_classifier_free_guidance else latents

          __UpperCAmelCase				:      Dict	 =    {"""image_embeds""": image_embeds}
          __UpperCAmelCase				:      Dict	 =    self.unet(
              sample=__lowercase						, timestep=__lowercase						, encoder_hidden_states=__lowercase						, added_cond_kwargs=__lowercase						, return_dict=__lowercase						, )[0]

          if do_classifier_free_guidance:
            __UpperCAmelCase ,					__UpperCAmelCase				:      Dict	 =    noise_pred.split(latents.shape[1]						, dim=1   )
            __UpperCAmelCase ,					__UpperCAmelCase				:      str	 =    noise_pred.chunk(2   )
            __UpperCAmelCase ,					__UpperCAmelCase				:      List[Any]	 =    variance_pred.chunk(2   )
            __UpperCAmelCase				:      Union[str, Any]	 =    noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
            __UpperCAmelCase				:      int	 =    torch.cat([noise_pred, variance_pred_text]						, dim=1   )

          if not (
              hasattr(self.scheduler.config						, """variance_type"""   )
              and self.scheduler.config.variance_type in ["learned", "learned_range"]
          ):
            __UpperCAmelCase ,					__UpperCAmelCase				:      List[Any]	 =    noise_pred.split(latents.shape[1]						, dim=1   )

          # compute the previous noisy sample x_t -> x_t-1
          __UpperCAmelCase				:      Optional[int]	 =    self.scheduler.step(
              __lowercase						, __lowercase						, __lowercase						, generator=__lowercase						, )[0]

        # post-processing
        __UpperCAmelCase				:      List[str]	 =    self.movq.decode(__lowercase						, force_not_quantize=__lowercase   )["""sample"""]

        if output_type not in ["pt", "np", "pil"]:
          raise ValueError(f"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}"""   )

        if output_type in ["np", "pil"]:
          __UpperCAmelCase				:      Any	 =    image * 0.5 + 0.5
          __UpperCAmelCase				:      str	 =    image.clamp(0						, 1   )
          __UpperCAmelCase				:      Union[str, Any]	 =    image.cpu().permute(0						, 2						, 3						, 1   ).float().numpy()

        if output_type == "pil":
          __UpperCAmelCase				:      List[str]	 =    self.numpy_to_pil(__lowercase   )

        if not return_dict:
          return (image,)

        return ImagePipelineOutput(images=__lowercase   )









"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TF_MODEL_FOR_PRETRAINING_MAPPING,
		    TFMobileBertForMaskedLM,
		    TFMobileBertForMultipleChoice,
		    TFMobileBertForNextSentencePrediction,
		    TFMobileBertForPreTraining,
		    TFMobileBertForQuestionAnswering,
		    TFMobileBertForSequenceClassification,
		    TFMobileBertForTokenClassification,
		    TFMobileBertModel,
		)


@require_tf
class 			__UpperCamelCase							(		a__						,			a__						,			unittest.TestCase		):
							lowerCamelCase  :   Optional[int]    =(
							    (
							        TFMobileBertModel,
							        TFMobileBertForMaskedLM,
							        TFMobileBertForNextSentencePrediction,
							        TFMobileBertForPreTraining,
							        TFMobileBertForQuestionAnswering,
							        TFMobileBertForSequenceClassification,
							        TFMobileBertForTokenClassification,
							        TFMobileBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							lowerCamelCase  :   Tuple    =(
							    {
							        """feature-extraction""": TFMobileBertModel,
							        """fill-mask""": TFMobileBertForMaskedLM,
							        """question-answering""": TFMobileBertForQuestionAnswering,
							        """text-classification""": TFMobileBertForSequenceClassification,
							        """token-classification""": TFMobileBertForTokenClassification,
							        """zero-shot""": TFMobileBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowerCamelCase  :   Any    =False
							lowerCamelCase  :   str    =False






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=False )      ->					Any:
								a :						Dict      =							super()._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ,   return_labels=lowerCAmelCase__ )

								if return_labels:
									if model_class in get_values(lowerCAmelCase__ ):
										a :						str      =							tf.zeros(self.model_tester.batch_size ,   dtype=tf.intaa )

								return inputs_dict


							class 			__UpperCamelCase							(		a__		):






														def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__=13 ,   lowerCAmelCase__=7 ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=True ,   lowerCAmelCase__=99 ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=4 ,   lowerCAmelCase__=37 ,   lowerCAmelCase__="gelu" ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=512 ,   lowerCAmelCase__=16 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.02 ,   lowerCAmelCase__=3 ,   lowerCAmelCase__=4 ,   lowerCAmelCase__=None ,   )      ->					List[str]:
															a :						Union[str, Any]      =							parent
															a :						str      =							batch_size
															a :						Optional[int]      =							seq_length
															a :						Tuple      =							is_training
															a :						int      =							use_input_mask
															a :						Union[str, Any]      =							use_token_type_ids
															a :						List[str]      =							use_labels
															a :						str      =							vocab_size
															a :						List[Any]      =							hidden_size
															a :						Any      =							num_hidden_layers
															a :						List[Any]      =							num_attention_heads
															a :						Tuple      =							intermediate_size
															a :						str      =							hidden_act
															a :						List[Any]      =							hidden_dropout_prob
															a :						Optional[Any]      =							attention_probs_dropout_prob
															a :						Union[str, Any]      =							max_position_embeddings
															a :						Tuple      =							type_vocab_size
															a :						Union[str, Any]      =							type_sequence_label_size
															a :						Optional[int]      =							initializer_range
															a :						Any      =							num_labels
															a :						str      =							num_choices
															a :						List[Any]      =							scope
															a :						str      =							embedding_size






														def        __a  (  self )      ->					Dict:
															a :						Optional[int]      =							ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size )

															a :						Optional[int]      =							None
															if self.use_input_mask:
																a :						int      =							random_attention_mask([self.batch_size, self.seq_length] )

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

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

															a :						Tuple      =							MobileBertConfig(
															    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 ,   embedding_size=self.embedding_size ,   )

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






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					int:
															a :						List[Any]      =							TFMobileBertModel(config=lowerCAmelCase__ )
															a :						int      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						List[Any]      =							model(lowerCAmelCase__ )

															a :						List[str]      =							[input_ids, input_mask]
															a :						Dict      =							model(lowerCAmelCase__ )

															a :						List[str]      =							model(lowerCAmelCase__ )

															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        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Optional[Any]:
															a :						List[Any]      =							TFMobileBertForMaskedLM(config=lowerCAmelCase__ )
															a :						Union[str, Any]      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						Optional[int]      =							model(lowerCAmelCase__ )
															self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.vocab_size) )






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					int:
															a :						int      =							TFMobileBertForNextSentencePrediction(config=lowerCAmelCase__ )
															a :						Any      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						List[str]      =							model(lowerCAmelCase__ )
															self.parent.assertEqual(result.logits.shape ,   (self.batch_size, 2) )






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					List[str]:
															a :						Dict      =							TFMobileBertForPreTraining(config=lowerCAmelCase__ )
															a :						Union[str, Any]      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						List[Any]      =							model(lowerCAmelCase__ )
															self.parent.assertEqual(
															    result.prediction_logits.shape ,   (self.batch_size, self.seq_length, self.vocab_size) )
															self.parent.assertEqual(result.seq_relationship_logits.shape ,   (self.batch_size, 2) )






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Tuple:
															a :						List[str]      =							self.num_labels
															a :						List[str]      =							TFMobileBertForSequenceClassification(config=lowerCAmelCase__ )
															a :						Optional[Any]      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						Dict      =							model(lowerCAmelCase__ )
															self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.num_labels) )






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Union[str, Any]:
															a :						Optional[int]      =							self.num_choices
															a :						Tuple      =							TFMobileBertForMultipleChoice(config=lowerCAmelCase__ )
															a :						Dict      =							tf.tile(tf.expand_dims(lowerCAmelCase__ ,   1 ) ,   (1, self.num_choices, 1) )
															a :						Optional[Any]      =							tf.tile(tf.expand_dims(lowerCAmelCase__ ,   1 ) ,   (1, self.num_choices, 1) )
															a :						Optional[int]      =							tf.tile(tf.expand_dims(lowerCAmelCase__ ,   1 ) ,   (1, self.num_choices, 1) )
															a :						Dict      =							{
															    "input_ids": multiple_choice_inputs_ids,
															    "attention_mask": multiple_choice_input_mask,
															    "token_type_ids": multiple_choice_token_type_ids,
															}
															a :						List[Any]      =							model(lowerCAmelCase__ )
															self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.num_choices) )






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Any:
															a :						Any      =							self.num_labels
															a :						Union[str, Any]      =							TFMobileBertForTokenClassification(config=lowerCAmelCase__ )
															a :						Any      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						Any      =							model(lowerCAmelCase__ )
															self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.num_labels) )






														def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					List[str]:
															a :						Dict      =							TFMobileBertForQuestionAnswering(config=lowerCAmelCase__ )
															a :						int      =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
															a :						Optional[Any]      =							model(lowerCAmelCase__ )
															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        __a  (  self )      ->					List[str]:
															a :						Dict      =							self.prepare_config_and_inputs()
															(
															    (
															    a
															),			(
															    a
															),			(
															    a
															),			(
															    a
															),			(
															    a
															),			(
															    a
															),			(
															    a
															),			
															) :						Dict      =							config_and_inputs
															a :						Optional[int]      =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
															return config, inputs_dict






							def        __a  (  self )      ->					List[str]:
								a :						List[str]      =							TFMobileBertModelTest.TFMobileBertModelTester(self )
								a :						str      =							ConfigTester(self ,   config_class=lowerCAmelCase__ ,   hidden_size=37 )






							def        __a  (  self )      ->					Any:
								self.config_tester.run_common_tests()






							def        __a  (  self )      ->					List[str]:
								a :						List[str]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Dict:
								a :						Any      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Any:
								a :						Union[str, Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase__ )






							def        __a  (  self )      ->					List[str]:
								a :						Tuple      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								a :						List[str]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								a :						Dict      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Dict:
								a :						Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Optional[Any]:
								a :						Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase__ )






							@slow
							def        __a  (  self )      ->					Any:
								# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								for model_name in ["google/mobilebert-uncased"]:
									a :						Union[str, Any]      =							TFMobileBertModel.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )


@require_tf
class 			__UpperCamelCase							(		unittest.TestCase		):






							@slow
							def        __a  (  self )      ->					Optional[int]:
								a :						Optional[int]      =							TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased" )
								a :						Any      =							tf.constant([[0, 1, 2, 3, 4, 5]] )
								a :						int      =							model(lowerCAmelCase__ )[0]

								a :						Optional[int]      =							[1, 6, 3_0522]
								self.assertEqual(output.shape ,   lowerCAmelCase__ )

								a :						Optional[Any]      =							tf.constant(
								    [
								        [
								            [-4.5_919_547, -9.248_295, -9.645_256],
								            [-6.7_306_175, -6.440_284, -6.6_052_837],
								            [-7.2_743_506, -6.7_847_915, -6.024_673],
								        ]
								    ] )
								tf.debugging.assert_near(output[:, :3, :3] ,   lowerCAmelCase__ ,   atol=1E-4 )


