'''simple docstring'''




import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, slow, torch_device

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin





class        SCREAMING_SNAKE_CASE   (				__a	,						__a	,						unittest.TestCase ):
  """simple docstring"""







  __A     =		UNetaDModel
  __A     =		"sample"



  @property
  def   a   (  self   :					Tuple					):


        """simple docstring"""



        _lowerCAmelCase		   =   4
        _lowerCAmelCase		   =   14
        _lowerCAmelCase		   =   16

        _lowerCAmelCase		   =   floats_tensor((batch_size, num_features, seq_len)					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.tensor([10] * batch_size					).to(__lowerCAmelCase					)

        return {"sample": noise, "timestep": time_step}



  @property
  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        return (4, 14, 16)



  @property
  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        return (4, 14, 16)



  def   a   (  self   :					str					):


        """simple docstring"""



        pass



  def   a   (  self   :					str					):


        """simple docstring"""



        pass



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        super().test_determinism()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					List[Any]					):


        """simple docstring"""



        super().test_outputs_equivalence()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Union[str, Any]					):


        """simple docstring"""



        super().test_from_save_pretrained()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					List[Any]					):


        """simple docstring"""



        super().test_from_save_pretrained_variant()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Any					):


        """simple docstring"""



        super().test_model_from_pretrained()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Dict					):


        """simple docstring"""



        super().test_output()



  def   a   (  self   :					int					):


        """simple docstring"""



        _lowerCAmelCase		   =   {
            'block_out_channels': (32, 64, 128, 256),
            'in_channels': 14,
            'out_channels': 14,
            'time_embedding_type': 'positional',
            'use_timestep_embedding': True,
            'flip_sin_to_cos': False,
            'freq_shift': 1.0,
            'out_block_type': 'OutConv1DBlock',
            'mid_block_type': 'MidResTemporalBlock1D',
            'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'),
            'up_block_types': ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D'),
            'act_fn': 'mish',
        }
        _lowerCAmelCase		   =   self.dummy_input
        return init_dict, inputs_dict



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					List[Any]					):


        """simple docstring"""



        _lowerCAmelCase   ,  _lowerCAmelCase		   =   UNetaDModel.from_pretrained(
            'bglick13/hopper-medium-v2-value-function-hor32'					,					output_loading_info=__lowerCAmelCase					,					subfolder='unet'					)
        self.assertIsNotNone(__lowerCAmelCase					)
        self.assertEqual(len(loading_info['missing_keys']					)					,					0					)

        model.to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   model(**self.dummy_input					)

        assert image is not None, "Make sure output is not None"



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					str					):


        """simple docstring"""



        _lowerCAmelCase		   =   UNetaDModel.from_pretrained('bglick13/hopper-medium-v2-value-function-hor32'					,					subfolder='unet'					)
        torch.manual_seed(0					)
        if torch.cuda.is_available():
              torch.cuda.manual_seed_all(0					)

        _lowerCAmelCase		   =   model.config.in_channels
        _lowerCAmelCase		   =   16
        _lowerCAmelCase		   =   torch.randn((1, seq_len, num_features)					).permute(
            0					,					2					,					1					)  # match original, we can update values and remove
        _lowerCAmelCase		   =   torch.full((num_features,)					,					0					)

        with torch.no_grad():
              _lowerCAmelCase		   =   model(__lowerCAmelCase					,					__lowerCAmelCase					).sample.permute(0					,					2					,					1					)

        _lowerCAmelCase		   =   output[0, -3:, -3:].flatten()
        # fmt: off
        _lowerCAmelCase		   =   torch.tensor([-2.1_3_7_1_7_2, 1.1_4_2_6_0_1_6, 0.3_6_8_8_6_8_7, -0.7_6_6_9_2_2, 0.7_3_0_3_1_4_6, 0.1_1_0_3_8_8_6_4, -0.4_7_6_0_6_3_3, 0.1_3_2_7_0_1_7_2, 0.0_2_5_9_1_3_4_8]					)
        # fmt: on
        self.assertTrue(torch.allclose(__lowerCAmelCase					,					__lowerCAmelCase					,					rtol=1e-3					)					)



  def   a   (  self   :					str					):


        """simple docstring"""



        pass



  @slow
  def   a   (  self   :					Dict					):


        """simple docstring"""



        _lowerCAmelCase		   =   'harmonai/maestro-150k'
        _lowerCAmelCase		   =   UNetaDModel.from_pretrained(__lowerCAmelCase					,					subfolder='unet'					)
        model.to(__lowerCAmelCase					)

        _lowerCAmelCase		   =   6_5536
        _lowerCAmelCase		   =   torch.sin(torch.arange(__lowerCAmelCase					)[None, None, :].repeat(1					,					2					,					1					)					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.tensor([1]					).to(__lowerCAmelCase					)

        with torch.no_grad():
              _lowerCAmelCase		   =   model(__lowerCAmelCase					,					__lowerCAmelCase					).sample

        _lowerCAmelCase		   =   output.abs().sum()
        _lowerCAmelCase		   =   output.abs().max()

        assert (output_sum - 2_2_4.0_8_9_6).abs() < 0.5
        assert (output_max - 0.0_6_0_7).abs() < 4e-4





class        SCREAMING_SNAKE_CASE   (				__a	,						__a	,						unittest.TestCase ):
  """simple docstring"""







  __A     =		UNetaDModel
  __A     =		"sample"



  @property
  def   a   (  self   :					Dict					):


        """simple docstring"""



        _lowerCAmelCase		   =   4
        _lowerCAmelCase		   =   14
        _lowerCAmelCase		   =   16

        _lowerCAmelCase		   =   floats_tensor((batch_size, num_features, seq_len)					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.tensor([10] * batch_size					).to(__lowerCAmelCase					)

        return {"sample": noise, "timestep": time_step}



  @property
  def   a   (  self   :					Any					):


        """simple docstring"""



        return (4, 14, 16)



  @property
  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        return (4, 14, 1)



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Dict					):


        """simple docstring"""



        super().test_determinism()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					int					):


        """simple docstring"""



        super().test_outputs_equivalence()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Dict					):


        """simple docstring"""



        super().test_from_save_pretrained()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Any					):


        """simple docstring"""



        super().test_from_save_pretrained_variant()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        super().test_model_from_pretrained()



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					Any					):


        """simple docstring"""



        _lowerCAmelCase   ,  _lowerCAmelCase		   =   self.prepare_init_args_and_inputs_for_common()
        _lowerCAmelCase		   =   self.model_class(**__lowerCAmelCase					)
        model.to(__lowerCAmelCase					)
        model.eval()

        with torch.no_grad():
              _lowerCAmelCase		   =   model(**__lowerCAmelCase					)

              if isinstance(__lowerCAmelCase					,					__lowerCAmelCase					):
                    _lowerCAmelCase		   =   output.sample

        self.assertIsNotNone(__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.Size((inputs_dict['sample'].shape[0], 1)					)
        self.assertEqual(output.shape					,					__lowerCAmelCase					,					'Input and output shapes do not match'					)



  def   a   (  self   :					Optional[Any]					):


        """simple docstring"""



        pass



  def   a   (  self   :					Union[str, Any]					):


        """simple docstring"""



        pass



  def   a   (  self   :					Union[str, Any]					):


        """simple docstring"""



        _lowerCAmelCase		   =   {
            'in_channels': 14,
            'out_channels': 14,
            'down_block_types': ['DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'],
            'up_block_types': [],
            'out_block_type': 'ValueFunction',
            'mid_block_type': 'ValueFunctionMidBlock1D',
            'block_out_channels': [32, 64, 128, 256],
            'layers_per_block': 1,
            'downsample_each_block': True,
            'use_timestep_embedding': True,
            'freq_shift': 1.0,
            'flip_sin_to_cos': False,
            'time_embedding_type': 'positional',
            'act_fn': 'mish',
        }
        _lowerCAmelCase		   =   self.dummy_input
        return init_dict, inputs_dict



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					List[str]					):


        """simple docstring"""



        _lowerCAmelCase   ,  _lowerCAmelCase		   =   UNetaDModel.from_pretrained(
            'bglick13/hopper-medium-v2-value-function-hor32'					,					output_loading_info=__lowerCAmelCase					,					subfolder='value_function'					)
        self.assertIsNotNone(__lowerCAmelCase					)
        self.assertEqual(len(vf_loading_info['missing_keys']					)					,					0					)

        value_function.to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   value_function(**self.dummy_input					)

        assert image is not None, "Make sure output is not None"



  @unittest.skipIf(torch_device == 'mps'					,					'mish op not supported in MPS'					)
  def   a   (  self   :					int					):


        """simple docstring"""



        _lowerCAmelCase   ,  _lowerCAmelCase		   =   UNetaDModel.from_pretrained(
            'bglick13/hopper-medium-v2-value-function-hor32'					,					output_loading_info=__lowerCAmelCase					,					subfolder='value_function'					)
        torch.manual_seed(0					)
        if torch.cuda.is_available():
              torch.cuda.manual_seed_all(0					)

        _lowerCAmelCase		   =   value_function.config.in_channels
        _lowerCAmelCase		   =   14
        _lowerCAmelCase		   =   torch.randn((1, seq_len, num_features)					).permute(
            0					,					2					,					1					)  # match original, we can update values and remove
        _lowerCAmelCase		   =   torch.full((num_features,)					,					0					)

        with torch.no_grad():
              _lowerCAmelCase		   =   value_function(__lowerCAmelCase					,					__lowerCAmelCase					).sample

        # fmt: off
        _lowerCAmelCase		   =   torch.tensor([1_6_5.2_5] * seq_len					)
        # fmt: on
        self.assertTrue(torch.allclose(__lowerCAmelCase					,					__lowerCAmelCase					,					rtol=1e-3					)					)



  def   a   (  self   :					List[Any]					):


        """simple docstring"""



        pass






'''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        SCREAMING_SNAKE_CASE   (				unittest.TestCase ):
  """simple docstring"""







  @slow
  def   a   (  self   :					List[str]					):


        """simple docstring"""



        _lowerCAmelCase		   =   XLMProphetNetForConditionalGeneration.from_pretrained('microsoft/xprophetnet-large-wiki100-cased'					)
        model.to(__lowerCAmelCase					)

        # encoder-decoder outputs
        _lowerCAmelCase		   =   torch.tensor([[17, 9_6208, 10_3471, 2]]					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.tensor(
            [[2, 250, 9953, 34, 6_9489, 1620, 32, 11_8424, 624, 210, 105, 2913, 1032, 351]]					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   model(
            input_ids=__lowerCAmelCase					,					attention_mask=__lowerCAmelCase					,					encoder_outputs=__lowerCAmelCase					,					decoder_input_ids=__lowerCAmelCase					)
        _lowerCAmelCase		   =   output[0]
        _lowerCAmelCase		   =   torch.Size((1, 14, 25_0012)					)
        self.assertEqual(output_predited_logis.shape					,					__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.tensor(
            [[[-6.3_9_8_6, -8.2_3_9_1, 1_2.5_1_8_9], [-6.3_2_8_9, -8.0_8_6_4, 1_2.6_2_1_1], [-6.2_4_1_8, -8.0_4_4_5, 1_2.7_9_6_8]]]					).to(__lowerCAmelCase					)
        self.assertTrue(torch.allclose(output_predited_logis[:, :3, :3]					,					__lowerCAmelCase					,					atol=1e-4					)					)

        # encoder outputs
        _lowerCAmelCase		   =   model.prophetnet.encoder(__lowerCAmelCase					)[0]
        _lowerCAmelCase		   =   torch.tensor(
            [[[-1.4_2_6_0, -0.7_6_2_8, 0.8_4_5_3], [-1.4_7_1_9, -0.1_3_9_1, 0.7_8_0_7], [-1.7_6_7_8, 0.0_1_1_4, 0.4_6_4_6]]]					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   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
        _lowerCAmelCase		   =   model.prophetnet.decoder(
            __lowerCAmelCase					,					encoder_hidden_states=__lowerCAmelCase					,					)
        _lowerCAmelCase		   =   decoder_outputs[1].view(1					,					model.config.ngram					,					14					,					-1					)
        _lowerCAmelCase		   =   model.lm_head(__lowerCAmelCase					)
        _lowerCAmelCase		   =   predicting_streams_logits[:, 0]
        self.assertTrue(torch.allclose(next_first_stream_logits[:, :3, :3]					,					__lowerCAmelCase					,					atol=1e-4					)					)



  @slow
  def   a   (  self   :					Tuple					):


        """simple docstring"""



        _lowerCAmelCase		   =   XLMProphetNetForConditionalGeneration.from_pretrained(
            'microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'					)
        model.to(__lowerCAmelCase					)

        _lowerCAmelCase		   =   torch.tensor([[17, 9_6208, 10_3471, 2]]					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   torch.tensor(
            [[2, 250, 9953, 34, 6_9489, 1620, 32, 11_8424, 624, 210, 105, 2913, 1032, 351]]					).to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   model(
            input_ids=__lowerCAmelCase					,					attention_mask=__lowerCAmelCase					,					encoder_outputs=__lowerCAmelCase					,					decoder_input_ids=__lowerCAmelCase					)
        _lowerCAmelCase		   =   output[0]
        _lowerCAmelCase		   =   torch.Size((1, 14, 25_0012)					)
        self.assertEqual(output_predited_logis.shape					,					__lowerCAmelCase					)
        # compare the actual values for a slice.
        _lowerCAmelCase		   =   torch.tensor(
            [[[-9.2_2_5_3, -9.7_1_7_3, -6.3_5_2_9], [-7.6_7_0_1, -9.0_1_4_5, -1.9_3_8_2], [-8.0_1_9_5, -7.0_0_0_4, -0.1_5_2_3]]]					).to(__lowerCAmelCase					)

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



  @slow
  def   a   (  self   :					int					):


        """simple docstring"""



        _lowerCAmelCase		   =   XLMProphetNetForConditionalGeneration.from_pretrained(
            'microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'					)
        model.to(__lowerCAmelCase					)
        _lowerCAmelCase		   =   512

        _lowerCAmelCase		   =   XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased-xglue-ntg'					)

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

        _lowerCAmelCase		   =   tokenizer(
            [EN_SENTENCE, RU_SENTENCE, ZH_SENTENCE]					,					padding=__lowerCAmelCase					,					max_length=255					,					return_tensors='pt'					).input_ids
        _lowerCAmelCase		   =   input_ids.to(__lowerCAmelCase					)

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

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





