



"""simple docstring"""
import flax.linen as nn
import jax
import jax.numpy as jnp



class       SCREAMING_SNAKE_CASE__  (					nn.Module ):
   __lowerCAmelCase  :   int
   __lowerCAmelCase  :   jnp.dtype						=						jnp.floataa

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	:					List[str]	=	nn.Conv(
        self.out_channels      ,     kernel_size=(3, 3)      ,     strides=(1, 1)      ,     padding=((1, 1), (1, 1))      ,     dtype=self.dtype      ,     )




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE						)  ->      Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					Dict	=	hidden_states.shape
    UpperCAmelCase	:					str	=	jax.image.resize(
        _SCREAMING_SNAKE_CASE      ,     shape=(batch, height * 2, width * 2, channels)      ,     method="""nearest"""      ,     )
    UpperCAmelCase	:					Optional[int]	=	self.conv(_SCREAMING_SNAKE_CASE						)
    return hidden_states



class       SCREAMING_SNAKE_CASE__  (					nn.Module ):
   __lowerCAmelCase  :   int
   __lowerCAmelCase  :   jnp.dtype						=						jnp.floataa

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	nn.Conv(
        self.out_channels      ,     kernel_size=(3, 3)      ,     strides=(2, 2)      ,     padding=((1, 1), (1, 1))      ,     dtype=self.dtype      ,     )




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					Any	=	self.conv(_SCREAMING_SNAKE_CASE						)
    return hidden_states



class       SCREAMING_SNAKE_CASE__  (					nn.Module ):
   __lowerCAmelCase  :   int
   __lowerCAmelCase  :   int						=						None
   __lowerCAmelCase  :   float						=						0.0
   __lowerCAmelCase  :   bool						=						None
   __lowerCAmelCase  :   jnp.dtype						=						jnp.floataa

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[Any]	=	self.in_channels if self.out_channels is None else self.out_channels

    UpperCAmelCase	:					Any	=	nn.GroupNorm(num_groups=32      ,     epsilon=1E-5						)
    UpperCAmelCase	:					Optional[Any]	=	nn.Conv(
        _SCREAMING_SNAKE_CASE      ,     kernel_size=(3, 3)      ,     strides=(1, 1)      ,     padding=((1, 1), (1, 1))      ,     dtype=self.dtype      ,     )

    UpperCAmelCase	:					str	=	nn.Dense(_SCREAMING_SNAKE_CASE      ,     dtype=self.dtype						)

    UpperCAmelCase	:					str	=	nn.GroupNorm(num_groups=32      ,     epsilon=1E-5						)
    UpperCAmelCase	:					List[str]	=	nn.Dropout(self.dropout_prob						)
    UpperCAmelCase	:					str	=	nn.Conv(
        _SCREAMING_SNAKE_CASE      ,     kernel_size=(3, 3)      ,     strides=(1, 1)      ,     padding=((1, 1), (1, 1))      ,     dtype=self.dtype      ,     )

    UpperCAmelCase	:					Tuple	=	self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

    UpperCAmelCase	:					Dict	=	None
    if use_nin_shortcut:
     UpperCAmelCase	:					str	=	nn.Conv(
         _SCREAMING_SNAKE_CASE      ,     kernel_size=(1, 1)      ,     strides=(1, 1)      ,     padding="""VALID"""      ,     dtype=self.dtype      ,     )




   def __call__(   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=True						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Tuple	=	hidden_states
    UpperCAmelCase	:					Dict	=	self.norma(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Tuple	=	nn.swish(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Optional[int]	=	self.conva(_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					str	=	self.time_emb_proj(nn.swish(_SCREAMING_SNAKE_CASE						)						)
    UpperCAmelCase	:					Optional[int]	=	jnp.expand_dims(jnp.expand_dims(_SCREAMING_SNAKE_CASE      ,     1						)      ,     1						)
    UpperCAmelCase	:					Union[str, Any]	=	hidden_states + temb

    UpperCAmelCase	:					Any	=	self.norma(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Union[str, Any]	=	nn.swish(_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					int	=	self.dropout(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Tuple	=	self.conva(_SCREAMING_SNAKE_CASE						)

    if self.conv_shortcut is not None:
     UpperCAmelCase	:					Dict	=	self.conv_shortcut(_SCREAMING_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   _UpperCamelCase  (   unittest.TestCase		):
							'''simple docstring'''







							__UpperCAmelCase						:    Union[str, Any]				      =MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							__UpperCAmelCase						:    Union[str, Any]				      =TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

							def 			snake_case				(     self    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			TextaTextGenerationPipeline(model=__a    ,   tokenizer=__a	)
										return generator, ["Something to write", "Something else"]

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

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

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

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

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

										__lowerCAmelCase				    =			3
										__lowerCAmelCase				    =			generator(
										    "Something there"    ,   num_return_sequences=__a    ,   num_beams=__a    ,   )
										__lowerCAmelCase				    =			[
										    {"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(__a    ,   __a	)

										__lowerCAmelCase				    =			generator("This is a test"    ,   do_sample=__a    ,   num_return_sequences=2    ,   return_tensors=__a	)
										self.assertEqual(
										    __a    ,   [
										        {"generated_token_ids": ANY(torch.Tensor	)},
										        {"generated_token_ids": ANY(torch.Tensor	)},
										    ]    ,   )
										__lowerCAmelCase				    =			generator.model.config.eos_token_id
										__lowerCAmelCase				    =			"<pad>"
										__lowerCAmelCase				    =			generator(
										    ["This is a test", "This is a second test"]    ,   do_sample=__a    ,   num_return_sequences=2    ,   batch_size=2    ,   return_tensors=__a    ,   )
										self.assertEqual(
										    __a    ,   [
										        [
										            {"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 			snake_case				(     self	):
										__lowerCAmelCase				    =			pipeline("text2text-generation"    ,   model="patrickvonplaten/t5-tiny-random"    ,   framework="tf"	)
										# do_sample=False necessary for reproducibility
										__lowerCAmelCase				    =			generator("Something there"    ,   do_sample=__a	)
										self.assertEqual(__a    ,   [{"generated_text": ""}]	)




