



'''simple docstring'''
from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
					import torch

_UpperCamelCase									=      logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
					from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):


					def 	lowercase_						( cls						:	Union[str, Any]			):

								"""simple docstring"""





								return cls


def 	lowercase_						( lowerCAmelCase__						:	Union[Tuple, List]      ,				lowerCAmelCase__						:	Optional[Union[List["torch.Generator"], "torch.Generator"]] = None      ,				lowerCAmelCase__						:	Optional["torch.device"] = None      ,				lowerCAmelCase__						:	Optional["torch.dtype"] = None      ,				lowerCAmelCase__						:	Optional["torch.layout"] = None      ,				):

			"""simple docstring"""





			__UpperCAmelCase :   Dict    					=  device
			__UpperCAmelCase :   Dict    					=  shape[0]

			__UpperCAmelCase :   str    					=  layout or torch.strided
			__UpperCAmelCase :   Tuple    					=  device or torch.device("""cpu"""			)

			if generator is not None:
						__UpperCAmelCase :   Dict    					=  generator.device.type if not isinstance(lowerCAmelCase__      ,				lowerCAmelCase__			) else generator[0].device.type
						if gen_device_type != device.type and gen_device_type == "cpu":
									__UpperCAmelCase :   Union[str, Any]    					=  """cpu"""
									if device != "mps":
												logger.info(
												    f'The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'
												    f' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'
												    f' slighly speed up this function by passing a generator that was created on the {device} device.'			)
						elif gen_device_type != device.type and gen_device_type == "cuda":
									raise ValueError(f'Cannot generate a {device} tensor from a generator of type {gen_device_type}.'			)

			if isinstance(lowerCAmelCase__      ,				lowerCAmelCase__			):
						__UpperCAmelCase :   List[Any]    					=  (1,) + shape[1:]
						__UpperCAmelCase :   Union[str, Any]    					=  [
						    torch.randn(lowerCAmelCase__      ,				generator=generator[i]      ,				device=lowerCAmelCase__      ,				dtype=lowerCAmelCase__      ,				layout=lowerCAmelCase__			)
						    for i in range(lowerCAmelCase__			)
						]
						__UpperCAmelCase :   str    					=  torch.cat(lowerCAmelCase__      ,				dim=0			).to(lowerCAmelCase__			)
			else:
						__UpperCAmelCase :   Optional[int]    					=  torch.randn(lowerCAmelCase__      ,				generator=lowerCAmelCase__      ,				device=lowerCAmelCase__      ,				dtype=lowerCAmelCase__      ,				layout=lowerCAmelCase__			).to(lowerCAmelCase__			)

			return latents






def 	lowercase_						( lowerCAmelCase__						:	Any			):

			"""simple docstring"""





			if is_torch_version("""<"""      ,				"""2.0.0"""			) or not hasattr(lowerCAmelCase__      ,				"""_dynamo"""			):
						return False
			return isinstance(lowerCAmelCase__      ,				torch._dynamo.eval_frame.OptimizedModule			)









'''simple docstring'''
from __future__ import annotations


def 	lowercase_						( lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int			):

			"""simple docstring"""





			assert (
			    c % greatest_common_divisor(lowerCAmelCase__      ,				lowerCAmelCase__			) == 0
			)  # greatest_common_divisor(a,b) function implemented below
			((__UpperCAmelCase)					,	(__UpperCAmelCase)					,	(__UpperCAmelCase)) :   int    					=  extended_gcd(lowerCAmelCase__      ,				lowerCAmelCase__			)  # extended_gcd(a,b) function implemented below
			__UpperCAmelCase :   Union[str, Any]    					=  c / d
			return (r * x, r * y)


def 	lowercase_						( lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int = 2			):

			"""simple docstring"""





			((__UpperCAmelCase)					,	(__UpperCAmelCase)) :   List[str]    					=  diophantine(lowerCAmelCase__      ,				lowerCAmelCase__      ,				lowerCAmelCase__			)  # Initial value
			__UpperCAmelCase :   List[str]    					=  greatest_common_divisor(lowerCAmelCase__      ,				lowerCAmelCase__			)
			__UpperCAmelCase :   List[str]    					=  a // d
			__UpperCAmelCase :   Tuple    					=  b // d

			for i in range(lowerCAmelCase__			):
						__UpperCAmelCase :   List[Any]    					=  xa + i * q
						__UpperCAmelCase :   Tuple    					=  ya - i * p
						print(lowerCAmelCase__      ,				lowerCAmelCase__			)


def 	lowercase_						( lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int			):

			"""simple docstring"""





			if a < b:
						__UpperCAmelCase					,	__UpperCAmelCase :   List[str]    					=  b, a

			while a % b != 0:
						__UpperCAmelCase					,	__UpperCAmelCase :   Optional[int]    					=  b, a % b

			return b


def 	lowercase_						( lowerCAmelCase__						:	int      ,				lowerCAmelCase__						:	int			):

			"""simple docstring"""





			assert a >= 0 and b >= 0

			if b == 0:
						__UpperCAmelCase					,	__UpperCAmelCase					,	__UpperCAmelCase :   List[str]    					=  a, 1, 0
			else:
						((__UpperCAmelCase)					,	(__UpperCAmelCase)					,	(__UpperCAmelCase)) :   str    					=  extended_gcd(lowerCAmelCase__      ,				a % b			)
						__UpperCAmelCase :   Optional[Any]    					=  q
						__UpperCAmelCase :   Dict    					=  p - q * (a // b)

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

			return (d, x, y)


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

					testmod(name='''diophantine''', verbose=True)
					testmod(name='''diophantine_all_soln''', verbose=True)
					testmod(name='''extended_gcd''', verbose=True)
					testmod(name='''greatest_common_divisor''', verbose=True)




