


"""simple docstring"""


import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import ImageGPTImageProcessor


class 			A__	(						unittest.TestCase):


     """simple docstring"""

     def __init__(	self:						Dict     ,		__a:						Optional[Any]     ,		__a:						Any=7     ,		__a:						List[Any]=3     ,		__a:						str=18     ,		__a:						List[str]=30     ,		__a:						Tuple=400     ,		__a:						str=True     ,		__a:						Any=None     ,		__a:						Optional[int]=True     ,		)->    Dict:
           lowerCamelCase					:					Union[str, Any]        =	size if size is not None else {"""height""": 18, """width""": 18}
           lowerCamelCase					:					List[Any]        =	parent
           lowerCamelCase					:					Optional[int]        =	batch_size
           lowerCamelCase					:					Union[str, Any]        =	num_channels
           lowerCamelCase					:					List[Any]        =	image_size
           lowerCamelCase					:					Optional[Any]        =	min_resolution
           lowerCamelCase					:					str        =	max_resolution
           lowerCamelCase					:					int        =	do_resize
           lowerCamelCase					:					Union[str, Any]        =	size
           lowerCamelCase					:					Any        =	do_normalize







     def 		a__						(	self:						Dict	)->    Dict:
           return {
               # here we create 2 clusters for the sake of simplicity
               "clusters": np.asarray(
                   [
                       [0.88_66_44_36_34_03_32_03, 0.66_18_82_93_69_54_49_83, 0.38_91_74_64_01_78_68_04],
                       [-0.60_42_55_91_46_88_11_04, -0.0_22_95_00_88_60_52_84_69, 0.54_23_79_73_69_00_32_96],
                   ]	),
               "do_resize": self.do_resize,
               "size": self.size,
               "do_normalize": self.do_normalize,
           }


@require_torch
@require_vision
class 			A__	(						lowerCAmelCase__ ,     unittest.TestCase):


     """simple docstring"""
     snake_case__	:					Any													=ImageGPTImageProcessor if is_vision_available() else None

     def 		a__						(	self:						List[str]	)->    str:
           lowerCamelCase					:					Union[str, Any]        =	ImageGPTImageProcessingTester(self	)

     @property
     def 		a__						(	self:						Optional[int]	)->    List[str]:
           return self.image_processor_tester.prepare_image_processor_dict()

     def 		a__						(	self:						Optional[int]	)->    int:
           lowerCamelCase					:					Any        =	self.image_processing_class(**self.image_processor_dict	)
           self.assertTrue(hasattr(_lowerCamelCase     ,		"""clusters"""	)	)
           self.assertTrue(hasattr(_lowerCamelCase     ,		"""do_resize"""	)	)
           self.assertTrue(hasattr(_lowerCamelCase     ,		"""size"""	)	)
           self.assertTrue(hasattr(_lowerCamelCase     ,		"""do_normalize"""	)	)

     def 		a__						(	self:						Optional[Any]	)->    Optional[int]:
           lowerCamelCase					:					Union[str, Any]        =	self.image_processing_class.from_dict(self.image_processor_dict	)
           self.assertEqual(image_processor.size     ,		{"""height""": 18, """width""": 18}	)

           lowerCamelCase					:					Union[str, Any]        =	self.image_processing_class.from_dict(self.image_processor_dict     ,		size=42	)
           self.assertEqual(image_processor.size     ,		{"""height""": 42, """width""": 42}	)

     def 		a__						(	self:						List[Any]	)->    int:
           lowerCamelCase					:					Dict        =	self.image_processing_class(**self.image_processor_dict	)
           lowerCamelCase					:					Tuple        =	json.loads(image_processor.to_json_string()	)
           for key, value in self.image_processor_dict.items():
                 if key == "clusters":
                       self.assertTrue(np.array_equal(_lowerCamelCase     ,		obj[key]	)	)
                 else:
                       self.assertEqual(obj[key]     ,		_lowerCamelCase	)

     def 		a__						(	self:						Dict	)->    List[str]:
           lowerCamelCase					:					Union[str, Any]        =	self.image_processing_class(**self.image_processor_dict	)

           with tempfile.TemporaryDirectory() as tmpdirname:
                 lowerCamelCase					:					List[str]        =	os.path.join(_lowerCamelCase     ,		"""image_processor.json"""	)
                 image_processor_first.to_json_file(_lowerCamelCase	)
                 lowerCamelCase					:					Tuple        =	self.image_processing_class.from_json_file(_lowerCamelCase	).to_dict()

           lowerCamelCase					:					str        =	image_processor_first.to_dict()
           for key, value in image_processor_first.items():
                 if key == "clusters":
                       self.assertTrue(np.array_equal(_lowerCamelCase     ,		image_processor_second[key]	)	)
                 else:
                       self.assertEqual(image_processor_first[key]     ,		_lowerCamelCase	)

     def 		a__						(	self:						Tuple	)->    Optional[Any]:
           lowerCamelCase					:					List[str]        =	self.image_processing_class(**self.image_processor_dict	)

           with tempfile.TemporaryDirectory() as tmpdirname:
                 image_processor_first.save_pretrained(_lowerCamelCase	)
                 lowerCamelCase					:					List[Any]        =	self.image_processing_class.from_pretrained(_lowerCamelCase	).to_dict()

           lowerCamelCase					:					Union[str, Any]        =	image_processor_first.to_dict()
           for key, value in image_processor_first.items():
                 if key == "clusters":
                       self.assertTrue(np.array_equal(_lowerCamelCase     ,		image_processor_second[key]	)	)
                 else:
                       self.assertEqual(image_processor_first[key]     ,		_lowerCamelCase	)







     @unittest.skip("""ImageGPT requires clusters at initialization"""	)
     def 		a__						(	self:						Optional[int]	)->    List[str]:
           pass




def   snake_case		(     )   ->      Optional[int]:
      lowerCamelCase					:					Optional[int]        =	load_dataset("""hf-internal-testing/fixtures_image_utils"""			,				split="""test"""    )

      lowerCamelCase					:					Any        =	Image.open(dataset[4]["""file"""]    )
      lowerCamelCase					:					Optional[int]        =	Image.open(dataset[5]["""file"""]    )

      lowerCamelCase					:					List[str]        =	[imagea, imagea]

      return images






@require_vision
@require_torch
class 			A__	(						unittest.TestCase):


     """simple docstring"""

     @slow
     def 		a__						(	self:						Optional[int]	)->    List[Any]:
           lowerCamelCase					:					Optional[Any]        =	ImageGPTImageProcessor.from_pretrained("""openai/imagegpt-small"""	)

           lowerCamelCase					:					Tuple        =	prepare_images()

           # test non-batched
           lowerCamelCase					:					Any        =	image_processing(images[0]     ,		return_tensors="""pt"""	)

           self.assertIsInstance(encoding.input_ids     ,		torch.LongTensor	)
           self.assertEqual(encoding.input_ids.shape     ,		(1, 1_024)	)

           lowerCamelCase					:					Any        =	[306, 191, 191]
           self.assertEqual(encoding.input_ids[0, :3].tolist()     ,		_lowerCamelCase	)

           # test batched
           lowerCamelCase					:					Union[str, Any]        =	image_processing(_lowerCamelCase     ,		return_tensors="""pt"""	)

           self.assertIsInstance(encoding.input_ids     ,		torch.LongTensor	)
           self.assertEqual(encoding.input_ids.shape     ,		(2, 1_024)	)

           lowerCamelCase					:					Tuple        =	[303, 13, 13]
           self.assertEqual(encoding.input_ids[1, -3:].tolist()     ,		_lowerCamelCase	)








"""simple docstring"""


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 			A__	(						nn.Module):


     """simple docstring"""

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







     def 		a__						(	self:						List[str]     ,		__a:						List[Any]	)->    Optional[Any]:
           return self.lineara(self.batchnorm(self.lineara(__a	)	)	)


class 			A__	(						__lowercase):


     """simple docstring"""

     def 		a__						(	self:						Tuple     ,		__a:						int     ,		*__a:						Any     ,		**__a:						Tuple	)->    Tuple:
           return (args[0] + 1,) + args[1:], kwargs


class 			A__	(						__lowercase):


     """simple docstring"""

     def 		a__						(	self:						Optional[int]     ,		__a:						List[str]     ,		__a:						List[Any]	)->    List[str]:
           return output + 1


class 			A__	(						unittest.TestCase):


     """simple docstring"""

     def 		a__						(	self:						int	)->    str:
           lowerCamelCase					:					List[str]        =	ModelForTest()
           lowerCamelCase					:					Dict        =	ModelHook()

           add_hook_to_module(__a     ,		__a	)
           self.assertEqual(test_model._hf_hook     ,		__a	)
           self.assertTrue(hasattr(__a     ,		"""_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(__a	)
           self.assertFalse(hasattr(__a     ,		"""_hf_hook"""	)	)
           self.assertFalse(hasattr(__a     ,		"""_old_forward"""	)	)

     def 		a__						(	self:						int	)->    str:
           lowerCamelCase					:					List[str]        =	ModelForTest()
           lowerCamelCase					:					Union[str, Any]        =	ModelHook()

           add_hook_to_module(__a     ,		__a	)
           add_hook_to_module(__a     ,		__a     ,		append=__a	)

           self.assertEqual(isinstance(test_model._hf_hook     ,		__a	)     ,		__a	)
           self.assertEqual(len(test_model._hf_hook.hooks	)     ,		2	)
           self.assertTrue(hasattr(__a     ,		"""_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(__a	)
           self.assertFalse(hasattr(__a     ,		"""_hf_hook"""	)	)
           self.assertFalse(hasattr(__a     ,		"""_old_forward"""	)	)

     def 		a__						(	self:						List[Any]	)->    List[str]:
           lowerCamelCase					:					str        =	ModelForTest()
           lowerCamelCase					:					Dict        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					Union[str, Any]        =	test_model(x + 1	)
           lowerCamelCase					:					Optional[int]        =	test_model(x + 2	)

           lowerCamelCase					:					List[Any]        =	PreForwardHook()
           add_hook_to_module(__a     ,		__a	)
           lowerCamelCase					:					Optional[int]        =	test_model(__a	)
           self.assertTrue(torch.allclose(__a     ,		__a     ,		atol=1e-5	)	)

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

           # You need to use the sequential hook to chain two or more hooks
           lowerCamelCase					:					Any        =	SequentialHook(PreForwardHook()     ,		PreForwardHook()	)
           add_hook_to_module(__a     ,		__a	)

           lowerCamelCase					:					Optional[Any]        =	test_model(__a	)
           assert torch.allclose(__a     ,		__a     ,		atol=1e-5	)

     def 		a__						(	self:						Any	)->    Optional[int]:
           lowerCamelCase					:					str        =	ModelForTest()
           lowerCamelCase					:					List[str]        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					int        =	test_model(__a	)

           lowerCamelCase					:					Dict        =	PostForwardHook()
           add_hook_to_module(__a     ,		__a	)
           lowerCamelCase					:					Tuple        =	test_model(__a	)
           self.assertTrue(torch.allclose(__a     ,		output + 1     ,		atol=1e-5	)	)

           # Attaching a hook to a model when it already has one replaces, does not chain
           lowerCamelCase					:					str        =	PostForwardHook()
           add_hook_to_module(__a     ,		__a	)
           lowerCamelCase					:					Optional[Any]        =	test_model(__a	)
           self.assertTrue(torch.allclose(__a     ,		output + 1     ,		atol=1e-5	)	)

           # You need to use the sequential hook to chain two or more hooks
           lowerCamelCase					:					Union[str, Any]        =	SequentialHook(PostForwardHook()     ,		PostForwardHook()	)
           add_hook_to_module(__a     ,		__a	)

           lowerCamelCase					:					str        =	test_model(__a	)
           assert torch.allclose(__a     ,		output + 2     ,		atol=1e-5	)

     def 		a__						(	self:						int	)->    Dict:
           lowerCamelCase					:					List[Any]        =	ModelForTest()
           lowerCamelCase					:					Optional[int]        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					List[str]        =	test_model(__a	)

           lowerCamelCase					:					Any        =	PostForwardHook()
           add_hook_to_module(__a     ,		__a	)
           lowerCamelCase					:					str        =	test_model(__a	)
           self.assertTrue(torch.allclose(__a     ,		output + 1	)	)
           self.assertTrue(outputa.requires_grad	)

           lowerCamelCase					:					Optional[int]        =	True
           lowerCamelCase					:					Optional[int]        =	test_model(__a	)
           self.assertFalse(outputa.requires_grad	)

     @require_multi_gpu
     def 		a__						(	self:						List[str]	)->    Union[str, Any]:
           lowerCamelCase					:					int        =	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					:					str        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					Dict        =	model(__a	)
           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(__a     ,		AlignDevicesHook(io_same_device=__a	)	)
           lowerCamelCase					:					Optional[int]        =	torch.randn(2     ,		3	).to(0	)
           lowerCamelCase					:					str        =	model(__a	)
           self.assertEqual(output.device     ,		torch.device(0	)	)

     def 		a__						(	self:						List[str]	)->    Tuple:
           lowerCamelCase					:					Union[str, Any]        =	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					:					Tuple        =	{"""execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True}

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

           # 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					:					List[Any]        =	torch.device(hook_kwargs["""execution_device"""]	)
           self.assertEqual(model.batchnorm.running_mean.device     ,		__a	)

           lowerCamelCase					:					Optional[Any]        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					Optional[Any]        =	model(__a	)
           self.assertEqual(output.device     ,		__a	)

           # 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					:					Any        =	{
               """execution_device""": 0 if torch.cuda.is_available() else """cpu""",
               """offload""": True,
               """offload_buffers""": True,
           }

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

           # 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					:					int        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					Optional[int]        =	model(__a	)
           self.assertEqual(output.device     ,		__a	)

           # 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:						Any	)->    List[str]:
           lowerCamelCase					:					int        =	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					:					int        =	0 if torch.cuda.is_available() else """cpu"""
           attach_align_device_hook(__a     ,		execution_device=__a     ,		offload=__a	)

           # 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					:					List[Any]        =	torch.device(__a	)
           self.assertEqual(model.batchnorm.running_mean.device     ,		__a	)

           lowerCamelCase					:					Dict        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					Optional[Any]        =	model(__a	)
           self.assertEqual(output.device     ,		__a	)

           # Removing hooks loads back the weights in the model.
           remove_hook_from_submodules(__a	)
           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(__a     ,		execution_device=__a     ,		offload=__a     ,		offload_buffers=__a	)

           # 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					:					Optional[int]        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					int        =	model(__a	)
           self.assertEqual(output.device     ,		__a	)

           # Removing hooks loads back the weights in the model.
           remove_hook_from_submodules(__a	)
           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]	)->    List[Any]:
           lowerCamelCase					:					List[Any]        =	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					:					Any        =	0 if torch.cuda.is_available() else """cpu"""
           attach_align_device_hook(
               __a     ,		execution_device=__a     ,		offload=__a     ,		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					:					List[Any]        =	torch.device(__a	)
           self.assertEqual(model.batchnorm.running_mean.device     ,		__a	)

           lowerCamelCase					:					Dict        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					int        =	model(__a	)
           self.assertEqual(output.device     ,		__a	)

           # Removing hooks loads back the weights in the model.
           remove_hook_from_submodules(__a	)
           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(
               __a     ,		execution_device=__a     ,		offload=__a     ,		weights_map=model.state_dict()     ,		offload_buffers=__a     ,		)

           # 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					:					Tuple        =	torch.randn(2     ,		3	)
           lowerCamelCase					:					Any        =	model(__a	)
           self.assertEqual(output.device     ,		__a	)

           # Removing hooks loads back the weights in the model.
           remove_hook_from_submodules(__a	)
           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"""	)	)




