






"""simple docstring"""





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





   '''simple docstring'''






   def __init__(   self			,				UpperCAmelCase_			,				UpperCAmelCase_=99			,				UpperCAmelCase_=13			,				UpperCAmelCase_=16			,				UpperCAmelCase_=7			,				UpperCAmelCase_=True			,				UpperCAmelCase_=True			,				UpperCAmelCase_=True			,				UpperCAmelCase_=False			,				UpperCAmelCase_=True			,				UpperCAmelCase_=2			,				UpperCAmelCase_=32			,				UpperCAmelCase_=4			,				UpperCAmelCase_=4			,				UpperCAmelCase_=30			,				UpperCAmelCase_=0			,				UpperCAmelCase_=1			,				UpperCAmelCase_=2			,				UpperCAmelCase_=None			,				)	-> Tuple:




          '''simple docstring'''




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

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

          lowercase__:    Tuple        =      None
          lowercase__:    Tuple        =      decoder_seq_length
          lowercase__:    int        =      2
          lowercase__:    Union[str, Any]        =      1





   def __lowercase    (   self)	-> Tuple:




          '''simple docstring'''




          lowercase__:    Union[str, Any]        =      ids_tensor([self.batch_size, self.decoder_seq_length]			,				self.vocab_size)

          lowercase__:    int        =      None
          if self.use_attention_mask:
                 lowercase__:    List[str]        =      ids_tensor([self.batch_size, self.decoder_seq_length]			,				vocab_size=2)

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

          lowercase__:    Any        =      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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				)	-> List[Any]:




          '''simple docstring'''




          lowercase__:    Union[str, Any]        =      True
          lowercase__:    List[str]        =      SpeechaTextaDecoder(config=UpperCAmelCase_).to(UpperCAmelCase_).eval()
          lowercase__:    Tuple        =      input_ids[:2]

          input_ids[input_ids == 0] += 1
          # first forward pass
          lowercase__:    List[str]        =      model(UpperCAmelCase_			,				use_cache=UpperCAmelCase_)
          lowercase__:    Union[str, Any]        =      model(UpperCAmelCase_)
          lowercase__:    List[Any]        =      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__:    Tuple        =      ids_tensor((2, 1)			,				config.vocab_size - 1) + 1

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

          print(UpperCAmelCase_)

          lowercase__:    str        =      model(UpperCAmelCase_)["last_hidden_state"]
          lowercase__:    Optional[int]        =      model(UpperCAmelCase_			,				past_key_values=UpperCAmelCase_)["last_hidden_state"]

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

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





   def __lowercase    (   self)	-> str:




          '''simple docstring'''




          lowercase__:    List[Any]        =      self.prepare_config_and_inputs()
          (
              (
              lowercase__
          )  ,     (
              lowercase__
          )  ,     (
              lowercase__
          )  ,     (
              lowercase__
          )  ,     
          ):    Optional[int]        =      config_and_inputs

          lowercase__:    List[Any]        =      {
              "input_ids": input_ids,
              "attention_mask": attention_mask,
          }
          return config, inputs_dict







@require_torch
class 		_a       (						lowercase_    ,					lowercase_    ,					lowercase_    ,					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 __lowercase    (   self			,				)	-> Any:




          '''simple docstring'''




          lowercase__:    List[str]        =      SpeechaTextaStandaloneDecoderModelTester(self			,				is_training=UpperCAmelCase_)
          lowercase__:    Dict        =      ConfigTester(self			,				config_class=UpperCAmelCase_)





   def __lowercase    (   self)	-> Any:




          '''simple docstring'''




          pass





   def __lowercase    (   self)	-> Dict:




          '''simple docstring'''




          pass





   def __lowercase    (   self)	-> List[str]:




          '''simple docstring'''




          pass





   def __lowercase    (   self)	-> Any:




          '''simple docstring'''




          self.config_tester.run_common_tests()





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




          '''simple docstring'''




          lowercase__:    Optional[int]        =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase_)





   def __lowercase    (   self)	-> List[str]:




          '''simple docstring'''




          return














"""simple docstring"""





UpperCamelCase			=      {
    """0""": 1,
    """1""": 1,
    """2""": 2,
    """3""": 6,
    """4""": 24,
    """5""": 1_20,
    """6""": 7_20,
    """7""": 50_40,
    """8""": 4_03_20,
    """9""": 36_28_80,
}

UpperCamelCase			=      {1_45: 1_45}

UpperCamelCase			=      {
    1_45: 0,
    1_69: 3,
    3_63_01: 3,
    14_54: 3,
    8_71: 2,
    4_53_61: 2,
    8_72: 2,
}






def     A(  snake_case_		):
       """simple docstring"""





       if n in CACHE_SUM_DIGIT_FACTORIALS:
              return CACHE_SUM_DIGIT_FACTORIALS[n]
       lowercase__:    Any        =      sum(DIGIT_FACTORIALS[let] for let in str(snake_case_		)		)
       lowercase__:    Optional[Any]        =      ret
       return ret






def     A(  snake_case_						,       snake_case_ = None		):
       """simple docstring"""





       lowercase__:    Optional[int]        =      previous or set()
       if n in CHAIN_LENGTH_CACHE:
              return CHAIN_LENGTH_CACHE[n]
       lowercase__:    Tuple        =      sum_digit_factorials(snake_case_		)
       if next_number in previous:
              lowercase__:    List[Any]        =      0
              return 0
       else:
              previous.add(snake_case_		)
              lowercase__:    Optional[int]        =      1 + chain_length(snake_case_						,       snake_case_		)
              lowercase__:    Any        =      ret
              return ret






def     A(  snake_case_ = 60						,       snake_case_ = 1000000		):
       """simple docstring"""





       return sum(1 for i in range(1						,       snake_case_		) if chain_length(snake_case_		) == num_terms		)


if __name__ == "__main__":
  print(F"{solution() = }")






