


'''simple docstring'''
import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import BertGenerationDecoder, BertGenerationEncoder
class        lowercase     :

		"""simple docstring"""

		def __init__(		self   ,       UpperCamelCase_   ,       UpperCamelCase_=13   ,       UpperCamelCase_=7   ,       UpperCamelCase_=True   ,       UpperCamelCase_=True   ,       UpperCamelCase_=99   ,       UpperCamelCase_=32   ,       UpperCamelCase_=5   ,       UpperCamelCase_=4   ,       UpperCamelCase_=37   ,       UpperCamelCase_="gelu"   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=50   ,       UpperCamelCase_=0.02   ,       UpperCamelCase_=True   ,       UpperCamelCase_=None   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			parent
							UpperCamelCase__       :List[Any]							 =			batch_size
							UpperCamelCase__       :Optional[int]							 =			seq_length
							UpperCamelCase__       :List[Any]							 =			is_training
							UpperCamelCase__       :Any							 =			use_input_mask
							UpperCamelCase__       :Any							 =			vocab_size
							UpperCamelCase__       :Optional[int]							 =			hidden_size
							UpperCamelCase__       :str							 =			num_hidden_layers
							UpperCamelCase__       :Union[str, Any]							 =			num_attention_heads
							UpperCamelCase__       :Dict							 =			intermediate_size
							UpperCamelCase__       :List[str]							 =			hidden_act
							UpperCamelCase__       :Dict							 =			hidden_dropout_prob
							UpperCamelCase__       :Union[str, Any]							 =			attention_probs_dropout_prob
							UpperCamelCase__       :Optional[int]							 =			max_position_embeddings
							UpperCamelCase__       :Union[str, Any]							 =			initializer_range
							UpperCamelCase__       :List[str]							 =			use_labels
							UpperCamelCase__       :str							 =			scope




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			ids_tensor([self.batch_size, self.seq_length]   ,       self.vocab_size							)

							UpperCamelCase__       :Optional[Any]							 =			None
							if self.use_input_mask:
												UpperCamelCase__       :Optional[Any]							 =			random_attention_mask([self.batch_size, self.seq_length]							)

							if self.use_labels:
												UpperCamelCase__       :Optional[int]							 =			ids_tensor([self.batch_size, self.seq_length]   ,       self.vocab_size							)

							UpperCamelCase__       :Union[str, Any]							 =			self.get_config()

							return config, input_ids, input_mask, token_labels




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							return BertGenerationConfig(
							    vocab_size=self.vocab_size   ,       hidden_size=self.hidden_size   ,       num_hidden_layers=self.num_hidden_layers   ,       num_attention_heads=self.num_attention_heads   ,       intermediate_size=self.intermediate_size   ,       hidden_act=self.hidden_act   ,       hidden_dropout_prob=self.hidden_dropout_prob   ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,       max_position_embeddings=self.max_position_embeddings   ,       is_decoder=UpperCamelCase_   ,       initializer_range=self.initializer_range   ,       )




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							(
							    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,           
							)       :Optional[int]							 =			self.prepare_config_and_inputs()

							UpperCamelCase__       :int							 =			True
							UpperCamelCase__       :List[str]							 =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
							UpperCamelCase__       :Optional[int]							 =			ids_tensor([self.batch_size, self.seq_length]   ,       vocab_size=2							)

							return (
							    config,
							    input_ids,
							    input_mask,
							    token_labels,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			BertGenerationEncoder(config=UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()
							UpperCamelCase__       :int							 =			model(UpperCamelCase_   ,       attention_mask=UpperCamelCase_							)
							UpperCamelCase__       :Union[str, Any]							 =			model(UpperCamelCase_							)
							self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)							)




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			True
							UpperCamelCase__       :Dict							 =			BertGenerationEncoder(config=UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()
							UpperCamelCase__       :int							 =			model(
							    UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       encoder_hidden_states=UpperCamelCase_   ,       encoder_attention_mask=UpperCamelCase_   ,       )
							UpperCamelCase__       :str							 =			model(
							    UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       encoder_hidden_states=UpperCamelCase_   ,       )
							self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)							)




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			True
							UpperCamelCase__       :Tuple							 =			True
							UpperCamelCase__       :Any							 =			BertGenerationDecoder(config=UpperCamelCase_							).to(UpperCamelCase_							).eval()

							# first forward pass
							UpperCamelCase__       :Tuple							 =			model(
							    UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       encoder_hidden_states=UpperCamelCase_   ,       encoder_attention_mask=UpperCamelCase_   ,       use_cache=UpperCamelCase_   ,       )
							UpperCamelCase__       :int							 =			outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							UpperCamelCase__       :Dict							 =			ids_tensor((self.batch_size, 3)   ,       config.vocab_size							)
							UpperCamelCase__       :Union[str, Any]							 =			ids_tensor((self.batch_size, 3)   ,       vocab_size=2							)

							# append to next input_ids and
							UpperCamelCase__       :str							 =			torch.cat([input_ids, next_tokens]   ,       dim=-1							)
							UpperCamelCase__       :Union[str, Any]							 =			torch.cat([input_mask, next_mask]   ,       dim=-1							)

							UpperCamelCase__       :Optional[int]							 =			model(
							    UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       encoder_hidden_states=UpperCamelCase_   ,       encoder_attention_mask=UpperCamelCase_   ,       output_hidden_states=UpperCamelCase_   ,       )['''hidden_states'''][0]
							UpperCamelCase__       :Tuple							 =			model(
							    UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       encoder_hidden_states=UpperCamelCase_   ,       encoder_attention_mask=UpperCamelCase_   ,       past_key_values=UpperCamelCase_   ,       output_hidden_states=UpperCamelCase_   ,       )['''hidden_states'''][0]

							# select random slice
							UpperCamelCase__       :List[str]							 =			ids_tensor((1,)   ,       output_from_past.shape[-1]							).item()
							UpperCamelCase__       :str							 =			output_from_no_past[:, -3:, random_slice_idx].detach()
							UpperCamelCase__       :Optional[int]							 =			output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]							)

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(UpperCamelCase_   ,       UpperCamelCase_   ,       atol=1e-3							)							)




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       *UpperCamelCase_   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :Optional[int]							 =			BertGenerationDecoder(UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()
							UpperCamelCase__       :Dict							 =			model(UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       labels=UpperCamelCase_							)
							self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.vocab_size)							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       :List[str]							 =			self.prepare_config_and_inputs()
							UpperCamelCase__       :Optional[Any]							 =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class        lowercase     (							__lowerCamelCase     ,		__lowerCamelCase     ,		__lowerCamelCase     ,		unittest.TestCase ):

		"""simple docstring"""

		_a   			=    (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
		_a   			=    (BertGenerationDecoder,) if is_torch_available() else ()
		_a   			=    (
		    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
		    if is_torch_available()
		    else {}
		)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			BertGenerationEncoderTester(self							)
							UpperCamelCase__       :Tuple							 =			ConfigTester(self   ,       config_class=UpperCamelCase_   ,       hidden_size=37							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[int]							 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       :List[str]							 =			self.model_tester.prepare_config_and_inputs()
							UpperCamelCase__       :Optional[int]							 =			'''bert'''
							self.model_tester.create_and_check_model(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(*UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							(
							    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,    (
							    UpperCamelCase__       
							) ,           
							)       :Any							 =			self.model_tester.prepare_config_and_inputs_for_decoder()

							UpperCamelCase__       :Optional[int]							 =			None

							self.model_tester.create_and_check_model_as_decoder(
							    UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       )




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase_							)




		@slow
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
							self.assertIsNotNone(UpperCamelCase_							)



@require_torch
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		@slow
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
							UpperCamelCase__       :List[Any]							 =			torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]							)
							with torch.no_grad():
												UpperCamelCase__       :Dict							 =			model(UpperCamelCase_							)[0]
							UpperCamelCase__       :Optional[Any]							 =			torch.Size([1, 8, 1024]							)
							self.assertEqual(output.shape   ,       UpperCamelCase_							)
							UpperCamelCase__       :Optional[Any]							 =			torch.tensor(
							    [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]]							)
							self.assertTrue(torch.allclose(output[:, :3, :3]   ,       UpperCamelCase_   ,       atol=1e-4							)							)



@require_torch
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		@slow
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
							UpperCamelCase__       :Optional[int]							 =			torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]							)
							with torch.no_grad():
												UpperCamelCase__       :Any							 =			model(UpperCamelCase_							)[0]
							UpperCamelCase__       :Tuple							 =			torch.Size([1, 8, 50358]							)
							self.assertEqual(output.shape   ,       UpperCamelCase_							)
							UpperCamelCase__       :Tuple							 =			torch.tensor(
							    [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]]							)
							self.assertTrue(torch.allclose(output[:, :3, :3]   ,       UpperCamelCase_   ,       atol=1e-4							)							)



'''simple docstring'''
import argparse
from collections import defaultdict

import yaml


__snake_case											=					'''docs/source/en/_toctree.yml'''


def       a				(      __a       )  ->     int:



					'''simple docstring'''






					UpperCamelCase__       :int							 =			defaultdict(__a       )
					UpperCamelCase__       :int							 =			[]
					UpperCamelCase__       :int							 =			[]
					for doc in doc_list:
										if "local" in doc:
															counts[doc["local"]] += 1

										if doc["title"].lower() == "overview":
															overview_doc.append({'''local''': doc['''local'''], '''title''': doc['''title''']}       )
										else:
															new_doc_list.append(__a       )

					UpperCamelCase__       :Union[str, Any]							 =			new_doc_list
					UpperCamelCase__       :Tuple							 =			[key for key, value in counts.items() if value > 1]

					UpperCamelCase__       :Union[str, Any]							 =			[]
					for duplicate_key in duplicates:
										UpperCamelCase__       :Dict							 =			list({doc['''title'''] for doc in doc_list if doc['''local'''] == duplicate_key}       )
										if len(__a       ) > 1:
															raise ValueError(
															    f'''{duplicate_key} is present several times in the documentation table of content at '''
															    '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '''
															    '''others.'''       )
										# Only add this once
										new_doc.append({'''local''': duplicate_key, '''title''': titles[0]}       )

					# Add none duplicate-keys
					new_doc.extend([doc for doc in doc_list if '''local''' not in counts or counts[doc['''local''']] == 1]       )
					UpperCamelCase__       :Union[str, Any]							 =			sorted(__a			, key=lambda __a       : s["title"].lower()       )

					# "overview" gets special treatment and is always first
					if len(__a       ) > 1:
										raise ValueError('''{doc_list} has two \'overview\' docs which is not allowed.'''       )

					overview_doc.extend(__a       )

					# Sort
					return overview_doc


def       a				(      __a=False       )  ->     Any:



					'''simple docstring'''






					with open(__a			, encoding='''utf-8'''       ) as f:
										UpperCamelCase__       :Any							 =			yaml.safe_load(f.read()       )

					# Get to the API doc
					UpperCamelCase__       :str							 =			0
					while content[api_idx]["title"] != "API":
										api_idx += 1
					UpperCamelCase__       :str							 =			content[api_idx]['''sections''']

					# Then to the model doc
					UpperCamelCase__       :Optional[Any]							 =			0
					while api_doc[scheduler_idx]["title"] != "Schedulers":
										scheduler_idx += 1

					UpperCamelCase__       :List[Any]							 =			api_doc[scheduler_idx]['''sections''']
					UpperCamelCase__       :Union[str, Any]							 =			clean_doc_toc(__a       )

					UpperCamelCase__       :List[Any]							 =			False
					if new_scheduler_doc != scheduler_doc:
										UpperCamelCase__       :Optional[int]							 =			True
										if overwrite:
															UpperCamelCase__       :Dict							 =			new_scheduler_doc

					if diff:
										if overwrite:
															UpperCamelCase__       :Any							 =			api_doc
															with open(__a			, '''w'''			, encoding='''utf-8'''       ) as f:
																				f.write(yaml.dump(__a			, allow_unicode=__a       )       )
										else:
															raise ValueError(
															    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''       )


def       a				(      __a=False       )  ->     Optional[Any]:



					'''simple docstring'''






					with open(__a			, encoding='''utf-8'''       ) as f:
										UpperCamelCase__       :str							 =			yaml.safe_load(f.read()       )

					# Get to the API doc
					UpperCamelCase__       :Optional[Any]							 =			0
					while content[api_idx]["title"] != "API":
										api_idx += 1
					UpperCamelCase__       :Any							 =			content[api_idx]['''sections''']

					# Then to the model doc
					UpperCamelCase__       :str							 =			0
					while api_doc[pipeline_idx]["title"] != "Pipelines":
										pipeline_idx += 1

					UpperCamelCase__       :Any							 =			False
					UpperCamelCase__       :Union[str, Any]							 =			api_doc[pipeline_idx]['''sections''']
					UpperCamelCase__       :Tuple							 =			[]

					# sort sub pipeline docs
					for pipeline_doc in pipeline_docs:
										if "section" in pipeline_doc:
															UpperCamelCase__       :Dict							 =			pipeline_doc['''section''']
															UpperCamelCase__       :Optional[Any]							 =			clean_doc_toc(__a       )
															if overwrite:
																				UpperCamelCase__       :Optional[int]							 =			new_sub_pipeline_doc
										new_pipeline_docs.append(__a       )

					# sort overall pipeline doc
					UpperCamelCase__       :Optional[Any]							 =			clean_doc_toc(__a       )

					if new_pipeline_docs != pipeline_docs:
										UpperCamelCase__       :int							 =			True
										if overwrite:
															UpperCamelCase__       :Union[str, Any]							 =			new_pipeline_docs

					if diff:
										if overwrite:
															UpperCamelCase__       :Dict							 =			api_doc
															with open(__a			, '''w'''			, encoding='''utf-8'''       ) as f:
																				f.write(yaml.dump(__a			, allow_unicode=__a       )       )
										else:
															raise ValueError(
															    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''       )


if __name__ == "__main__":
		__snake_case											=					argparse.ArgumentParser()
		parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''')
		__snake_case											=					parser.parse_args()

		check_scheduler_doc(args.fix_and_overwrite)
		check_pipeline_doc(args.fix_and_overwrite)