

import argparse
from collections import defaultdict

import yaml


lowerCAmelCase  : List[Any]      =	'''docs/source/en/_toctree.yml'''

def  A_	(				a	):

      """simple docstring"""



      SCREAMING_SNAKE_CASE_					:	Tuple     							=  defaultdict(a	)
      SCREAMING_SNAKE_CASE_					:	Any     							=  []
      SCREAMING_SNAKE_CASE_					:	Tuple     							=  []
      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	)

      SCREAMING_SNAKE_CASE_					:	List[Any]     							=  new_doc_list
      SCREAMING_SNAKE_CASE_					:	List[Any]     							=  [key for key, value in counts.items() if value > 1]

      SCREAMING_SNAKE_CASE_					:	Optional[Any]     							=  []
      for duplicate_key in duplicates:
            SCREAMING_SNAKE_CASE_					:	Tuple     							=  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]	)
      SCREAMING_SNAKE_CASE_					:	Optional[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	):

      """simple docstring"""



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

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

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

      SCREAMING_SNAKE_CASE_					:	int     							=  api_doc[scheduler_idx]['sections']
      SCREAMING_SNAKE_CASE_					:	Tuple     							=  clean_doc_toc(a	)

      SCREAMING_SNAKE_CASE_					:	Optional[Any]     							=  False
      if new_scheduler_doc != scheduler_doc:
            SCREAMING_SNAKE_CASE_					:	Dict     							=  True
            if overwrite:
                  SCREAMING_SNAKE_CASE_					:	Dict     							=  new_scheduler_doc

      if diff:
            if overwrite:
                  SCREAMING_SNAKE_CASE_					:	Tuple     							=  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	):

      """simple docstring"""



      with open(a       , encoding='utf-8'	) as f:
            SCREAMING_SNAKE_CASE_					:	int     							=  yaml.safe_load(f.read()	)

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

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

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

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

      # sort overall pipeline doc
      SCREAMING_SNAKE_CASE_					:	Optional[int]     							=  clean_doc_toc(a	)

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

      if diff:
            if overwrite:
                  SCREAMING_SNAKE_CASE_					:	Tuple     							=  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__":
    lowerCAmelCase  : Tuple      =	argparse.ArgumentParser()
    parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
    lowerCAmelCase  : Optional[Any]      =	parser.parse_args()

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







from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
				import tensorflow as tf

				from transformers import TFBlipTextModel
				from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST



class        lowerCamelCase__				:





			def __init__(       self	:	Optional[Any]  ,  lowercase__	:	int  ,  lowercase__	:	List[str]=12  ,  lowercase__	:	Optional[int]=7  ,  lowercase__	:	List[Any]=True  ,  lowercase__	:	str=True  ,  lowercase__	:	Optional[Any]=True  ,  lowercase__	:	List[str]=99  ,  lowercase__	:	Optional[int]=32  ,  lowercase__	:	Tuple=32  ,  lowercase__	:	Dict=2  ,  lowercase__	:	Optional[int]=4  ,  lowercase__	:	str=37  ,  lowercase__	:	Optional[Any]=0.1  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	Union[str, Any]=5_12  ,  lowercase__	:	List[str]=0.0_2  ,  lowercase__	:	Tuple=0  ,  lowercase__	:	List[Any]=None  ,  ):
										_lowerCAmelCase							=							parent
										_lowerCAmelCase							=							batch_size
										_lowerCAmelCase							=							seq_length
										_lowerCAmelCase							=							is_training
										_lowerCAmelCase							=							use_input_mask
										_lowerCAmelCase							=							use_labels
										_lowerCAmelCase							=							vocab_size
										_lowerCAmelCase							=							hidden_size
										_lowerCAmelCase							=							projection_dim
										_lowerCAmelCase							=							num_hidden_layers
										_lowerCAmelCase							=							num_attention_heads
										_lowerCAmelCase							=							intermediate_size
										_lowerCAmelCase							=							dropout
										_lowerCAmelCase							=							attention_dropout
										_lowerCAmelCase							=							max_position_embeddings
										_lowerCAmelCase							=							initializer_range
										_lowerCAmelCase							=							scope
										_lowerCAmelCase							=							bos_token_id





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size						)

										_lowerCAmelCase							=							None
										if self.use_input_mask:
																	_lowerCAmelCase							=							random_attention_mask([self.batch_size, self.seq_length]						)

										if input_mask is not None:
																	_lowerCAmelCase							=							input_mask.numpy()
																	_lowerCAmelCase			,		_lowerCAmelCase							=							input_mask.shape
																	_lowerCAmelCase							=							np.random.randint(1  ,  seq_length - 1  ,  size=(batch_size,)						)
																	for batch_idx, start_index in enumerate(lowercase__						):
																								_lowerCAmelCase							=							1
																								_lowerCAmelCase							=							0

										_lowerCAmelCase							=							self.get_config()

										return config, input_ids, tf.convert_to_tensor(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										return BlipTextConfig(
										    vocab_size=self.vocab_size  ,  hidden_size=self.hidden_size  ,  projection_dim=self.projection_dim  ,  num_hidden_layers=self.num_hidden_layers  ,  num_attention_heads=self.num_attention_heads  ,  intermediate_size=self.intermediate_size  ,  dropout=self.dropout  ,  attention_dropout=self.attention_dropout  ,  max_position_embeddings=self.max_position_embeddings  ,  initializer_range=self.initializer_range  ,  bos_token_id=self.bos_token_id  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	int  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Union[str, Any]						):
										_lowerCAmelCase							=							TFBlipTextModel(config=lowercase__						)
										_lowerCAmelCase							=							model(lowercase__  ,  attention_mask=lowercase__  ,  training=lowercase__						)
										_lowerCAmelCase							=							model(lowercase__  ,  training=lowercase__						)
										self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)						)
										self.parent.assertEqual(result.pooler_output.shape  ,  (self.batch_size, self.hidden_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							self.prepare_config_and_inputs()
										_lowerCAmelCase			,		_lowerCAmelCase			,		_lowerCAmelCase							=							config_and_inputs
										_lowerCAmelCase							=							{'input_ids': input_ids, 'attention_mask': input_mask}
										return config, inputs_dict







@require_tf
class        lowerCamelCase__				(   UpperCAmelCase       ,unittest.TestCase				):
			UpperCamelCase__    							=(TFBlipTextModel,) if is_tf_available() else ()
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										_lowerCAmelCase							=							BlipTextModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=lowercase__  ,  hidden_size=37						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										self.config_tester.run_common_tests()





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										pass





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										pass





			@unittest.skip(reason='Blip does not use inputs_embeds'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										pass





			@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										pass





			@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										pass





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_lowerCAmelCase							=							TFBlipTextModel.from_pretrained(lowercase__						)
																	self.assertIsNotNone(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  lowercase__	:	Any=True						):
										super().test_pt_tf_model_equivalence(allow_missing_keys=lowercase__						)






