'''simple docstring'''




import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


__UpperCAmelCase  							=namedtuple("ExampleNamedTuple", "a b c")




class 					a__   (     unittest.TestCase				):






     def      SCREAMING_SNAKE_CASE__     (				self	:	Any					):





         """simple docstring"""



         __lowerCamelCase						=						torch.randn(5						,       2					)
         __lowerCamelCase						=						torch.device('''cuda'''					) if torch.cuda.is_available() else torch.device('''cpu'''					)

         __lowerCamelCase						=						send_to_device(a						,       a					)
         self.assertTrue(torch.equal(resulta.cpu()						,       a					)					)

         __lowerCamelCase						=						send_to_device((tensor, [tensor, tensor], 1)						,       a					)
         self.assertIsInstance(a						,       a					)
         self.assertTrue(torch.equal(resulta[0].cpu()						,       a					)					)
         self.assertIsInstance(resulta[1]						,       a					)
         self.assertTrue(torch.equal(resulta[1][0].cpu()						,       a					)					)
         self.assertTrue(torch.equal(resulta[1][1].cpu()						,       a					)					)
         self.assertEqual(resulta[2]						,       1					)

         __lowerCamelCase						=						send_to_device({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}						,       a					)
         self.assertIsInstance(a						,       a					)
         self.assertTrue(torch.equal(resulta['''a'''].cpu()						,       a					)					)
         self.assertIsInstance(resulta['''b''']						,       a					)
         self.assertTrue(torch.equal(resulta['''b'''][0].cpu()						,       a					)					)
         self.assertTrue(torch.equal(resulta['''b'''][1].cpu()						,       a					)					)
         self.assertEqual(resulta['''c''']						,       1					)

         __lowerCamelCase						=						send_to_device(ExampleNamedTuple(a=a						,       b=[tensor, tensor]						,       c=1					)						,       a					)
         self.assertIsInstance(a						,       a					)
         self.assertTrue(torch.equal(resulta.a.cpu()						,       a					)					)
         self.assertIsInstance(resulta.b						,       a					)
         self.assertTrue(torch.equal(resulta.b[0].cpu()						,       a					)					)
         self.assertTrue(torch.equal(resulta.b[1].cpu()						,       a					)					)
         self.assertEqual(resulta.c						,       1					)

         __lowerCamelCase						=						send_to_device(UserDict({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}					)						,       a					)
         self.assertIsInstance(a						,       a					)
         self.assertTrue(torch.equal(resulta['''a'''].cpu()						,       a					)					)
         self.assertIsInstance(resulta['''b''']						,       a					)
         self.assertTrue(torch.equal(resulta['''b'''][0].cpu()						,       a					)					)
         self.assertTrue(torch.equal(resulta['''b'''][1].cpu()						,       a					)					)
         self.assertEqual(resulta['''c''']						,       1					)






     def      SCREAMING_SNAKE_CASE__     (				self	:	List[Any]					):





         """simple docstring"""



         with self.assertRaises(a					) as cm:
             __lowerCamelCase						=						recursively_apply(torch.tensor						,       (torch.tensor(1					), 1)						,       error_on_other_type=a					)
         self.assertEqual(
             str(cm.exception					)						,       '''Unsupported types (<class \'int\'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed.'''						,       )






     def      SCREAMING_SNAKE_CASE__     (				self	:	int					):





         """simple docstring"""



         __lowerCamelCase						=						torch.tensor([1, 2, 3, 4, 5]					)
         self.assertEqual(listify(a					)						,       [1, 2, 3, 4, 5]					)

         __lowerCamelCase						=						torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]					)
         self.assertEqual(listify(a					)						,       [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]					)

         __lowerCamelCase						=						torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]					)
         self.assertEqual(
             listify(a					)						,       [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]					)






     def      SCREAMING_SNAKE_CASE__     (				self	:	Dict					):





         """simple docstring"""



         with patch_environment(aa=1						,       BB=2					):
             self.assertEqual(os.environ.get('''AA'''					)						,       '''1'''					)
             self.assertEqual(os.environ.get('''BB'''					)						,       '''2'''					)

         self.assertNotIn('''AA'''						,       os.environ					)
         self.assertNotIn('''BB'''						,       os.environ					)






     def      SCREAMING_SNAKE_CASE__     (				self	:	Tuple					):





         """simple docstring"""



         __lowerCamelCase						=						RegressionModel()
         __lowerCamelCase						=						model.forward
         __lowerCamelCase						=						convert_outputs_to_fpaa(model.forward					)
         __lowerCamelCase						=						extract_model_from_parallel(a						,       keep_fpaa_wrapper=a					)
         __lowerCamelCase						=						pickle.dumps(a					)






     @require_cuda
     def      SCREAMING_SNAKE_CASE__     (				self	:	Optional[Any]					):





         """simple docstring"""



         __lowerCamelCase						=						RegressionModel()
         __lowerCamelCase						=						model.forward
         __lowerCamelCase						=						torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
         __lowerCamelCase						=						convert_outputs_to_fpaa(model.forward					)
         __lowerCamelCase						=						extract_model_from_parallel(a						,       keep_fpaa_wrapper=a					)
         __lowerCamelCase						=						pickle.dumps(a					)






     @require_cuda
     @require_torch_min_version(version='''2.0'''					)
     def      SCREAMING_SNAKE_CASE__     (				self	:	Optional[int]					):





         """simple docstring"""



         __lowerCamelCase						=						RegressionModel()
         __lowerCamelCase						=						model.forward
         __lowerCamelCase						=						torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
         __lowerCamelCase						=						convert_outputs_to_fpaa(model.forward					)
         __lowerCamelCase						=						torch.compile(model.forward						,       backend='''inductor'''					)
         __lowerCamelCase						=						torch.randn(4						,       10					).cuda()
         __lowerCamelCase						=						model(a					)






     def      SCREAMING_SNAKE_CASE__     (				self	:	Optional[Any]					):





         """simple docstring"""



         __lowerCamelCase						=						RegressionModel()
         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         __lowerCamelCase						=						torch.nn.parallel.DataParallel(a					)
         __lowerCamelCase						=						extract_model_from_parallel(a					)

         self.assertEqual(a						,       a					)






     @require_torch_min_version(version='''2.0'''					)
     def      SCREAMING_SNAKE_CASE__     (				self	:	str					):





         """simple docstring"""



         __lowerCamelCase						=						RegressionModel()
         __lowerCamelCase						=						torch.compile(a					)

         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         __lowerCamelCase						=						torch.nn.parallel.DataParallel(a					)
         __lowerCamelCase						=						torch.compile(a					)
         __lowerCamelCase						=						extract_model_from_parallel(a					)

         self.assertEqual(compiled_model._orig_mod						,       compiled_model_unwrapped._orig_mod					)






     def      SCREAMING_SNAKE_CASE__     (				self	:	Tuple					):





         """simple docstring"""



         self.assertEqual(find_device([1, '''a''', torch.tensor([1, 2, 3]					)]					)						,       torch.device('''cpu'''					)					)
         self.assertEqual(find_device({'''a''': 1, '''b''': torch.tensor([1, 2, 3]					)}					)						,       torch.device('''cpu'''					)					)
         self.assertIsNone(find_device([1, '''a''']					)					)

'''simple docstring'''




from __future__ import annotations






def __lowerCAmelCase				(							UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__       )			->					tuple[float, float]:
    assert (
        c % greatest_common_divisor(UpperCamelCase__       ,						UpperCamelCase__       ) == 0
    )  # greatest_common_divisor(a,b) function implemented below
    ((__lowerCamelCase)			,						(__lowerCamelCase)			,						(__lowerCamelCase))						=						extended_gcd(UpperCamelCase__       ,						UpperCamelCase__       )  # extended_gcd(a,b) function implemented below
    __lowerCamelCase						=						c / d
    return (r * x, r * y)






def __lowerCAmelCase				(							UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__ = 2       )			->					None:
    ((__lowerCamelCase)			,						(__lowerCamelCase))						=						diophantine(UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__       )  # Initial value
    __lowerCamelCase						=						greatest_common_divisor(UpperCamelCase__       ,						UpperCamelCase__       )
    __lowerCamelCase						=						a // d
    __lowerCamelCase						=						b // d

    for i in range(UpperCamelCase__       ):
        __lowerCamelCase						=						xa + i * q
        __lowerCamelCase						=						ya - i * p
        print(UpperCamelCase__       ,						UpperCamelCase__       )






def __lowerCAmelCase				(							UpperCamelCase__       ,						UpperCamelCase__       )			->					int:
    if a < b:
        __lowerCamelCase			,						__lowerCamelCase						=						b, a

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

    return b






def __lowerCAmelCase				(							UpperCamelCase__       ,						UpperCamelCase__       )			->					tuple[int, int, int]:
    assert a >= 0 and b >= 0

    if b == 0:
        __lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase						=						a, 1, 0
    else:
        ((__lowerCamelCase)			,						(__lowerCamelCase)			,						(__lowerCamelCase))						=						extended_gcd(UpperCamelCase__       ,						a % b       )
        __lowerCamelCase						=						q
        __lowerCamelCase						=						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)
