


'''simple docstring'''




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 			_lowercase (	__lowercase						,					unittest.TestCase							):
   _SCREAMING_SNAKE_CASE  :  str          =	TextToVideoSDPipeline
   _SCREAMING_SNAKE_CASE  :  Tuple          =	TEXT_TO_IMAGE_PARAMS
   _SCREAMING_SNAKE_CASE  :  List[str]          =	TEXT_TO_IMAGE_BATCH_PARAMS
   # No `output_type`.
   _SCREAMING_SNAKE_CASE  :  List[str]          =	frozenset(
       [
           "num_inference_steps",
           "generator",
           "latents",
           "return_dict",
           "callback",
           "callback_steps",
       ]							)
   def a  (						self       :      Union[str, Any]      ) ->							Dict:
        torch.manual_seed(0      )
        __snake_case           =						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       ,   )
        __snake_case           =						DDIMScheduler(
            beta_start=0.0_0_0_8_5       ,   beta_end=0.0_1_2       ,   beta_schedule='scaled_linear'       ,   clip_sample=SCREAMING_SNAKE_CASE_       ,   set_alpha_to_one=SCREAMING_SNAKE_CASE_       ,   )
        torch.manual_seed(0      )
        __snake_case           =						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      )
        __snake_case           =						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       ,   )
        __snake_case           =						CLIPTextModel(SCREAMING_SNAKE_CASE_      )
        __snake_case           =						CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

        __snake_case           =						{
            'unet': unet,
            'scheduler': scheduler,
            'vae': vae,
            'text_encoder': text_encoder,
            'tokenizer': tokenizer,
        }
        return components
   def a  (						self       :      Any       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]=0      ) ->							Union[str, Any]:
        if str(SCREAMING_SNAKE_CASE_      ).startswith('mps'      ):
             __snake_case           =						torch.manual_seed(SCREAMING_SNAKE_CASE_      )
        else:
             __snake_case           =						torch.Generator(device=SCREAMING_SNAKE_CASE_      ).manual_seed(SCREAMING_SNAKE_CASE_      )
        __snake_case           =						{
            '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 a  (						self       :      Any      ) ->							Union[str, Any]:
        __snake_case           =						'cpu'  # ensure determinism for the device-dependent torch.Generator
        __snake_case           =						self.get_dummy_components()
        __snake_case           =						TextToVideoSDPipeline(**SCREAMING_SNAKE_CASE_      )
        __snake_case           =						sd_pipe.to(SCREAMING_SNAKE_CASE_      )
        sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_      )

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

        assert frames[0].shape == (64, 64, 3)
        __snake_case           =						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 a  (						self       :      Optional[int]      ) ->							str:
        self._test_attention_slicing_forward_pass(test_mean_pixel_difference=SCREAMING_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 a  (						self       :      Any      ) ->							int:
        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=SCREAMING_SNAKE_CASE_       ,   expected_max_diff=1e-2      )
   @unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'      )
   def a  (						self       :      List[str]      ) ->							Optional[int]:
        pass
   @unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'      )
   def a  (						self       :      Optional[int]      ) ->							Any:
        pass
   @unittest.skip(reason='`num_images_per_prompt` argument is not supported for this pipeline.'      )
   def a  (						self       :      List[Any]      ) ->							List[str]:
        pass





   def a  (						self       :      Dict      ) ->							Optional[int]:
        return super().test_progress_bar()




@slow
@skip_mps
class 			_lowercase (	unittest.TestCase							):
   def a  (						self       :      List[Any]      ) ->							str:
        __snake_case           =						load_numpy(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy'      )

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

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

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

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





   def a  (						self       :      Dict      ) ->							Any:
        __snake_case           =						load_numpy(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy'      )

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

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

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

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










from __future__ import annotations

from collections.abc import Callable

lowercase__							:	Optional[Any]											=		list[list[float | int]]


def 		lowerCamelCase__       (						_A	,      _A				):







	'''simple docstring'''





	snake_case_						       =	len(_A				)
	snake_case_						       =	[[0 for _ in range(size + 1				)] for _ in range(_A				)]
	snake_case_						       =	42
	snake_case_						       =	42
	snake_case_						       =	42
	snake_case_						       =	42
	snake_case_						       =	42
	snake_case_						       =	42

	for row in range(_A				):
		for col in range(_A				):
			snake_case_						       =	matrix[row][col]

		snake_case_						       =	vector[row][0]

	snake_case_						       =	0
	snake_case_						       =	0
	while row < size and col < size:
		# pivoting
		snake_case_						       =	max((abs(augmented[rowa][col]				), rowa) for rowa in range(_A	,      _A				)				)[
		    1
		]
		if augmented[pivot_row][col] == 0:
			col += 1
			continue
		else:
			snake_case_       , snake_case_						       =	augmented[pivot_row], augmented[row]

		for rowa in range(row + 1	,      _A				):
			snake_case_						       =	augmented[rowa][col] / augmented[row][col]
			snake_case_						       =	0
			for cola in range(col + 1	,      size + 1				):
				augmented[rowa][cola] -= augmented[row][cola] * ratio

		row += 1
		col += 1

	# back substitution
	for col in range(1	,      _A				):
		for row in range(_A				):
			snake_case_						       =	augmented[row][col] / augmented[col][col]
			for cola in range(_A	,      size + 1				):
				augmented[row][cola] -= augmented[col][cola] * ratio

    # round to get rid of numbers like 2.000000000000004
	return [
	    [round(augmented[row][size] / augmented[row][row]	,      10				)] for row in range(_A				)
	]


def 		lowerCamelCase__       (						_A				):







	'''simple docstring'''





	snake_case_						       =	len(_A				)
	snake_case_						       =	[[0 for _ in range(_A				)] for _ in range(_A				)]
	snake_case_						       =	[[0] for _ in range(_A				)]
	snake_case_						       =	42
	snake_case_						       =	42
	snake_case_						       =	42
	snake_case_						       =	42

	for x_val, y_val in enumerate(_A				):
		for col in range(_A				):
			snake_case_						       =	(x_val + 1) ** (size - col - 1)
		snake_case_						       =	y_val

	snake_case_						       =	solve(_A	,      _A				)

	def interpolated_func(_A				) -> int:
		return sum(
		    round(coeffs[x_val][0]				) * (var ** (size - x_val - 1))
		    for x_val in range(_A				)				)

	return interpolated_func


def 		lowerCamelCase__       (						_A				):







	'''simple docstring'''





	return (
	    1
	    - variable
	    + variable**2
	    - variable**3
	    + variable**4
	    - variable**5
	    + variable**6
	    - variable**7
	    + variable**8
	    - variable**9
	    + variable**10
	)


def 		lowerCamelCase__       (						_A = question_function	,      _A = 10				):







	'''simple docstring'''





	snake_case_						       =	[func(_A				) for x_val in range(1	,      order + 1				)]

	snake_case_						       =	[
	    interpolate(data_points[:max_coeff]				) for max_coeff in range(1	,      order + 1				)
	]

	snake_case_						       =	0
	snake_case_						       =	42
	snake_case_						       =	42

	for poly in polynomials:
		snake_case_						       =	1
		while func(_A				) == poly(_A				):
			x_val += 1

		ret += poly(_A				)

	return ret


if __name__ == "__main__":
		print(f'''{solution() = }''')
