


'''simple docstring'''



import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test






class 	UpperCamelCase__(       unittest.TestCase	):




				@parameterized.expand([(None,), ('''foo.json''',)]				)
				def 					a__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			Tuple				)->					List[str]:

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   temperature=0.7				,   length_penalty=1.0				,   bad_words_ids=[[1, 2, 3], [4, 5]]				,   )
										with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(lowerCAmelCase				,   config_name=lowerCAmelCase				)
																UpperCAmelCase					    =     GenerationConfig.from_pretrained(lowerCAmelCase				,   config_name=lowerCAmelCase				)

										# Checks parameters that were specified
										self.assertEqual(loaded_config.do_sample				,   lowerCAmelCase				)
										self.assertEqual(loaded_config.temperature				,   0.7				)
										self.assertEqual(loaded_config.length_penalty				,   1.0				)
										self.assertEqual(loaded_config.bad_words_ids				,   [[1, 2, 3], [4, 5]]				)

										# Checks parameters that were not specified (defaults)
										self.assertEqual(loaded_config.top_k				,   50				)
										self.assertEqual(loaded_config.max_length				,   20				)
										self.assertEqual(loaded_config.max_time				,   lowerCAmelCase				)




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

										"""simple docstring"""




										UpperCAmelCase					    =     AutoConfig.from_pretrained('''gpt2'''				)
										UpperCAmelCase					    =     GenerationConfig.from_model_config(lowerCAmelCase				)
										UpperCAmelCase					    =     GenerationConfig()

										# The generation config has loaded a few non-default parameters from the model config
										self.assertNotEqual(lowerCAmelCase				,   lowerCAmelCase				)

										# One of those parameters is eos_token_id -- check if it matches
										self.assertNotEqual(generation_config_from_model.eos_token_id				,   default_generation_config.eos_token_id				)
										self.assertEqual(generation_config_from_model.eos_token_id				,   model_config.eos_token_id				)




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

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig()
										UpperCAmelCase					    =     {
										    '''max_new_tokens''': 1024,
										    '''foo''': '''bar''',
										}
										UpperCAmelCase					    =     copy.deepcopy(lowerCAmelCase				)
										UpperCAmelCase					    =     generation_config.update(**lowerCAmelCase				)

										# update_kwargs was not modified (no side effects)
										self.assertEqual(lowerCAmelCase				,   lowerCAmelCase				)

										# update_kwargs was used to update the config on valid attributes
										self.assertEqual(generation_config.max_new_tokens				,   1024				)

										# `.update()` returns a dictionary of unused kwargs
										self.assertEqual(lowerCAmelCase				,   {'''foo''': '''bar'''}				)




				def 					a__(		self      :			int				)->					Any:

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig()
										UpperCAmelCase					    =     '''bar'''

										with tempfile.TemporaryDirectory('''test-generation-config'''				) as tmp_dir:
																generation_config.save_pretrained(lowerCAmelCase				)

																UpperCAmelCase					    =     GenerationConfig.from_pretrained(lowerCAmelCase				)
										# update_kwargs was used to update the config on valid attributes
										self.assertEqual(new_config.foo				,   '''bar'''				)

										UpperCAmelCase					    =     GenerationConfig.from_model_config(lowerCAmelCase				)
										assert not hasattr(lowerCAmelCase				,   '''foo'''				)  # no new kwargs should be initialized if from config





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

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig()
										self.assertEqual(default_config.temperature				,   1.0				)
										self.assertEqual(default_config.do_sample				,   lowerCAmelCase				)
										self.assertEqual(default_config.num_beams				,   1				)

										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   temperature=0.7				,   length_penalty=1.0				,   bad_words_ids=[[1, 2, 3], [4, 5]]				,   )
										self.assertEqual(config.temperature				,   0.7				)
										self.assertEqual(config.do_sample				,   lowerCAmelCase				)
										self.assertEqual(config.num_beams				,   1				)

										with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(lowerCAmelCase				)
																UpperCAmelCase					    =     GenerationConfig.from_pretrained(lowerCAmelCase				,   temperature=1.0				)

										self.assertEqual(loaded_config.temperature				,   1.0				)
										self.assertEqual(loaded_config.do_sample				,   lowerCAmelCase				)
										self.assertEqual(loaded_config.num_beams				,   1				)  # default value







@is_staging_test
class 	UpperCamelCase__(       unittest.TestCase	):




				@classmethod
				def 					a__(		cls      :			List[str]				)->					List[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     TOKEN
										HfFolder.save_token(lowerCAmelCase				)




				@classmethod
				def 					a__(		cls      :			int				)->					int:

										"""simple docstring"""




										try:
																delete_repo(token=cls._token				,   repo_id='''test-generation-config'''				)
										except HTTPError:
																pass

										try:
																delete_repo(token=cls._token				,   repo_id='''valid_org/test-generation-config-org'''				)
										except HTTPError:
																pass




				def 					a__(		self      :			Tuple				)->					int:

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   temperature=0.7				,   length_penalty=1.0				,   )
										config.push_to_hub('''test-generation-config'''				,   use_auth_token=self._token				)

										UpperCAmelCase					    =     GenerationConfig.from_pretrained(F"""{USER}/test-generation-config"""				)
										for k, v in config.to_dict().items():
																if k != "transformers_version":
																						self.assertEqual(lowerCAmelCase				,   getattr(lowerCAmelCase				,   lowerCAmelCase				)				)

        # Reset repo
										delete_repo(token=self._token				,   repo_id='''test-generation-config'''				)

										# Push to hub via save_pretrained
										with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(
																    lowerCAmelCase				,   repo_id='''test-generation-config'''				,   push_to_hub=lowerCAmelCase				,   use_auth_token=self._token				)

										UpperCAmelCase					    =     GenerationConfig.from_pretrained(F"""{USER}/test-generation-config"""				)
										for k, v in config.to_dict().items():
																if k != "transformers_version":
																						self.assertEqual(lowerCAmelCase				,   getattr(lowerCAmelCase				,   lowerCAmelCase				)				)





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

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   temperature=0.7				,   length_penalty=1.0				,   )
										config.push_to_hub('''valid_org/test-generation-config-org'''				,   use_auth_token=self._token				)

										UpperCAmelCase					    =     GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''				)
										for k, v in config.to_dict().items():
																if k != "transformers_version":
																						self.assertEqual(lowerCAmelCase				,   getattr(lowerCAmelCase				,   lowerCAmelCase				)				)

        # Reset repo
										delete_repo(token=self._token				,   repo_id='''valid_org/test-generation-config-org'''				)

										# Push to hub via save_pretrained
										with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(
																    lowerCAmelCase				,   repo_id='''valid_org/test-generation-config-org'''				,   push_to_hub=lowerCAmelCase				,   use_auth_token=self._token				)

										UpperCAmelCase					    =     GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''				)
										for k, v in config.to_dict().items():
																if k != "transformers_version":
																						self.assertEqual(lowerCAmelCase				,   getattr(lowerCAmelCase				,   lowerCAmelCase				)				)









'''simple docstring'''



import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD






class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			str				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[Any]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			int				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet, attn in zip(self.resnets				,   self.attentions				):
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




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

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)
										UpperCAmelCase					    =     resnets

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[str]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet in self.resnets:
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			Optional[int]				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			Union[str, Any]=True				)->					Optional[int]:

										"""simple docstring"""




										for resnet, attn in zip(self.resnets				,   self.attentions				):
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




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

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Union[str, Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict=True				)->					Tuple:

										"""simple docstring"""




										for resnet in self.resnets:
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			int				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     [
										    FlaxResnetBlockaD(
										        in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
										]

										UpperCAmelCase					    =     []

										for _ in range(self.num_layers				):
																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.in_channels				,   n_heads=self.num_attention_heads				,   d_head=self.in_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions





				def __call__(		self      :			Dict				,   lowerCAmelCase      :			Optional[Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Any=True				)->					Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     self.resnets[0](lowerCAmelCase				,   lowerCAmelCase				)
										for attn, resnet in zip(self.attentions				,   self.resnets[1:]				):
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										return hidden_states





