






"""simple docstring"""
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device


if is_torch_available():
      import torch

      from transformers import XLMProphetNetForConditionalGeneration, XLMProphetNetTokenizer



@require_torch
class 						a_ (      unittest.TestCase					):







      '''simple docstring'''

      @slow
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		XLMProphetNetForConditionalGeneration.from_pretrained('microsoft/xprophetnet-large-wiki100-cased'   )
       model.to(lowerCamelCase_   )

       # encoder-decoder outputs
       lowerCamelCase__	:     Dict								=		torch.tensor([[1_7, 9_6_2_0_8, 1_0_3_4_7_1, 2]]   ).to(lowerCamelCase_   )
       lowerCamelCase__	:     Dict								=		torch.tensor(
           [[2, 2_5_0, 9_9_5_3, 3_4, 6_9_4_8_9, 1_6_2_0, 3_2, 1_1_8_4_2_4, 6_2_4, 2_1_0, 1_0_5, 2_9_1_3, 1_0_3_2, 3_5_1]]   ).to(lowerCamelCase_   )
       lowerCamelCase__	:     Any								=		model(
           input_ids=lowerCamelCase_,	attention_mask=lowerCamelCase_,	encoder_outputs=lowerCamelCase_,	decoder_input_ids=lowerCamelCase_   )
       lowerCamelCase__	:     Any								=		output[0]
       lowerCamelCase__	:     str								=		torch.Size((1, 1_4, 2_5_0_0_1_2)   )
       self.assertEqual(output_predited_logis.shape,	lowerCamelCase_   )
       lowerCamelCase__	:     Optional[int]								=		torch.tensor(
           [[[-6.3_986, -8.2_391, 12.5_189], [-6.3_289, -8.0_864, 12.6_211], [-6.2_418, -8.0_445, 12.7_968]]]   ).to(lowerCamelCase_   )
       self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3],	lowerCamelCase_,	atol=1e-4   )   )

       # encoder outputs
       lowerCamelCase__	:     List[Any]								=		model.prophetnet.encoder(lowerCamelCase_   )[0]
       lowerCamelCase__	:     List[Any]								=		torch.tensor(
           [[[-1.4_260, -0.7_628, 0.8_453], [-1.4_719, -0.1_391, 0.7_807], [-1.7_678, 0.0_114, 0.4_646]]]   ).to(lowerCamelCase_   )
       lowerCamelCase__	:     Optional[int]								=		torch.Size((1, 4, 1_0_2_4)   )
       self.assertEqual(encoder_outputs.shape,	lowerCamelCase_   )
       self.assertTrue(torch.allclose(encoder_outputs[:, :3, :3],	lowerCamelCase_,	atol=1e-4   )   )

       # decoder outputs
       lowerCamelCase__	:     Optional[int]								=		model.prophetnet.decoder(
           lowerCamelCase_,	encoder_hidden_states=lowerCamelCase_,	)
       lowerCamelCase__	:     Dict								=		decoder_outputs[1].view(1,	model.config.ngram,	1_4,	-1   )
       lowerCamelCase__	:     List[Any]								=		model.lm_head(lowerCamelCase_   )
       lowerCamelCase__	:     List[Any]								=		predicting_streams_logits[:, 0]
       self.assertTrue(torch.allclose(next_first_stream_logits[:, :3, :3],	lowerCamelCase_,	atol=1e-4   )   )




      @slow
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     int								=		XLMProphetNetForConditionalGeneration.from_pretrained(
           'microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'   )
       model.to(lowerCamelCase_   )

       lowerCamelCase__	:     Optional[Any]								=		torch.tensor([[1_7, 9_6_2_0_8, 1_0_3_4_7_1, 2]]   ).to(lowerCamelCase_   )
       lowerCamelCase__	:     List[Any]								=		torch.tensor(
           [[2, 2_5_0, 9_9_5_3, 3_4, 6_9_4_8_9, 1_6_2_0, 3_2, 1_1_8_4_2_4, 6_2_4, 2_1_0, 1_0_5, 2_9_1_3, 1_0_3_2, 3_5_1]]   ).to(lowerCamelCase_   )
       lowerCamelCase__	:     Any								=		model(
           input_ids=lowerCamelCase_,	attention_mask=lowerCamelCase_,	encoder_outputs=lowerCamelCase_,	decoder_input_ids=lowerCamelCase_   )
       lowerCamelCase__	:     List[str]								=		output[0]
       lowerCamelCase__	:     List[Any]								=		torch.Size((1, 1_4, 2_5_0_0_1_2)   )
       self.assertEqual(output_predited_logis.shape,	lowerCamelCase_   )
       # compare the actual values for a slice.
       lowerCamelCase__	:     List[Any]								=		torch.tensor(
           [[[-9.2_253, -9.7_173, -6.3_529], [-7.6_701, -9.0_145, -1.9_382], [-8.0_195, -7.0_004, -0.1_523]]]   ).to(lowerCamelCase_   )

       self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3],	lowerCamelCase_,	atol=1e-4   )   )





      @slow
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     int								=		XLMProphetNetForConditionalGeneration.from_pretrained(
           'microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'   )
       model.to(lowerCamelCase_   )
       lowerCamelCase__	:     Union[str, Any]								=		5_1_2

       lowerCamelCase__	:     Optional[Any]								=		XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'   )

       lowerCamelCase__	:     int								=		(
           'Microsoft Corporation intends to officially end free support for the Windows 7 operating system after'
           ' January 14, 2020, according to the official portal of the organization. From that day, users of this'
           ' system will not be able to receive security updates, which could make their computers vulnerable to'
           ' cyber attacks.'
       )
       lowerCamelCase__	:     Dict								=		(
           'орпорация Microsoft намерена официально прекратить бесплатную поддержку операционной системы Windows 7'
           ' после 14 января 2020 года, сообщается на официальном портале организации . С указанного дня пользователи'
           ' этой системы не смогут получать обновления безопасности, из-за чего их компьютеры могут стать уязвимыми'
           ' к кибератакам.'
       )
       lowerCamelCase__	:     List[Any]								=		(
           '根据该组织的官方门户网站，微软公司打算在2020年1月14日之后正式终止对Windows 7操作系统的免费支持。从那时起，该系统的用户将无法接收安全更新，这可能会使他们的计算机容易受到网络攻击。'
       )

       lowerCamelCase__	:     Dict								=		tokenizer(
           [EN_SENTENCE, RU_SENTENCE, ZH_SENTENCE],	padding=lowerCamelCase_,	max_length=2_5_5,	return_tensors='pt'   ).input_ids
       lowerCamelCase__	:     Any								=		input_ids.to(lowerCamelCase_   )

       lowerCamelCase__	:     Optional[Any]								=		model.generate(
           lowerCamelCase_,	num_beams=1_0,	length_penalty=1.0,	no_repeat_ngram_size=3,	early_stopping=lowerCamelCase_   )
       lowerCamelCase__	:     List[Any]								=		[tokenizer.decode(lowerCamelCase_,	skip_special_tokens=lowerCamelCase_   ) for g in summary_ids]
       lowerCamelCase__	:     int								=		'Microsoft to end Windows 7 free support after January 14, 2020'
       lowerCamelCase__	:     int								=		'Microsoft намерена прекратить бесплатную поддержку Windows 7 после 14 января 2020 года'
       lowerCamelCase__	:     str								=		'微软打算终止对Windows 7操作系统的免费支持'
       self.assertListEqual(
           [EXPECTED_TITLE_EN, EXPECTED_TITLE_RU, EXPECTED_TITLE_ZH],	lowerCamelCase_,	)

       lowerCamelCase__	:     Any								=		model.generate(
           lowerCamelCase_,	num_beams=1,	length_penalty=1.0,	no_repeat_ngram_size=3,	early_stopping=lowerCamelCase_   )
       lowerCamelCase__	:     int								=		[
           tokenizer.convert_ids_to_tokens(lowerCamelCase_,	skip_special_tokens=lowerCamelCase_   ) for g in summary_ids_beama
       ]
       lowerCamelCase__	:     Tuple								=		'▁Microsoft ▁to ▁end ▁free ▁support ▁for ▁Windows ▁7'.split(' '   )
       lowerCamelCase__	:     Any								=		'▁Microsoft ▁намерен а ▁прекрати ть ▁бес плат ную ▁поддержку ▁Windows ▁7 ▁после ▁14 ▁января ▁2020 ▁года'.split(
           ' '   )
       lowerCamelCase__	:     Any								=		'微软 公司 打算 终止 对 Windows ▁7 操作 系统的 免费 支持'.split(' '   )
       self.assertListEqual(
           [EXPECTED_TITLE_EN_BEAM1_TOK, EXPECTED_TITLE_RU_BEAM1_TOK, EXPECTED_TITLE_ZH_BEAM1_TOK],	lowerCamelCase_,	)









"""simple docstring"""
import unittest

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

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class 						a_ (      snake_case_    ,		unittest.TestCase					):







      '''simple docstring'''

      lowerCamelCase__			:							Optional[Any]					=      KandinskyPriorPipeline
      lowerCamelCase__			:							Any					=      ['prompt']
      lowerCamelCase__			:							Any					=      ['prompt', 'negative_prompt']
      lowerCamelCase__			:							str					=      [
          'num_images_per_prompt',
          'generator',
          'num_inference_steps',
          'latents',
          'negative_prompt',
          'guidance_scale',
          'output_type',
          'return_dict',
      ]
      lowerCamelCase__			:							Dict					=      False




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return 3_2




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return 3_2




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return self.time_input_dim




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return self.time_input_dim * 4




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return 1_0_0




      @property
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Any								=		CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'   )
       return tokenizer




      @property
      def      a__				(self   ):

       '''simple docstring'''



       torch.manual_seed(0   )
       lowerCamelCase__	:     int								=		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=3_7,	layer_norm_eps=1e-05,	num_attention_heads=4,	num_hidden_layers=5,	pad_token_id=1,	vocab_size=1_0_0_0,	)
       return CLIPTextModelWithProjection(lowerCamelCase_   )




      @property
      def      a__				(self   ):

       '''simple docstring'''



       torch.manual_seed(0   )

       lowerCamelCase__	:     Optional[int]								=		{
           'num_attention_heads': 2,
           'attention_head_dim': 1_2,
           'embedding_dim': self.text_embedder_hidden_size,
           'num_layers': 1,
       }

       lowerCamelCase__	:     Optional[int]								=		PriorTransformer(**lowerCamelCase_   )
       # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
       lowerCamelCase__	:     Dict								=		nn.Parameter(torch.ones(model.clip_std.shape   )   )
       return model




      @property
      def      a__				(self   ):

       '''simple docstring'''



       torch.manual_seed(0   )
       lowerCamelCase__	:     List[str]								=		CLIPVisionConfig(
           hidden_size=self.text_embedder_hidden_size,	image_size=2_2_4,	projection_dim=self.text_embedder_hidden_size,	intermediate_size=3_7,	num_attention_heads=4,	num_channels=3,	num_hidden_layers=5,	patch_size=1_4,	)

       lowerCamelCase__	:     int								=		CLIPVisionModelWithProjection(lowerCamelCase_   )
       return model




      @property
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Union[str, Any]								=		CLIPImageProcessor(
           crop_size=2_2_4,	do_center_crop=lowerCamelCase_,	do_normalize=lowerCamelCase_,	do_resize=lowerCamelCase_,	image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073],	image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711],	resample=3,	size=2_2_4,	)

       return image_processor




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		self.dummy_prior
       lowerCamelCase__	:     Optional[Any]								=		self.dummy_image_encoder
       lowerCamelCase__	:     Optional[Any]								=		self.dummy_text_encoder
       lowerCamelCase__	:     Union[str, Any]								=		self.dummy_tokenizer
       lowerCamelCase__	:     List[Any]								=		self.dummy_image_processor

       lowerCamelCase__	:     List[Any]								=		UnCLIPScheduler(
           variance_type='fixed_small_log',	prediction_type='sample',	num_train_timesteps=1_0_0_0,	clip_sample=lowerCamelCase_,	clip_sample_range=10.0,	)

       lowerCamelCase__	:     Union[str, Any]								=		{
           'prior': prior,
           'image_encoder': image_encoder,
           'text_encoder': text_encoder,
           'tokenizer': tokenizer,
           'scheduler': scheduler,
           'image_processor': image_processor,
       }

       return components




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_=0   ):

       '''simple docstring'''



       if str(lowerCamelCase_   ).startswith('mps'   ):
        lowerCamelCase__	:     List[Any]								=		torch.manual_seed(lowerCamelCase_   )
       else:
        lowerCamelCase__	:     Optional[int]								=		torch.Generator(device=lowerCamelCase_   ).manual_seed(lowerCamelCase_   )
       lowerCamelCase__	:     List[Any]								=		{
           'prompt': 'horse',
           'generator': generator,
           'guidance_scale': 4.0,
           'num_inference_steps': 2,
           'output_type': 'np',
       }
       return inputs




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     List[str]								=		'cpu'

       lowerCamelCase__	:     str								=		self.get_dummy_components()

       lowerCamelCase__	:     Any								=		self.pipeline_class(**lowerCamelCase_   )
       lowerCamelCase__	:     Dict								=		pipe.to(lowerCamelCase_   )

       pipe.set_progress_bar_config(disable=lowerCamelCase_   )

       lowerCamelCase__	:     Tuple								=		pipe(**self.get_dummy_inputs(lowerCamelCase_   )   )
       lowerCamelCase__	:     Dict								=		output.image_embeds

       lowerCamelCase__	:     Optional[Any]								=		pipe(
           **self.get_dummy_inputs(lowerCamelCase_   ),	return_dict=lowerCamelCase_,	)[0]

       lowerCamelCase__	:     Dict								=		image[0, -1_0:]
       lowerCamelCase__	:     Tuple								=		image_from_tuple[0, -1_0:]

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

       lowerCamelCase__	:     Dict								=		np.array(
           [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]   )

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




      @skip_mps
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Optional[int]								=		torch_device == 'cpu'
       lowerCamelCase__	:     Any								=		True
       lowerCamelCase__	:     List[Any]								=		False

       self._test_inference_batch_single_identical(
           test_max_difference=lowerCamelCase_,	relax_max_difference=lowerCamelCase_,	test_mean_pixel_difference=lowerCamelCase_,	)





      @skip_mps
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		torch_device == 'cpu'
       lowerCamelCase__	:     int								=		False

       self._test_attention_slicing_forward_pass(
           test_max_difference=lowerCamelCase_,	test_mean_pixel_difference=lowerCamelCase_,	)

