
"""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 __lowerCAmelCase						:





     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      ,	):

      '''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 		UpperCAmelCase   (      self ):

      '''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 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	):

      '''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 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.prepare_config_and_inputs()
      (
          __UpperCamelCase      
      )							    =			config_and_inputs

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


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





     def 		UpperCAmelCase   (      self      ,	):

      '''simple docstring'''




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





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      pass





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      pass





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      pass





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self.config_tester.run_common_tests()





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




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







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      return







# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


__lowerCamelCase   	=  {
    '''configuration_tvlt''': ['''TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TvltConfig'''],
    '''feature_extraction_tvlt''': ['''TvltFeatureExtractor'''],
    '''processing_tvlt''': ['''TvltProcessor'''],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowerCamelCase   	=  [
           '''TVLT_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''TvltModel''',
           '''TvltForPreTraining''',
           '''TvltForAudioVisualClassification''',
           '''TvltPreTrainedModel''',
       ]

try:
       if not is_vision_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowerCamelCase   	=  ['''TvltImageProcessor''']


if TYPE_CHECKING:
       from .configuration_tvlt import TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP, TvltConfig
       from .processing_tvlt import TvltProcessor
       from .feature_extraction_tvlt import TvltFeatureExtractor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tvlt import (
                  TVLT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TvltForAudioVisualClassification,
                  TvltForPreTraining,
                  TvltModel,
                  TvltPreTrainedModel,
              )

       try:
              if not is_vision_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .image_processing_tvlt import TvltImageProcessor


else:
       import sys

       __lowerCamelCase   	=  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



