






'''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 							__A       (     self				)       ->     List[str]:

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[int]										=     torch.randn(5 ,       2				)
					__UpperCAmelCase   :      Any										=     torch.device("""cuda"""				) if torch.cuda.is_available() else torch.device("""cpu"""				)

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

					__UpperCAmelCase   :      List[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				)

					__UpperCAmelCase   :      int										=     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				)

					__UpperCAmelCase   :      Union[str, Any]										=     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				)

					__UpperCAmelCase   :      Tuple										=     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 							__A       (     self				)       ->     int:

					'''simple docstring'''



					with self.assertRaises(__UpperCAmelCase				) as cm:
								__UpperCAmelCase   :      int										=     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 							__A       (     self				)       ->     int:

					'''simple docstring'''
					__UpperCAmelCase   :      Union[str, Any]										=     torch.tensor([1, 2, 3, 4, 5]				)
					self.assertEqual(listify(__UpperCAmelCase				) ,       [1, 2, 3, 4, 5]				)

					__UpperCAmelCase   :      Tuple										=     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]]				)

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





		def 							__A       (     self				)       ->     Tuple:

					'''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				)       ->     Dict:

					'''simple docstring'''
					__UpperCAmelCase   :      str										=     RegressionModel()
					__UpperCAmelCase   :      int										=     model.forward
					__UpperCAmelCase   :      Optional[Any]										=     convert_outputs_to_fpaa(model.forward				)
					__UpperCAmelCase   :      List[str]										=     extract_model_from_parallel(__UpperCAmelCase ,       keep_fpaa_wrapper=__UpperCAmelCase				)
					__UpperCAmelCase   :      Dict										=     pickle.dumps(__UpperCAmelCase				)





		@require_cuda
		def 							__A       (     self				)       ->     int:

					'''simple docstring'''
					__UpperCAmelCase   :      List[Any]										=     RegressionModel()
					__UpperCAmelCase   :      Dict										=     model.forward
					__UpperCAmelCase   :      Optional[Any]										=     torch.cuda.amp.autocast(dtype=torch.floataa				)(model.forward				)
					__UpperCAmelCase   :      List[str]										=     convert_outputs_to_fpaa(model.forward				)
					__UpperCAmelCase   :      Any										=     extract_model_from_parallel(__UpperCAmelCase ,       keep_fpaa_wrapper=__UpperCAmelCase				)
					__UpperCAmelCase   :      Tuple										=     pickle.dumps(__UpperCAmelCase				)





		@require_cuda
		@require_torch_min_version(version="""2.0"""				)
		def 							__A       (     self				)       ->     Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      List[str]										=     RegressionModel()
					__UpperCAmelCase   :      Dict										=     model.forward
					__UpperCAmelCase   :      List[str]										=     torch.cuda.amp.autocast(dtype=torch.floataa				)(model.forward				)
					__UpperCAmelCase   :      Optional[Any]										=     convert_outputs_to_fpaa(model.forward				)
					__UpperCAmelCase   :      Any										=     torch.compile(model.forward ,       backend="""inductor"""				)
					__UpperCAmelCase   :      Union[str, Any]										=     torch.randn(4 ,       10				).cuda()
					__UpperCAmelCase   :      Optional[int]										=     model(__UpperCAmelCase				)





		def 							__A       (     self				)       ->     List[str]:

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

					self.assertEqual(__UpperCAmelCase ,       __UpperCAmelCase				)





		@require_torch_min_version(version="""2.0"""				)
		def 							__A       (     self				)       ->     int:

					'''simple docstring'''
					__UpperCAmelCase   :      int										=     RegressionModel()
					__UpperCAmelCase   :      str										=     torch.compile(__UpperCAmelCase				)

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

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





		def 							__A       (     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'''
import torch

from transformers import AutoTokenizer



class _A  (    __SCREAMING_SNAKE_CASE    ):





		def __init__(     self ,       __UpperCAmelCase				)       ->     Union[str, Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      Any										=     AutoTokenizer.from_pretrained(__UpperCAmelCase				)





		def 							__A       (     self ,       __UpperCAmelCase				)       ->     Any:

					'''simple docstring'''

					if isinstance(__UpperCAmelCase ,       __UpperCAmelCase				) and all(isinstance(_x ,       __UpperCAmelCase				) for _x in x				):
								__UpperCAmelCase   :      Dict										=     None
								for l in x:
											__UpperCAmelCase   :      Dict										=     self.tokenizer(
											    __UpperCAmelCase ,       padding="""max_length""" ,       max_length=384 ,       truncation=__UpperCAmelCase ,       return_tensors="""pt""" ,       )
											__UpperCAmelCase   :      Union[str, Any]										=     torch.tensor([len(__UpperCAmelCase				)]				)
											if d is not None:
														for k in d.keys():
																	__UpperCAmelCase   :      Dict										=     torch.cat((d[k], t[k]) ,       0				)
											else:
														__UpperCAmelCase   :      List[str]										=     t

								__UpperCAmelCase   :      Union[str, Any]										=     torch.tensor(self.tokenizer.convert_tokens_to_ids("""[E]"""				)				)
								__UpperCAmelCase   :      Any										=     torch.tensor(self.tokenizer.convert_tokens_to_ids("""[/E]"""				)				)

					elif isinstance(__UpperCAmelCase ,       __UpperCAmelCase				) and all(isinstance(_x ,       __UpperCAmelCase				) for _x in x				):
								__UpperCAmelCase   :      int										=     self.tokenizer(
								    __UpperCAmelCase ,       padding="""max_length""" ,       max_length=384 ,       truncation=__UpperCAmelCase ,       return_tensors="""pt""" ,       )

					else:
								raise Exception(
								    """Type of parameter x was not recognized! Only `list of strings` for query or `list of lists of"""
								    """ strings` for supports are supported."""				)

					return d





		def 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase=0.70				)       ->     Dict:

					'''simple docstring'''
					__UpperCAmelCase   :      Tuple										=     []
					for idx in range(len(W_query["""input_ids"""]				)				):
								__UpperCAmelCase   :      Optional[int]										=     range(p_start.shape[1]				)

								__UpperCAmelCase   :      Union[str, Any]										=     []
								for start_id in start_indexes:
											for end_id in end_indexes:
														if start_id < end_id:
																	output.append(
																	    (
																	        start_id,
																	        end_id,
																	        p_start[idx][start_id].item(),
																	        p_end[idx][end_id].item(),
																	    )				)

								output.sort(key=lambda __UpperCAmelCase				: (tup[2] * tup[3]) ,       reverse=__UpperCAmelCase				)
								__UpperCAmelCase   :      Optional[Any]										=     []
								for k in range(len(__UpperCAmelCase				)				):
											if output[k][2] * output[k][3] >= thresh:
														__UpperCAmelCase				,  __UpperCAmelCase   :      Tuple										=     output[k][0], output[k][1]
														__UpperCAmelCase   :      int										=     self.tokenizer.decode(W_query["""input_ids"""][idx][c_start_pos:c_end_pos]				)
														temp.append((decoded, output[k][2] * output[k][3])				)

								final_outputs.append(__UpperCAmelCase				)

					return final_outputs




