


import datasets

from .evaluate import evaluate


UpperCamelCase								=					'\\n@article{hendrycks2021cuad,\n      title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},\n      author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},\n      journal={arXiv preprint arXiv:2103.06268},\n      year={2021}\n}\n'

UpperCamelCase								=					'\nThis metric wrap the official scoring script for version 1 of the Contract\nUnderstanding Atticus Dataset (CUAD).\nContract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510\ncommercial legal contracts that have been manually labeled to identify 41 categories of important\nclauses that lawyers look for when reviewing contracts in connection with corporate transactions.\n'

UpperCamelCase								=					'\nComputes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - \'id\': id of the question-answer pair as given in the references (see below)\n        - \'prediction_text\': list of possible texts for the answer, as a list of strings\n        depending on a threshold on the confidence probability of each prediction.\n    references: List of question-answers dictionaries with the following key-values:\n        - \'id\': id of the question-answer pair (see above),\n        - \'answers\': a Dict in the CUAD dataset format\n            {\n                \'text\': list of possible texts for the answer, as a list of strings\n                \'answer_start\': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    \'exact_match\': Exact match (the normalized answer exactly match the gold answer)\n    \'f1\': The F-score of predicted tokens versus the gold answer\n    \'aupr\': Area Under the Precision-Recall curve\n    \'prec_at_80_recall\': Precision at 80% recall\n    \'prec_at_90_recall\': Precision at 90% recall\nExamples:\n    >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n    >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]\n    >>> cuad_metric = datasets.load_metric("cuad")\n    >>> results = cuad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}\n'





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,    _KWARGS_DESCRIPTION   )
class 			__lowerCamelCase	(   datasets.Metric   ):




    """simple docstring"""

    def        a				(	self					:					Optional[Any]       )				->	Optional[int]:
         return datasets.MetricInfo(
             description=_DESCRIPTION       ,     citation=_CITATION       ,     inputs_description=_KWARGS_DESCRIPTION       ,     features=datasets.Features(
                 {
                     "predictions": {
                         "id": datasets.Value("string"       ),
                         "prediction_text": datasets.features.Sequence(datasets.Value("string"       )       ),
                     },
                     "references": {
                         "id": datasets.Value("string"       ),
                         "answers": datasets.features.Sequence(
                             {
                                 "text": datasets.Value("string"       ),
                                 "answer_start": datasets.Value("int32"       ),
                             }       ),
                     },
                 }       )       ,     codebase_urls=["https://www.atticusprojectai.org/cuad"]       ,     reference_urls=["https://www.atticusprojectai.org/cuad"]       ,     )


    def        a				(	self					:					Any       ,     SCREAMING_SNAKE_CASE__					:					Any       ,     SCREAMING_SNAKE_CASE__					:					List[Any]       )				->	Union[str, Any]:
         lowerCAmelCase__          =			{prediction["id"]: prediction["prediction_text"] for prediction in predictions}
         lowerCAmelCase__          =			[
             {
                 "paragraphs": [
                     {
                         "qas": [
                             {
                                 "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
                                 "id": ref["id"],
                             }
                             for ref in references
                         ]
                     }
                 ]
             }
         ]
         lowerCAmelCase__          =			evaluate(dataset=SCREAMING_SNAKE_CASE__       ,     predictions=SCREAMING_SNAKE_CASE__       )
         return score






import inspect
import unittest

import torch
import torch.nn as nn

from accelerate.hooks import (
    AlignDevicesHook,
    ModelHook,
    SequentialHook,
    add_hook_to_module,
    attach_align_device_hook,
    remove_hook_from_module,
    remove_hook_from_submodules,
)
from accelerate.test_utils import require_multi_gpu





class 			__lowerCamelCase	(   nn.Module   ):




    """simple docstring"""

    def __init__(	self					:					Dict       )				->	Optional[int]:
         super().__init__()
         lowerCAmelCase__          =			nn.Linear(3       ,     4       )
         lowerCAmelCase__          =			nn.BatchNormad(4       )
         lowerCAmelCase__          =			nn.Linear(4       ,     5       )


    def        a				(	self					:					Union[str, Any]       ,     SCREAMING_SNAKE_CASE__					:					int       )				->	List[Any]:
         return self.lineara(self.batchnorm(self.lineara(SCREAMING_SNAKE_CASE__       )       )       )






class 			__lowerCamelCase	(   UpperCamelCase__   ):




    """simple docstring"""

    def        a				(	self					:					Any       ,     SCREAMING_SNAKE_CASE__					:					str       ,     *SCREAMING_SNAKE_CASE__					:					Tuple       ,     **SCREAMING_SNAKE_CASE__					:					Any       )				->	Union[str, Any]:
         return (args[0] + 1,) + args[1:], kwargs






class 			__lowerCamelCase	(   UpperCamelCase__   ):




    """simple docstring"""

    def        a				(	self					:					List[Any]       ,     SCREAMING_SNAKE_CASE__					:					Tuple       ,     SCREAMING_SNAKE_CASE__					:					str       )				->	Dict:
         return output + 1






class 			__lowerCamelCase	(   unittest.TestCase   ):




    """simple docstring"""

    def        a				(	self					:					List[str]       )				->	Tuple:
         lowerCAmelCase__          =			ModelForTest()
         lowerCAmelCase__          =			ModelHook()

         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         self.assertEqual(test_model._hf_hook       ,     SCREAMING_SNAKE_CASE__       )
         self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,     "_old_forward"       )       )

         # Check adding the hook did not change the name or the signature
         self.assertEqual(test_model.forward.__name__       ,     "forward"       )
         self.assertListEqual(list(inspect.signature(test_model.forward       ).parameters       )       ,     ["x"]       )

         remove_hook_from_module(SCREAMING_SNAKE_CASE__       )
         self.assertFalse(hasattr(SCREAMING_SNAKE_CASE__       ,     "_hf_hook"       )       )
         self.assertFalse(hasattr(SCREAMING_SNAKE_CASE__       ,     "_old_forward"       )       )


    def        a				(	self					:					Union[str, Any]       )				->	int:
         lowerCAmelCase__          =			ModelForTest()
         lowerCAmelCase__          =			ModelHook()

         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       ,     append=SCREAMING_SNAKE_CASE__       )

         self.assertEqual(isinstance(test_model._hf_hook       ,     SCREAMING_SNAKE_CASE__       )       ,     SCREAMING_SNAKE_CASE__       )
         self.assertEqual(len(test_model._hf_hook.hooks       )       ,     2       )
         self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__       ,     "_old_forward"       )       )

         # Check adding the hook did not change the name or the signature
         self.assertEqual(test_model.forward.__name__       ,     "forward"       )
         self.assertListEqual(list(inspect.signature(test_model.forward       ).parameters       )       ,     ["x"]       )

         remove_hook_from_module(SCREAMING_SNAKE_CASE__       )
         self.assertFalse(hasattr(SCREAMING_SNAKE_CASE__       ,     "_hf_hook"       )       )
         self.assertFalse(hasattr(SCREAMING_SNAKE_CASE__       ,     "_old_forward"       )       )


    def        a				(	self					:					List[str]       )				->	Any:
         lowerCAmelCase__          =			ModelForTest()
         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			test_model(x + 1       )
         lowerCAmelCase__          =			test_model(x + 2       )

         lowerCAmelCase__          =			PreForwardHook()
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       ,     atol=1e-5       )       )

         # Attaching a hook to a model when it already has one replaces, does not chain
         lowerCAmelCase__          =			PreForwardHook()
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       ,     atol=1e-5       )       )

         # You need to use the sequential hook to chain two or more hooks
         lowerCAmelCase__          =			SequentialHook(PreForwardHook()       ,     PreForwardHook()       )
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         assert torch.allclose(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       ,     atol=1e-5       )


    def        a				(	self					:					Any       )				->	Union[str, Any]:
         lowerCAmelCase__          =			ModelForTest()
         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			PostForwardHook()
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__       ,     output + 1       ,     atol=1e-5       )       )

         # Attaching a hook to a model when it already has one replaces, does not chain
         lowerCAmelCase__          =			PostForwardHook()
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__       ,     output + 1       ,     atol=1e-5       )       )

         # You need to use the sequential hook to chain two or more hooks
         lowerCAmelCase__          =			SequentialHook(PostForwardHook()       ,     PostForwardHook()       )
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         assert torch.allclose(SCREAMING_SNAKE_CASE__       ,     output + 2       ,     atol=1e-5       )


    def        a				(	self					:					Optional[int]       )				->	int:
         lowerCAmelCase__          =			ModelForTest()
         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			PostForwardHook()
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       )
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__       ,     output + 1       )       )
         self.assertTrue(outputa.requires_grad       )

         lowerCAmelCase__          =			True
         lowerCAmelCase__          =			test_model(SCREAMING_SNAKE_CASE__       )
         self.assertFalse(outputa.requires_grad       )


    @require_multi_gpu
    def        a				(	self					:					Optional[Any]       )				->	List[str]:
         lowerCAmelCase__          =			ModelForTest()
         # Everything is on CPU
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # This will move each submodule on different devices
         add_hook_to_module(model.lineara       ,     AlignDevicesHook(execution_device=0       )       )
         add_hook_to_module(model.batchnorm       ,     AlignDevicesHook(execution_device=0       )       )
         add_hook_to_module(model.lineara       ,     AlignDevicesHook(execution_device=1       )       )

         self.assertEqual(model.lineara.weight.device       ,     torch.device(0       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device(0       )       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     torch.device(0       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device(1       )       )

         # We can still make a forward pass. The input does not need to be on any particular device
         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     torch.device(1       )       )

         # We can add a general hook to put back output on same device as input.
         add_hook_to_module(SCREAMING_SNAKE_CASE__       ,     AlignDevicesHook(io_same_device=SCREAMING_SNAKE_CASE__       )       )
         lowerCAmelCase__          =			torch.randn(2       ,     3       ).to(0       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     torch.device(0       )       )


    def        a				(	self					:					List[str]       )				->	List[str]:
         lowerCAmelCase__          =			ModelForTest()

         # Everything is on CPU
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # This will move each submodule on different devices
         lowerCAmelCase__          =			{"execution_device": 0 if torch.cuda.is_available() else "cpu", "offload": True}

         add_hook_to_module(model.lineara       ,     AlignDevicesHook(**SCREAMING_SNAKE_CASE__       )       )
         add_hook_to_module(model.batchnorm       ,     AlignDevicesHook(**SCREAMING_SNAKE_CASE__       )       )
         add_hook_to_module(model.lineara       ,     AlignDevicesHook(**SCREAMING_SNAKE_CASE__       )       )

         # Parameters have been offloaded, so on the meta device
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         # Buffers are not included in the offload by default, so are on the execution device
         lowerCAmelCase__          =			torch.device(hook_kwargs["execution_device"]       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     SCREAMING_SNAKE_CASE__       )

         # Removing hooks loads back the weights in the model.
         remove_hook_from_module(model.lineara       )
         remove_hook_from_module(model.batchnorm       )
         remove_hook_from_module(model.lineara       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # Now test with buffers included in the offload
         lowerCAmelCase__          =			{
             "execution_device": 0 if torch.cuda.is_available() else "cpu",
             "offload": True,
             "offload_buffers": True,
         }

         add_hook_to_module(model.lineara       ,     AlignDevicesHook(**SCREAMING_SNAKE_CASE__       )       )
         add_hook_to_module(model.batchnorm       ,     AlignDevicesHook(**SCREAMING_SNAKE_CASE__       )       )
         add_hook_to_module(model.lineara       ,     AlignDevicesHook(**SCREAMING_SNAKE_CASE__       )       )

         # Parameters have been offloaded, so on the meta device, buffers included
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     torch.device("meta"       )       )

         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     SCREAMING_SNAKE_CASE__       )

         # Removing hooks loads back the weights in the model.
         remove_hook_from_module(model.lineara       )
         remove_hook_from_module(model.batchnorm       )
         remove_hook_from_module(model.lineara       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )


    def        a				(	self					:					Optional[int]       )				->	Union[str, Any]:
         lowerCAmelCase__          =			ModelForTest()

         # Everything is on CPU
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # This will move each submodule on different devices
         lowerCAmelCase__          =			0 if torch.cuda.is_available() else "cpu"
         attach_align_device_hook(SCREAMING_SNAKE_CASE__       ,     execution_device=SCREAMING_SNAKE_CASE__       ,     offload=SCREAMING_SNAKE_CASE__       )

         # Parameters have been offloaded, so on the meta device
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         # Buffers are not included in the offload by default, so are on the execution device
         lowerCAmelCase__          =			torch.device(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     SCREAMING_SNAKE_CASE__       )

         # Removing hooks loads back the weights in the model.
         remove_hook_from_submodules(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # Now test with buffers included in the offload
         attach_align_device_hook(SCREAMING_SNAKE_CASE__       ,     execution_device=SCREAMING_SNAKE_CASE__       ,     offload=SCREAMING_SNAKE_CASE__       ,     offload_buffers=SCREAMING_SNAKE_CASE__       )

         # Parameters have been offloaded, so on the meta device, buffers included
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     torch.device("meta"       )       )

         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     SCREAMING_SNAKE_CASE__       )

         # Removing hooks loads back the weights in the model.
         remove_hook_from_submodules(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )


    def        a				(	self					:					Optional[Any]       )				->	str:
         lowerCAmelCase__          =			ModelForTest()

         # Everything is on CPU
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # This will move each submodule on different devices
         lowerCAmelCase__          =			0 if torch.cuda.is_available() else "cpu"
         attach_align_device_hook(
             SCREAMING_SNAKE_CASE__       ,     execution_device=SCREAMING_SNAKE_CASE__       ,     offload=SCREAMING_SNAKE_CASE__       ,     weights_map=model.state_dict()       )

         # Parameters have been offloaded, so on the meta device
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         # Buffers are not included in the offload by default, so are on the execution device
         lowerCAmelCase__          =			torch.device(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     SCREAMING_SNAKE_CASE__       )

         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     SCREAMING_SNAKE_CASE__       )

         # Removing hooks loads back the weights in the model.
         remove_hook_from_submodules(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )

         # Now test with buffers included in the offload
         attach_align_device_hook(
             SCREAMING_SNAKE_CASE__       ,     execution_device=SCREAMING_SNAKE_CASE__       ,     offload=SCREAMING_SNAKE_CASE__       ,     weights_map=model.state_dict()       ,     offload_buffers=SCREAMING_SNAKE_CASE__       ,     )

         # Parameters have been offloaded, so on the meta device, buffers included
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("meta"       )       )
         self.assertEqual(model.batchnorm.running_mean.device       ,     torch.device("meta"       )       )

         lowerCAmelCase__          =			torch.randn(2       ,     3       )
         lowerCAmelCase__          =			model(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(output.device       ,     SCREAMING_SNAKE_CASE__       )

         # Removing hooks loads back the weights in the model.
         remove_hook_from_submodules(SCREAMING_SNAKE_CASE__       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.batchnorm.weight.device       ,     torch.device("cpu"       )       )
         self.assertEqual(model.lineara.weight.device       ,     torch.device("cpu"       )       )
