
import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, 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.models.speech_to_text_a.modeling_speech_to_text_a import (
       SpeechaTextaDecoder,
       SpeechaTextaForCausalLM,
   )

@require_torch
class 						UpperCAmelCase__   :



     '''simple docstring'''




     def __init__( self       :       Dict	,      UpperCamelCase       :       Dict	,      UpperCamelCase       :       Union[str, Any]=99	,      UpperCamelCase       :       Optional[Any]=13	,      UpperCamelCase       :       Optional[Any]=16	,      UpperCamelCase       :       Tuple=7	,      UpperCamelCase       :       Any=True	,      UpperCamelCase       :       str=True	,      UpperCamelCase       :       Any=True	,      UpperCamelCase       :       Union[str, Any]=False	,      UpperCamelCase       :       Optional[Any]=True	,      UpperCamelCase       :       List[Any]=2	,      UpperCamelCase       :       Tuple=32	,      UpperCamelCase       :       List[str]=4	,      UpperCamelCase       :       str=4	,      UpperCamelCase       :       Any=30	,      UpperCamelCase       :       Any=0	,      UpperCamelCase       :       str=1	,      UpperCamelCase       :       Union[str, Any]=2	,      UpperCamelCase       :       Optional[Any]=None	,      ):





            """simple docstring"""



            _lowercase					:				List[str]           =			parent
            _lowercase					:				Union[str, Any]           =			batch_size
            _lowercase					:				List[Any]           =			decoder_seq_length
            # For common tests
            _lowercase					:				Tuple           =			self.decoder_seq_length
            _lowercase					:				str           =			is_training
            _lowercase					:				Optional[Any]           =			use_attention_mask
            _lowercase					:				Any           =			use_labels

            _lowercase					:				str           =			vocab_size
            _lowercase					:				Tuple           =			d_model
            _lowercase					:				List[str]           =			d_model
            _lowercase					:				Any           =			decoder_layers
            _lowercase					:				Dict           =			decoder_layers
            _lowercase					:				Any           =			decoder_ffn_dim
            _lowercase					:				Dict           =			decoder_attention_heads
            _lowercase					:				Union[str, Any]           =			decoder_attention_heads
            _lowercase					:				int           =			eos_token_id
            _lowercase					:				Any           =			bos_token_id
            _lowercase					:				Union[str, Any]           =			pad_token_id
            _lowercase					:				Any           =			decoder_start_token_id
            _lowercase					:				str           =			use_cache
            _lowercase					:				List[str]           =			max_position_embeddings

            _lowercase					:				Tuple           =			None
            _lowercase					:				int           =			decoder_seq_length
            _lowercase					:				Optional[int]           =			2
            _lowercase					:				List[Any]           =			1




     def   lowerCAmelCase_   ( self       :       Any		):





            """simple docstring"""



            _lowercase					:				List[str]           =			ids_tensor([self.batch_size, self.decoder_seq_length]	,      self.vocab_size		)

            _lowercase					:				Optional[int]           =			None
            if self.use_attention_mask:
                   _lowercase					:				Union[str, Any]           =			ids_tensor([self.batch_size, self.decoder_seq_length]	,      vocab_size=2		)

            _lowercase					:				Any           =			None
            if self.use_labels:
                   _lowercase					:				int           =			ids_tensor([self.batch_size, self.decoder_seq_length]	,      self.vocab_size		)

            _lowercase					:				int           =			SpeechaTextaConfig(
                vocab_size=self.vocab_size	,      d_model=self.d_model	,      decoder_layers=self.decoder_layers	,      decoder_ffn_dim=self.decoder_ffn_dim	,      decoder_attention_heads=self.decoder_attention_heads	,      eos_token_id=self.eos_token_id	,      bos_token_id=self.bos_token_id	,      use_cache=self.use_cache	,      pad_token_id=self.pad_token_id	,      decoder_start_token_id=self.decoder_start_token_id	,      max_position_embeddings=self.max_position_embeddings	,      )

            return (
                config,
                input_ids,
                attention_mask,
                lm_labels,
            )




     def   lowerCAmelCase_   ( self       :       Any	,      UpperCamelCase       :       str	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       int	,      UpperCamelCase       :       int	,      ):





            """simple docstring"""



            _lowercase					:				Tuple           =			True
            _lowercase					:				Dict           =			SpeechaTextaDecoder(config=__UpperCamelCase		).to(__UpperCamelCase		).eval()
            _lowercase					:				Optional[Any]           =			input_ids[:2]

            input_ids[input_ids == 0] += 1
            # first forward pass
            _lowercase					:				str           =			model(__UpperCamelCase	,      use_cache=__UpperCamelCase		)
            _lowercase					:				Optional[Any]           =			model(__UpperCamelCase		)
            _lowercase					:				Optional[int]           =			model(__UpperCamelCase	,      use_cache=__UpperCamelCase		)

            self.parent.assertTrue(len(__UpperCamelCase		) == len(__UpperCamelCase		)		)
            self.parent.assertTrue(len(__UpperCamelCase		) == len(__UpperCamelCase		) + 1		)

            _lowercase					:				Optional[int]           =			outputs['''past_key_values''']

            # create hypothetical next token and extent to next_input_ids
            _lowercase					:				List[Any]           =			ids_tensor((2, 1)	,      config.vocab_size - 1		) + 1

            # append to next input_ids and
            _lowercase					:				Any           =			torch.cat([input_ids, next_tokens]	,      dim=-1		)

            print(__UpperCamelCase		)

            _lowercase					:				Union[str, Any]           =			model(__UpperCamelCase		)['''last_hidden_state''']
            _lowercase					:				Optional[Any]           =			model(__UpperCamelCase	,      past_key_values=__UpperCamelCase		)['''last_hidden_state''']

            # select random slice
            _lowercase					:				Union[str, Any]           =			ids_tensor((1,)	,      output_from_past.shape[-1]		).item()
            _lowercase					:				Optional[int]           =			output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
            _lowercase					:				List[Any]           =			output_from_past[:, 0, random_slice_idx].detach()

            # test that outputs are equal for slice
            assert torch.allclose(__UpperCamelCase	,      __UpperCamelCase	,      atol=1E-3		)




     def   lowerCAmelCase_   ( self       :       List[str]		):





            """simple docstring"""



            _lowercase					:				Dict           =			self.prepare_config_and_inputs()
            (
                (
                _lowercase					
            )       ,	(
                _lowercase					
            )       ,	(
                _lowercase					
            )       ,	(
                _lowercase					
            )       ,						
            )					:				Optional[int]           =			config_and_inputs

            _lowercase					:				List[str]           =			{
                '''input_ids''': input_ids,
                '''attention_mask''': attention_mask,
            }
            return config, inputs_dict




@require_torch
class 						UpperCAmelCase__   (      UpperCAmelCase_      ,						UpperCAmelCase_      ,						UpperCAmelCase_      ,						unittest.TestCase		):



     '''simple docstring'''
     UpperCAmelCase_         =  (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
     UpperCAmelCase_         =  (SpeechaTextaForCausalLM,) if is_torch_available() else ()
     UpperCAmelCase_         =  {'''text-generation''': SpeechaTextaForCausalLM} if is_torch_available() else {}
     UpperCAmelCase_         =  True
     UpperCAmelCase_         =  False




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





            """simple docstring"""



            _lowercase					:				str           =			SpeechaTextaStandaloneDecoderModelTester(self	,      is_training=__UpperCamelCase		)
            _lowercase					:				int           =			ConfigTester(self	,      config_class=__UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Dict		):





            """simple docstring"""



            pass




     def   lowerCAmelCase_   ( self       :       Optional[int]		):





            """simple docstring"""



            pass




     def   lowerCAmelCase_   ( self       :       Dict		):





            """simple docstring"""



            pass




     def   lowerCAmelCase_   ( self       :       Dict		):





            """simple docstring"""



            self.config_tester.run_common_tests()




     def   lowerCAmelCase_   ( self       :       Optional[int]		):





            """simple docstring"""



            _lowercase					:				Union[str, Any]           =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_decoder_model_past(*__UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Tuple		):





            """simple docstring"""



            return



import torch

from diffusers import DDIMScheduler

from .test_schedulers import SchedulerCommonTest

class 						UpperCAmelCase__   (      A_		):



     '''simple docstring'''
     UpperCAmelCase_         =  (DDIMScheduler,)
     UpperCAmelCase_         =  (('''eta''', 0.0), ('''num_inference_steps''', 5_0))




     def   lowerCAmelCase_   ( self       :       List[Any]	,      **UpperCamelCase       :       str		):





            """simple docstring"""



            _lowercase					:				Any           =			{
                '''num_train_timesteps''': 10_00,
                '''beta_start''': 0.0001,
                '''beta_end''': 0.02,
                '''beta_schedule''': '''linear''',
                '''clip_sample''': True,
            }

            config.update(**UpperCamelCase		)
            return config




     def   lowerCAmelCase_   ( self       :       str	,      **UpperCamelCase       :       int		):





            """simple docstring"""



            _lowercase					:				str           =			self.scheduler_classes[0]
            _lowercase					:				Dict           =			self.get_scheduler_config(**UpperCamelCase		)
            _lowercase					:				List[str]           =			scheduler_class(**UpperCamelCase		)

            _lowercase       ,	_lowercase					:				int           =			10, 0.0

            _lowercase					:				List[str]           =			self.dummy_model()
            _lowercase					:				int           =			self.dummy_sample_deter

            scheduler.set_timesteps(UpperCamelCase		)

            for t in scheduler.timesteps:
                   _lowercase					:				Union[str, Any]           =			model(UpperCamelCase	,      UpperCamelCase		)
                   _lowercase					:				List[Any]           =			scheduler.step(UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase		).prev_sample

            return sample




     def   lowerCAmelCase_   ( self       :       Dict		):





            """simple docstring"""



            for timesteps in [1_00, 5_00, 10_00]:
                   self.check_over_configs(num_train_timesteps=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       List[Any]		):





            """simple docstring"""



            for steps_offset in [0, 1]:
                   self.check_over_configs(steps_offset=UpperCamelCase		)

            _lowercase					:				List[Any]           =			self.scheduler_classes[0]
            _lowercase					:				Tuple           =			self.get_scheduler_config(steps_offset=1		)
            _lowercase					:				Union[str, Any]           =			scheduler_class(**UpperCamelCase		)
            scheduler.set_timesteps(5		)
            assert torch.equal(scheduler.timesteps	,      torch.LongTensor([8_01, 6_01, 4_01, 2_01, 1]		)		)




     def   lowerCAmelCase_   ( self       :       Optional[Any]		):





            """simple docstring"""



            for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1]	,      [0.002, 0.02, 0.2, 2]		):
                   self.check_over_configs(beta_start=UpperCamelCase	,      beta_end=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Any		):





            """simple docstring"""



            for schedule in ["linear", "squaredcos_cap_v2"]:
                   self.check_over_configs(beta_schedule=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Optional[int]		):





            """simple docstring"""



            for prediction_type in ["epsilon", "v_prediction"]:
                   self.check_over_configs(prediction_type=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Optional[Any]		):





            """simple docstring"""



            for clip_sample in [True, False]:
                   self.check_over_configs(clip_sample=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Any		):





            """simple docstring"""



            for timestep_spacing in ["trailing", "leading"]:
                   self.check_over_configs(timestep_spacing=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       List[str]		):





            """simple docstring"""



            for rescale_betas_zero_snr in [True, False]:
                   self.check_over_configs(rescale_betas_zero_snr=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Optional[Any]		):





            """simple docstring"""



            self.check_over_configs(thresholding=UpperCamelCase		)
            for threshold in [0.5, 1.0, 2.0]:
                   for prediction_type in ["epsilon", "v_prediction"]:
                          self.check_over_configs(
                              thresholding=UpperCamelCase	,      prediction_type=UpperCamelCase	,      sample_max_value=UpperCamelCase	,      )




     def   lowerCAmelCase_   ( self       :       str		):





            """simple docstring"""



            for t in [1, 10, 49]:
                   self.check_over_forward(time_step=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Optional[int]		):





            """simple docstring"""



            for t, num_inference_steps in zip([1, 10, 50]	,      [10, 50, 5_00]		):
                   self.check_over_forward(time_step=UpperCamelCase	,      num_inference_steps=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       Optional[Any]		):





            """simple docstring"""



            for t, eta in zip([1, 10, 49]	,      [0.0, 0.5, 1.0]		):
                   self.check_over_forward(time_step=UpperCamelCase	,      eta=UpperCamelCase		)




     def   lowerCAmelCase_   ( self       :       List[Any]		):





            """simple docstring"""



            _lowercase					:				int           =			self.scheduler_classes[0]
            _lowercase					:				str           =			self.get_scheduler_config()
            _lowercase					:				int           =			scheduler_class(**UpperCamelCase		)

            assert torch.sum(torch.abs(scheduler._get_variance(0	,      0		) - 0.0		)		) < 1E-5
            assert torch.sum(torch.abs(scheduler._get_variance(4_20	,      4_00		) - 0.14771		)		) < 1E-5
            assert torch.sum(torch.abs(scheduler._get_variance(9_80	,      9_60		) - 0.32460		)		) < 1E-5
            assert torch.sum(torch.abs(scheduler._get_variance(0	,      0		) - 0.0		)		) < 1E-5
            assert torch.sum(torch.abs(scheduler._get_variance(4_87	,      4_86		) - 0.00979		)		) < 1E-5
            assert torch.sum(torch.abs(scheduler._get_variance(9_99	,      9_98		) - 0.02		)		) < 1E-5




     def   lowerCAmelCase_   ( self       :       List[Any]		):





            """simple docstring"""



            _lowercase					:				Any           =			self.full_loop()

            _lowercase					:				Optional[Any]           =			torch.sum(torch.abs(UpperCamelCase		)		)
            _lowercase					:				Union[str, Any]           =			torch.mean(torch.abs(UpperCamelCase		)		)

            assert abs(result_sum.item() - 172.0067		) < 1E-2
            assert abs(result_mean.item() - 0.223967		) < 1E-3




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





            """simple docstring"""



            _lowercase					:				List[Any]           =			self.full_loop(prediction_type='''v_prediction'''		)

            _lowercase					:				int           =			torch.sum(torch.abs(UpperCamelCase		)		)
            _lowercase					:				Union[str, Any]           =			torch.mean(torch.abs(UpperCamelCase		)		)

            assert abs(result_sum.item() - 52.5302		) < 1E-2
            assert abs(result_mean.item() - 0.0684		) < 1E-3




     def   lowerCAmelCase_   ( self       :       Any		):





            """simple docstring"""



            _lowercase					:				Tuple           =			self.full_loop(set_alpha_to_one=UpperCamelCase	,      beta_start=0.01		)
            _lowercase					:				List[Any]           =			torch.sum(torch.abs(UpperCamelCase		)		)
            _lowercase					:				Any           =			torch.mean(torch.abs(UpperCamelCase		)		)

            assert abs(result_sum.item() - 149.8295		) < 1E-2
            assert abs(result_mean.item() - 0.1951		) < 1E-3




     def   lowerCAmelCase_   ( self       :       Any		):





            """simple docstring"""



            _lowercase					:				int           =			self.full_loop(set_alpha_to_one=UpperCamelCase	,      beta_start=0.01		)
            _lowercase					:				Optional[int]           =			torch.sum(torch.abs(UpperCamelCase		)		)
            _lowercase					:				Dict           =			torch.mean(torch.abs(UpperCamelCase		)		)

            assert abs(result_sum.item() - 149.0784		) < 1E-2
            assert abs(result_mean.item() - 0.1941		) < 1E-3