




import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







@skip_mps
class __snake_case			(			a ,  unittest.TestCase		):
   UpperCAmelCase__  :    int          =  TextToVideoSDPipeline
   UpperCAmelCase__  :    List[Any]          =  TEXT_TO_IMAGE_PARAMS
   UpperCAmelCase__  :    int          =  TEXT_TO_IMAGE_BATCH_PARAMS
   # No `output_type`.
   UpperCAmelCase__  :    Tuple          =  frozenset(
       [
           '''num_inference_steps''',
           '''generator''',
           '''latents''',
           '''return_dict''',
           '''callback''',
           '''callback_steps''',
       ]		)







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        torch.manual_seed(0)
        UpperCAmelCase_          =							UNetaDConditionModel(
            block_out_channels=(32, 64, 64, 64)					,						layers_per_block=2					,						sample_size=32					,						in_channels=4					,						out_channels=4					,						down_block_types=('''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''DownBlock3D''')					,						up_block_types=('''UpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''')					,						cross_attention_dim=32					,						attention_head_dim=4					,						)
        UpperCAmelCase_          =							DDIMScheduler(
            beta_start=0.0_0_0_8_5					,						beta_end=0.0_1_2					,						beta_schedule='''scaled_linear'''					,						clip_sample=_snake_case					,						set_alpha_to_one=_snake_case					,						)
        torch.manual_seed(0)
        UpperCAmelCase_          =							AutoencoderKL(
            block_out_channels=[32, 64]					,						in_channels=3					,						out_channels=3					,						down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']					,						up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']					,						latent_channels=4					,						sample_size=128					,						)
        torch.manual_seed(0)
        UpperCAmelCase_          =							CLIPTextConfig(
            bos_token_id=0					,						eos_token_id=2					,						hidden_size=32					,						intermediate_size=37					,						layer_norm_eps=1e-05					,						num_attention_heads=4					,						num_hidden_layers=5					,						pad_token_id=1					,						vocab_size=1000					,						hidden_act='''gelu'''					,						projection_dim=512					,						)
        UpperCAmelCase_          =							CLIPTextModel(_snake_case)
        UpperCAmelCase_          =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

        UpperCAmelCase_          =							{
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''text_encoder''': text_encoder,
            '''tokenizer''': tokenizer,
        }
        return components







   def lowerCamelCase						(      self		:						Union[str, Any]					,						_snake_case		:						Any					,						_snake_case		:						List[str]=0):

        """simple docstring"""


        if str(_snake_case).startswith('''mps'''):
             UpperCAmelCase_          =							torch.manual_seed(_snake_case)
        else:
             UpperCAmelCase_          =							torch.Generator(device=_snake_case).manual_seed(_snake_case)
        UpperCAmelCase_          =							{
            '''prompt''': '''A painting of a squirrel eating a burger''',
            '''generator''': generator,
            '''num_inference_steps''': 2,
            '''guidance_scale''': 6.0,
            '''output_type''': '''pt''',
        }
        return inputs







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        UpperCAmelCase_          =							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        UpperCAmelCase_          =							self.get_dummy_components()
        UpperCAmelCase_          =							TextToVideoSDPipeline(**_snake_case)
        UpperCAmelCase_          =							sd_pipe.to(_snake_case)
        sd_pipe.set_progress_bar_config(disable=_snake_case)

        UpperCAmelCase_          =							self.get_dummy_inputs(_snake_case)
        UpperCAmelCase_          =							'''np'''
        UpperCAmelCase_          =							sd_pipe(**_snake_case).frames
        UpperCAmelCase_          =							frames[0][-3:, -3:, -1]

        assert frames[0].shape == (64, 64, 3)
        UpperCAmelCase_          =							np.array([1_5_8.0, 1_6_0.0, 1_5_3.0, 1_2_5.0, 1_0_0.0, 1_2_1.0, 1_1_1.0, 9_3.0, 1_1_3.0])

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







   def lowerCamelCase						(      self		:						Any):

        """simple docstring"""


        self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case					,						expected_max_diff=3e-3)







   @unittest.skipIf(
       torch_device != '''cuda''' or not is_xformers_available()					,						reason='''XFormers attention is only available with CUDA and `xformers` installed'''					,						)
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case					,						expected_max_diff=1e-2)







   @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''')
   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        pass







   @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''')
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        pass







   @unittest.skip(reason='''`num_images_per_prompt` argument is not supported for this pipeline.''')
   def lowerCamelCase						(      self		:						Dict):

        """simple docstring"""


        pass







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        return super().test_progress_bar()







@slow
@skip_mps
class __snake_case			(			unittest.TestCase		):







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy''')

        UpperCAmelCase_          =							TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''')
        UpperCAmelCase_          =							DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
        UpperCAmelCase_          =							pipe.to('''cuda''')

        UpperCAmelCase_          =							'''Spiderman is surfing'''
        UpperCAmelCase_          =							torch.Generator(device='''cpu''').manual_seed(0)

        UpperCAmelCase_          =							pipe(_snake_case					,						generator=_snake_case					,						num_inference_steps=25					,						output_type='''pt''').frames
        UpperCAmelCase_          =							video_frames.cpu().numpy()

        assert np.abs(expected_video - video).mean() < 5e-2







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy''')

        UpperCAmelCase_          =							TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''')
        UpperCAmelCase_          =							pipe.to('''cuda''')

        UpperCAmelCase_          =							'''Spiderman is surfing'''
        UpperCAmelCase_          =							torch.Generator(device='''cpu''').manual_seed(0)

        UpperCAmelCase_          =							pipe(_snake_case					,						generator=_snake_case					,						num_inference_steps=2					,						output_type='''pt''').frames
        UpperCAmelCase_          =							video_frames.cpu().numpy()

        assert np.abs(expected_video - video).mean() < 5e-2











"""simple docstring"""

from sympy import diff, lambdify, symbols
from sympy.functions import *  # noqa: F403




def __lowercase			(							_a		,			_a		,			_a = "x"		,			_a = 10**-10		,			_a = 1		,			):
    snake_case_ :     Any				=	symbols(_a    )
    snake_case_ :     int				=	lambdify(_a		,			_a    )
    snake_case_ :     Optional[Any]				=	lambdify(_a		,			diff(_a		,			_a    )    )

    snake_case_ :     Optional[Any]				=	starting_point

    while True:
        if diff_function(_a    ) != 0:
            snake_case_ :     Optional[int]				=	prev_guess - multiplicity * func(_a    ) / diff_function(
                _a    )
        else:
            raise ZeroDivisionError('''Could not find root'''    ) from None

        # Precision is checked by comparing the difference of consecutive guesses
        if abs(next_guess - prev_guess    ) < precision:
            return next_guess

        snake_case_ :     int				=	next_guess


# Let's Execute
if __name__ == "__main__":
     # Find root of trigonometric function
     # Find value of pi
     print(f'The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}')

     # Find root of polynomial
     # Find fourth Root of 5
     print(f'The root of x**4 - 5 = 0 is {newton_raphson("x**4 -5", 0.4 +5j)}')

     # Find value of e
     print(
         '''The root of log(y) - 1 = 0 is ''',
         f'{newton_raphson("log(y) - 1", 2, variable="y")}',
     )

     # Exponential Roots
     print(
         '''The root of exp(x) - 1 = 0 is''',
         f'{newton_raphson("exp(x) - 1", 10, precision=0.005)}',
     )

     # Find root of cos(x)
     print(f'The root of cos(x) = 0 is {newton_raphson("cos(x)", 0)}')





