

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   lowercase_   :



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

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

        _snake_case						=							None
        _snake_case						=							decoder_seq_length
        _snake_case						=							2
        _snake_case						=							1



      def 						UpperCamelCase_		(      self				:    Any		)   ->					Any:
        _snake_case						=							ids_tensor([self.batch_size, self.decoder_seq_length]      , self.vocab_size		)

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

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

        _snake_case						=							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				:    List[str]      , A__				:    List[str]      , A__				:    Union[str, Any]      , A__				:    str      , A__				:    Union[str, Any]      , )   ->					Any:
        _snake_case						=							True
        _snake_case						=							TrOCRDecoder(config=__lowerCamelCase		).to(__lowerCamelCase		).eval()
        _snake_case						=							input_ids[:2]

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

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

        _snake_case						=							outputs['''past_key_values''']

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

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

        _snake_case						=							model(__lowerCamelCase		)['''last_hidden_state''']
        _snake_case						=							model(__lowerCamelCase      , past_key_values=__lowerCamelCase		)['''last_hidden_state''']

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

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







      def 						UpperCamelCase_		(      self				:    str		)   ->					int:
        _snake_case						=							self.prepare_config_and_inputs()
        _snake_case,					_snake_case,					_snake_case,					_snake_case						=							config_and_inputs

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



@require_torch
class   lowercase_   (  UpperCamelCase__       ,							UpperCamelCase__       ,							UpperCamelCase__       ,							unittest.TestCase					):
      UpperCamelCase_      :		Optional[int]										=    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
      UpperCamelCase_      :		Union[str, Any]										=    (TrOCRForCausalLM,) if is_torch_available() else ()
      UpperCamelCase_      :		Tuple										=    {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
      UpperCamelCase_      :		List[Any]										=    True
      UpperCamelCase_      :		Optional[int]										=    False



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Union[str, Any]:
        _snake_case						=							TrOCRStandaloneDecoderModelTester(self      , is_training=__lowerCamelCase		)
        _snake_case						=							ConfigTester(self      , config_class=__lowerCamelCase		)



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Optional[Any]:
        pass



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Tuple:
        pass



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



      def 						UpperCamelCase_		(      self				:    str		)   ->					Tuple:
        self.config_tester.run_common_tests()



      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					List[str]:
        _snake_case						=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_decoder_model_past(*__lowerCamelCase		)



      def 						UpperCamelCase_		(      self				:    Any		)   ->					Any:
        return







      @unittest.skip('''The model doesn\'t support left padding'''		)  # and it's not used enough to be worth fixing :)
      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Union[str, Any]:
        pass




from math import factorial



def 				snake_case_(_UpperCamelCase			,	_UpperCamelCase					)							->     int:



  """simple docstring"""






  if n < k or k < 0:
    raise ValueError('''Please enter positive integers for n and k where n >= k'''					)
  return factorial(_UpperCamelCase					) // (factorial(_UpperCamelCase					) * factorial(n - k					))


if __name__ == "__main__":
 print(
     '''The number of five-card hands possible from a standard''',
     f'''fifty-two card deck is: {combinations(52, 5)}\n''',
 )

 print(
     '''If a class of 40 students must be arranged into groups of''',
     f'''4 for group projects, there are {combinations(40, 4)} ways''',
     '''to arrange them.\n''',
 )

 print(
     '''If 10 teams are competing in a Formula One race, there''',
     f'''are {combinations(10, 3)} ways that first, second and''',
     '''third place can be awarded.''',
 )

