
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      UpperCamelCase_			(			unittest.TestCase		):







      '''simple docstring'''



      @slow
      def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]    )     ->Optional[Any]:
          lowercase						    =			XLMProphetNetForConditionalGeneration.from_pretrained("microsoft/xprophetnet-large-wiki100-cased"    )
          model.to(lowerCAmelCase__    )

          # encoder-decoder outputs
          lowercase						    =			torch.tensor([[17, 96208, 103471, 2]]    ).to(lowerCAmelCase__    )
          lowercase						    =			torch.tensor(
              [[2, 250, 9953, 34, 69489, 1620, 32, 118424, 624, 210, 105, 2913, 1032, 351]]    ).to(lowerCAmelCase__    )
          lowercase						    =			model(
              input_ids=lowerCAmelCase__		,						attention_mask=lowerCAmelCase__		,						encoder_outputs=lowerCAmelCase__		,						decoder_input_ids=lowerCAmelCase__    )
          lowercase						    =			output[0]
          lowercase						    =			torch.Size((1, 14, 250012)    )
          self.assertEqual(output_predited_logis.shape		,						lowerCAmelCase__    )
          lowercase						    =			torch.tensor(
              [[[-6.39_86, -8.23_91, 12.51_89], [-6.32_89, -8.08_64, 12.62_11], [-6.24_18, -8.04_45, 12.79_68]]]    ).to(lowerCAmelCase__    )
          self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3]		,						lowerCAmelCase__		,						atol=1E-4    )    )

          # encoder outputs
          lowercase						    =			model.prophetnet.encoder(lowerCAmelCase__    )[0]
          lowercase						    =			torch.tensor(
              [[[-1.42_60, -0.76_28, 0.84_53], [-1.47_19, -0.13_91, 0.78_07], [-1.76_78, 0.01_14, 0.46_46]]]    ).to(lowerCAmelCase__    )
          lowercase						    =			torch.Size((1, 4, 1024)    )
          self.assertEqual(encoder_outputs.shape		,						lowerCAmelCase__    )
          self.assertTrue(torch.allclose(encoder_outputs[:, :3, :3]		,						lowerCAmelCase__		,						atol=1E-4    )    )

          # decoder outputs
          lowercase						    =			model.prophetnet.decoder(
              lowerCAmelCase__		,						encoder_hidden_states=lowerCAmelCase__		,						)
          lowercase						    =			decoder_outputs[1].view(1		,						model.config.ngram		,						14		,						-1    )
          lowercase						    =			model.lm_head(lowerCAmelCase__    )
          lowercase						    =			predicting_streams_logits[:, 0]
          self.assertTrue(torch.allclose(next_first_stream_logits[:, :3, :3]		,						lowerCAmelCase__		,						atol=1E-4    )    )





      @slow
      def 					SCREAMING_SNAKE_CASE( self       :str    )     ->Union[str, Any]:
          lowercase						    =			XLMProphetNetForConditionalGeneration.from_pretrained(
              "microsoft/xprophetnet-large-wiki100-cased-xglue-ntg"    )
          model.to(lowerCAmelCase__    )

          lowercase						    =			torch.tensor([[17, 96208, 103471, 2]]    ).to(lowerCAmelCase__    )
          lowercase						    =			torch.tensor(
              [[2, 250, 9953, 34, 69489, 1620, 32, 118424, 624, 210, 105, 2913, 1032, 351]]    ).to(lowerCAmelCase__    )
          lowercase						    =			model(
              input_ids=lowerCAmelCase__		,						attention_mask=lowerCAmelCase__		,						encoder_outputs=lowerCAmelCase__		,						decoder_input_ids=lowerCAmelCase__    )
          lowercase						    =			output[0]
          lowercase						    =			torch.Size((1, 14, 250012)    )
          self.assertEqual(output_predited_logis.shape		,						lowerCAmelCase__    )
          # compare the actual values for a slice.
          lowercase						    =			torch.tensor(
              [[[-9.22_53, -9.71_73, -6.35_29], [-7.67_01, -9.01_45, -1.93_82], [-8.01_95, -7.00_04, -0.15_23]]]    ).to(lowerCAmelCase__    )

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





      @slow
      def 					SCREAMING_SNAKE_CASE( self       :str    )     ->List[Any]:
          lowercase						    =			XLMProphetNetForConditionalGeneration.from_pretrained(
              "microsoft/xprophetnet-large-wiki100-cased-xglue-ntg"    )
          model.to(lowerCAmelCase__    )
          lowercase						    =			512

          lowercase						    =			XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased-xglue-ntg"    )

          lowercase						    =			(
              "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."
          )
          lowercase						    =			(
              "орпорация Microsoft намерена официально прекратить бесплатную поддержку операционной системы Windows 7"
              " после 14 января 2020 года, сообщается на официальном портале организации . С указанного дня пользователи"
              " этой системы не смогут получать обновления безопасности, из-за чего их компьютеры могут стать уязвимыми"
              " к кибератакам."
          )
          lowercase						    =			(
              "根据该组织的官方门户网站，微软公司打算在2020年1月14日之后正式终止对Windows 7操作系统的免费支持。从那时起，该系统的用户将无法接收安全更新，这可能会使他们的计算机容易受到网络攻击。"
          )

          lowercase						    =			tokenizer(
              [EN_SENTENCE, RU_SENTENCE, ZH_SENTENCE]		,						padding=lowerCAmelCase__		,						max_length=255		,						return_tensors="pt"    ).input_ids
          lowercase						    =			input_ids.to(lowerCAmelCase__    )

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

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


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import VideoMAEImageProcessor





class      UpperCamelCase_			(			unittest.TestCase		):







						'''simple docstring'''



						def __init__( self       :Any		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :Optional[int]=7		,						lowerCAmelCase__       :Tuple=3		,						lowerCAmelCase__       :List[str]=10		,						lowerCAmelCase__       :Dict=18		,						lowerCAmelCase__       :int=30		,						lowerCAmelCase__       :List[str]=400		,						lowerCAmelCase__       :Union[str, Any]=True		,						lowerCAmelCase__       :List[Any]=None		,						lowerCAmelCase__       :List[Any]=True		,						lowerCAmelCase__       :int=[0.5, 0.5, 0.5]		,						lowerCAmelCase__       :Optional[Any]=[0.5, 0.5, 0.5]		,						lowerCAmelCase__       :Dict=None		,						)     ->Union[str, Any]:
										lowercase						    =			size if size is not None else {"shortest_edge": 18}
										lowercase						    =			crop_size if crop_size is not None else {"height": 18, "width": 18}

										lowercase						    =			parent
										lowercase						    =			batch_size
										lowercase						    =			num_channels
										lowercase						    =			num_frames
										lowercase						    =			image_size
										lowercase						    =			min_resolution
										lowercase						    =			max_resolution
										lowercase						    =			do_resize
										lowercase						    =			size
										lowercase						    =			do_normalize
										lowercase						    =			image_mean
										lowercase						    =			image_std
										lowercase						    =			crop_size





						def 					SCREAMING_SNAKE_CASE( self       :List[Any]    )     ->int:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "crop_size": self.crop_size,
										}





@require_torch
@require_vision
class      UpperCamelCase_			(			__a		,						unittest.TestCase		):







						'''simple docstring'''



						UpperCamelCase			:     Union[str, Any]							      =				VideoMAEImageProcessor if is_vision_available() else None





						def 					SCREAMING_SNAKE_CASE( self       :List[str]    )     ->List[Any]:
										lowercase						    =			VideoMAEImageProcessingTester(self    )





						@property
						def 					SCREAMING_SNAKE_CASE( self       :List[Any]    )     ->Any:
										return self.image_processor_tester.prepare_image_processor_dict()





						def 					SCREAMING_SNAKE_CASE( self       :Dict    )     ->str:
										lowercase						    =			self.image_processing_class(**self.image_processor_dict    )
										self.assertTrue(hasattr(lowerCAmelCase__		,						"image_mean"    )    )
										self.assertTrue(hasattr(lowerCAmelCase__		,						"image_std"    )    )
										self.assertTrue(hasattr(lowerCAmelCase__		,						"do_normalize"    )    )
										self.assertTrue(hasattr(lowerCAmelCase__		,						"do_resize"    )    )
										self.assertTrue(hasattr(lowerCAmelCase__		,						"do_center_crop"    )    )
										self.assertTrue(hasattr(lowerCAmelCase__		,						"size"    )    )





						def 					SCREAMING_SNAKE_CASE( self       :str    )     ->Tuple:
										lowercase						    =			self.image_processing_class.from_dict(self.image_processor_dict    )
										self.assertEqual(image_processor.size		,						{"shortest_edge": 18}    )
										self.assertEqual(image_processor.crop_size		,						{"height": 18, "width": 18}    )

										lowercase						    =			self.image_processing_class.from_dict(self.image_processor_dict		,						size=42		,						crop_size=84    )
										self.assertEqual(image_processor.size		,						{"shortest_edge": 42}    )
										self.assertEqual(image_processor.crop_size		,						{"height": 84, "width": 84}    )





						def 					SCREAMING_SNAKE_CASE( self       :int    )     ->Any:
										pass





						def 					SCREAMING_SNAKE_CASE( self       :List[Any]    )     ->List[Any]:
										# Initialize image_processing
										lowercase						    =			self.image_processing_class(**self.image_processor_dict    )
										# create random PIL videos
										lowercase						    =			prepare_video_inputs(self.image_processor_tester		,						equal_resolution=lowerCAmelCase__    )
										for video in video_inputs:
														self.assertIsInstance(lowerCAmelCase__		,						lowerCAmelCase__    )
														self.assertIsInstance(video[0]		,						Image.Image    )

										# Test not batched input
										lowercase						    =			image_processing(video_inputs[0]		,						return_tensors="pt"    ).pixel_values
										self.assertEqual(
										    encoded_videos.shape		,						(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["height"],
										        self.image_processor_tester.crop_size["width"],
										    )		,						)

										# Test batched
										lowercase						    =			image_processing(lowerCAmelCase__		,						return_tensors="pt"    ).pixel_values
										self.assertEqual(
										    encoded_videos.shape		,						(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["height"],
										        self.image_processor_tester.crop_size["width"],
										    )		,						)





						def 					SCREAMING_SNAKE_CASE( self       :Dict    )     ->Optional[int]:
										# Initialize image_processing
										lowercase						    =			self.image_processing_class(**self.image_processor_dict    )
										# create random numpy tensors
										lowercase						    =			prepare_video_inputs(self.image_processor_tester		,						equal_resolution=lowerCAmelCase__		,						numpify=lowerCAmelCase__    )
										for video in video_inputs:
														self.assertIsInstance(lowerCAmelCase__		,						lowerCAmelCase__    )
														self.assertIsInstance(video[0]		,						np.ndarray    )

										# Test not batched input
										lowercase						    =			image_processing(video_inputs[0]		,						return_tensors="pt"    ).pixel_values
										self.assertEqual(
										    encoded_videos.shape		,						(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["height"],
										        self.image_processor_tester.crop_size["width"],
										    )		,						)

										# Test batched
										lowercase						    =			image_processing(lowerCAmelCase__		,						return_tensors="pt"    ).pixel_values
										self.assertEqual(
										    encoded_videos.shape		,						(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["height"],
										        self.image_processor_tester.crop_size["width"],
										    )		,						)





						def 					SCREAMING_SNAKE_CASE( self       :Any    )     ->Dict:
										# Initialize image_processing
										lowercase						    =			self.image_processing_class(**self.image_processor_dict    )
										# create random PyTorch tensors
										lowercase						    =			prepare_video_inputs(self.image_processor_tester		,						equal_resolution=lowerCAmelCase__		,						torchify=lowerCAmelCase__    )
										for video in video_inputs:
														self.assertIsInstance(lowerCAmelCase__		,						lowerCAmelCase__    )
														self.assertIsInstance(video[0]		,						torch.Tensor    )

										# Test not batched input
										lowercase						    =			image_processing(video_inputs[0]		,						return_tensors="pt"    ).pixel_values
										self.assertEqual(
										    encoded_videos.shape		,						(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["height"],
										        self.image_processor_tester.crop_size["width"],
										    )		,						)

										# Test batched
										lowercase						    =			image_processing(lowerCAmelCase__		,						return_tensors="pt"    ).pixel_values
										self.assertEqual(
										    encoded_videos.shape		,						(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["height"],
										        self.image_processor_tester.crop_size["width"],
										    )		,						)
