




'''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      lowerCAmelCase (			unittest.TestCase):
       __lowercase	:   str										=      MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
       __lowercase	:   Union[str, Any]										=      TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	Optional[Any]:



            '''simple docstring'''




            __snake_case            =	TextaTextGenerationPipeline(model=__SCREAMING_SNAKE_CASE     ,	tokenizer=__SCREAMING_SNAKE_CASE	)
            return generator, ["Something to write", "Something else"]


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




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

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

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

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


       @require_torch
       def    lowerCAmelCase   ( self	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	pipeline('''text2text-generation'''     ,	model='''patrickvonplaten/t5-tiny-random'''     ,	framework='''pt'''	)
            # do_sample=False necessary for reproducibility
            __snake_case            =	generator('''Something there'''     ,	do_sample=__SCREAMING_SNAKE_CASE	)
            self.assertEqual(__SCREAMING_SNAKE_CASE     ,	[{'''generated_text''': ''''''}]	)

            __snake_case            =	3
            __snake_case            =	generator(
                '''Something there'''     ,	num_return_sequences=__SCREAMING_SNAKE_CASE     ,	num_beams=__SCREAMING_SNAKE_CASE     ,	)
            __snake_case            =	[
                {'''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(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)

            __snake_case            =	generator('''This is a test'''     ,	do_sample=__SCREAMING_SNAKE_CASE     ,	num_return_sequences=2     ,	return_tensors=__SCREAMING_SNAKE_CASE	)
            self.assertEqual(
                __SCREAMING_SNAKE_CASE     ,	[
                    {'''generated_token_ids''': ANY(torch.Tensor	)},
                    {'''generated_token_ids''': ANY(torch.Tensor	)},
                ]     ,	)
            __snake_case            =	generator.model.config.eos_token_id
            __snake_case            =	'''<pad>'''
            __snake_case            =	generator(
                ['''This is a test''', '''This is a second test''']     ,	do_sample=__SCREAMING_SNAKE_CASE     ,	num_return_sequences=2     ,	batch_size=2     ,	return_tensors=__SCREAMING_SNAKE_CASE     ,	)
            self.assertEqual(
                __SCREAMING_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	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	pipeline('''text2text-generation'''     ,	model='''patrickvonplaten/t5-tiny-random'''     ,	framework='''tf'''	)
            # do_sample=False necessary for reproducibility
            __snake_case            =	generator('''Something there'''     ,	do_sample=__SCREAMING_SNAKE_CASE	)
            self.assertEqual(__SCREAMING_SNAKE_CASE     ,	[{'''generated_text''': ''''''}]	)








import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class 							lowerCAmelCase__   ( unittest.TestCase						):

						@property
						def 			A_					(      self					)       ->   int:


								'''simple docstring'''

								torch.manual_seed(0					)
								_UpperCamelCase         =						UNetaDModel(
								    block_out_channels=(32, 64)							,				layers_per_block=2							,				sample_size=32							,				in_channels=3							,				out_channels=3							,				down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")							,				up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")							,				)
								return model







						def 			A_					(      self					)       ->   Any:


								'''simple docstring'''

								_UpperCamelCase         =						self.dummy_uncond_unet
								_UpperCamelCase         =						PNDMScheduler()

								_UpperCamelCase         =						PNDMPipeline(unet=a							,				scheduler=a					)
								pndm.to(a					)
								pndm.set_progress_bar_config(disable=a					)

								_UpperCamelCase         =						torch.manual_seed(0					)
								_UpperCamelCase         =						pndm(generator=a							,				num_inference_steps=20							,				output_type="""numpy"""					).images

								_UpperCamelCase         =						torch.manual_seed(0					)
								_UpperCamelCase         =						pndm(generator=a							,				num_inference_steps=20							,				output_type="""numpy"""							,				return_dict=a					)[0]

								_UpperCamelCase         =						image[0, -3:, -3:, -1]
								_UpperCamelCase         =						image_from_tuple[0, -3:, -3:, -1]

								assert image.shape == (1, 32, 32, 3)
								_UpperCamelCase         =						np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]					)

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







@slow
@require_torch
class 							lowerCAmelCase__   ( unittest.TestCase						):

						def 			A_					(      self					)       ->   Optional[Any]:


								'''simple docstring'''

								_UpperCamelCase         =						"""google/ddpm-cifar10-32"""

								_UpperCamelCase         =						UNetaDModel.from_pretrained(a					)
								_UpperCamelCase         =						PNDMScheduler()

								_UpperCamelCase         =						PNDMPipeline(unet=a							,				scheduler=a					)
								pndm.to(a					)
								pndm.set_progress_bar_config(disable=a					)
								_UpperCamelCase         =						torch.manual_seed(0					)
								_UpperCamelCase         =						pndm(generator=a							,				output_type="""numpy"""					).images

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

								assert image.shape == (1, 32, 32, 3)
								_UpperCamelCase         =						np.array([0.1564, 0.1_4645, 0.1406, 0.1_4715, 0.1_2425, 0.1_4045, 0.1_3115, 0.1_2175, 0.125]					)

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



