





'''simple docstring'''







from typing import List, Optional, Union

import torch
from transformers import (
    XLMRobertaTokenizer,
)

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


snake_case_     :       List[Any]						=  logging.get_logger(__name__)  # pylint: disable=invalid-name

snake_case_     :       Optional[int]						=  "\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline\n        >>> import torch\n\n        >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\")\n        >>> pipe_prior.to(\"cuda\")\n\n        >>> prompt = \"red cat, 4k photo\"\n        >>> out = pipe_prior(prompt)\n        >>> image_emb = out.image_embeds\n        >>> negative_image_emb = out.negative_image_embeds\n\n        >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\")\n        >>> pipe.to(\"cuda\")\n\n        >>> image = pipe(\n        ...     prompt,\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=negative_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=100,\n        ... ).images\n\n        >>> image[0].save(\"cat.png\")\n        ```\n"





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			Dict,			SCREAMING_SNAKE_CASE__      :			Tuple,			SCREAMING_SNAKE_CASE__      :			List[Any]=8   ) ->	str:
						UpperCAmelCase_			:      Optional[int]     						=  h // scale_factor**2
						if h % scale_factor**2 != 0:
												new_h += 1
						UpperCAmelCase_			:      str     						=  w // scale_factor**2
						if w % scale_factor**2 != 0:
												new_w += 1
						return new_h * scale_factor, new_w * scale_factor


class 					__a      (lowerCamelCase     ):

							def __init__(							self							:     List[str]       ,     __magic_name__							:     MultilingualCLIP       ,     __magic_name__							:     XLMRobertaTokenizer       ,     __magic_name__							:     UNetaDConditionModel       ,     __magic_name__							:     Union[DDIMScheduler, DDPMScheduler]       ,     __magic_name__							:     VQModel       ,     )					->       Dict:







													"""simple docstring"""
													super().__init__()

													self.register_modules(
													    text_encoder=__magic_name__       ,     tokenizer=__magic_name__       ,     unet=__magic_name__       ,     scheduler=__magic_name__       ,     movq=__magic_name__       ,     )
													UpperCAmelCase_			:      Optional[Any]     						=  2 ** (len(self.movq.config.block_out_channels   ) - 1)

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







													"""simple docstring"""
													if latents is None:
																			UpperCAmelCase_			:      List[str]     						=  randn_tensor(__magic_name__       ,     generator=__magic_name__       ,     device=__magic_name__       ,     dtype=__magic_name__   )
													else:
																			if latents.shape != shape:
																									raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}"""   )
																			UpperCAmelCase_			:      Optional[Any]     						=  latents.to(__magic_name__   )

													UpperCAmelCase_			:      Optional[int]     						=  latents * scheduler.init_noise_sigma
													return latents

							def     UpperCAmelCase__	(							self							:     str       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     int       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Optional[int]=None       ,     )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  len(__magic_name__   ) if isinstance(__magic_name__       ,     __magic_name__   ) else 1
													# get prompt text embeddings
													UpperCAmelCase_			:      List[str]     						=  self.tokenizer(
													    __magic_name__       ,     padding='''max_length'''       ,     truncation=__magic_name__       ,     max_length=77       ,     return_attention_mask=__magic_name__       ,     add_special_tokens=__magic_name__       ,     return_tensors='''pt'''       ,     )

													UpperCAmelCase_			:      Tuple     						=  text_inputs.input_ids
													UpperCAmelCase_			:      Optional[int]     						=  self.tokenizer(__magic_name__       ,     padding='''longest'''       ,     return_tensors='''pt'''   ).input_ids

													if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(__magic_name__       ,     __magic_name__   ):
																			UpperCAmelCase_			:      Dict     						=  self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]   )
																			logger.warning(
																			    '''The following part of your input was truncated because CLIP can only handle sequences up to'''
																			    F""" {self.tokenizer.model_max_length} tokens: {removed_text}"""   )

													UpperCAmelCase_			:      List[Any]     						=  text_input_ids.to(__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  text_inputs.attention_mask.to(__magic_name__   )

													UpperCAmelCase_						,       UpperCAmelCase_			:      Union[str, Any]     						=  self.text_encoder(
													    input_ids=__magic_name__       ,     attention_mask=__magic_name__   )

													UpperCAmelCase_			:      str     						=  prompt_embeds.repeat_interleave(__magic_name__       ,     dim=0   )
													UpperCAmelCase_			:      Union[str, Any]     						=  text_encoder_hidden_states.repeat_interleave(__magic_name__       ,     dim=0   )
													UpperCAmelCase_			:      List[Any]     						=  text_mask.repeat_interleave(__magic_name__       ,     dim=0   )

													if do_classifier_free_guidance:
																			UpperCAmelCase_			:      List[str]
																			if negative_prompt is None:
																									UpperCAmelCase_			:      Optional[int]     						=  [''''''] * batch_size
																			elif type(__magic_name__   ) is not type(__magic_name__   ):
																									raise TypeError(
																									    F"""`negative_prompt` should be the same type to `prompt`, but got {type(__magic_name__   )} !="""
																									    F""" {type(__magic_name__   )}."""   )
																			elif isinstance(__magic_name__       ,     __magic_name__   ):
																									UpperCAmelCase_			:      Dict     						=  [negative_prompt]
																			elif batch_size != len(__magic_name__   ):
																									raise ValueError(
																									    F"""`negative_prompt`: {negative_prompt} has batch size {len(__magic_name__   )}, but `prompt`:"""
																									    F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
																									    ''' the batch size of `prompt`.'''   )
																			else:
																									UpperCAmelCase_			:      List[Any]     						=  negative_prompt

																			UpperCAmelCase_			:      List[str]     						=  self.tokenizer(
																			    __magic_name__       ,     padding='''max_length'''       ,     max_length=77       ,     truncation=__magic_name__       ,     return_attention_mask=__magic_name__       ,     add_special_tokens=__magic_name__       ,     return_tensors='''pt'''       ,     )
																			UpperCAmelCase_			:      str     						=  uncond_input.input_ids.to(__magic_name__   )
																			UpperCAmelCase_			:      Optional[Any]     						=  uncond_input.attention_mask.to(__magic_name__   )

																			UpperCAmelCase_						,       UpperCAmelCase_			:      Union[str, Any]     						=  self.text_encoder(
																			    input_ids=__magic_name__       ,     attention_mask=__magic_name__   )

																			# duplicate unconditional embeddings for each generation per prompt, using mps friendly method

																			UpperCAmelCase_			:      List[str]     						=  negative_prompt_embeds.shape[1]
																			UpperCAmelCase_			:      List[str]     						=  negative_prompt_embeds.repeat(1       ,     __magic_name__   )
																			UpperCAmelCase_			:      int     						=  negative_prompt_embeds.view(batch_size * num_images_per_prompt       ,     __magic_name__   )

																			UpperCAmelCase_			:      str     						=  uncond_text_encoder_hidden_states.shape[1]
																			UpperCAmelCase_			:      List[Any]     						=  uncond_text_encoder_hidden_states.repeat(1       ,     __magic_name__       ,     1   )
																			UpperCAmelCase_			:      Any     						=  uncond_text_encoder_hidden_states.view(
																			    batch_size * num_images_per_prompt       ,     __magic_name__       ,     -1   )
																			UpperCAmelCase_			:      Dict     						=  uncond_text_mask.repeat_interleave(__magic_name__       ,     dim=0   )

																			# done duplicates

																			# For classifier free guidance, we need to do two forward passes.
																			# Here we concatenate the unconditional and text embeddings into a single batch
																			# to avoid doing two forward passes
																			UpperCAmelCase_			:      str     						=  torch.cat([negative_prompt_embeds, prompt_embeds]   )
																			UpperCAmelCase_			:      Dict     						=  torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]   )

																			UpperCAmelCase_			:      Optional[Any]     						=  torch.cat([uncond_text_mask, text_mask]   )

													return prompt_embeds, text_encoder_hidden_states, text_mask

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Union[str, Any]=0   )					->       List[str]:







													"""simple docstring"""
													if is_accelerate_available():
																			from accelerate import cpu_offload
													else:
																			raise ImportError('''Please install accelerate via `pip install accelerate`'''   )

													UpperCAmelCase_			:      List[Any]     						=  torch.device(F"""cuda:{gpu_id}"""   )

													UpperCAmelCase_			:      Any     						=  [
													    self.unet,
													    self.text_encoder,
													    self.movq,
													]
													for cpu_offloaded_model in models:
																			if cpu_offloaded_model is not None:
																									cpu_offload(__magic_name__       ,     __magic_name__   )

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







													"""simple docstring"""
													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=__magic_name__   )
																			torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

													UpperCAmelCase_			:      Any     						=  None
													for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
																			UpperCAmelCase_						,       UpperCAmelCase_			:      Dict     						=  cpu_offload_with_hook(__magic_name__       ,     __magic_name__       ,     prev_module_hook=__magic_name__   )

													if self.safety_checker is not None:
																			UpperCAmelCase_						,       UpperCAmelCase_			:      Dict     						=  cpu_offload_with_hook(self.safety_checker       ,     __magic_name__       ,     prev_module_hook=__magic_name__   )

													# We'll offload the last model manually.
													UpperCAmelCase_			:      List[str]     						=  hook

							@property
							# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
							def     UpperCAmelCase__	(							self							:     int   )					->       int:







													"""simple docstring"""
													if not hasattr(self.unet       ,     '''_hf_hook'''   ):
																			return self.device
													for module in self.unet.modules():
																			if (
																			    hasattr(__magic_name__       ,     '''_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(__magic_name__   )
							def __call__(							self							:     List[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     Union[torch.FloatTensor, List[torch.FloatTensor]]       ,     __magic_name__							:     Union[torch.FloatTensor, List[torch.FloatTensor]]       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 1_00       ,     __magic_name__							:     float = 4.0       ,     __magic_name__							:     int = 1       ,     __magic_name__							:     Optional[Union[torch.Generator, List[torch.Generator]]] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     )					->       Dict:







													"""simple docstring"""

													if isinstance(__magic_name__       ,     __magic_name__   ):
																			UpperCAmelCase_			:      Optional[int]     						=  1
													elif isinstance(__magic_name__       ,     __magic_name__   ):
																			UpperCAmelCase_			:      Dict     						=  len(__magic_name__   )
													else:
																			raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(__magic_name__   )}"""   )

													UpperCAmelCase_			:      List[Any]     						=  self._execution_device

													UpperCAmelCase_			:      Any     						=  batch_size * num_images_per_prompt
													UpperCAmelCase_			:      int     						=  guidance_scale > 1.0

													UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_			:      str     						=  self._encode_prompt(
													    __magic_name__       ,     __magic_name__       ,     __magic_name__       ,     __magic_name__       ,     __magic_name__   )

													if isinstance(__magic_name__       ,     __magic_name__   ):
																			UpperCAmelCase_			:      str     						=  torch.cat(__magic_name__       ,     dim=0   )
													if isinstance(__magic_name__       ,     __magic_name__   ):
																			UpperCAmelCase_			:      List[str]     						=  torch.cat(__magic_name__       ,     dim=0   )

													if do_classifier_free_guidance:
																			UpperCAmelCase_			:      Dict     						=  image_embeds.repeat_interleave(__magic_name__       ,     dim=0   )
																			UpperCAmelCase_			:      List[Any]     						=  negative_image_embeds.repeat_interleave(__magic_name__       ,     dim=0   )

													UpperCAmelCase_			:      Any     						=  torch.cat([negative_image_embeds, image_embeds]       ,     dim=0   ).to(
													    dtype=prompt_embeds.dtype       ,     device=__magic_name__   )

													self.scheduler.set_timesteps(__magic_name__       ,     device=__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  self.scheduler.timesteps

													UpperCAmelCase_			:      List[Any]     						=  self.unet.config.in_channels

													UpperCAmelCase_						,       UpperCAmelCase_			:      Any     						=  get_new_h_w(__magic_name__       ,     __magic_name__       ,     self.movq_scale_factor   )

													# create initial latent
													UpperCAmelCase_			:      int     						=  self.prepare_latents(
													    (batch_size, num_channels_latents, height, width)       ,     text_encoder_hidden_states.dtype       ,     __magic_name__       ,     __magic_name__       ,     __magic_name__       ,     self.scheduler       ,     )

													for i, t in enumerate(self.progress_bar(__magic_name__   )   ):
																			# expand the latents if we are doing classifier free guidance
																			UpperCAmelCase_			:      Optional[Any]     						=  torch.cat([latents] * 2   ) if do_classifier_free_guidance else latents

																			UpperCAmelCase_			:      List[Any]     						=  {'''text_embeds''': prompt_embeds, '''image_embeds''': image_embeds}
																			UpperCAmelCase_			:      List[str]     						=  self.unet(
																			    sample=__magic_name__       ,     timestep=__magic_name__       ,     encoder_hidden_states=__magic_name__       ,     added_cond_kwargs=__magic_name__       ,     return_dict=__magic_name__       ,     )[0]

																			if do_classifier_free_guidance:
																									UpperCAmelCase_						,       UpperCAmelCase_			:      Tuple     						=  noise_pred.split(latents.shape[1]       ,     dim=1   )
																									UpperCAmelCase_						,       UpperCAmelCase_			:      str     						=  noise_pred.chunk(2   )
																									UpperCAmelCase_						,       UpperCAmelCase_			:      List[str]     						=  variance_pred.chunk(2   )
																									UpperCAmelCase_			:      str     						=  noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
																									UpperCAmelCase_			:      Dict     						=  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[str]     						=  noise_pred.split(latents.shape[1]       ,     dim=1   )

																			# compute the previous noisy sample x_t -> x_t-1
																			UpperCAmelCase_			:      List[str]     						=  self.scheduler.step(
																			    __magic_name__       ,     __magic_name__       ,     __magic_name__       ,     generator=__magic_name__       ,     ).prev_sample
													# post-processing
													UpperCAmelCase_			:      Any     						=  self.movq.decode(__magic_name__       ,     force_not_quantize=__magic_name__   )['''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_			:      Tuple     						=  image * 0.5 + 0.5
																			UpperCAmelCase_			:      Optional[int]     						=  image.clamp(0       ,     1   )
																			UpperCAmelCase_			:      Any     						=  image.cpu().permute(0       ,     2       ,     3       ,     1   ).float().numpy()

													if output_type == "pil":
																			UpperCAmelCase_			:      List[Any]     						=  self.numpy_to_pil(__magic_name__   )

													if not return_dict:
																			return (image,)

													return ImagePipelineOutput(images=__magic_name__   )












'''simple docstring'''







import unittest

from transformers import LiltConfig, 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 (
					    LiltForQuestionAnswering,
					    LiltForSequenceClassification,
					    LiltForTokenClassification,
					    LiltModel,
					)
					from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST


class 					__a      :

							def __init__(							self							:     int       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Any=13       ,     __magic_name__							:     Any=7       ,     __magic_name__							:     Union[str, Any]=True       ,     __magic_name__							:     Union[str, Any]=True       ,     __magic_name__							:     str=True       ,     __magic_name__							:     Optional[int]=True       ,     __magic_name__							:     List[Any]=99       ,     __magic_name__							:     int=24       ,     __magic_name__							:     Optional[int]=2       ,     __magic_name__							:     Tuple=6       ,     __magic_name__							:     Union[str, Any]=37       ,     __magic_name__							:     Optional[Any]="gelu"       ,     __magic_name__							:     Any=0.1       ,     __magic_name__							:     str=0.1       ,     __magic_name__							:     Tuple=5_12       ,     __magic_name__							:     Union[str, Any]=16       ,     __magic_name__							:     Tuple=2       ,     __magic_name__							:     Tuple=0.0_2       ,     __magic_name__							:     Optional[Any]=3       ,     __magic_name__							:     Optional[int]=None       ,     __magic_name__							:     Any=10_00       ,     )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  parent
													UpperCAmelCase_			:      Optional[int]     						=  batch_size
													UpperCAmelCase_			:      List[str]     						=  seq_length
													UpperCAmelCase_			:      Dict     						=  is_training
													UpperCAmelCase_			:      List[str]     						=  use_input_mask
													UpperCAmelCase_			:      Any     						=  use_token_type_ids
													UpperCAmelCase_			:      Any     						=  use_labels
													UpperCAmelCase_			:      Any     						=  vocab_size
													UpperCAmelCase_			:      Dict     						=  hidden_size
													UpperCAmelCase_			:      Tuple     						=  num_hidden_layers
													UpperCAmelCase_			:      Tuple     						=  num_attention_heads
													UpperCAmelCase_			:      int     						=  intermediate_size
													UpperCAmelCase_			:      Union[str, Any]     						=  hidden_act
													UpperCAmelCase_			:      Optional[int]     						=  hidden_dropout_prob
													UpperCAmelCase_			:      Optional[Any]     						=  attention_probs_dropout_prob
													UpperCAmelCase_			:      Union[str, Any]     						=  max_position_embeddings
													UpperCAmelCase_			:      int     						=  type_vocab_size
													UpperCAmelCase_			:      List[Any]     						=  type_sequence_label_size
													UpperCAmelCase_			:      int     						=  initializer_range
													UpperCAmelCase_			:      Dict     						=  num_labels
													UpperCAmelCase_			:      List[str]     						=  scope
													UpperCAmelCase_			:      List[str]     						=  range_bbox

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







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size   )

													UpperCAmelCase_			:      List[str]     						=  ids_tensor([self.batch_size, self.seq_length, 4]       ,     self.range_bbox   )
													# Ensure that bbox is legal
													for i in range(bbox.shape[0]   ):
																			for j in range(bbox.shape[1]   ):
																									if bbox[i, j, 3] < bbox[i, j, 1]:
																															UpperCAmelCase_			:      List[str]     						=  bbox[i, j, 3]
																															UpperCAmelCase_			:      Dict     						=  bbox[i, j, 1]
																															UpperCAmelCase_			:      Optional[Any]     						=  t
																									if bbox[i, j, 2] < bbox[i, j, 0]:
																															UpperCAmelCase_			:      List[str]     						=  bbox[i, j, 2]
																															UpperCAmelCase_			:      Tuple     						=  bbox[i, j, 0]
																															UpperCAmelCase_			:      Union[str, Any]     						=  t

													UpperCAmelCase_			:      int     						=  None
													if self.use_input_mask:
																			UpperCAmelCase_			:      Optional[int]     						=  ids_tensor([self.batch_size, self.seq_length]       ,     vocab_size=2   )

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

													UpperCAmelCase_			:      Dict     						=  None
													UpperCAmelCase_			:      Tuple     						=  None
													if self.use_labels:
																			UpperCAmelCase_			:      Any     						=  ids_tensor([self.batch_size]       ,     self.type_sequence_label_size   )
																			UpperCAmelCase_			:      Dict     						=  ids_tensor([self.batch_size, self.seq_length]       ,     self.num_labels   )

													UpperCAmelCase_			:      int     						=  self.get_config()

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

							def     UpperCAmelCase__	(							self							:     Any   )					->       List[Any]:







													"""simple docstring"""
													return LiltConfig(
													    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       ,     )

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







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  LiltModel(config=__magic_name__   )
													model.to(__magic_name__   )
													model.eval()
													UpperCAmelCase_			:      Optional[Any]     						=  model(__magic_name__       ,     bbox=__magic_name__       ,     attention_mask=__magic_name__       ,     token_type_ids=__magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  model(__magic_name__       ,     bbox=__magic_name__       ,     token_type_ids=__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  model(__magic_name__       ,     bbox=__magic_name__   )

													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     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Any       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     str       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     List[Any]       ,     )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  self.num_labels
													UpperCAmelCase_			:      List[Any]     						=  LiltForTokenClassification(config=__magic_name__   )
													model.to(__magic_name__   )
													model.eval()
													UpperCAmelCase_			:      List[Any]     						=  model(
													    __magic_name__       ,     bbox=__magic_name__       ,     attention_mask=__magic_name__       ,     token_type_ids=__magic_name__       ,     labels=__magic_name__   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.num_labels)   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     Any       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     int       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Any       ,     )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  LiltForQuestionAnswering(config=__magic_name__   )
													model.to(__magic_name__   )
													model.eval()
													UpperCAmelCase_			:      Optional[Any]     						=  model(
													    __magic_name__       ,     bbox=__magic_name__       ,     attention_mask=__magic_name__       ,     token_type_ids=__magic_name__       ,     start_positions=__magic_name__       ,     end_positions=__magic_name__       ,     )
													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     UpperCAmelCase__	(							self							:     List[Any]   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  self.prepare_config_and_inputs()
													(
													    (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       
													)			:      Optional[int]     						=  config_and_inputs
													UpperCAmelCase_			:      Tuple     						=  {
													    '''input_ids''': input_ids,
													    '''bbox''': bbox,
													    '''token_type_ids''': token_type_ids,
													    '''attention_mask''': input_mask,
													}
													return config, inputs_dict





@require_torch
class 					__a      (lowerCamelCase       ,		lowerCamelCase       ,		lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   Tuple									=       (
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							__a					:   Any									=       (
							    {
							        "feature-extraction": LiltModel,
							        "question-answering": LiltForQuestionAnswering,
							        "text-classification": LiltForSequenceClassification,
							        "token-classification": LiltForTokenClassification,
							        "zero-shot": LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							__a					:   Union[str, Any]									=       False
							__a					:   int									=       False

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







													"""simple docstring"""
													return True

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







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  LiltModelTester(self   )
													UpperCAmelCase_			:      List[Any]     						=  ConfigTester(self       ,     config_class=__magic_name__       ,     hidden_size=37   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       str:







													"""simple docstring"""
													self.config_tester.run_common_tests()

							def     UpperCAmelCase__	(							self							:     str   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     str   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			UpperCAmelCase_			:      Tuple     						=  type
																			self.model_tester.create_and_check_model(*__magic_name__   )

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







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*__magic_name__   )

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







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*__magic_name__   )



							@slow
							def     UpperCAmelCase__	(							self							:     int   )					->       Union[str, Any]:







													"""simple docstring"""
													for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_			:      Optional[int]     						=  LiltModel.from_pretrained(__magic_name__   )
																			self.assertIsNotNone(__magic_name__   )





@require_torch
@slow
class 					__a      (unittest.TestCase     ):

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''   ).to(__magic_name__   )

													UpperCAmelCase_			:      Any     						=  torch.tensor([[1, 2]]       ,     device=__magic_name__   )
													UpperCAmelCase_			:      int     						=  torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]       ,     device=__magic_name__   )

													# forward pass
													with torch.no_grad():
																			UpperCAmelCase_			:      Optional[int]     						=  model(input_ids=__magic_name__       ,     bbox=__magic_name__   )

													UpperCAmelCase_			:      int     						=  torch.Size([1, 2, 7_68]   )
													UpperCAmelCase_			:      List[str]     						=  torch.tensor(
													    [[-0.0_6_5_3, 0.0_9_5_0, -0.0_0_6_1], [-0.0_5_4_5, 0.0_9_2_6, -0.0_3_2_4]]       ,     device=__magic_name__       ,     )

													self.assertTrue(outputs.last_hidden_state.shape       ,     __magic_name__   )
													self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]       ,     __magic_name__       ,     atol=1E-3   )   )





