



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,
)


A_		:  Optional[int]										=  namedtuple('''ExampleNamedTuple''', '''a b c''')



class 		_lowercase		(							unittest.TestCase						):

   def 		A				(   self      :							List[str]				)							->					Any:


    """simple docstring"""






    a							=		torch.randn(5				, 2				)
    a							=		torch.device("cuda"				) if torch.cuda.is_available() else torch.device("cpu"				)

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

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

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

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

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

   def 		A				(   self      :							List[Any]				)							->					Optional[Any]:


    """simple docstring"""






    with self.assertRaises(__lowerCAmelCase				) as cm:
     a							=		recursively_apply(torch.tensor				, (torch.tensor(1				), 1)				, error_on_other_type=__lowerCAmelCase				)
    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 		A				(   self      :							List[str]				)							->					Optional[int]:


    """simple docstring"""






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

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

    a							=		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(__lowerCAmelCase				)				, [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]				)

   def 		A				(   self      :							Tuple				)							->					int:


    """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 		A				(   self      :							Tuple				)							->					List[Any]:


    """simple docstring"""






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

   @require_cuda
   def 		A				(   self      :							Optional[Any]				)							->					List[Any]:


    """simple docstring"""






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

   @require_cuda
   @require_torch_min_version(version="2.0"				)
   def 		A				(   self      :							Optional[Any]				)							->					Union[str, Any]:


    """simple docstring"""






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

   def 		A				(   self      :							int				)							->					int:


    """simple docstring"""






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

    self.assertEqual(__lowerCAmelCase				, __lowerCAmelCase				)

   @require_torch_min_version(version="2.0"				)
   def 		A				(   self      :							Dict				)							->					List[str]:


    """simple docstring"""






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

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

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




   def 		A				(   self      :							Union[str, Any]				)							->					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

import math
import random







def 				__UpperCamelCase (					__UpperCAmelCase  :					int ,   __UpperCAmelCase  :					int ,   __UpperCAmelCase  :					int			)							->	list[int]:
  lowercase				:							Union[str, Any]									=   d * e - 1
  lowercase				:							List[str]									=   0
  lowercase				:							Tuple									=   0
  while p == 0:
    lowercase				:							Optional[Any]									=   random.randint(2 ,   n - 1			)
    lowercase				:							Dict									=   k
    while True:
      if t % 2 == 0:
        lowercase				:							List[Any]									=   t // 2
        lowercase				:							Union[str, Any]									=   (g**t) % n
        lowercase				:							Optional[Any]									=   math.gcd(x - 1 ,   __UpperCAmelCase			)
        if x > 1 and y > 1:
          lowercase				:							Dict									=   y
          lowercase				:							Any									=   n // y
          break  # find the correct factors
      else:
        break  # t is not divisible by 2, break and choose another g
  return sorted([p, q]			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()