code: 
  
  
  
  
  '''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_							:
  
  
  
  
  					'''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,						):
  							_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        __SCREAMING_SNAKE_CASE			(					self				):
  							_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			 = 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        __SCREAMING_SNAKE_CASE			(					self,						__UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase,						):
  							_snake_case			 = True
  							_snake_case			 = SpeechaTextaDecoder(config=__UpperCamelCase				).to(__UpperCamelCase				).eval()
  							_snake_case			 = input_ids[:2]
  
  							input_ids[input_ids == 0] += 1
  							# first forward pass
  							_snake_case			 = model(__UpperCamelCase,						use_cache=__UpperCamelCase				)
  							_snake_case			 = model(__UpperCamelCase				)
  							_snake_case			 = model(__UpperCamelCase,						use_cache=__UpperCamelCase				)
  
  							self.parent.assertTrue(len(__UpperCamelCase				) == len(__UpperCamelCase				)				)
  							self.parent.assertTrue(len(__UpperCamelCase				) == len(__UpperCamelCase				) + 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				)
  
  							print(__UpperCamelCase				)
  
  							_snake_case			 = model(__UpperCamelCase				)["last_hidden_state"]
  							_snake_case			 = model(__UpperCamelCase,						past_key_values=__UpperCamelCase				)["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(__UpperCamelCase,						__UpperCamelCase,						atol=1E-3				)
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							_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   lowerCamelCase_							( UpperCAmelCase_, UpperCAmelCase_, UpperCAmelCase_, unittest.TestCase							):
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  					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        __SCREAMING_SNAKE_CASE			(					self,						):
  							_snake_case			 = SpeechaTextaStandaloneDecoderModelTester(self,						is_training=__UpperCamelCase				)
  							_snake_case			 = ConfigTester(self,						config_class=__UpperCamelCase				)
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							pass
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							pass
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							pass
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							self.config_tester.run_common_tests()
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							_snake_case			 = self.model_tester.prepare_config_and_inputs()
  							self.model_tester.create_and_check_decoder_model_past(*__UpperCamelCase				)
  
  
  
  
  
  
  					def        __SCREAMING_SNAKE_CASE			(					self				):
  							return
  
  
  
  
  
  
style_context: 
  
  
  
  
  '''simple docstring'''
  
  
  
  
  
  
  
  import importlib
  import importlib.metadata
  import os
  import warnings
  from distutils.util import strtobool
  from functools import lru_cache
  
  import torch
  from packaging import version
  from packaging.version import parse
  
  from .environment import parse_flag_from_env
  from .versions import compare_versions, is_torch_version
  
  
  try:
  						import torch_xla.core.xla_model as xm  # noqa: F401
  
  						_lowerCamelCase							:		int										=						True
  except ImportError:
  						_lowerCamelCase							:		List[str]										=						False
  
  
  # Cache this result has it's a C FFI call which can be pretty time-consuming
  _lowerCamelCase							:		List[Any]										=						torch.distributed.is_available()
  
  
  
  
  
  
  def 	_lowercase	(			lowercase_						):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		_snake_case			 = importlib.util.find_spec(lowercase_						) is not None
  		if package_exists:
  				try:
  						_snake_case			 = importlib.metadata.metadata(lowercase_						)
  						return True
  				except importlib.metadata.PackageNotFoundError:
  						return False
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _torch_distributed_available
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		try:
  				pass
  		except ImportError:
  				print(
  				    "Intel(R) oneCCL Bindings for PyTorch* is required to run DDP on Intel(R) GPUs, but it is not"
  				    " detected. If you see \"ValueError: Invalid backend: 'ccl'\" error, please install Intel(R) oneCCL"
  				    " Bindings for PyTorch*."						)
  		return (
  		    importlib.util.find_spec("torch_ccl"						) is not None
  		    or importlib.util.find_spec("oneccl_bindings_for_pytorch"						) is not None
  		)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return importlib.metadata.version("oneccl_bind_pt"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("transformer_engine"						)
  
  
  
  
  
  
  @lru_cache
  def 	_lowercase	(			lowercase_=True						):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		if torch.cuda.is_available():
  				return False
  		if _tpu_available and check_device:
  				try:
  						# Will raise a RuntimeError if no XLA configuration is found
  						_snake_case			 = xm.xla_device()
  						return True
  				except RuntimeError:
  						return False
  		return _tpu_available
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("deepspeed"						)
  
  
  
  
  
  
  def 	_lowercase	(			lowercase_=False						):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		if is_tpu_available():
  				return not ignore_tpu
  		if torch.cuda.is_available():
  				return torch.cuda.is_bfaa_supported()
  		if is_npu_available():
  				return False
  		return True
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		_snake_case			 = _is_package_available("bitsandbytes"						)
  		if package_exists:
  				_snake_case			 = version.parse(importlib.metadata.version("bitsandbytes"						)						)
  				return compare_versions(lowercase_		,							">="		,							"0.39.0"						)
  		return False
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		_snake_case			 = _is_package_available("bitsandbytes"						)
  		if package_exists:
  				_snake_case			 = version.parse(importlib.metadata.version("bitsandbytes"						)						)
  				return compare_versions(lowercase_		,							">="		,							"0.37.2"						)
  		return False
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("bitsandbytes"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		if strtobool(os.environ.get("ACCELERATE_USE_MEGATRON_LM"		,							"False"						)						) == 1:
  				_snake_case			 = importlib.util.find_spec("megatron"						) is not None
  				if package_exists:
  						try:
  								_snake_case			 = parse(importlib.metadata.version("megatron-lm"						)						)
  								return compare_versions(lowercase_		,							">="		,							"2.2.0"						)
  						except Exception as e:
  								warnings.warn(f'''Parse Megatron version failed. Exception:{e}'''						)
  								return False
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("safetensors"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("transformers"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("datasets"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("aim"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("tensorboard"						) or _is_package_available("tensorboardX"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("wandb"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("comet_ml"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("boto3"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		if _is_package_available("rich"						):
  				if "ACCELERATE_DISABLE_RICH" in os.environ:
  						warnings.warn(
  						    "`ACCELERATE_DISABLE_RICH` is deprecated and will be removed in v0.22.0 and deactivated by default. Please use `ACCELERATE_ENABLE_RICH` if you wish to use `rich`."						)
  						return not parse_flag_from_env("ACCELERATE_DISABLE_RICH"		,							lowercase_						)
  				return parse_flag_from_env("ACCELERATE_ENABLE_RICH"		,							lowercase_						)
  		return False
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("sagemaker"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("tqdm"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return _is_package_available("mlflow"						)
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		return is_torch_version(">="		,							"1.12"						) and torch.backends.mps.is_available() and torch.backends.mps.is_built()
  
  
  
  
  
  
  def 	_lowercase	(			):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def get_major_and_minor_from_version(lowercase_						):
  				return str(version.parse(lowercase_						).major						) + "." + str(version.parse(lowercase_						).minor						)
  
  		_snake_case			 = importlib.metadata.version("torch"						)
  		if importlib.util.find_spec("intel_extension_for_pytorch"						) is None:
  				return False
  		_snake_case			 = "N/A"
  		try:
  				_snake_case			 = importlib.metadata.version("intel_extension_for_pytorch"						)
  		except importlib.metadata.PackageNotFoundError:
  				return False
  		_snake_case			 = get_major_and_minor_from_version(_torch_version						)
  		_snake_case			 = get_major_and_minor_from_version(_ipex_version						)
  		if torch_major_and_minor != ipex_major_and_minor:
  				warnings.warn(
  				    f'''Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,'''
  				    f''' but PyTorch {_torch_version} is found. Please switch to the matching version and run again.'''						)
  				return False
  		return True
  
  
  
  
  
  
  @lru_cache
  def 	_lowercase	(			lowercase_=False						):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		if importlib.util.find_spec("torch"						) is None or importlib.util.find_spec("torch_npu"						) is None:
  				return False
  
  		import torch
  		import torch_npu  # noqa: F401
  
  		if check_device:
  				try:
  						# Will raise a RuntimeError if no NPU is found
  						_snake_case			 = torch.npu.device_count()
  						return torch.npu.is_available()
  				except RuntimeError:
  						return False
  		return hasattr(lowercase_		,							"npu"						) and torch.npu.is_available()
  
  
  
  
  
  
  @lru_cache
  def 	_lowercase	(			lowercase_=False						):
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		if not parse_flag_from_env("ACCELERATE_USE_XPU"		,							default=lowercase_						):
  				return False
  		if is_ipex_available():
  				import torch
  
  				if is_torch_version("<="		,							"1.12"						):
  						return False
  		else:
  				return False
  
  		import intel_extension_for_pytorch  # noqa: F401
  
  		if check_device:
  				try:
  						# Will raise a RuntimeError if no XPU  is found
  						_snake_case			 = torch.xpu.device_count()
  						return torch.xpu.is_available()
  				except RuntimeError:
  						return False
  		return hasattr(lowercase_		,							"xpu"						) and torch.xpu.is_available()
  
  
  
  
  
  