






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		:	int      					=			namedtuple("ExampleNamedTuple", "a b c")
class    a   (			unittest.TestCase       ):



      """simple docstring"""


      def      UpperCAmelCase				(				self :      int )							->				Optional[int]:
        __UpperCAmelCase							:     List[str]									=  torch.randn(5		,				2 )
        __UpperCAmelCase							:     Optional[int]									=  torch.device("""cuda""" ) if torch.cuda.is_available() else torch.device("""cpu""" )

        __UpperCAmelCase							:     List[str]									=  send_to_device(lowercase_		,				lowercase_ )
        self.assertTrue(torch.equal(resulta.cpu()		,				lowercase_ ) )

        __UpperCAmelCase							:     Optional[int]									=  send_to_device((tensor, [tensor, tensor], 1)		,				lowercase_ )
        self.assertIsInstance(lowercase_		,				lowercase_ )
        self.assertTrue(torch.equal(resulta[0].cpu()		,				lowercase_ ) )
        self.assertIsInstance(resulta[1]		,				lowercase_ )
        self.assertTrue(torch.equal(resulta[1][0].cpu()		,				lowercase_ ) )
        self.assertTrue(torch.equal(resulta[1][1].cpu()		,				lowercase_ ) )
        self.assertEqual(resulta[2]		,				1 )

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

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

        __UpperCAmelCase							:     Optional[Any]									=  send_to_device(UserDict({"""a""": tensor, """b""": [tensor, tensor], """c""": 1} )		,				lowercase_ )
        self.assertIsInstance(lowercase_		,				lowercase_ )
        self.assertTrue(torch.equal(resulta["""a"""].cpu()		,				lowercase_ ) )
        self.assertIsInstance(resulta["""b"""]		,				lowercase_ )
        self.assertTrue(torch.equal(resulta["""b"""][0].cpu()		,				lowercase_ ) )
        self.assertTrue(torch.equal(resulta["""b"""][1].cpu()		,				lowercase_ ) )
        self.assertEqual(resulta["""c"""]		,				1 )
      def      UpperCAmelCase				(				self :      Optional[int] )							->				List[str]:
        with self.assertRaises(lowercase_ ) as cm:
          __UpperCAmelCase							:     int									=  recursively_apply(torch.tensor		,				(torch.tensor(1 ), 1)		,				error_on_other_type=lowercase_ )
        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 :      Union[str, Any] )							->				List[str]:
        __UpperCAmelCase							:     int									=  torch.tensor([1, 2, 3, 4, 5] )
        self.assertEqual(listify(lowercase_ )		,				[1, 2, 3, 4, 5] )

        __UpperCAmelCase							:     int									=  torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] )
        self.assertEqual(listify(lowercase_ )		,				[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] )

        __UpperCAmelCase							:     Union[str, Any]									=  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(lowercase_ )		,				[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]] )
      def      UpperCAmelCase				(				self :      List[Any] )							->				List[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      UpperCAmelCase				(				self :      List[str] )							->				Optional[int]:
        __UpperCAmelCase							:     Optional[Any]									=  RegressionModel()
        __UpperCAmelCase							:     Tuple									=  model.forward
        __UpperCAmelCase							:     Union[str, Any]									=  convert_outputs_to_fpaa(model.forward )
        __UpperCAmelCase							:     Tuple									=  extract_model_from_parallel(lowercase_		,				keep_fpaa_wrapper=lowercase_ )
        __UpperCAmelCase							:     List[str]									=  pickle.dumps(lowercase_ )
      @require_cuda
      def      UpperCAmelCase				(				self :      Dict )							->				List[Any]:
        __UpperCAmelCase							:     Optional[int]									=  RegressionModel()
        __UpperCAmelCase							:     List[Any]									=  model.forward
        __UpperCAmelCase							:     int									=  torch.cuda.amp.autocast(dtype=torch.floataa )(model.forward )
        __UpperCAmelCase							:     str									=  convert_outputs_to_fpaa(model.forward )
        __UpperCAmelCase							:     Optional[int]									=  extract_model_from_parallel(lowercase_		,				keep_fpaa_wrapper=lowercase_ )
        __UpperCAmelCase							:     Optional[int]									=  pickle.dumps(lowercase_ )
      @require_cuda
      @require_torch_min_version(version="""2.0""" )
      def      UpperCAmelCase				(				self :      Dict )							->				str:
        __UpperCAmelCase							:     int									=  RegressionModel()
        __UpperCAmelCase							:     Any									=  model.forward
        __UpperCAmelCase							:     Optional[int]									=  torch.cuda.amp.autocast(dtype=torch.floataa )(model.forward )
        __UpperCAmelCase							:     Optional[int]									=  convert_outputs_to_fpaa(model.forward )
        __UpperCAmelCase							:     Dict									=  torch.compile(model.forward		,				backend="""inductor""" )
        __UpperCAmelCase							:     Optional[int]									=  torch.randn(4		,				10 ).cuda()
        __UpperCAmelCase							:     List[Any]									=  model(lowercase_ )
      def      UpperCAmelCase				(				self :      Optional[int] )							->				List[str]:
        __UpperCAmelCase							:     Tuple									=  RegressionModel()
        # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
        __UpperCAmelCase							:     Union[str, Any]									=  torch.nn.parallel.DataParallel(lowercase_ )
        __UpperCAmelCase							:     Optional[Any]									=  extract_model_from_parallel(lowercase_ )

        self.assertEqual(lowercase_		,				lowercase_ )
      @require_torch_min_version(version="""2.0""" )
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Optional[int]:
        __UpperCAmelCase							:     Any									=  RegressionModel()
        __UpperCAmelCase							:     Dict									=  torch.compile(lowercase_ )

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

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




      def      UpperCAmelCase				(				self :      List[Any] )							->				Optional[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"""] ) )










def     lowerCamelCase__				(   __lowerCamelCase      :					list  ,		__lowerCamelCase      :					int		):
  for index, item in enumerate(__lowerCamelCase		):
    if item == target:
      return index
  return -1






def     lowerCamelCase__				(   __lowerCamelCase      :					list  ,		__lowerCamelCase      :					int  ,		__lowerCamelCase      :					int  ,		__lowerCamelCase      :					int		):
  if not (0 <= high < len(__lowerCamelCase		) and 0 <= low < len(__lowerCamelCase		)):
    raise Exception("""Invalid upper or lower bound!"""		)
  if high < low:
    return -1
  if sequence[low] == target:
    return low
  if sequence[high] == target:
    return high
  return rec_linear_search(__lowerCamelCase  ,		low + 1  ,		high - 1  ,		__lowerCamelCase		)


if __name__ == "__main__":
       a		:	List[str]      					=			input("Enter numbers separated by comma:\n").strip()
       a		:	Tuple      					=			[int(item.strip()) for item in user_input.split(",")]

       a		:	List[str]      					=			int(input("Enter a single number to be found in the list:\n").strip())
       a		:	Optional[Any]      					=			linear_search(sequence, target)
       if result != -1:
              print(f"""linear_search({sequence}, {target}) = {result}""")
       else:
              print(f"""{target} was not found in {sequence}""")


