



import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor






@require_tokenizers
@require_vision
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    def       _lowerCAmelCase (	self		):
        A			:	Any										=       tempfile.mkdtemp()

        # fmt: off
        A			:	List[str]										=       ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
        # fmt: on
        A			:	Any										=       os.path.join(self.tmpdirname,		VOCAB_FILES_NAMES["""vocab_file"""]		)
        with open(self.vocab_file,		"""w""",		encoding="""utf-8"""		) as vocab_writer:
            vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]		)		)

        A			:	List[str]										=       {
            'do_resize': True,
            'size': {'height': 18, 'width': 18},
            'do_normalize': True,
            'image_mean': [0.5, 0.5, 0.5],
            'image_std': [0.5, 0.5, 0.5],
        }
        A			:	Optional[Any]										=       os.path.join(self.tmpdirname,		__a		)
        with open(self.image_processor_file,		"""w""",		encoding="""utf-8"""		) as fp:
            json.dump(__a,		__a		)






    def       _lowerCAmelCase (	self,		**lowerCamelCase__		):
        return BertTokenizer.from_pretrained(self.tmpdirname,		**__a		)






    def       _lowerCAmelCase (	self,		**lowerCamelCase__		):
        return ViTImageProcessor.from_pretrained(self.tmpdirname,		**__a		)






    def       _lowerCAmelCase (	self		):
        shutil.rmtree(self.tmpdirname		)






    def       _lowerCAmelCase (	self		):
        A			:	Optional[Any]										=       [np.random.randint(255,		size=(3, 30, 400),		dtype=np.uinta		)]

        A			:	List[str]										=       [Image.fromarray(np.moveaxis(__a,		0,		-1		)		) for x in image_inputs]

        return image_inputs






    def       _lowerCAmelCase (	self		):
        A			:	Union[str, Any]										=       self.get_tokenizer()
        A			:	Dict										=       self.get_image_processor()

        A			:	Any										=       VisionTextDualEncoderProcessor(tokenizer=__a,		image_processor=__a		)

        processor.save_pretrained(self.tmpdirname		)
        A			:	Any										=       VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname		)

        self.assertEqual(processor.tokenizer.get_vocab(),		tokenizer.get_vocab()		)
        self.assertIsInstance(processor.tokenizer,		(BertTokenizer, BertTokenizerFast)		)

        self.assertEqual(processor.image_processor.to_json_string(),		image_processor.to_json_string()		)
        self.assertIsInstance(processor.image_processor,		__a		)






    def       _lowerCAmelCase (	self		):
        A			:	Optional[Any]										=       VisionTextDualEncoderProcessor(
            tokenizer=self.get_tokenizer(),		image_processor=self.get_image_processor()		)
        processor.save_pretrained(self.tmpdirname		)

        A			:	Optional[Any]										=       self.get_tokenizer(bos_token="""(BOS)""",		eos_token="""(EOS)"""		)
        A			:	Tuple										=       self.get_image_processor(do_normalize=__a,		padding_value=1.0		)

        A			:	Union[str, Any]										=       VisionTextDualEncoderProcessor.from_pretrained(
            self.tmpdirname,		bos_token="""(BOS)""",		eos_token="""(EOS)""",		do_normalize=__a,		padding_value=1.0		)

        self.assertEqual(processor.tokenizer.get_vocab(),		tokenizer_add_kwargs.get_vocab()		)
        self.assertIsInstance(processor.tokenizer,		(BertTokenizer, BertTokenizerFast)		)

        self.assertEqual(processor.image_processor.to_json_string(),		image_processor_add_kwargs.to_json_string()		)
        self.assertIsInstance(processor.image_processor,		__a		)






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       self.get_image_processor()
        A			:	int										=       self.get_tokenizer()

        A			:	str										=       VisionTextDualEncoderProcessor(tokenizer=__a,		image_processor=__a		)

        A			:	int										=       self.prepare_image_inputs()

        A			:	List[str]										=       image_processor(__a,		return_tensors="""np"""		)
        A			:	List[str]										=       processor(images=__a,		return_tensors="""np"""		)

        for key in input_feat_extract.keys():
            self.assertAlmostEqual(input_feat_extract[key].sum(),		input_processor[key].sum(),		delta=1e-2		)






    def       _lowerCAmelCase (	self		):
        A			:	Dict										=       self.get_image_processor()
        A			:	int										=       self.get_tokenizer()

        A			:	Union[str, Any]										=       VisionTextDualEncoderProcessor(tokenizer=__a,		image_processor=__a		)

        A			:	Optional[int]										=       'lower newer'

        A			:	Dict										=       processor(text=__a		)

        A			:	List[Any]										=       tokenizer(__a		)

        for key in encoded_tok.keys():
            self.assertListEqual(encoded_tok[key],		encoded_processor[key]		)






    def       _lowerCAmelCase (	self		):
        A			:	Dict										=       self.get_image_processor()
        A			:	Union[str, Any]										=       self.get_tokenizer()

        A			:	int										=       VisionTextDualEncoderProcessor(tokenizer=__a,		image_processor=__a		)

        A			:	List[Any]										=       'lower newer'
        A			:	Optional[Any]										=       self.prepare_image_inputs()

        A			:	Union[str, Any]										=       processor(text=__a,		images=__a		)

        self.assertListEqual(list(inputs.keys()		),		["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]		)

        # test if it raises when no input is passed
        with self.assertRaises(__a		):
            processor()






    def       _lowerCAmelCase (	self		):
        A			:	Union[str, Any]										=       self.get_image_processor()
        A			:	Any										=       self.get_tokenizer()

        A			:	Dict										=       VisionTextDualEncoderProcessor(tokenizer=__a,		image_processor=__a		)

        A			:	int										=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        A			:	int										=       processor.batch_decode(__a		)
        A			:	Optional[Any]										=       tokenizer.batch_decode(__a		)

        self.assertListEqual(__a,		__a		)






    def       _lowerCAmelCase (	self		):
        A			:	List[str]										=       self.get_image_processor()
        A			:	Dict										=       self.get_tokenizer()

        A			:	Dict										=       VisionTextDualEncoderProcessor(tokenizer=__a,		image_processor=__a		)

        A			:	Union[str, Any]										=       'lower newer'
        A			:	Tuple										=       self.prepare_image_inputs()

        A			:	Union[str, Any]										=       processor(text=__a,		images=__a		)

        self.assertListEqual(list(inputs.keys()		),		processor.model_input_names		)







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




    '''simple docstring'''






    def __init__(	self		):
        super().__init__()
        A			:	Union[str, Any]										=       nn.Linear(3,		4		)
        A			:	Union[str, Any]										=       nn.BatchNormad(4		)
        A			:	Optional[Any]										=       nn.Linear(4,		5		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        return self.lineara(self.batchnorm(self.lineara(lowerCamelCase__		)		)		)






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		*lowerCamelCase__,		**lowerCamelCase__		):
        return (args[0] + 1,) + args[1:], kwargs






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__		):
        return output + 1






class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       ModelForTest()
        A			:	Any										=       ModelHook()

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






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       ModelForTest()
        A			:	Optional[int]										=       ModelHook()

        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__		)
        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__,		append=lowerCamelCase__		)

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






    def       _lowerCAmelCase (	self		):
        A			:	Any										=       ModelForTest()
        A			:	Tuple										=       torch.randn(2,		3		)
        A			:	Optional[int]										=       test_model(x + 1		)
        A			:	List[str]										=       test_model(x + 2		)

        A			:	List[str]										=       PreForwardHook()
        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__		)
        A			:	List[str]										=       test_model(lowerCamelCase__		)
        self.assertTrue(torch.allclose(lowerCamelCase__,		lowerCamelCase__,		atol=1e-5		)		)

        # Attaching a hook to a model when it already has one replaces, does not chain
        A			:	Optional[Any]										=       PreForwardHook()
        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__		)
        A			:	Tuple										=       test_model(lowerCamelCase__		)
        self.assertTrue(torch.allclose(lowerCamelCase__,		lowerCamelCase__,		atol=1e-5		)		)

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

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






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       ModelForTest()
        A			:	Any										=       torch.randn(2,		3		)
        A			:	Any										=       test_model(lowerCamelCase__		)

        A			:	List[Any]										=       PostForwardHook()
        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__		)
        A			:	str										=       test_model(lowerCamelCase__		)
        self.assertTrue(torch.allclose(lowerCamelCase__,		output + 1,		atol=1e-5		)		)

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

        # You need to use the sequential hook to chain two or more hooks
        A			:	Dict										=       SequentialHook(PostForwardHook(),		PostForwardHook()		)
        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__		)

        A			:	List[str]										=       test_model(lowerCamelCase__		)
        assert torch.allclose(lowerCamelCase__,		output + 2,		atol=1e-5		)






    def       _lowerCAmelCase (	self		):
        A			:	List[Any]										=       ModelForTest()
        A			:	Tuple										=       torch.randn(2,		3		)
        A			:	Union[str, Any]										=       test_model(lowerCamelCase__		)

        A			:	List[Any]										=       PostForwardHook()
        add_hook_to_module(lowerCamelCase__,		lowerCamelCase__		)
        A			:	List[Any]										=       test_model(lowerCamelCase__		)
        self.assertTrue(torch.allclose(lowerCamelCase__,		output + 1		)		)
        self.assertTrue(outputa.requires_grad		)

        A			:	int										=       True
        A			:	Tuple										=       test_model(lowerCamelCase__		)
        self.assertFalse(outputa.requires_grad		)






    @require_multi_gpu
    def       _lowerCAmelCase (	self		):
        A			:	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
        A			:	str										=       torch.randn(2,		3		)
        A			:	int										=       model(lowerCamelCase__		)
        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(lowerCamelCase__,		AlignDevicesHook(io_same_device=lowerCamelCase__		)		)
        A			:	int										=       torch.randn(2,		3		).to(0		)
        A			:	str										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		torch.device(0		)		)






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

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

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

        A			:	int										=       torch.randn(2,		3		)
        A			:	List[Any]										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		lowerCamelCase__		)

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

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

        # 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"""		)		)

        A			:	int										=       torch.randn(2,		3		)
        A			:	str										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		lowerCamelCase__		)

        # 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       _lowerCAmelCase (	self		):
        A			:	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
        A			:	Tuple										=       0 if torch.cuda.is_available() else """cpu"""
        attach_align_device_hook(lowerCamelCase__,		execution_device=lowerCamelCase__,		offload=lowerCamelCase__		)

        # 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
        A			:	Optional[int]										=       torch.device(lowerCamelCase__		)
        self.assertEqual(model.batchnorm.running_mean.device,		lowerCamelCase__		)

        A			:	List[str]										=       torch.randn(2,		3		)
        A			:	Optional[int]										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		lowerCamelCase__		)

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

        # 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"""		)		)

        A			:	List[str]										=       torch.randn(2,		3		)
        A			:	List[str]										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		lowerCamelCase__		)

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

        A			:	Optional[Any]										=       torch.randn(2,		3		)
        A			:	Tuple										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		lowerCamelCase__		)

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

        # 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"""		)		)

        A			:	List[Any]										=       torch.randn(2,		3		)
        A			:	Dict										=       model(lowerCamelCase__		)
        self.assertEqual(output.device,		lowerCamelCase__		)

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


