





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


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



class 				_lowerCAmelCase			(	unittest.TestCase							):


  '''simple docstring'''







  def UpperCamelCase_       (					self					:    str							):


    '''simple docstring'''





    _snake_case      : Union[str, Any]														=		torch.randn(5					, 2							)
    _snake_case      : Optional[Any]														=		torch.device('cuda'							) if torch.cuda.is_available() else torch.device('cpu'							)

    _snake_case      : List[Any]														=		send_to_device(UpperCamelCase					, UpperCamelCase							)
    self.assertTrue(torch.equal(resulta.cpu()					, UpperCamelCase							)							)

    _snake_case      : Optional[Any]														=		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							)

    _snake_case      : List[Any]														=		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							)

    _snake_case      : List[str]														=		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							)

    _snake_case      : List[str]														=		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 UpperCamelCase_       (					self					:    Dict							):


    '''simple docstring'''





    with self.assertRaises(UpperCamelCase							) as cm:
      _snake_case      : str														=		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 UpperCamelCase_       (					self					:    str							):


    '''simple docstring'''





    _snake_case      : int														=		torch.tensor([1, 2, 3, 4, 5]							)
    self.assertEqual(listify(UpperCamelCase							)					, [1, 2, 3, 4, 5]							)

    _snake_case      : Dict														=		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]]							)

    _snake_case      : List[str]														=		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 UpperCamelCase_       (					self					:    str							):


    '''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 UpperCamelCase_       (					self					:    str							):


    '''simple docstring'''





    _snake_case      : Any														=		RegressionModel()
    _snake_case      : Dict														=		model.forward
    _snake_case      : List[Any]														=		convert_outputs_to_fpaa(model.forward							)
    _snake_case      : Union[str, Any]														=		extract_model_from_parallel(UpperCamelCase					, keep_fpaa_wrapper=UpperCamelCase							)
    _snake_case      : Any														=		pickle.dumps(UpperCamelCase							)







  @require_cuda
  def UpperCamelCase_       (					self					:    List[Any]							):


    '''simple docstring'''





    _snake_case      : Dict														=		RegressionModel()
    _snake_case      : Any														=		model.forward
    _snake_case      : Union[str, Any]														=		torch.cuda.amp.autocast(dtype=torch.floataa							)(model.forward							)
    _snake_case      : Optional[int]														=		convert_outputs_to_fpaa(model.forward							)
    _snake_case      : Union[str, Any]														=		extract_model_from_parallel(UpperCamelCase					, keep_fpaa_wrapper=UpperCamelCase							)
    _snake_case      : Optional[int]														=		pickle.dumps(UpperCamelCase							)







  @require_cuda
  @require_torch_min_version(version='2.0'							)
  def UpperCamelCase_       (					self					:    List[str]							):


    '''simple docstring'''





    _snake_case      : Dict														=		RegressionModel()
    _snake_case      : Tuple														=		model.forward
    _snake_case      : Tuple														=		torch.cuda.amp.autocast(dtype=torch.floataa							)(model.forward							)
    _snake_case      : Dict														=		convert_outputs_to_fpaa(model.forward							)
    _snake_case      : List[Any]														=		torch.compile(model.forward					, backend='inductor'							)
    _snake_case      : str														=		torch.randn(4					, 10							).cuda()
    _snake_case      : List[str]														=		model(UpperCamelCase							)







  def UpperCamelCase_       (					self					:    Any							):


    '''simple docstring'''





    _snake_case      : Any														=		RegressionModel()
    # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
    _snake_case      : str														=		torch.nn.parallel.DataParallel(UpperCamelCase							)
    _snake_case      : Optional[Any]														=		extract_model_from_parallel(UpperCamelCase							)

    self.assertEqual(UpperCamelCase					, UpperCamelCase							)







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


    '''simple docstring'''





    _snake_case      : int														=		RegressionModel()
    _snake_case      : List[str]														=		torch.compile(UpperCamelCase							)

    # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
    _snake_case      : Any														=		torch.nn.parallel.DataParallel(UpperCamelCase							)
    _snake_case      : Union[str, Any]														=		torch.compile(UpperCamelCase							)
    _snake_case      : Optional[Any]														=		extract_model_from_parallel(UpperCamelCase							)

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







  def UpperCamelCase_       (					self					:    Optional[Any]							):


    '''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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A_			: Dict				=			{"configuration_qdqbert": ["QDQBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "QDQBertConfig"]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A_			: Tuple				=			[
          "QDQBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "QDQBertForMaskedLM",
          "QDQBertForMultipleChoice",
          "QDQBertForNextSentencePrediction",
          "QDQBertForQuestionAnswering",
          "QDQBertForSequenceClassification",
          "QDQBertForTokenClassification",
          "QDQBertLayer",
          "QDQBertLMHeadModel",
          "QDQBertModel",
          "QDQBertPreTrainedModel",
          "load_tf_weights_in_qdqbert",
      ]


if TYPE_CHECKING:
      from .configuration_qdqbert import QDQBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, QDQBertConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_qdqbert import (
                QDQBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                QDQBertForMaskedLM,
                QDQBertForMultipleChoice,
                QDQBertForNextSentencePrediction,
                QDQBertForQuestionAnswering,
                QDQBertForSequenceClassification,
                QDQBertForTokenClassification,
                QDQBertLayer,
                QDQBertLMHeadModel,
                QDQBertModel,
                QDQBertPreTrainedModel,
                load_tf_weights_in_qdqbert,
            )


else:
      import sys

      A_			: Tuple				=			_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

