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







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





    """simple docstring"""


    UpperCamelCase      =			parent
    UpperCamelCase      =			batch_size
    UpperCamelCase      =			decoder_seq_length
    # For common tests
    UpperCamelCase      =			self.decoder_seq_length
    UpperCamelCase      =			is_training
    UpperCamelCase      =			use_attention_mask
    UpperCamelCase      =			use_labels

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

    UpperCamelCase      =			None
    UpperCamelCase      =			decoder_seq_length
    UpperCamelCase      =			2
    UpperCamelCase      =			1







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





    """simple docstring"""


    UpperCamelCase      =			ids_tensor([self.batch_size, self.decoder_seq_length]      ,      self.vocab_size							)

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

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

    UpperCamelCase      =			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	:	Tuple      ,      lowerCamelCase_	:	List[Any]      ,      lowerCamelCase_	:	Optional[int]      ,      lowerCamelCase_	:	Dict      ,      lowerCamelCase_	:	List[Any]      ,      ):





    """simple docstring"""


    UpperCamelCase      =			True
    UpperCamelCase      =			SpeechaTextaDecoder(config=lowerCamelCase_							).to(lowerCamelCase_							).eval()
    UpperCamelCase      =			input_ids[:2]

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

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

    UpperCamelCase      =			outputs["""past_key_values"""]

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

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

    print(lowerCamelCase_							)

    UpperCamelCase      =			model(lowerCamelCase_							)["""last_hidden_state"""]
    UpperCamelCase      =			model(lowerCamelCase_      ,      past_key_values=lowerCamelCase_							)["""last_hidden_state"""]

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

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







   def 	lowerCamelCase_  (    self	:	str							):





    """simple docstring"""


    UpperCamelCase      =			self.prepare_config_and_inputs()
    (
        (
        UpperCamelCase
    )     ,    (
        UpperCamelCase
    )     ,    (
        UpperCamelCase
    )     ,    (
        UpperCamelCase
    )     ,    
    )      =			config_and_inputs

    UpperCamelCase      =			{
        """input_ids""": input_ids,
        """attention_mask""": attention_mask,
    }
    return config, inputs_dict





@require_torch
class SCREAMING_SNAKE_CASE_		(			__lowerCAmelCase     ,    __lowerCAmelCase     ,    __lowerCAmelCase     ,    unittest.TestCase		):
   __lowerCAmelCase											=							(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
   __lowerCAmelCase											=							(SpeechaTextaForCausalLM,) if is_torch_available() else ()
   __lowerCAmelCase											=							{"""text-generation""": SpeechaTextaForCausalLM} if is_torch_available() else {}
   __lowerCAmelCase											=							True
   __lowerCAmelCase											=							False







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





    """simple docstring"""


    UpperCamelCase      =			SpeechaTextaStandaloneDecoderModelTester(self      ,      is_training=lowerCamelCase_							)
    UpperCamelCase      =			ConfigTester(self      ,      config_class=lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    pass







   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    pass







   def 	lowerCamelCase_  (    self	:	Any							):





    """simple docstring"""


    pass







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    self.config_tester.run_common_tests()







   def 	lowerCamelCase_  (    self	:	Dict							):





    """simple docstring"""


    UpperCamelCase      =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_decoder_model_past(*lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	Optional[int]							):





    """simple docstring"""


    return

import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
     import transformer_engine.pytorch as te





def  lowercase(  UpperCamelCase_       ,				UpperCamelCase_=True       ,				UpperCamelCase_=True       ,				UpperCamelCase_=True					)		->  int:







 '''simple docstring'''
 if not is_fpa_available():
  raise ImportError("""Using `convert_model` requires transformer_engine to be installed."""					)
 for name, module in model.named_children():
  if isinstance(UpperCamelCase_       ,				nn.Linear					) and to_transformer_engine and _convert_linear:
   # Return early if the linear layer weights are not multiples of 16
   if any(p % 16 != 0 for p in module.weight.shape					):
    return
   UpperCamelCase      =			module.bias is not None
   UpperCamelCase      =			te.Linear(
       module.in_features       ,				module.out_features       ,				bias=UpperCamelCase_       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   if has_bias:
    UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  elif isinstance(UpperCamelCase_       ,				nn.LayerNorm					) and to_transformer_engine and _convert_ln:
   UpperCamelCase      =			te.LayerNorm(module.normalized_shape[0]       ,				eps=module.eps       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  elif isinstance(UpperCamelCase_       ,				te.Linear					) and not to_transformer_engine and _convert_linear:
   UpperCamelCase      =			module.bias is not None
   UpperCamelCase      =			nn.Linear(
       module.in_features       ,				module.out_features       ,				bias=UpperCamelCase_       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   if has_bias:
    UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  elif isinstance(UpperCamelCase_       ,				te.LayerNorm					) and not to_transformer_engine and _convert_ln:
   UpperCamelCase      =			nn.LayerNorm(module.normalized_shape[0]       ,				eps=module.eps       ,				params_dtype=module.weight.dtype					)
   UpperCamelCase      =			module.weight.data.clone()
   UpperCamelCase      =			module.bias.data.clone()

   setattr(UpperCamelCase_       ,				UpperCamelCase_       ,				UpperCamelCase_					)
  else:
   convert_model(
       UpperCamelCase_       ,				to_transformer_engine=UpperCamelCase_       ,				_convert_linear=_convert_linear       ,				_convert_ln=_convert_ln       ,				)





def  lowercase(  UpperCamelCase_					)		->  Any:







 '''simple docstring'''
 if not is_fpa_available():
  raise ImportError("""Using `has_transformer_engine_layers` requires transformer_engine to be installed."""					)
 for m in model.modules():
  if isinstance(UpperCamelCase_       ,				(te.LayerNorm, te.Linear)					):
   return True
 return False
