


import argparse
import collections
import os
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
lowerCAmelCase	=							'src/transformers'
lowerCAmelCase	=							'docs/source/en'
lowerCAmelCase	=							'.'
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		with open(SCREAMING_SNAKE_CASE						,    '''r'''						,    encoding='''utf-8'''						,    newline='''\n'''					) as f:
				lowercase__						=				f.readlines()
		# Find the start prompt.
		lowercase__						=				0
		while not lines[start_index].startswith(SCREAMING_SNAKE_CASE					):
				start_index += 1
		start_index += 1

		lowercase__						=				start_index
		while not lines[end_index].startswith(SCREAMING_SNAKE_CASE					):
				end_index += 1
		end_index -= 1

		while len(lines[start_index]					) <= 1:
				start_index += 1
		while len(lines[end_index]					) <= 1:
				end_index -= 1
		end_index += 1
		return "".join(lines[start_index:end_index]					), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
lowerCAmelCase	=							'Model|Encoder|Decoder|ForConditionalGeneration'
# Regexes that match TF/Flax/PT model names.
lowerCAmelCase	=							re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
lowerCAmelCase	=							re.compile(R'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
lowerCAmelCase	=							re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# This is to make sure the transformers module imported is the one in the repo.
lowerCAmelCase	=							direct_transformers_import(TRANSFORMERS_PATH)
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		lowercase__						=				re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)'''						,    SCREAMING_SNAKE_CASE					)
		return [m.group(0					) for m in matches]
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		lowercase__						=				2 if text == '''✅''' or text == '''❌''' else len(SCREAMING_SNAKE_CASE					)
		lowercase__						=				(width - text_length) // 2
		lowercase__						=				width - text_length - left_indent
		return " " * left_indent + text + " " * right_indent
def       _a			(				):


		"""simple docstring"""




		lowercase__						=				transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
		lowercase__						=				{
		    name: config_maping_names[code]
		    for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
		    if code in config_maping_names
		}
		lowercase__						=				{name: config.replace('''Config'''						,    ''''''					) for name, config in model_name_to_config.items()}

		# Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
		lowercase__						=				collections.defaultdict(SCREAMING_SNAKE_CASE					)
		lowercase__						=				collections.defaultdict(SCREAMING_SNAKE_CASE					)
		lowercase__						=				collections.defaultdict(SCREAMING_SNAKE_CASE					)
		lowercase__						=				collections.defaultdict(SCREAMING_SNAKE_CASE					)
		lowercase__						=				collections.defaultdict(SCREAMING_SNAKE_CASE					)

		# Let's lookup through all transformers object (once).
		for attr_name in dir(SCREAMING_SNAKE_CASE					):
				lowercase__						=				None
				if attr_name.endswith('''Tokenizer'''					):
						lowercase__						=				slow_tokenizers
						lowercase__						=				attr_name[:-9]
				elif attr_name.endswith('''TokenizerFast'''					):
						lowercase__						=				fast_tokenizers
						lowercase__						=				attr_name[:-13]
				elif _re_tf_models.match(SCREAMING_SNAKE_CASE					) is not None:
						lowercase__						=				tf_models
						lowercase__						=				_re_tf_models.match(SCREAMING_SNAKE_CASE					).groups()[0]
				elif _re_flax_models.match(SCREAMING_SNAKE_CASE					) is not None:
						lowercase__						=				flax_models
						lowercase__						=				_re_flax_models.match(SCREAMING_SNAKE_CASE					).groups()[0]
				elif _re_pt_models.match(SCREAMING_SNAKE_CASE					) is not None:
						lowercase__						=				pt_models
						lowercase__						=				_re_pt_models.match(SCREAMING_SNAKE_CASE					).groups()[0]

				if lookup_dict is not None:
						while len(SCREAMING_SNAKE_CASE					) > 0:
								if attr_name in model_name_to_prefix.values():
										lowercase__						=				True
										break
								# Try again after removing the last word in the name
								lowercase__						=				''''''.join(camel_case_split(SCREAMING_SNAKE_CASE					)[:-1]					)

    # Let's build that table!
		lowercase__						=				list(model_name_to_config.keys()					)
		model_names.sort(key=str.lower					)
		lowercase__						=				['''Model''', '''Tokenizer slow''', '''Tokenizer fast''', '''PyTorch support''', '''TensorFlow support''', '''Flax Support''']
		# We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
		lowercase__						=				[len(SCREAMING_SNAKE_CASE					) + 2 for c in columns]
		lowercase__						=				max([len(SCREAMING_SNAKE_CASE					) for name in model_names]					) + 2

		# Build the table per se
		lowercase__						=				'''|''' + '''|'''.join([_center_text(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					) for c, w in zip(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					)]					) + '''|\n'''
		# Use ":-----:" format to center-aligned table cell texts
		table += "|" + "|".join([''':''' + '''-''' * (w - 2) + ''':''' for w in widths]					) + "|\n"

		lowercase__						=				{True: '''✅''', False: '''❌'''}
		for name in model_names:
				lowercase__						=				model_name_to_prefix[name]
				lowercase__						=				[
				    name,
				    check[slow_tokenizers[prefix]],
				    check[fast_tokenizers[prefix]],
				    check[pt_models[prefix]],
				    check[tf_models[prefix]],
				    check[flax_models[prefix]],
				]
				table += "|" + "|".join([_center_text(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					) for l, w in zip(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					)]					) + "|\n"
		return table
def       _a			(				SCREAMING_SNAKE_CASE=False					):


		"""simple docstring"""




		lowercase__					,		lowercase__					,		lowercase__					,		lowercase__						=				_find_text_in_file(
		    filename=os.path.join(SCREAMING_SNAKE_CASE						,    '''index.md'''					)						,    start_prompt='''<!--This table is updated automatically from the auto modules'''						,    end_prompt='''<!-- End table-->'''						,    )
		lowercase__						=				get_model_table_from_auto_modules()

		if current_table != new_table:
				if overwrite:
						with open(os.path.join(SCREAMING_SNAKE_CASE						,    '''index.md'''					)						,    '''w'''						,    encoding='''utf-8'''						,    newline='''\n'''					) as f:
								f.writelines(lines[:start_index] + [new_table] + lines[end_index:]					)
				else:
						raise ValueError(
						    '''The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this.'''					)


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

			check_model_table(args.fix_and_overwrite)






import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class 	_a							(      UpperCamelCase__					,     UpperCamelCase__					,     UpperCamelCase__					,     unittest.TestCase      ):
							_lowercase     :      int  				=			AltDiffusionPipeline
							_lowercase     :      Tuple  				=			TEXT_TO_IMAGE_PARAMS
							_lowercase     :      List[str]  				=			TEXT_TO_IMAGE_BATCH_PARAMS
							_lowercase     :      Union[str, Any]  				=			TEXT_TO_IMAGE_IMAGE_PARAMS
							_lowercase     :      int  				=			TEXT_TO_IMAGE_IMAGE_PARAMS

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





									"""simple docstring"""







									torch.manual_seed(0							)
									lowercase__						=				UNetaDConditionModel(
									    block_out_channels=(32, 64)   ,						layers_per_block=2   ,						sample_size=32   ,						in_channels=4   ,						out_channels=4   ,						down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,						up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,						cross_attention_dim=32   ,						)
									lowercase__						=				DDIMScheduler(
									    beta_start=0.00085   ,						beta_end=0.012   ,						beta_schedule='''scaled_linear'''   ,						clip_sample=UpperCamelCase_   ,						set_alpha_to_one=UpperCamelCase_   ,						)
									torch.manual_seed(0							)
									lowercase__						=				AutoencoderKL(
									    block_out_channels=[32, 64]   ,						in_channels=3   ,						out_channels=3   ,						down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,						up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,						latent_channels=4   ,						)

									# TODO: address the non-deterministic text encoder (fails for save-load tests)
									# torch.manual_seed(0)
									# text_encoder_config = RobertaSeriesConfig(
									#     hidden_size=32,
									#     project_dim=32,
									#     intermediate_size=37,
									#     layer_norm_eps=1e-05,
									#     num_attention_heads=4,
									#     num_hidden_layers=5,
									#     vocab_size=5002,
									# )
									# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

									torch.manual_seed(0							)
									lowercase__						=				CLIPTextConfig(
									    bos_token_id=0   ,						eos_token_id=2   ,						hidden_size=32   ,						projection_dim=32   ,						intermediate_size=37   ,						layer_norm_eps=1E-0_5   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						pad_token_id=1   ,						vocab_size=5_002   ,						)
									lowercase__						=				CLIPTextModel(UpperCamelCase_							)

									lowercase__						=				XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''							)
									lowercase__						=				77

									lowercase__						=				{
									    '''unet''': unet,
									    '''scheduler''': scheduler,
									    '''vae''': vae,
									    '''text_encoder''': text_encoder,
									    '''tokenizer''': tokenizer,
									    '''safety_checker''': None,
									    '''feature_extractor''': None,
									}
									return components

							def 				lowerCamelCase_			(							self:     List[str]   ,						UpperCamelCase_:     Optional[int]   ,						UpperCamelCase_:     str=0							)      ->       Union[str, Any]:





									"""simple docstring"""







									if str(UpperCamelCase_							).startswith('''mps'''							):
											lowercase__						=				torch.manual_seed(UpperCamelCase_							)
									else:
											lowercase__						=				torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
									lowercase__						=				{
									    '''prompt''': '''A painting of a squirrel eating a burger''',
									    '''generator''': generator,
									    '''num_inference_steps''': 2,
									    '''guidance_scale''': 6.0,
									    '''output_type''': '''numpy''',
									}
									return inputs

							def 				lowerCamelCase_			(							self:     Optional[Any]							)      ->       Dict:





									"""simple docstring"""







									super().test_attention_slicing_forward_pass(expected_max_diff=3E-3							)

							def 				lowerCamelCase_			(							self:     Dict							)      ->       str:





									"""simple docstring"""







									super().test_inference_batch_single_identical(expected_max_diff=3E-3							)

							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       Any:





									"""simple docstring"""







									lowercase__						=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator

									lowercase__						=				self.get_dummy_components()
									torch.manual_seed(0							)
									lowercase__						=				RobertaSeriesConfig(
									    hidden_size=32   ,						project_dim=32   ,						intermediate_size=37   ,						layer_norm_eps=1E-0_5   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						vocab_size=5_002   ,						)
									# TODO: remove after fixing the non-deterministic text encoder
									lowercase__						=				RobertaSeriesModelWithTransformation(UpperCamelCase_							)
									lowercase__						=				text_encoder

									lowercase__						=				AltDiffusionPipeline(**UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				self.get_dummy_inputs(UpperCamelCase_							)
									lowercase__						=				'''A photo of an astronaut'''
									lowercase__						=				alt_pipe(**UpperCamelCase_							)
									lowercase__						=				output.images
									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									lowercase__						=				np.array(
									    [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


							def 				lowerCamelCase_			(							self:     Optional[Any]							)      ->       Tuple:





									"""simple docstring"""







									lowercase__						=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator

									lowercase__						=				self.get_dummy_components()
									lowercase__						=				PNDMScheduler(skip_prk_steps=UpperCamelCase_							)
									torch.manual_seed(0							)
									lowercase__						=				RobertaSeriesConfig(
									    hidden_size=32   ,						project_dim=32   ,						intermediate_size=37   ,						layer_norm_eps=1E-0_5   ,						num_attention_heads=4   ,						num_hidden_layers=5   ,						vocab_size=5_002   ,						)
									# TODO: remove after fixing the non-deterministic text encoder
									lowercase__						=				RobertaSeriesModelWithTransformation(UpperCamelCase_							)
									lowercase__						=				text_encoder
									lowercase__						=				AltDiffusionPipeline(**UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				self.get_dummy_inputs(UpperCamelCase_							)
									lowercase__						=				alt_pipe(**UpperCamelCase_							)
									lowercase__						=				output.images
									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									lowercase__						=				np.array(
									    [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


@slow
@require_torch_gpu
class 	_a							(      unittest.TestCase      ):

							def 				lowerCamelCase_			(							self:     Dict							)      ->       Optional[int]:





									"""simple docstring"""







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()

							def 				lowerCamelCase_			(							self:     Union[str, Any]							)      ->       Union[str, Any]:





									"""simple docstring"""







									lowercase__						=				AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,						safety_checker=UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				'''A painting of a squirrel eating a burger'''
									lowercase__						=				torch.manual_seed(0							)
									lowercase__						=				alt_pipe([prompt]   ,						generator=UpperCamelCase_   ,						guidance_scale=6.0   ,						num_inference_steps=20   ,						output_type='''np'''							)

									lowercase__						=				output.images

									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									lowercase__						=				np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


							def 				lowerCamelCase_			(							self:     Union[str, Any]							)      ->       Dict:





									"""simple docstring"""







									lowercase__						=				DDIMScheduler.from_pretrained('''BAAI/AltDiffusion'''   ,						subfolder='''scheduler'''							)

									lowercase__						=				AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,						scheduler=UpperCamelCase_   ,						safety_checker=UpperCamelCase_							)
									lowercase__						=				alt_pipe.to(UpperCamelCase_							)
									alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__						=				'''A painting of a squirrel eating a burger'''
									lowercase__						=				torch.manual_seed(0							)

									lowercase__						=				alt_pipe([prompt]   ,						generator=UpperCamelCase_   ,						num_inference_steps=2   ,						output_type='''numpy'''							)
									lowercase__						=				output.images

									lowercase__						=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									lowercase__						=				np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323]							)

									assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2


