





from __future__ import annotations





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




    """simple docstring"""




    assert n > 1 and a > 0 and greatest_common_divisor(_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							) == 1
    (_A)    =       extended_gcd(_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)  # Implemented below
    _A    =       (b * s) % n
    return x





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)       ->   int:




    """simple docstring"""




    (_A)    =       extended_euclid(_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)  # Implemented below
    if b < 0:
        _A    =       (b % n + n) % n
    return b





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




    """simple docstring"""




    _A    =       invert_modulo(_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)
    _A    =       (b * s) % n
    return x





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)       ->   tuple[int, int, int]:




    """simple docstring"""




    assert a >= 0 and b >= 0

    if b == 0:
        _A    =       a, 1, 0
    else:
        (_A)    =       extended_gcd(_SCREAMING_SNAKE_CASE       ,    a % b							)
        _A    =       q
        _A    =       p - q * (a // b)

    assert a % d == 0 and b % d == 0
    assert d == a * x + b * y

    return (d, x, y)





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)       ->   tuple[int, int]:




    """simple docstring"""




    if b == 0:
        return (1, 0)
    (_A)    =       extended_euclid(_SCREAMING_SNAKE_CASE       ,    a % b							)
    _A    =       a // b
    return (y, x - k * y)





def 							__lowerCAmelCase(					_SCREAMING_SNAKE_CASE       ,    _SCREAMING_SNAKE_CASE							)       ->   int:




    """simple docstring"""




    if a < b:
        _A    =       b, a

    while a % b != 0:
        _A    =       b, a % b

    return b


if __name__ == "__main__":
 from doctest import testmod

 testmod(name="modular_division", verbose=True)
 testmod(name="modular_division2", verbose=True)
 testmod(name="invert_modulo", verbose=True)
 testmod(name="extended_gcd", verbose=True)
 testmod(name="extended_euclid", verbose=True)
 testmod(name="greatest_common_divisor", verbose=True)












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





