



"""simple docstring"""





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




class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					42
							_UpperCAmelCase      				=					jnp.floataa






							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase							 =		nn.Conv(
									    self.out_channels	,       kernel_size=(3, 3)	,       strides=(1, 1)	,       padding=((1, 1), (1, 1))	,       dtype=self.dtype	,       )






							def __call__(  self	,       snake_case						)			->							Optional[Any]:
									_UpperCAmelCase ,					_UpperCAmelCase ,					_UpperCAmelCase ,					_UpperCAmelCase							 =		hidden_states.shape
									_UpperCAmelCase							 =		jax.image.resize(
									    snake_case	,       shape=(batch, height * 2, width * 2, channels)	,       method='nearest'	,       )
									_UpperCAmelCase							 =		self.conv(snake_case						)
									return hidden_states







class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					42
							_UpperCAmelCase      				=					jnp.floataa






							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									_UpperCAmelCase							 =		nn.Conv(
									    self.out_channels	,       kernel_size=(3, 3)	,       strides=(2, 2)	,       padding=((1, 1), (1, 1))	,       dtype=self.dtype	,       )






							def __call__(  self	,       snake_case						)			->							Tuple:
									# pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
									# hidden_states = jnp.pad(hidden_states, pad_width=pad)
									_UpperCAmelCase							 =		self.conv(snake_case						)
									return hidden_states







class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					42
							_UpperCAmelCase      				=					None
							_UpperCAmelCase      				=					0.0
							_UpperCAmelCase      				=					None
							_UpperCAmelCase      				=					jnp.floataa






							def        lowerCamelCase_   (  self						)			->							List[Any]:
									_UpperCAmelCase							 =		self.in_channels if self.out_channels is None else self.out_channels

									_UpperCAmelCase							 =		nn.GroupNorm(num_groups=32	,       epsilon=1E-5						)
									_UpperCAmelCase							 =		nn.Conv(
									    snake_case	,       kernel_size=(3, 3)	,       strides=(1, 1)	,       padding=((1, 1), (1, 1))	,       dtype=self.dtype	,       )

									_UpperCAmelCase							 =		nn.Dense(snake_case	,       dtype=self.dtype						)

									_UpperCAmelCase							 =		nn.GroupNorm(num_groups=32	,       epsilon=1E-5						)
									_UpperCAmelCase							 =		nn.Dropout(self.dropout_prob						)
									_UpperCAmelCase							 =		nn.Conv(
									    snake_case	,       kernel_size=(3, 3)	,       strides=(1, 1)	,       padding=((1, 1), (1, 1))	,       dtype=self.dtype	,       )

									_UpperCAmelCase							 =		self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

									_UpperCAmelCase							 =		None
									if use_nin_shortcut:
											_UpperCAmelCase							 =		nn.Conv(
											    snake_case	,       kernel_size=(1, 1)	,       strides=(1, 1)	,       padding='VALID'	,       dtype=self.dtype	,       )






							def __call__(  self	,       snake_case	,       snake_case	,       snake_case=True						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		hidden_states
									_UpperCAmelCase							 =		self.norma(snake_case						)
									_UpperCAmelCase							 =		nn.swish(snake_case						)
									_UpperCAmelCase							 =		self.conva(snake_case						)

									_UpperCAmelCase							 =		self.time_emb_proj(nn.swish(snake_case						)						)
									_UpperCAmelCase							 =		jnp.expand_dims(jnp.expand_dims(snake_case	,       1						)	,       1						)
									_UpperCAmelCase							 =		hidden_states + temb

									_UpperCAmelCase							 =		self.norma(snake_case						)
									_UpperCAmelCase							 =		nn.swish(snake_case						)
									_UpperCAmelCase							 =		self.dropout(snake_case	,       snake_case						)
									_UpperCAmelCase							 =		self.conva(snake_case						)

									if self.conv_shortcut is not None:
											_UpperCAmelCase							 =		self.conv_shortcut(snake_case						)

									return hidden_states + residual







"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
					import torch




@is_pipeline_test
class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							_UpperCAmelCase      				=					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		TextaTextGenerationPipeline(model=snake_case	,       tokenizer=snake_case						)
									return generator, ["Something to write", "Something else"]






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		generator('Something there'						)
									self.assertEqual(snake_case	,       [{'generated_text': ANY(snake_case						)}]						)
									# These are encoder decoder, they don't just append to incoming string
									self.assertFalse(outputs[0]['generated_text'].startswith('Something there'						)						)

									_UpperCAmelCase							 =		generator(['This is great !', 'Something else']	,       num_return_sequences=2	,       do_sample=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									    ]	,       )

									_UpperCAmelCase							 =		generator(
									    ['This is great !', 'Something else']	,       num_return_sequences=2	,       batch_size=2	,       do_sample=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									    ]	,       )

									with self.assertRaises(snake_case						):
											generator(4						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		pipeline('text2text-generation'	,       model='patrickvonplaten/t5-tiny-random'	,       framework='pt'						)
									# do_sample=False necessary for reproducibility
									_UpperCAmelCase							 =		generator('Something there'	,       do_sample=snake_case						)
									self.assertEqual(snake_case	,       [{'generated_text': ''}]						)

									_UpperCAmelCase							 =		3
									_UpperCAmelCase							 =		generator(
									    'Something there'	,       num_return_sequences=snake_case	,       num_beams=snake_case	,       )
									_UpperCAmelCase							 =		[
									    {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'},
									    {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'},
									    {'generated_text': ''},
									]
									self.assertEqual(snake_case	,       snake_case						)

									_UpperCAmelCase							 =		generator('This is a test'	,       do_sample=snake_case	,       num_return_sequences=2	,       return_tensors=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        {'generated_token_ids': ANY(torch.Tensor						)},
									        {'generated_token_ids': ANY(torch.Tensor						)},
									    ]	,       )
									_UpperCAmelCase							 =		generator.model.config.eos_token_id
									_UpperCAmelCase							 =		'<pad>'
									_UpperCAmelCase							 =		generator(
									    ['This is a test', 'This is a second test']	,       do_sample=snake_case	,       num_return_sequences=2	,       batch_size=2	,       return_tensors=snake_case	,       )
									self.assertEqual(
									    snake_case	,       [
									        [
									            {'generated_token_ids': ANY(torch.Tensor						)},
									            {'generated_token_ids': ANY(torch.Tensor						)},
									        ],
									        [
									            {'generated_token_ids': ANY(torch.Tensor						)},
									            {'generated_token_ids': ANY(torch.Tensor						)},
									        ],
									    ]	,       )






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Any:
									_UpperCAmelCase							 =		pipeline('text2text-generation'	,       model='patrickvonplaten/t5-tiny-random'	,       framework='tf'						)
									# do_sample=False necessary for reproducibility
									_UpperCAmelCase							 =		generator('Something there'	,       do_sample=snake_case						)
									self.assertEqual(snake_case	,       [{'generated_text': ''}]						)


