





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       :				List[str]           =			namedtuple("""ExampleNamedTuple""", """a b c""")







class 		_lowerCAmelCase(		unittest.TestCase):

  """simple docstring"""







  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     __A    =  torch.randn(5    ,							2      )
     __A    =  torch.device('''cuda'''      ) if torch.cuda.is_available() else torch.device('''cpu'''      )

     __A    =  send_to_device(UpperCAmelCase    ,							UpperCAmelCase      )
     self.assertTrue(torch.equal(resulta.cpu()    ,							UpperCAmelCase      )      )

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

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

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

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

  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Union[str, Any]:
     with self.assertRaises(UpperCAmelCase      ) as cm:
        __A    =  recursively_apply(torch.tensor    ,							(torch.tensor(1      ), 1)    ,							error_on_other_type=UpperCAmelCase      )
     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      )->   Tuple:
     __A    =  torch.tensor([1, 2, 3, 4, 5]      )
     self.assertEqual(listify(UpperCAmelCase      )    ,							[1, 2, 3, 4, 5]      )

     __A    =  torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]      )
     self.assertEqual(listify(UpperCAmelCase      )    ,							[[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(UpperCAmelCase      )    ,							[[[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      )->   str:
     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      )->   List[str]:
     __A    =  RegressionModel()
     __A    =  model.forward
     __A    =  convert_outputs_to_fpaa(model.forward      )
     __A    =  extract_model_from_parallel(UpperCAmelCase    ,							keep_fpaa_wrapper=UpperCAmelCase      )
     __A    =  pickle.dumps(UpperCAmelCase      )

  @require_cuda
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Tuple:
     __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(UpperCAmelCase    ,							keep_fpaa_wrapper=UpperCAmelCase      )
     __A    =  pickle.dumps(UpperCAmelCase      )

  @require_cuda
  @require_torch_min_version(version='''2.0'''      )
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   List[str]:
     __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(UpperCAmelCase      )

  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     __A    =  RegressionModel()
     # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
     __A    =  torch.nn.parallel.DataParallel(UpperCAmelCase      )
     __A    =  extract_model_from_parallel(UpperCAmelCase      )

     self.assertEqual(UpperCAmelCase    ,							UpperCAmelCase      )

  @require_torch_min_version(version='''2.0'''      )
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Tuple:
     __A    =  RegressionModel()
     __A    =  torch.compile(UpperCAmelCase      )

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

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





  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     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''']      )      )








import warnings

from ...utils import logging
from .image_processing_detr import DetrImageProcessor


_UpperCamelCase       :				Tuple           =			logging.get_logger(__name__)







class 		_lowerCAmelCase(		_a):

  """simple docstring"""







  def __init__(   self    ,							*UpperCAmelCase    ,							**UpperCAmelCase      )->   None:
     warnings.warn(
         '''The class DetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
         ''' Please use DetrImageProcessor instead.'''    ,							UpperCAmelCase    ,							)
     super().__init__(*UpperCAmelCase    ,							**UpperCAmelCase      )

