





import unittest

from transformers import TrOCRConfig
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.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM





@require_torch
class 				A_				:







       """simple docstring"""





       def __init__(    self	:	str						,__A	:	Optional[Any]						,__A	:	str=99						,__A	:	List[Any]=13						,__A	:	Any=16						,__A	:	Optional[int]=7						,__A	:	Dict=True						,__A	:	int=True						,__A	:	Optional[int]=True						,__A	:	Union[str, Any]=False						,__A	:	List[Any]=True						,__A	:	int=2						,__A	:	List[Any]=32						,__A	:	Tuple=4						,__A	:	List[Any]=4						,__A	:	Optional[int]=30						,__A	:	Optional[Any]=0						,__A	:	int=1						,__A	:	Optional[int]=2						,__A	:	Union[str, Any]=None						,)						->					Dict:
         _lowercase							= parent
         _lowercase							= batch_size
         _lowercase							= decoder_seq_length
         # For common tests
         _lowercase							= self.decoder_seq_length
         _lowercase							= is_training
         _lowercase							= use_attention_mask
         _lowercase							= use_labels

         _lowercase							= vocab_size
         _lowercase							= d_model
         _lowercase							= d_model
         _lowercase							= decoder_layers
         _lowercase							= decoder_layers
         _lowercase							= decoder_ffn_dim
         _lowercase							= decoder_attention_heads
         _lowercase							= decoder_attention_heads
         _lowercase							= eos_token_id
         _lowercase							= bos_token_id
         _lowercase							= pad_token_id
         _lowercase							= decoder_start_token_id
         _lowercase							= use_cache
         _lowercase							= max_position_embeddings

         _lowercase							= None
         _lowercase							= decoder_seq_length
         _lowercase							= 2
         _lowercase							= 1







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

         _lowercase							= None
         if self.use_attention_mask:
           _lowercase							= ids_tensor([self.batch_size, self.decoder_seq_length]						,vocab_size=2  )

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

         _lowercase							= TrOCRConfig(
             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    __UpperCAmelCase (    self	:	Any						,__A	:	Union[str, Any]						,__A	:	int						,__A	:	Any						,__A	:	Optional[Any]						,)						->					List[Any]:
         _lowercase							= True
         _lowercase							= TrOCRDecoder(config=__A  ).to(__A  ).eval()
         _lowercase							= input_ids[:2]

         input_ids[input_ids == 0] += 1
         # first forward pass
         _lowercase							= model(__A						,use_cache=__A  )
         _lowercase							= model(__A  )
         _lowercase							= model(__A						,use_cache=__A  )

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

         _lowercase							= outputs['past_key_values']

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

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

         _lowercase							= model(__A  )['last_hidden_state']
         _lowercase							= model(__A						,past_key_values=__A  )['last_hidden_state']

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

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







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					str:
         _lowercase							= self.prepare_config_and_inputs()
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= config_and_inputs

         _lowercase							= {'input_ids': input_ids, 'attention_mask': attention_mask}
         return config, inputs_dict





@require_torch
class 				A_				(      UpperCAmelCase					,	UpperCAmelCase					,	UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : str      =  (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE_      : int      =  (TrOCRForCausalLM,) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE_      : str      =  {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
       SCREAMING_SNAKE_CASE_      : Dict      =  True
       SCREAMING_SNAKE_CASE_      : Tuple      =  False







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Tuple:
         _lowercase							= TrOCRStandaloneDecoderModelTester(self						,is_training=__A  )
         _lowercase							= ConfigTester(self						,config_class=__A  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Dict:
         pass







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







       def    __UpperCAmelCase (    self	:	int  )						->					Dict:
         pass







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Optional[Any]:
         self.config_tester.run_common_tests()







       def    __UpperCAmelCase (    self	:	Dict  )						->					str:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past(*__A  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Any:
         return







       @unittest.skip('The model doesn\'t support left padding'  )  # and it's not used enough to be worth fixing :)
       def    __UpperCAmelCase (    self	:	Dict  )						->					Any:
         pass






import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : int      =  (DEISMultistepScheduler,)
       SCREAMING_SNAKE_CASE_      : List[Any]      =  (('''num_inference_steps''', 2_5),)







       def    __UpperCAmelCase (    self	:	List[Any]						,**__A	:	int  )						->					List[Any]:
         _lowercase							= {
             'num_train_timesteps': 1000,
             'beta_start': 0.0001,
             'beta_end': 0.02,
             'beta_schedule': 'linear',
             'solver_order': 2,
         }

         config.update(**__A  )
         return config







       def    __UpperCAmelCase (    self	:	Dict						,__A	:	str=0						,**__A	:	Optional[int]  )						->					Tuple:
         _lowercase							= dict(self.forward_default_kwargs  )
         _lowercase							= kwargs.pop('num_inference_steps'						,__A  )
         _lowercase							= self.dummy_sample
         _lowercase							= 0.1 * sample
         _lowercase							= [residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
           _lowercase							= self.get_scheduler_config(**__A  )
           _lowercase							= scheduler_class(**__A  )
           scheduler.set_timesteps(__A  )
           # copy over dummy past residuals
           _lowercase							= dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
             scheduler.save_config(__A  )
             _lowercase							= scheduler_class.from_pretrained(__A  )
             new_scheduler.set_timesteps(__A  )
             # copy over dummy past residuals
             _lowercase							= dummy_past_residuals[: new_scheduler.config.solver_order]

           _lowercase						,		_lowercase							= sample, sample
           for t in range(__A						,time_step + scheduler.config.solver_order + 1  ):
             _lowercase							= scheduler.step(__A						,__A						,__A						,**__A  ).prev_sample
             _lowercase							= new_scheduler.step(__A						,__A						,__A						,**__A  ).prev_sample

             assert torch.sum(torch.abs(output - new_output  )  ) < 1e-5, "Scheduler outputs are not identical"







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         pass







       def    __UpperCAmelCase (    self	:	int						,__A	:	Dict=0						,**__A	:	Optional[Any]  )						->					Dict:
         _lowercase							= dict(self.forward_default_kwargs  )
         _lowercase							= kwargs.pop('num_inference_steps'						,__A  )
         _lowercase							= self.dummy_sample
         _lowercase							= 0.1 * sample
         _lowercase							= [residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
           _lowercase							= self.get_scheduler_config()
           _lowercase							= scheduler_class(**__A  )
           scheduler.set_timesteps(__A  )

           # copy over dummy past residuals (must be after setting timesteps)
           _lowercase							= dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
             scheduler.save_config(__A  )
             _lowercase							= scheduler_class.from_pretrained(__A  )
             # copy over dummy past residuals
             new_scheduler.set_timesteps(__A  )

             # copy over dummy past residual (must be after setting timesteps)
             _lowercase							= dummy_past_residuals[: new_scheduler.config.solver_order]

           _lowercase							= scheduler.step(__A						,__A						,__A						,**__A  ).prev_sample
           _lowercase							= new_scheduler.step(__A						,__A						,__A						,**__A  ).prev_sample

           assert torch.sum(torch.abs(output - new_output  )  ) < 1e-5, "Scheduler outputs are not identical"







       def    __UpperCAmelCase (    self	:	Tuple						,__A	:	Tuple=None						,**__A	:	Optional[Any]  )						->					Tuple:
         if scheduler is None:
           _lowercase							= self.scheduler_classes[0]
           _lowercase							= self.get_scheduler_config(**__A  )
           _lowercase							= scheduler_class(**__A  )

         _lowercase							= self.scheduler_classes[0]
         _lowercase							= self.get_scheduler_config(**__A  )
         _lowercase							= scheduler_class(**__A  )

         _lowercase							= 10
         _lowercase							= self.dummy_model()
         _lowercase							= self.dummy_sample_deter
         scheduler.set_timesteps(__A  )

         for i, t in enumerate(scheduler.timesteps  ):
           _lowercase							= model(__A						,__A  )
           _lowercase							= scheduler.step(__A						,__A						,__A  ).prev_sample

         return sample







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Any:
         _lowercase							= dict(self.forward_default_kwargs  )

         _lowercase							= kwargs.pop('num_inference_steps'						,__A  )

         for scheduler_class in self.scheduler_classes:
           _lowercase							= self.get_scheduler_config()
           _lowercase							= scheduler_class(**__A  )

           _lowercase							= self.dummy_sample
           _lowercase							= 0.1 * sample

           if num_inference_steps is not None and hasattr(__A						,'set_timesteps'  ):
             scheduler.set_timesteps(__A  )
           elif num_inference_steps is not None and not hasattr(__A						,'set_timesteps'  ):
             _lowercase							= num_inference_steps

           # copy over dummy past residuals (must be done after set_timesteps)
           _lowercase							= [residual + 0.2, residual + 0.15, residual + 0.10]
           _lowercase							= dummy_past_residuals[: scheduler.config.solver_order]

           _lowercase							= scheduler.timesteps[5]
           _lowercase							= scheduler.timesteps[6]

           _lowercase							= scheduler.step(__A						,__A						,__A						,**__A  ).prev_sample
           _lowercase							= scheduler.step(__A						,__A						,__A						,**__A  ).prev_sample

           self.assertEqual(output_a.shape						,sample.shape  )
           self.assertEqual(output_a.shape						,output_a.shape  )







       def    __UpperCAmelCase (    self	:	int  )						->					Optional[Any]:
         # make sure that iterating over schedulers with same config names gives same results
         # for defaults
         _lowercase							= DEISMultistepScheduler(**self.get_scheduler_config()  )
         _lowercase							= self.full_loop(scheduler=__A  )
         _lowercase							= torch.mean(torch.abs(__A  )  )

         assert abs(result_mean.item() - 0.23916  ) < 1e-3

         _lowercase							= DPMSolverSinglestepScheduler.from_config(scheduler.config  )
         _lowercase							= DPMSolverMultistepScheduler.from_config(scheduler.config  )
         _lowercase							= UniPCMultistepScheduler.from_config(scheduler.config  )
         _lowercase							= DEISMultistepScheduler.from_config(scheduler.config  )

         _lowercase							= self.full_loop(scheduler=__A  )
         _lowercase							= torch.mean(torch.abs(__A  )  )

         assert abs(result_mean.item() - 0.23916  ) < 1e-3







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Optional[int]:
         for timesteps in [25, 50, 100, 999, 1000]:
           self.check_over_configs(num_train_timesteps=__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					str:
         self.check_over_configs(thresholding=__A  )
         for order in [1, 2, 3]:
           for solver_type in ["logrho"]:
             for threshold in [0.5, 1.0, 2.0]:
               for prediction_type in ["epsilon", "sample"]:
                 self.check_over_configs(
                     thresholding=__A						,prediction_type=__A						,sample_max_value=__A						,algorithm_type='deis'						,solver_order=__A						,solver_type=__A						,)







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Tuple:
         for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=__A  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         for algorithm_type in ["deis"]:
           for solver_type in ["logrho"]:
             for order in [1, 2, 3]:
               for prediction_type in ["epsilon", "sample"]:
                 self.check_over_configs(
                     solver_order=__A						,solver_type=__A						,prediction_type=__A						,algorithm_type=__A						,)
                 _lowercase							= self.full_loop(
                     solver_order=__A						,solver_type=__A						,prediction_type=__A						,algorithm_type=__A						,)
                 assert not torch.isnan(__A  ).any(), "Samples have nan numbers"







       def    __UpperCAmelCase (    self	:	Dict  )						->					int:
         self.check_over_configs(lower_order_final=__A  )
         self.check_over_configs(lower_order_final=__A  )







       def    __UpperCAmelCase (    self	:	str  )						->					Tuple:
         for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
           self.check_over_forward(num_inference_steps=__A						,time_step=0  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Any:
         _lowercase							= self.full_loop()
         _lowercase							= torch.mean(torch.abs(__A  )  )

         assert abs(result_mean.item() - 0.23916  ) < 1e-3







       def    __UpperCAmelCase (    self	:	Any  )						->					List[str]:
         _lowercase							= self.full_loop(prediction_type='v_prediction'  )
         _lowercase							= torch.mean(torch.abs(__A  )  )

         assert abs(result_mean.item() - 0.091  ) < 1e-3







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					List[Any]:
         _lowercase							= self.scheduler_classes[0]
         _lowercase							= self.get_scheduler_config(thresholding=__A						,dynamic_thresholding_ratio=0  )
         _lowercase							= scheduler_class(**__A  )

         _lowercase							= 10
         _lowercase							= self.dummy_model()
         _lowercase							= self.dummy_sample_deter.half()
         scheduler.set_timesteps(__A  )

         for i, t in enumerate(scheduler.timesteps  ):
           _lowercase							= model(__A						,__A  )
           _lowercase							= scheduler.step(__A						,__A						,__A  ).prev_sample

         assert sample.dtype == torch.floataa