

'''simple docstring'''







import tempfile
import unittest

import transformers
from transformers import XGLMConfig, XGLMTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
							import jax
							import jax.numpy as jnp
							import numpy as np

							from transformers.modeling_flax_pytorch_utils import (
							    convert_pytorch_state_dict_to_flax,
							    load_flax_weights_in_pytorch_model,
							)
							from transformers.models.xglm.modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel


if is_torch_available():
							import torch
@require_flax
class 					__a   :

						def __init__(		self	:						Optional[int]    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Any=14    ,			lowercase__	:						int=7    ,			lowercase__	:						Tuple=True    ,			lowercase__	:						Tuple=True    ,			lowercase__	:						Optional[int]=True    ,			lowercase__	:						int=99    ,			lowercase__	:						str=32    ,			lowercase__	:						Union[str, Any]=5    ,			lowercase__	:						Any=4    ,			lowercase__	:						List[str]=37    ,			lowercase__	:						str="gelu"    ,			lowercase__	:						Optional[int]=0.1    ,			lowercase__	:						Any=0.1    ,			lowercase__	:						List[str]=5_12    ,			lowercase__	:						Union[str, Any]=0.02    ,			lowercase__	:						List[str]=None    ,			)						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     parent
							_lowercase							=     batch_size
							_lowercase							=     seq_length
							_lowercase							=     is_training
							_lowercase							=     use_input_mask
							_lowercase							=     use_labels
							_lowercase							=     vocab_size
							_lowercase							=     d_model
							_lowercase							=     num_hidden_layers
							_lowercase							=     num_attention_heads
							_lowercase							=     ffn_dim
							_lowercase							=     activation_function
							_lowercase							=     activation_dropout
							_lowercase							=     attention_dropout
							_lowercase							=     max_position_embeddings
							_lowercase							=     initializer_range
							_lowercase							=     None
							_lowercase							=     0
							_lowercase							=     2
							_lowercase							=     1

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

							"""simple docstring"""



							_lowercase							=     np.clip(ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)    ,			3    ,			self.vocab_size)

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

							_lowercase							=     XGLMConfig(
							    vocab_size=self.vocab_size    ,			d_model=self.hidden_size    ,			num_layers=self.num_hidden_layers    ,			attention_heads=self.num_attention_heads    ,			ffn_dim=self.ffn_dim    ,			activation_function=self.activation_function    ,			activation_dropout=self.activation_dropout    ,			attention_dropout=self.attention_dropout    ,			max_position_embeddings=self.max_position_embeddings    ,			initializer_range=self.initializer_range    ,			use_cache=lowercase__    ,			bos_token_id=self.bos_token_id    ,			eos_token_id=self.eos_token_id    ,			pad_token_id=self.pad_token_id    ,			)

							return (config, input_ids, input_mask)

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

							"""simple docstring"""



							_lowercase							=     self.prepare_config_and_inputs()
							_lowercase		,		_lowercase		,		_lowercase							=     config_and_inputs
							_lowercase							=     {"""input_ids""": input_ids, """attention_mask""": attention_mask}
							return config, inputs_dict

						def 	_UpperCAmelCase							(		self	:						Any)						->str:

							"""simple docstring"""



							_lowercase		,		_lowercase		,		_lowercase							=     self.prepare_config_and_inputs()

							_lowercase							=     floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
							_lowercase							=     ids_tensor([self.batch_size, self.seq_length]    ,			vocab_size=2)

							return (
							    config,
							    input_ids,
							    attention_mask,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)

						def 	_UpperCAmelCase							(		self	:						List[Any]    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						List[str]    ,			lowercase__	:						List[str]    ,			lowercase__	:						List[str])						->int:

							"""simple docstring"""



							_lowercase							=     20
							_lowercase							=     model_class_name(lowercase__)

							_lowercase							=     model.init_cache(input_ids.shape[0]    ,			lowercase__)
							_lowercase							=     jnp.ones((input_ids.shape[0], max_decoder_length)    ,			dtype="""i4""")

							_lowercase							=     jnp.broadcast_to(
							    jnp.arange(input_ids.shape[-1] - 1)[None, :]    ,			(input_ids.shape[0], input_ids.shape[-1] - 1))
							_lowercase							=     model(
							    input_ids[:, :-1]    ,			attention_mask=lowercase__    ,			past_key_values=lowercase__    ,			position_ids=lowercase__    ,			)

							_lowercase							=     jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,			dtype="""i4""")
							_lowercase							=     model(
							    input_ids[:, -1:]    ,			attention_mask=lowercase__    ,			past_key_values=outputs_cache.past_key_values    ,			position_ids=lowercase__    ,			)

							_lowercase							=     model(lowercase__)

							_lowercase							=     np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
							self.parent.assertTrue(diff < 1e-3    ,			msg=f"""Max diff is {diff}""")


						def 	_UpperCAmelCase							(		self	:						Any    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						Tuple    ,			lowercase__	:						Tuple)						->Union[str, Any]:

							"""simple docstring"""



							_lowercase							=     20
							_lowercase							=     model_class_name(lowercase__)

							_lowercase							=     jnp.concatenate(
							    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]))]    ,			axis=-1    ,			)

							_lowercase							=     model.init_cache(input_ids.shape[0]    ,			lowercase__)
							_lowercase							=     jnp.broadcast_to(
							    jnp.arange(input_ids.shape[-1] - 1)[None, :]    ,			(input_ids.shape[0], input_ids.shape[-1] - 1))

							_lowercase							=     model(
							    input_ids[:, :-1]    ,			attention_mask=lowercase__    ,			past_key_values=lowercase__    ,			position_ids=lowercase__    ,			)
							_lowercase							=     jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,			dtype="""i4""")
							_lowercase							=     model(
							    input_ids[:, -1:]    ,			past_key_values=outputs_cache.past_key_values    ,			attention_mask=lowercase__    ,			position_ids=lowercase__    ,			)

							_lowercase							=     model(lowercase__    ,			attention_mask=lowercase__)
							_lowercase							=     np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
							self.parent.assertTrue(diff < 1e-3    ,			msg=f"""Max diff is {diff}""")






@require_sentencepiece
@require_flax
class 					__a   (		_snake_case							,_snake_case							,unittest.TestCase  ):
						__SCREAMING_SNAKE_CASE     :					Dict  =      (FlaxXGLMModel, FlaxXGLMForCausalLM) if is_flax_available() else ()
						__SCREAMING_SNAKE_CASE     :					Optional[Any]  =      (FlaxXGLMForCausalLM,) if is_flax_available() else ()

						def 	_UpperCAmelCase							(		self	:						List[Any])						->List[Any]:

							"""simple docstring"""



							_lowercase							=     FlaxXGLMModelTester(self)

						def 	_UpperCAmelCase							(		self	:						str)						->List[Any]:

							"""simple docstring"""



							for model_class_name in self.all_model_classes:
								_lowercase		,		_lowercase		,		_lowercase							=     self.model_tester.prepare_config_and_inputs()
								self.model_tester.check_use_cache_forward(lowercase__    ,			lowercase__    ,			lowercase__    ,			lowercase__)

						def 	_UpperCAmelCase							(		self	:						List[str])						->Optional[int]:

							"""simple docstring"""



							for model_class_name in self.all_model_classes:
								_lowercase		,		_lowercase		,		_lowercase							=     self.model_tester.prepare_config_and_inputs()
								self.model_tester.check_use_cache_forward_with_attn_mask(
								    lowercase__    ,			lowercase__    ,			lowercase__    ,			lowercase__)

						@slow
						def 	_UpperCAmelCase							(		self	:						Any)						->Dict:

							"""simple docstring"""



							_lowercase							=     XGLMTokenizer.from_pretrained("""XGLM"""    ,			padding_side="""left""")
							_lowercase							=     tokenizer(["""Hello this is a long string""", """Hey"""]    ,			return_tensors="""np"""    ,			padding=lowercase__    ,			truncation=lowercase__)

							_lowercase							=     FlaxXGLMForCausalLM.from_pretrained("""facebook/xglm-564M""")
							_lowercase							=     1
							_lowercase							=     False

							_lowercase							=     jax.jit(model.generate)

							_lowercase							=     jit_generate(inputs["""input_ids"""]    ,			attention_mask=inputs["""attention_mask"""]).sequences

							_lowercase							=     tokenizer.batch_decode(lowercase__    ,			skip_special_tokens=lowercase__)

							_lowercase							=     [
							    """Hello this is a long string of questions, but I'm not sure if I'm""",
							    """Hey, I'm a newbie to the forum and I'""",
							]

							self.assertListEqual(lowercase__    ,			lowercase__)

						@is_pt_flax_cross_test
						def 	_UpperCAmelCase							(		self	:						Optional[int])						->Union[str, Any]:

							"""simple docstring"""



							_lowercase		,		_lowercase							=     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								with self.subTest(model_class.__name__):
									# prepare inputs
									_lowercase							=     self._prepare_for_class(lowercase__    ,			lowercase__)
									_lowercase							=     {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

									# load corresponding PyTorch class
									_lowercase							=     model_class.__name__[4:]  # Skip the "Flax" at the beginning
									_lowercase							=     getattr(lowercase__    ,			lowercase__)

									_lowercase		,		_lowercase							=     pt_inputs["""input_ids"""].shape
									_lowercase							=     np.random.randint(0    ,			seq_length - 1    ,			size=(batch_size,))
									for batch_idx, start_index in enumerate(lowercase__):
										_lowercase							=     0
										_lowercase							=     1
										_lowercase							=     0
										_lowercase							=     1
									_lowercase							=     pt_model_class(lowercase__).eval()
									# Flax models don't use the `use_cache` option and cache is not returned as a default.
									# So we disable `use_cache` here for PyTorch model.
									_lowercase							=     False
									_lowercase							=     model_class(lowercase__    ,			dtype=jnp.floataa)
									_lowercase							=     convert_pytorch_state_dict_to_flax(pt_model.state_dict()    ,			lowercase__)
									_lowercase							=     fx_state

									with torch.no_grad():
										_lowercase							=     pt_model(**lowercase__).to_tuple()

									_lowercase							=     fx_model(**lowercase__).to_tuple()
									self.assertEqual(len(lowercase__)    ,			len(lowercase__)    ,			"""Output lengths differ between Flax and PyTorch""")
									for fx_output, pt_output in zip(lowercase__    ,			lowercase__):
										self.assert_almost_equals(fx_output[:, -1]    ,			pt_output[:, -1].numpy()    ,			4e-2)

									with tempfile.TemporaryDirectory() as tmpdirname:
										pt_model.save_pretrained(lowercase__)
										_lowercase							=     model_class.from_pretrained(lowercase__    ,			from_pt=lowercase__)

									_lowercase							=     fx_model_loaded(**lowercase__).to_tuple()
									self.assertEqual(
									    len(lowercase__)    ,			len(lowercase__)    ,			"""Output lengths differ between Flax and PyTorch""")
									for fx_output_loaded, pt_output in zip(lowercase__    ,			lowercase__):
										self.assert_almost_equals(fx_output_loaded[:, -1]    ,			pt_output[:, -1].numpy()    ,			4e-2)

						@is_pt_flax_cross_test
						def 	_UpperCAmelCase							(		self	:						Tuple)						->Optional[int]:

							"""simple docstring"""



							_lowercase		,		_lowercase							=     self.model_tester.prepare_config_and_inputs_for_common()
							for model_class in self.all_model_classes:
								with self.subTest(model_class.__name__):
									# prepare inputs
									_lowercase							=     self._prepare_for_class(lowercase__    ,			lowercase__)
									_lowercase							=     {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

									# load corresponding PyTorch class
									_lowercase							=     model_class.__name__[4:]  # Skip the "Flax" at the beginning
									_lowercase							=     getattr(lowercase__    ,			lowercase__)

									_lowercase							=     pt_model_class(lowercase__).eval()
									_lowercase							=     False
									_lowercase							=     model_class(lowercase__    ,			dtype=jnp.floataa)

									_lowercase							=     load_flax_weights_in_pytorch_model(lowercase__    ,			fx_model.params)
									_lowercase		,		_lowercase							=     pt_inputs["""input_ids"""].shape
									_lowercase							=     np.random.randint(0    ,			seq_length - 1    ,			size=(batch_size,))
									for batch_idx, start_index in enumerate(lowercase__):
										_lowercase							=     0
										_lowercase							=     1
										_lowercase							=     0
										_lowercase							=     1

									# make sure weights are tied in PyTorch
									pt_model.tie_weights()

									with torch.no_grad():
										_lowercase							=     pt_model(**lowercase__).to_tuple()

									_lowercase							=     fx_model(**lowercase__).to_tuple()
									self.assertEqual(len(lowercase__)    ,			len(lowercase__)    ,			"""Output lengths differ between Flax and PyTorch""")
									for fx_output, pt_output in zip(lowercase__    ,			lowercase__):
										self.assert_almost_equals(fx_output[:, -1]    ,			pt_output[:, -1].numpy()    ,			4e-2)

									with tempfile.TemporaryDirectory() as tmpdirname:
										fx_model.save_pretrained(lowercase__)
										_lowercase							=     pt_model_class.from_pretrained(lowercase__    ,			from_flax=lowercase__)

									with torch.no_grad():
										_lowercase							=     pt_model_loaded(**lowercase__).to_tuple()

									self.assertEqual(
									    len(lowercase__)    ,			len(lowercase__)    ,			"""Output lengths differ between Flax and PyTorch""")
									for fx_output, pt_output in zip(lowercase__    ,			lowercase__):
										self.assert_almost_equals(fx_output[:, -1]    ,			pt_output[:, -1].numpy()    ,			4e-2)


						@slow
						def 	_UpperCAmelCase							(		self	:						Tuple)						->List[str]:

							"""simple docstring"""



							for model_class_name in self.all_model_classes:
								_lowercase							=     model_class_name.from_pretrained("""facebook/xglm-564M""")
								_lowercase							=     model(np.ones((1, 1)))
								self.assertIsNotNone(lowercase__)







'''simple docstring'''







import argparse
import copy
import json
import multiprocessing
import os
import time

from create_dummy_models import COMPOSITE_MODELS, create_tiny_models
from huggingface_hub import ModelFilter, hf_api

import transformers
from transformers import AutoFeatureExtractor, AutoImageProcessor, AutoTokenizer
from transformers.image_processing_utils import BaseImageProcessor






def 					_SCREAMING_SNAKE_CASE		(						):
	_lowercase							=     set()
	# Each auto modeling files contains multiple mappings. Let's get them in a dynamic way.
	for module_name in ["modeling_auto", "modeling_tf_auto", "modeling_flax_auto"]:
		_lowercase							=     getattr(transformers.models.auto							,       snake_case_							,       snake_case_	)
		if module is None:
			continue
		# all mappings in a single auto modeling file
		_lowercase							=     [
		    x
		    for x in dir(snake_case_	)
		    if x.endswith("""_MAPPING_NAMES"""	)
		    and (x.startswith("""MODEL_"""	) or x.startswith("""TF_MODEL_"""	) or x.startswith("""FLAX_MODEL_"""	))
		]
		for name in mapping_names:
			_lowercase							=     getattr(snake_case_							,       snake_case_	)
			if mapping is not None:
				for v in mapping.values():
					if isinstance(snake_case_							,       (list, tuple)	):
						model_names.update(snake_case_	)
					elif isinstance(snake_case_							,       snake_case_	):
						model_names.add(snake_case_	)

	return sorted(snake_case_	)






def 					_SCREAMING_SNAKE_CASE		(						):
	# All model names defined in auto mappings
	_lowercase							=     set(get_all_model_names()	)

	with open("""tests/utils/tiny_model_summary.json"""	) as fp:
		_lowercase							=     json.load(snake_case_	)
	_lowercase							=     set()
	for model_base_name in tiny_model_info:
		tiny_models_names.update(tiny_model_info[model_base_name]["""model_classes"""]	)

	# Remove a tiny model name if one of its framework implementation hasn't yet a tiny version on the Hub.
	_lowercase							=     model_names.difference(snake_case_	)
	for model_name in copy.copy(snake_case_	):
		if not model_name.startswith("""TF"""	) and F"""TF{model_name}""" in not_on_hub:
			tiny_models_names.remove(snake_case_	)
		elif model_name.startswith("""TF"""	) and model_name[2:] in not_on_hub:
			tiny_models_names.remove(snake_case_	)

	return sorted(snake_case_	)






def 					_SCREAMING_SNAKE_CASE		(						snake_case_	):
	_lowercase							=     COMPOSITE_MODELS.values()

	# All tiny model base names on Hub
	_lowercase							=     get_all_model_names()
	_lowercase							=     hf_api.list_models(
	    filter=ModelFilter(
	        author="""hf-internal-testing"""							,       )	)
	_lowercase							=     set()
	for x in models:
		_lowercase							=     x.modelId
		_lowercase		,		_lowercase							=     model.split("""/"""	)
		if not model.startswith("""tiny-random-"""	):
			continue
		_lowercase							=     model.replace("""tiny-random-"""							,       """"""	)
		if not model[0].isupper():
			continue
		if model not in model_names and model not in special_models:
			continue
		_models.add(snake_case_	)

	_lowercase							=     sorted(_models	)
	# All tiny model names on Hub
	_lowercase							=     {}
	for model in models:
		_lowercase							=     F"""hf-internal-testing/tiny-random-{model}"""
		_lowercase							=     model.split("""-"""	)[0]
		try:
			_lowercase							=     hf_api.repo_info(snake_case_	)
			_lowercase							=     {
			    """tokenizer_classes""": set(),
			    """processor_classes""": set(),
			    """model_classes""": set(),
			    """sha""": repo_info.sha,
			}
		except Exception:
			continue
		try:
			time.sleep(1	)
			_lowercase							=     AutoTokenizer.from_pretrained(snake_case_	)
			content["tokenizer_classes"].add(tokenizer_fast.__class__.__name__	)
		except Exception:
			pass
		try:
			time.sleep(1	)
			_lowercase							=     AutoTokenizer.from_pretrained(snake_case_							,       use_fast=snake_case_	)
			content["tokenizer_classes"].add(tokenizer_slow.__class__.__name__	)
		except Exception:
			pass
		try:
			time.sleep(1	)
			_lowercase							=     AutoImageProcessor.from_pretrained(snake_case_	)
			content["processor_classes"].add(img_p.__class__.__name__	)
		except Exception:
			pass
		try:
			time.sleep(1	)
			_lowercase							=     AutoFeatureExtractor.from_pretrained(snake_case_	)
			if not isinstance(snake_case_							,       snake_case_	):
				content["processor_classes"].add(feat_p.__class__.__name__	)
		except Exception:
			pass
		try:
			time.sleep(1	)
			_lowercase							=     getattr(snake_case_							,       snake_case_	)
			_lowercase							=     model_class.from_pretrained(snake_case_	)
			content["model_classes"].add(m.__class__.__name__	)
		except Exception:
			pass
		try:
			time.sleep(1	)
			_lowercase							=     getattr(snake_case_							,       F"""TF{model}"""	)
			_lowercase							=     model_class.from_pretrained(snake_case_	)
			content["model_classes"].add(m.__class__.__name__	)
		except Exception:
			pass

		_lowercase							=     sorted(content["""tokenizer_classes"""]	)
		_lowercase							=     sorted(content["""processor_classes"""]	)
		_lowercase							=     sorted(content["""model_classes"""]	)

		_lowercase							=     content
		with open(os.path.join(snake_case_							,       """hub_tiny_model_summary.json"""	)							,       """w"""	) as fp:
			json.dump(snake_case_							,       snake_case_							,       ensure_ascii=snake_case_							,       indent=4	)


if __name__ == "__main__":
							_lowerCamelCase    =  argparse.ArgumentParser()
							parser.add_argument('--num_workers', default=1, type=int, help='The number of workers to run.')
							_lowerCamelCase    =  parser.parse_args()

							# This has to be `spawn` to avoid hanging forever!
							multiprocessing.set_start_method('spawn')

							_lowerCamelCase    =  'tiny_models'
							_lowerCamelCase    =  True
							_lowerCamelCase    =  None
							_lowerCamelCase    =  get_tiny_model_names_from_repo()
							_lowerCamelCase    =  True
							_lowerCamelCase    =  True
							_lowerCamelCase    =  'hf-internal-testing'

							create_tiny_models(
							    output_path,
							    all,
							    model_types,
							    models_to_skip,
							    no_check,
							    upload,
							    organization,
							    token=os.environ.get('TOKEN', None),
							    num_workers=args.num_workers,
							)




