





import timeit







class     lowerCamelCase:
     '''simple docstring'''


     def __init__(						self							,    snake_case_       ):
         if isinstance(snake_case_							,    snake_case_       ):  # Initializes a matrix identical to the one provided.
             _A    =       arg
             _A    =       len(snake_case_       )
         else:  # Initializes a square matrix of the given size and set values to zero.
             _A    =       arg
             _A    =       [[0 for _ in range(self.n       )] for _ in range(self.n       )]




     def __mul__(						self							,    snake_case_       ):
         _A    =       Matrix(self.n       )
         for i in range(self.n       ):
             for j in range(self.n       ):
                 for k in range(self.n       ):
                     matrix.t[i][j] += self.t[i][k] * b.t[k][j]
         return matrix





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)       ->   int:




    """simple docstring"""




    _A    =       Matrix([[1, 0], [0, 1]]							)
    while b > 0:
        if b & 1:
            matrix *= a
        a *= a
        b >>= 1
    return matrix





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)       ->   Dict:




    """simple docstring"""




    if n == 1:
        return fa
    elif n == 2:
        return fa
    _A    =       Matrix([[1, 1], [1, 0]]							)
    _A    =       modular_exponentiation(lowerCAmelCase__       ,    n - 2							)
    return fa * matrix.t[0][0] + fa * matrix.t[0][1]





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




    """simple docstring"""




    if n == 1:
        return fa
    elif n == 2:
        return fa

    _A    =       fa
    _A    =       fa
    n -= 2

    while n > 0:
        _A    =       fn_a + fn_a, fn_a
        n -= 1

    return fn_a





def 							__lowerCAmelCase(					)       ->   Optional[int]:




    """simple docstring"""




    _A    =       '\nfrom random import randint\nfrom __main__ import fibonacci_with_matrix_exponentiation\n'
    _A    =       'fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)'
    _A    =       timeit.timeit(setup=lowerCAmelCase__       ,    stmt=lowerCAmelCase__       ,    number=100							)
    print('With matrix exponentiation the average execution time is '       ,    exec_time / 100							)
    return exec_time





def 							__lowerCAmelCase(					)       ->   Dict:




    """simple docstring"""




    _A    =       '\nfrom random import randint\nfrom __main__ import simple_fibonacci\n'
    _A    =       'simple_fibonacci(randint(1,70000), 1, 1)'
    _A    =       timeit.timeit(setup=lowerCAmelCase__       ,    stmt=lowerCAmelCase__       ,    number=100							)
    print(
        'Without matrix exponentiation the average execution time is '       ,    exec_time / 100							)
    return exec_time





def 							__lowerCAmelCase(					)       ->   str:




    """simple docstring"""




    matrix_exponentiation_time()
    simple_fibonacci_time()


if __name__ == "__main__":
 main()












import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor







class     lowerCamelCase(	unittest.TestCase  ):
     '''simple docstring'''


     @property
     def 					lowerCAmelCase__		(						self       ):
         _A    =       1
         _A    =       3
         _A    =       8
         _A    =       8

         _A    =       torch.rand((batch_size, num_channels, height, width)       )

         return sample




     def 					lowerCAmelCase__		(						self							,    snake_case_       ):
         if isinstance(image[0]							,    PIL.Image.Image       ):
             return np.stack([np.array(snake_case_       ) for i in image]							,    axis=0       )
         elif isinstance(snake_case_							,    torch.Tensor       ):
             return image.cpu().numpy().transpose(0							,    2							,    3							,    1       )
         return image




     def 					lowerCAmelCase__		(						self       ):
         _A    =       VaeImageProcessor(do_resize=snake_case_							,    do_normalize=snake_case_       )

         _A    =       self.dummy_sample
         _A    =       self.to_np(snake_case_       )

         for output_type in ["pt", "np", "pil"]:
             _A    =       image_processor.postprocess(
                 image_processor.preprocess(snake_case_       )							,    output_type=snake_case_							,    )
             _A    =       self.to_np(snake_case_       )
             _A    =       (input_np * 255).round() if output_type == 'pil' else input_np
             assert (
                 np.abs(in_np - out_np       ).max() < 1E-6
             ), F"decoded output does not match input for output_type {output_type}"




     def 					lowerCAmelCase__		(						self       ):
         _A    =       VaeImageProcessor(do_resize=snake_case_							,    do_normalize=snake_case_       )
         _A    =       self.dummy_sample.cpu().numpy().transpose(0							,    2							,    3							,    1       )

         for output_type in ["pt", "np", "pil"]:
             _A    =       image_processor.postprocess(image_processor.preprocess(snake_case_       )							,    output_type=snake_case_       )

             _A    =       self.to_np(snake_case_       )
             _A    =       (input_np * 255).round() if output_type == 'pil' else input_np
             assert (
                 np.abs(in_np - out_np       ).max() < 1E-6
             ), F"decoded output does not match input for output_type {output_type}"




     def 					lowerCAmelCase__		(						self       ):
         _A    =       VaeImageProcessor(do_resize=snake_case_							,    do_normalize=snake_case_       )

         _A    =       self.dummy_sample.cpu().numpy().transpose(0							,    2							,    3							,    1       )
         _A    =       image_processor.numpy_to_pil(snake_case_       )

         for output_type in ["pt", "np", "pil"]:
             _A    =       image_processor.postprocess(image_processor.preprocess(snake_case_       )							,    output_type=snake_case_       )
             for i, o in zip(snake_case_							,    snake_case_       ):
                 _A    =       np.array(snake_case_       )
                 _A    =       self.to_np(snake_case_       ) if output_type == 'pil' else (self.to_np(snake_case_       ) * 255).round()
                 assert (
                     np.abs(in_np - out_np       ).max() < 1E-6
                 ), F"decoded output does not match input for output_type {output_type}"




     def 					lowerCAmelCase__		(						self       ):
         _A    =       VaeImageProcessor(do_resize=snake_case_							,    do_normalize=snake_case_       )

         _A    =       self.dummy_sample
         _A    =       input_pt_ad.squeeze(0       )

         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )
         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )

         _A    =       self.to_np(self.dummy_sample       )
         _A    =       input_np_ad.squeeze(0       )

         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )
         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )

         assert np.abs(out_pt_ad - out_pt_ad       ).max() < 1E-6
         assert np.abs(out_np_ad - out_np_ad       ).max() < 1E-6




     def 					lowerCAmelCase__		(						self       ):
         _A    =       VaeImageProcessor(do_resize=snake_case_							,    do_normalize=snake_case_       )

         _A    =       self.dummy_sample
         _A    =       list(snake_case_       )

         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )

         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )

         _A    =       self.to_np(self.dummy_sample       )
         list(snake_case_       )

         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )

         _A    =       image_processor.postprocess(
             image_processor.preprocess(snake_case_       )							,    output_type='np'							,    )

         assert np.abs(out_pt_ad - out_pt_list       ).max() < 1E-6
         assert np.abs(out_np_ad - out_np_list       ).max() < 1E-6





