






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 ,					):
							"""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=UpperCAmelCase					).to(UpperCAmelCase					).eval()
							_UpperCAmelCase             =    input_ids[:2]

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

							self.parent.assertTrue(len(UpperCAmelCase					) == len(UpperCAmelCase					)					)
							self.parent.assertTrue(len(UpperCAmelCase					) == len(UpperCAmelCase					) + 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(UpperCAmelCase					)

							_UpperCAmelCase             =    model(UpperCAmelCase					)['last_hidden_state']
							_UpperCAmelCase             =    model(UpperCAmelCase ,					past_key_values=UpperCAmelCase					)['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(UpperCAmelCase ,					UpperCAmelCase ,					atol=1e-3					)

	def  UpperCamelCase					(							self					):
							"""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 __lowerCamelCase				(   snake_case__  , snake_case__  , snake_case__  , 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  UpperCamelCase					(							self ,					):
							"""simple docstring"""







							_UpperCAmelCase             =    SpeechaTextaStandaloneDecoderModelTester(self ,					is_training=UpperCAmelCase					)
							_UpperCAmelCase             =    ConfigTester(self ,					config_class=UpperCAmelCase					)

	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(*UpperCAmelCase					)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							return








import dataclasses
import enum
import functools
import math
import re

# The following script is adapted from the script of TaPas.
# Original: https://github.com/google-research/tapas/master/wikisql_utils.py
from typing import Any, List, Text


_a   			=						'''none'''
_a   			=						'''none'''


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase			)->						Any:





						"""simple docstring"""



						_UpperCAmelCase             =    value.split(__lowerCAmelCase			)
						return len(__lowerCAmelCase			) > 1 and any((len(__lowerCAmelCase			) == 3 for x in split)			)


def __A					(   __lowerCAmelCase			)->						List[str]:





						"""simple docstring"""



						if isinstance(__lowerCAmelCase				,	__lowerCAmelCase			):
												return value
						if isinstance(__lowerCAmelCase				,	__lowerCAmelCase			):
												return float(__lowerCAmelCase			)
						if not isinstance(__lowerCAmelCase				,	__lowerCAmelCase			):
												raise ValueError('Argument value is not a string. Can\'t parse it as float'			)
						_UpperCAmelCase             =    value

						try:
												# Example: 1,000.7
												if "." in sanitized and "," in sanitized:
																		return float(sanitized.replace(','				,	''			)			)
												# 1,000
												if "," in sanitized and _split_thousands(','				,	__lowerCAmelCase			):
																		return float(sanitized.replace(','				,	''			)			)
												# 5,5556
												if "," in sanitized and sanitized.count(','			) == 1 and not _split_thousands(','				,	__lowerCAmelCase			):
																		return float(sanitized.replace(','				,	'.'			)			)
												# 0.0.0.1
												if sanitized.count('.'			) > 1:
																		return float(sanitized.replace('.'				,	''			)			)
												# 0,0,0,1
												if sanitized.count(','			) > 1:
																		return float(sanitized.replace(','				,	''			)			)
												return float(__lowerCAmelCase			)
						except ValueError:
												# Avoid adding the sanitized value in the error message.
												raise ValueError('Unable to convert value to float'			)


def __A					(   __lowerCAmelCase			)->						Any:





						"""simple docstring"""



						if answer is None:
												return None
						try:
												_UpperCAmelCase             =    convert_to_float(__lowerCAmelCase			)
												if isinstance(__lowerCAmelCase				,	__lowerCAmelCase			) and math.isnan(__lowerCAmelCase			):
																		return None
												return value
						except ValueError:
												return answer.lower()


_a   			=						{
    '''text''': lambda x: x,
    '''real''': convert_to_float,
}





class __lowerCamelCase				(   enum.Enum):





	"""simple docstring"""






	UpperCamelCase__			=   0
	UpperCamelCase__			=   1
	UpperCamelCase__			=   2
	UpperCamelCase__			=   3
	UpperCamelCase__			=   4
	UpperCamelCase__			=   5





class __lowerCamelCase				(   enum.Enum):





	"""simple docstring"""






	UpperCamelCase__			=   0
	UpperCamelCase__			=   1
	UpperCamelCase__			=   2





@dataclasses.dataclass
class __lowerCamelCase				:





	"""simple docstring"""






	UpperCamelCase__			=   42
	UpperCamelCase__			=   42
	UpperCamelCase__			=   42



_a   			=						re.compile(r'''\w+|[^\w\s]+''', re.UNICODE | re.MULTILINE | re.DOTALL)


def __A					(   __lowerCAmelCase			)->						Optional[Any]:





						"""simple docstring"""



						return list(_TOKENIZER.findall(x.lower()			)			)


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)->						Any:





						"""simple docstring"""



						if operator == _Operator.EQUALS:
												return src == tgt
						elif operator == _Operator.GREATER:
												return src > tgt
						elif operator == _Operator.LESSER:
												return src < tgt
						raise ValueError(F"""Unknown operator: {operator}"""			)


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)->						str:





						"""simple docstring"""



						_UpperCAmelCase             =    table['types']
						return _TYPE_CONVERTER[types[column]](__lowerCAmelCase			)


def __A					(   __lowerCAmelCase			)->						str:





						"""simple docstring"""



						return isinstance(__lowerCAmelCase				,	__lowerCAmelCase			)


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)->						str:





						"""simple docstring"""



						for cond in conditions:
												_UpperCAmelCase             =    row[cond.column]

												_UpperCAmelCase             =    _parse_value(__lowerCAmelCase				,	cond.column				,	cond.cmp_value			)

												if _is_string(__lowerCAmelCase			) and _is_string(__lowerCAmelCase			):
																		_UpperCAmelCase             =    _normalize_for_match(__lowerCAmelCase			)
																		_UpperCAmelCase             =    _normalize_for_match(__lowerCAmelCase			)

												if not isinstance(__lowerCAmelCase				,	type(__lowerCAmelCase			)			):
																		raise ValueError('Type difference {} != {}'.format(type(__lowerCAmelCase			)				,	type(__lowerCAmelCase			)			)			)

												if not _compare(cond.operator				,	__lowerCAmelCase				,	__lowerCAmelCase			):
																		return False
						return True


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)->						str:





						"""simple docstring"""



						if not answer_coordinates:
												if aggregation_op == _Aggregation.COUNT:
																		return 0.0
												else:
																		return EMPTY_ANSWER_AGG

    # Count can support non numeric answers.
						if aggregation_op == _Aggregation.COUNT:
												return float(len(__lowerCAmelCase			)			)

						# If we have just one answer, if float returns it or try a conversion.
						_UpperCAmelCase             =    [table['rows'][i][j] for (i, j) in answer_coordinates]
						if len(__lowerCAmelCase			) == 1:
												try:
																		return convert_to_float(values[0]			)
												except ValueError as e:
																		if aggregation_op != _Aggregation.NONE:
																								raise e

						if aggregation_op == _Aggregation.NONE:
												return None

						# Other aggregation only support numeric values. Bail out if we have strings.
						if not all((isinstance(__lowerCAmelCase				,	(int, float)			) for v in values)			):
												return None

						if aggregation_op == _Aggregation.SUM:
												return float(sum(__lowerCAmelCase			)			)
						elif aggregation_op == _Aggregation.AVERAGE:
												return sum(__lowerCAmelCase			) / len(__lowerCAmelCase			)
						else:
												raise ValueError(F"""Unknown aggregation: {aggregation_op}"""			)


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase			)->						Any:





						"""simple docstring"""



						_UpperCAmelCase             =    sql_query['agg']
						if aggregation_op_index >= 3:
												_UpperCAmelCase             =    _Aggregation(__lowerCAmelCase			)
						else:
												_UpperCAmelCase             =    _Aggregation.NONE

						_UpperCAmelCase             =    sql_query['sel']
						_UpperCAmelCase             =    [
						    _Condition(__lowerCAmelCase				,	_Operator(__lowerCAmelCase			)				,	__lowerCAmelCase			)
						    for column, operator, cmp_value in zip(
						        sql_query['conds']['column_index']				,	sql_query['conds']['operator_index']				,	sql_query['conds']['condition']			)
						]

						_UpperCAmelCase             =    []
						for row in range(len(table['rows']			)			):
												if _respect_conditions(__lowerCAmelCase				,	table['rows'][row]				,	__lowerCAmelCase			):
																		indices.append((row, target_column)			)

						if not indices:
												return [], aggregation_op

						if len(__lowerCAmelCase			) == 1:
												return indices, aggregation_op

						# Parsing of MIN/MAX.
						if aggregation_op_index in (1, 2):
												_UpperCAmelCase             =    {2: min, 1: max}
												_UpperCAmelCase             =    [(table['rows'][i][j], index) for index, (i, j) in enumerate(__lowerCAmelCase			)]
												_UpperCAmelCase             =    functools.reduce(operators[sql_query['agg']]				,	__lowerCAmelCase			)

												_UpperCAmelCase             =    [indices[reduced[1]]]
												return ret, _Aggregation.NONE

						return indices, aggregation_op


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)->						Dict:





						"""simple docstring"""



						if float_answer is not None:
												return [str(__lowerCAmelCase			)]
						return [str(table['real_rows'][r][c]			) for r, c in answer_coordinates]


def __A					(   __lowerCAmelCase				,	__lowerCAmelCase			)->						List:





						"""simple docstring"""



						_UpperCAmelCase      ,				_UpperCAmelCase             =    _get_answer_coordinates(__lowerCAmelCase				,	__lowerCAmelCase			)
						_UpperCAmelCase             =    _get_float_answer(__lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)
						_UpperCAmelCase             =    _get_answer_text(__lowerCAmelCase				,	__lowerCAmelCase				,	__lowerCAmelCase			)
						# keep the original data the same with TaPas
						if len(__lowerCAmelCase			) == 0:
												_UpperCAmelCase             =    [EMPTY_ANSWER]
						return answer_text
