

import warnings
from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


lowerCAmelCase__								=		logging.get_logger(__name__)

lowerCAmelCase__								=		{
    """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/config.json""",
    # See all BART models at https://huggingface.co/models?filter=bart
}







class 	lowercase    (				_lowercase							):


		"""simple docstring"""




		a__					       =  "bart"
		a__					       =  ["past_key_values"]
		a__					       =  {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


		def __init__(				self	,       __snake_case=5_02_65	,       __snake_case=10_24	,       __snake_case=12	,       __snake_case=40_96	,       __snake_case=16	,       __snake_case=12	,       __snake_case=40_96	,       __snake_case=16	,       __snake_case=0.0	,       __snake_case=0.0	,       __snake_case="gelu"	,       __snake_case=10_24	,       __snake_case=0.1	,       __snake_case=0.0	,       __snake_case=0.0	,       __snake_case=0.0_2	,       __snake_case=0.0	,       __snake_case=False	,       __snake_case=True	,       __snake_case=3	,       __snake_case=1	,       __snake_case=0	,       __snake_case=2	,       __snake_case=True	,       __snake_case=2	,       __snake_case=2	,       **__snake_case	,       ):
			_UpperCamelCase   :		Any			=						vocab_size
			_UpperCamelCase   :		Optional[Any]			=						max_position_embeddings
			_UpperCamelCase   :		Tuple			=						d_model
			_UpperCamelCase   :		Optional[int]			=						encoder_ffn_dim
			_UpperCamelCase   :		Union[str, Any]			=						encoder_layers
			_UpperCamelCase   :		Tuple			=						encoder_attention_heads
			_UpperCamelCase   :		List[str]			=						decoder_ffn_dim
			_UpperCamelCase   :		Union[str, Any]			=						decoder_layers
			_UpperCamelCase   :		List[Any]			=						decoder_attention_heads
			_UpperCamelCase   :		Any			=						dropout
			_UpperCamelCase   :		int			=						attention_dropout
			_UpperCamelCase   :		List[Any]			=						activation_dropout
			_UpperCamelCase   :		List[str]			=						activation_function
			_UpperCamelCase   :		Tuple			=						init_std
			_UpperCamelCase   :		str			=						encoder_layerdrop
			_UpperCamelCase   :		Optional[int]			=						decoder_layerdrop
			_UpperCamelCase   :		Optional[int]			=						classifier_dropout
			_UpperCamelCase   :		Union[str, Any]			=						use_cache
			_UpperCamelCase   :		Dict			=						encoder_layers
			_UpperCamelCase   :		Union[str, Any]			=						scale_embedding  # scale factor will be sqrt(d_model) if True

			super().__init__(
			    num_labels=__snake_case	,       pad_token_id=__snake_case	,       bos_token_id=__snake_case	,       eos_token_id=__snake_case	,       is_encoder_decoder=__snake_case	,       decoder_start_token_id=__snake_case	,       forced_eos_token_id=__snake_case	,       **__snake_case	,       )

			# ensure backward compatibility for BART CNN models
			if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated'	,       __snake_case):
				_UpperCamelCase   :		int			=						self.bos_token_id
				warnings.warn(
				    f'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '''
				    'The config can simply be saved and uploaded again to be fixed.')







class 	lowercase    (				_lowercase							):


		"""simple docstring"""




		@property
		def 		A__					(				self):
			if self.task in ["default", "seq2seq-lm"]:
				_UpperCamelCase   :		int			=						OrderedDict(
				    [
				        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
				        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
				    ])

				if self.use_past:
					_UpperCamelCase   :		str			=						{0: 'batch'}
					_UpperCamelCase   :		Tuple			=						{0: 'batch', 1: 'past_decoder_sequence + sequence'}
				else:
					_UpperCamelCase   :		Tuple			=						{0: 'batch', 1: 'decoder_sequence'}
					_UpperCamelCase   :		List[Any]			=						{0: 'batch', 1: 'decoder_sequence'}

				if self.use_past:
					self.fill_with_past_key_values_(__snake_case	,       direction='inputs')
			elif self.task == "causal-lm":
				# TODO: figure this case out.
				_UpperCamelCase   :		Optional[int]			=						OrderedDict(
				    [
				        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
				        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
				    ])
				if self.use_past:
					_UpperCamelCase	,		_UpperCamelCase   :		Tuple			=						self.num_layers
					for i in range(__snake_case):
						_UpperCamelCase   :		Union[str, Any]			=						{0: 'batch', 2: 'past_sequence + sequence'}
						_UpperCamelCase   :		List[str]			=						{0: 'batch', 2: 'past_sequence + sequence'}
			else:
				_UpperCamelCase   :		Any			=						OrderedDict(
				    [
				        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
				        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
				        ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
				        ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
				    ])

			return common_inputs


		@property
		def 		A__					(				self):
			if self.task in ["default", "seq2seq-lm"]:
				_UpperCamelCase   :		Optional[int]			=						super().outputs
			else:
				_UpperCamelCase   :		Any			=						super(__snake_case	,       self).outputs
				if self.use_past:
					_UpperCamelCase	,		_UpperCamelCase   :		List[Any]			=						self.num_layers
					for i in range(__snake_case):
						_UpperCamelCase   :		List[str]			=						{0: 'batch', 2: 'past_sequence + sequence'}
						_UpperCamelCase   :		Union[str, Any]			=						{0: 'batch', 2: 'past_sequence + sequence'}
			return common_outputs


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			_UpperCamelCase   :		Union[str, Any]			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)

			# Generate decoder inputs
			_UpperCamelCase   :		Union[str, Any]			=						seq_length if not self.use_past else 1
			_UpperCamelCase   :		Any			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)
			_UpperCamelCase   :		List[str]			=						{f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
			_UpperCamelCase   :		Union[str, Any]			=						dict(**__snake_case	,       **__snake_case)

			if self.use_past:
				if not is_torch_available():
					raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.')
				else:
					import torch
				_UpperCamelCase	,		_UpperCamelCase   :		Tuple			=						common_inputs['input_ids'].shape
				_UpperCamelCase   :		Optional[int]			=						common_inputs['decoder_input_ids'].shape[1]
				_UpperCamelCase	,		_UpperCamelCase   :		List[Any]			=						self.num_attention_heads
				_UpperCamelCase   :		Union[str, Any]			=						(
				    batch,
				    num_encoder_attention_heads,
				    encoder_seq_length,
				    self._config.hidden_size // num_encoder_attention_heads,
				)
				_UpperCamelCase   :		List[Any]			=						decoder_seq_length + 3
				_UpperCamelCase   :		Optional[int]			=						(
				    batch,
				    num_decoder_attention_heads,
				    decoder_past_length,
				    self._config.hidden_size // num_decoder_attention_heads,
				)

				_UpperCamelCase   :		Dict			=						torch.cat(
				    [common_inputs['decoder_attention_mask'], torch.ones(__snake_case	,       __snake_case)]	,       dim=1)

				_UpperCamelCase   :		int			=						[]
				# If the number of encoder and decoder layers are present in the model configuration, both are considered
				_UpperCamelCase	,		_UpperCamelCase   :		Any			=						self.num_layers
				_UpperCamelCase   :		int			=						min(__snake_case	,       __snake_case)
				_UpperCamelCase   :		Optional[int]			=						max(__snake_case	,       __snake_case) - min_num_layers
				_UpperCamelCase   :		int			=						'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

				for _ in range(__snake_case):
					common_inputs["past_key_values"].append(
					    (
					        torch.zeros(__snake_case),
					        torch.zeros(__snake_case),
					        torch.zeros(__snake_case),
					        torch.zeros(__snake_case),
					    ))
				# TODO: test this.
				_UpperCamelCase   :		List[Any]			=						encoder_shape if remaining_side_name == 'encoder' else decoder_shape
				for _ in range(__snake_case	,       __snake_case):
					common_inputs["past_key_values"].append((torch.zeros(__snake_case), torch.zeros(__snake_case)))
			return common_inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			_UpperCamelCase   :		Any			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)

			if self.use_past:
				if not is_torch_available():
					raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.')
				else:
					import torch
				_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						common_inputs['input_ids'].shape
				# Not using the same length for past_key_values
				_UpperCamelCase   :		Tuple			=						seqlen + 2
				_UpperCamelCase	,		_UpperCamelCase   :		Tuple			=						self.num_layers
				_UpperCamelCase	,		_UpperCamelCase   :		str			=						self.num_attention_heads
				_UpperCamelCase   :		Tuple			=						(
				    batch,
				    num_encoder_attention_heads,
				    past_key_values_length,
				    self._config.hidden_size // num_encoder_attention_heads,
				)

				_UpperCamelCase   :		Optional[int]			=						common_inputs['attention_mask'].dtype
				_UpperCamelCase   :		Optional[Any]			=						torch.cat(
				    [common_inputs['attention_mask'], torch.ones(__snake_case	,       __snake_case	,       dtype=__snake_case)]	,       dim=1)
				_UpperCamelCase   :		List[Any]			=						[
				    (torch.zeros(__snake_case), torch.zeros(__snake_case)) for _ in range(__snake_case)
				]
			return common_inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			# Copied from OnnxConfig.generate_dummy_inputs
			# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
			# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
			_UpperCamelCase   :		int			=						compute_effective_axis_dimension(
			    __snake_case	,       fixed_dimension=OnnxConfig.default_fixed_batch	,       num_token_to_add=0)

			# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
			_UpperCamelCase   :		Optional[int]			=						tokenizer.num_special_tokens_to_add(__snake_case)
			_UpperCamelCase   :		Tuple			=						compute_effective_axis_dimension(
			    __snake_case	,       fixed_dimension=OnnxConfig.default_fixed_sequence	,       num_token_to_add=__snake_case)

			# Generate dummy inputs according to compute batch and sequence
			_UpperCamelCase   :		Union[str, Any]			=						[' '.join([tokenizer.unk_token]) * seq_length] * batch_size
			_UpperCamelCase   :		Any			=						dict(tokenizer(__snake_case	,       return_tensors=__snake_case))
			return common_inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			if self.task in ["default", "seq2seq-lm"]:
				_UpperCamelCase   :		Optional[int]			=						self._generate_dummy_inputs_for_default_and_seqaseq_lm(
				    __snake_case	,       batch_size=__snake_case	,       seq_length=__snake_case	,       is_pair=__snake_case	,       framework=__snake_case)

			elif self.task == "causal-lm":
				_UpperCamelCase   :		str			=						self._generate_dummy_inputs_for_causal_lm(
				    __snake_case	,       batch_size=__snake_case	,       seq_length=__snake_case	,       is_pair=__snake_case	,       framework=__snake_case)
			else:
				_UpperCamelCase   :		List[Any]			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    __snake_case	,       batch_size=__snake_case	,       seq_length=__snake_case	,       is_pair=__snake_case	,       framework=__snake_case)

			return common_inputs






		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case):
			if self.task in ["default", "seq2seq-lm"]:
				_UpperCamelCase   :		Dict			=						super()._flatten_past_key_values_(__snake_case	,       __snake_case	,       __snake_case	,       __snake_case)
			else:
				_UpperCamelCase   :		int			=						super(__snake_case	,       self)._flatten_past_key_values_(
				    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)







lowerCAmelCase__								=		range(2, 2_0 + 1)
lowerCAmelCase__								=		[1_0**k for k in range(ks[-1] + 1)]
lowerCAmelCase__								=		{}






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Optional[Any]							,       UpperCAmelCase_							:     List[str]							,       UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     int	)						->				Tuple:




	'''simple docstring'''




	_UpperCamelCase   :		Dict			=						sum(a_i[j] for j in range(UpperCAmelCase_							,       len(UpperCAmelCase_	)	)	)
	_UpperCamelCase   :		Optional[int]			=						sum(a_i[j] * base[j] for j in range(min(len(UpperCAmelCase_	)							,       UpperCAmelCase_	)	)	)

	_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						0, 0
	_UpperCamelCase   :		Optional[int]			=						n - i

	_UpperCamelCase   :		Union[str, Any]			=						memo.get(UpperCAmelCase_	)

	if sub_memo is not None:
		_UpperCamelCase   :		str			=						sub_memo.get(UpperCAmelCase_	)

		if jumps is not None and len(UpperCAmelCase_	) > 0:
			# find and make the largest jump without going over
			_UpperCamelCase   :		str			=						-1
			for _k in range(len(UpperCAmelCase_	) - 1							,       -1							,       -1	):
				if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
					_UpperCamelCase   :		Optional[Any]			=						_k
					break

			if max_jump >= 0:
				_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase   :		Optional[Any]			=						jumps[max_jump]
				# since the difference between jumps is cached, add c
				_UpperCamelCase   :		Tuple			=						diff + c
				for j in range(min(UpperCAmelCase_							,       len(UpperCAmelCase_	)	)	):
					_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						divmod(UpperCAmelCase_							,       1_0	)
				if new_c > 0:
					add(UpperCAmelCase_							,       UpperCAmelCase_							,       UpperCAmelCase_	)

		else:
			_UpperCamelCase   :		Union[str, Any]			=						[]
	else:
		_UpperCamelCase   :		List[Any]			=						{c: []}
		_UpperCamelCase   :		Optional[int]			=						sub_memo

	if dn >= max_dn or c + diff >= base[k]:
		return diff, dn

	if k > ks[0]:
		while True:
			# keep doing smaller jumps
			_UpperCamelCase	,		_UpperCamelCase   :		Optional[Any]			=						next_term(UpperCAmelCase_							,       k - 1							,       i + dn							,       UpperCAmelCase_	)
			diff += _diff
			dn += terms_jumped

			if dn >= max_dn or c + diff >= base[k]:
				break
	else:
		# would be too small a jump, just compute sequential terms instead
		_UpperCamelCase	,		_UpperCamelCase   :		Any			=						compute(UpperCAmelCase_							,       UpperCAmelCase_							,       i + dn							,       UpperCAmelCase_	)
		diff += _diff
		dn += terms_jumped

	_UpperCamelCase   :		List[str]			=						sub_memo[c]

	# keep jumps sorted by # of terms skipped
	_UpperCamelCase   :		Union[str, Any]			=						0
	while j < len(UpperCAmelCase_	):
		if jumps[j][1] > dn:
			break
		j += 1

	# cache the jump for this value digitsum(b) and c
	sub_memo[c].insert(UpperCAmelCase_							,       (diff, dn, k)	)
	return (diff, dn)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     int							,       UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     Any	)						->				Dict:




	'''simple docstring'''




	if i >= n:
		return 0, i
	if k > len(UpperCAmelCase_	):
		a_i.extend([0 for _ in range(k - len(UpperCAmelCase_	)	)]	)

	# note: a_i -> b * 10^k + c
	# ds_b -> digitsum(b)
	# ds_c -> digitsum(c)
	_UpperCamelCase   :		Any			=						i
	_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase   :		Any			=						0, 0, 0
	for j in range(len(UpperCAmelCase_	)	):
		if j >= k:
			ds_b += a_i[j]
		else:
			ds_c += a_i[j]

	while i < n:
		i += 1
		_UpperCamelCase   :		Union[str, Any]			=						ds_c + ds_b
		diff += addend
		_UpperCamelCase   :		Union[str, Any]			=						0
		for j in range(UpperCAmelCase_	):
			_UpperCamelCase   :		Union[str, Any]			=						a_i[j] + addend
			_UpperCamelCase	,		_UpperCamelCase   :		Any			=						divmod(UpperCAmelCase_							,       1_0	)

			ds_c += a_i[j]

		if addend > 0:
			break

	if addend > 0:
		add(UpperCAmelCase_							,       UpperCAmelCase_							,       UpperCAmelCase_	)
	return diff, i - start_i






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Union[str, Any]							,       UpperCAmelCase_							:     Optional[int]							,       UpperCAmelCase_							:     Any	)						->				Dict:




	'''simple docstring'''




	for j in range(UpperCAmelCase_							,       len(UpperCAmelCase_	)	):
		_UpperCamelCase   :		List[str]			=						digits[j] + addend
		if s >= 1_0:
			_UpperCamelCase	,		_UpperCamelCase   :		Any			=						divmod(UpperCAmelCase_							,       1_0	)
			_UpperCamelCase   :		Union[str, Any]			=						addend // 1_0 + quotient
		else:
			_UpperCamelCase   :		Dict			=						s
			_UpperCamelCase   :		Optional[Any]			=						addend // 1_0

		if addend == 0:
			break

	while addend > 0:
		_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						divmod(UpperCAmelCase_							,       1_0	)
		digits.append(UpperCAmelCase_	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     int = 1_0**1_5	)						->				int:




	'''simple docstring'''




	_UpperCamelCase   :		Optional[Any]			=						[1]
	_UpperCamelCase   :		Optional[int]			=						1
	_UpperCamelCase   :		int			=						0
	while True:
		_UpperCamelCase	,		_UpperCamelCase   :		List[Any]			=						next_term(UpperCAmelCase_							,       2_0							,       i + dn							,       UpperCAmelCase_	)
		dn += terms_jumped
		if dn == n - i:
			break

	_UpperCamelCase   :		str			=						0
	for j in range(len(UpperCAmelCase_	)	):
		a_n += digits[j] * 1_0**j
	return a_n


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




