


import os
import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from huggingface_hub.file_download import http_get
from requests.exceptions import HTTPError

from transformers import (
    AlbertTokenizer,
    AutoTokenizer,
    BertTokenizer,
    BertTokenizerFast,
    GPTaTokenizerFast,
    is_tokenizers_available,
)
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_tokenizers
from transformers.tokenization_utils import Trie


sys.path.append(str(Path(__file__).parent.parent / "utils"))

from test_module.custom_tokenization import CustomTokenizer  # noqa E402


if is_tokenizers_available():
       from test_module.custom_tokenization_fast import CustomTokenizerFast

class       __magic_name__	(     unittest.TestCase):


     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   mock.Mock()
      UpperCamelCase__					:    Tuple          =   500
      UpperCamelCase__					:    Dict          =   {}
      UpperCamelCase__					:    Dict          =   HTTPError
      UpperCamelCase__					:    Optional[Any]          =   {}

      # Download this model to make sure it's in the cache.
      UpperCamelCase__					:    List[Any]          =   BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert'''			)

      # Under the mock environment we get a 500 error when trying to reach the tokenizer.
      with mock.patch('''requests.Session.request'''							,	return_value=lowerCamelCase__			) as mock_head:
       UpperCamelCase__					:    Any          =   BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert'''			)
       # This check we did call the fake head request
       mock_head.assert_called()


     @require_tokenizers
     def      UpperCAmelCase__	(      self							:     str			)  ->      Any:





      '''simple docstring'''






      UpperCamelCase__					:    Union[str, Any]          =   mock.Mock()
      UpperCamelCase__					:    Any          =   500
      UpperCamelCase__					:    str          =   {}
      UpperCamelCase__					:    Optional[Any]          =   HTTPError
      UpperCamelCase__					:    Tuple          =   {}

      # Download this model to make sure it's in the cache.
      UpperCamelCase__					:    Optional[Any]          =   GPTaTokenizerFast.from_pretrained('''gpt2'''			)

      # Under the mock environment we get a 500 error when trying to reach the tokenizer.
      with mock.patch('''requests.Session.request'''							,	return_value=lowerCamelCase__			) as mock_head:
       UpperCamelCase__					:    str          =   GPTaTokenizerFast.from_pretrained('''gpt2'''			)
       # This check we did call the fake head request
       mock_head.assert_called()


     def      UpperCAmelCase__	(      self							:     Any			)  ->      List[str]:





      '''simple docstring'''






      try:
       UpperCamelCase__					:    Dict          =   tempfile.mktemp()
       with open(lowerCamelCase__							,	'''wb'''			) as f:
        http_get('''https://huggingface.co/albert-base-v1/resolve/main/spiece.model'''							,	lowerCamelCase__			)

       UpperCamelCase__					:    Dict          =   AlbertTokenizer.from_pretrained(lowerCamelCase__			)
      finally:
       os.remove(lowerCamelCase__			)

      # Supporting this legacy load introduced a weird bug where the tokenizer would load local files if they are in
      # the current folder and have the right name.
      if os.path.isfile('''tokenizer.json'''			):
       # We skip the test if the user has a `tokenizer.json` in this folder to avoid deleting it.
       return
      try:
       with open('''tokenizer.json'''							,	'''wb'''			) as f:
        http_get('''https://huggingface.co/hf-internal-testing/tiny-random-bert/blob/main/tokenizer.json'''							,	lowerCamelCase__			)
       UpperCamelCase__					:    Union[str, Any]          =   AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''			)
       # The tiny random BERT has a vocab size of 1024, tiny gpt2 as a vocab size of 1000
       self.assertEqual(tokenizer.vocab_size							,	1000			)
       # Tokenizer should depend on the remote checkpoint, not the local tokenizer.json file.

      finally:
       os.remove('''tokenizer.json'''			)


     def      UpperCAmelCase__	(      self							:     str			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    Any          =   AlbertTokenizer.from_pretrained('''https://huggingface.co/albert-base-v1/resolve/main/spiece.model'''			)



@is_staging_test
class       __magic_name__	(     unittest.TestCase):
     A:		List[Any]			=      ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]


     @classmethod
     def      UpperCAmelCase__	(      cls							:     List[str]			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    List[str]          =   TOKEN
      HfFolder.save_token(lowerCamelCase__			)


     @classmethod
     def      UpperCAmelCase__	(      cls							:     Any			)  ->      Optional[Any]:





      '''simple docstring'''






      try:
       delete_repo(token=cls._token							,	repo_id='''test-tokenizer'''			)
      except HTTPError:
       pass

      try:
       delete_repo(token=cls._token							,	repo_id='''valid_org/test-tokenizer-org'''			)
      except HTTPError:
       pass

      try:
       delete_repo(token=cls._token							,	repo_id='''test-dynamic-tokenizer'''			)
      except HTTPError:
       pass


     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      Optional[int]:





      '''simple docstring'''






      with tempfile.TemporaryDirectory() as tmp_dir:
       UpperCamelCase__					:    int          =   os.path.join(lowerCamelCase__							,	'''vocab.txt'''			)
       with open(lowerCamelCase__							,	'''w'''							,	encoding='''utf-8'''			) as vocab_writer:
        vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]			)			)
       UpperCamelCase__					:    Tuple          =   BertTokenizer(lowerCamelCase__			)

      tokenizer.push_to_hub('''test-tokenizer'''							,	use_auth_token=self._token			)
      UpperCamelCase__					:    Union[str, Any]          =   BertTokenizer.from_pretrained(F"{USER}/test-tokenizer"			)
      self.assertDictEqual(new_tokenizer.vocab							,	tokenizer.vocab			)

      # Reset repo
      delete_repo(token=self._token							,	repo_id='''test-tokenizer'''			)

      # Push to hub via save_pretrained
      with tempfile.TemporaryDirectory() as tmp_dir:
       tokenizer.save_pretrained(lowerCamelCase__							,	repo_id='''test-tokenizer'''							,	push_to_hub=lowerCamelCase__							,	use_auth_token=self._token			)

      UpperCamelCase__					:    str          =   BertTokenizer.from_pretrained(F"{USER}/test-tokenizer"			)
      self.assertDictEqual(new_tokenizer.vocab							,	tokenizer.vocab			)


     def      UpperCAmelCase__	(      self							:     str			)  ->      Optional[int]:





      '''simple docstring'''






      with tempfile.TemporaryDirectory() as tmp_dir:
       UpperCamelCase__					:    Dict          =   os.path.join(lowerCamelCase__							,	'''vocab.txt'''			)
       with open(lowerCamelCase__							,	'''w'''							,	encoding='''utf-8'''			) as vocab_writer:
        vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]			)			)
       UpperCamelCase__					:    List[str]          =   BertTokenizer(lowerCamelCase__			)

      tokenizer.push_to_hub('''valid_org/test-tokenizer-org'''							,	use_auth_token=self._token			)
      UpperCamelCase__					:    Optional[Any]          =   BertTokenizer.from_pretrained('''valid_org/test-tokenizer-org'''			)
      self.assertDictEqual(new_tokenizer.vocab							,	tokenizer.vocab			)

      # Reset repo
      delete_repo(token=self._token							,	repo_id='''valid_org/test-tokenizer-org'''			)

      # Push to hub via save_pretrained
      with tempfile.TemporaryDirectory() as tmp_dir:
       tokenizer.save_pretrained(
           lowerCamelCase__							,	repo_id='''valid_org/test-tokenizer-org'''							,	push_to_hub=lowerCamelCase__							,	use_auth_token=self._token			)

      UpperCamelCase__					:    Union[str, Any]          =   BertTokenizer.from_pretrained('''valid_org/test-tokenizer-org'''			)
      self.assertDictEqual(new_tokenizer.vocab							,	tokenizer.vocab			)


     @require_tokenizers
     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      int:





      '''simple docstring'''






      CustomTokenizer.register_for_auto_class()
      with tempfile.TemporaryDirectory() as tmp_dir:
       UpperCamelCase__					:    List[Any]          =   os.path.join(lowerCamelCase__							,	'''vocab.txt'''			)
       with open(lowerCamelCase__							,	'''w'''							,	encoding='''utf-8'''			) as vocab_writer:
        vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]			)			)
       UpperCamelCase__					:    Any          =   CustomTokenizer(lowerCamelCase__			)

      # No fast custom tokenizer
      tokenizer.push_to_hub('''test-dynamic-tokenizer'''							,	use_auth_token=self._token			)

      UpperCamelCase__					:    str          =   AutoTokenizer.from_pretrained(F"{USER}/test-dynamic-tokenizer"							,	trust_remote_code=lowerCamelCase__			)
      # Can't make an isinstance check because the new_model.config is from the CustomTokenizer class of a dynamic module
      self.assertEqual(tokenizer.__class__.__name__							,	'''CustomTokenizer'''			)

      # Fast and slow custom tokenizer
      CustomTokenizerFast.register_for_auto_class()
      with tempfile.TemporaryDirectory() as tmp_dir:
       UpperCamelCase__					:    List[Any]          =   os.path.join(lowerCamelCase__							,	'''vocab.txt'''			)
       with open(lowerCamelCase__							,	'''w'''							,	encoding='''utf-8'''			) as vocab_writer:
        vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]			)			)

       UpperCamelCase__					:    List[Any]          =   BertTokenizerFast.from_pretrained(lowerCamelCase__			)
       bert_tokenizer.save_pretrained(lowerCamelCase__			)
       UpperCamelCase__					:    Optional[int]          =   CustomTokenizerFast.from_pretrained(lowerCamelCase__			)

      tokenizer.push_to_hub('''test-dynamic-tokenizer'''							,	use_auth_token=self._token			)

      UpperCamelCase__					:    str          =   AutoTokenizer.from_pretrained(F"{USER}/test-dynamic-tokenizer"							,	trust_remote_code=lowerCamelCase__			)
      # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module
      self.assertEqual(tokenizer.__class__.__name__							,	'''CustomTokenizerFast'''			)
      UpperCamelCase__					:    int          =   AutoTokenizer.from_pretrained(
          F"{USER}/test-dynamic-tokenizer"							,	use_fast=lowerCamelCase__							,	trust_remote_code=lowerCamelCase__			)
      # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module
      self.assertEqual(tokenizer.__class__.__name__							,	'''CustomTokenizer'''			)



class       __magic_name__	(     unittest.TestCase):


     def      UpperCAmelCase__	(      self							:     str			)  ->      Dict:





      '''simple docstring'''






      UpperCamelCase__					:    Any          =   Trie()
      trie.add('''Hello 友達'''			)
      self.assertEqual(trie.data							,	{'''H''': {'''e''': {'''l''': {'''l''': {'''o''': {''' ''': {'''友''': {'''達''': {'''''': 1}}}}}}}}}			)
      trie.add('''Hello'''			)
      trie.data
      self.assertEqual(trie.data							,	{'''H''': {'''e''': {'''l''': {'''l''': {'''o''': {'''''': 1, ''' ''': {'''友''': {'''達''': {'''''': 1}}}}}}}}}			)


     def      UpperCAmelCase__	(      self							:     str			)  ->      str:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   Trie()
      self.assertEqual(trie.split('''[CLS] This is a extra_id_100'''			)							,	['''[CLS] This is a extra_id_100''']			)
      trie.add('''[CLS]'''			)
      trie.add('''extra_id_1'''			)
      trie.add('''extra_id_100'''			)
      self.assertEqual(trie.split('''[CLS] This is a extra_id_100'''			)							,	['''[CLS]''', ''' This is a ''', '''extra_id_100''']			)


     def      UpperCAmelCase__	(      self							:     int			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   Trie()
      trie.add('''A'''			)
      self.assertEqual(trie.split('''ABC'''			)							,	['''A''', '''BC''']			)
      self.assertEqual(trie.split('''BCA'''			)							,	['''BC''', '''A''']			)


     def      UpperCAmelCase__	(      self							:     Dict			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    Any          =   Trie()
      trie.add('''TOKEN]'''			)
      trie.add('''[SPECIAL_TOKEN]'''			)
      self.assertEqual(trie.split('''This is something [SPECIAL_TOKEN]'''			)							,	['''This is something ''', '''[SPECIAL_TOKEN]''']			)


     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      Optional[Any]:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   Trie()
      trie.add('''A'''			)
      trie.add('''P'''			)
      trie.add('''[SPECIAL_TOKEN]'''			)
      self.assertEqual(trie.split('''This is something [SPECIAL_TOKEN]'''			)							,	['''This is something ''', '''[SPECIAL_TOKEN]''']			)


     def      UpperCAmelCase__	(      self							:     Any			)  ->      List[str]:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   Trie()
      trie.add('''AB'''			)
      trie.add('''B'''			)
      trie.add('''C'''			)
      self.assertEqual(trie.split('''ABC'''			)							,	['''AB''', '''C''']			)


     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   Trie()
      trie.add('''ABC'''			)
      trie.add('''B'''			)
      trie.add('''CD'''			)
      self.assertEqual(trie.split('''ABCD'''			)							,	['''ABC''', '''D''']			)


     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   Trie()
      UpperCamelCase__					:    Optional[Any]          =   trie.cut_text('''ABC'''							,	[0, 0, 2, 1, 2, 3]			)
      self.assertEqual(lowerCamelCase__							,	['''AB''', '''C''']			)






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


     def __init__(      self							:     Dict			)  ->      str:





      '''simple docstring'''






      super().__init__()
      UpperCamelCase__					:    int          =   nn.Linear(3							,	4			)
      UpperCamelCase__					:    str          =   nn.BatchNormad(4			)
      UpperCamelCase__					:    List[str]          =   nn.Linear(4							,	5			)


     def      UpperCAmelCase__	(      self							:     int							,	lowerCamelCase__							:     Dict			)  ->      Tuple:





      '''simple docstring'''






      return self.lineara(self.batchnorm(self.lineara(lowerCamelCase__			)			)			)



class       __magic_name__	(     __lowerCAmelCase):


     def      UpperCAmelCase__	(      self							:     Union[str, Any]							,	lowerCamelCase__							:     Any							,	*lowerCamelCase__							:     int							,	**lowerCamelCase__							:     List[str]			)  ->      Any:





      '''simple docstring'''






      return (args[0] + 1,) + args[1:], kwargs



class       __magic_name__	(     __lowerCAmelCase):


     def      UpperCAmelCase__	(      self							:     Any							,	lowerCamelCase__							:     Any							,	lowerCamelCase__							:     Any			)  ->      Any:





      '''simple docstring'''






      return output + 1



class       __magic_name__	(     unittest.TestCase):


     def      UpperCAmelCase__	(      self							:     Tuple			)  ->      Tuple:





      '''simple docstring'''






      UpperCamelCase__					:    Tuple          =   ModelForTest()
      UpperCamelCase__					:    Union[str, 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      UpperCAmelCase__	(      self							:     Dict			)  ->      str:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   ModelForTest()
      UpperCamelCase__					:    List[str]          =   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      UpperCAmelCase__	(      self							:     List[str]			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[Any]          =   ModelForTest()
      UpperCamelCase__					:    List[str]          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    int          =   test_model(x + 1			)
      UpperCamelCase__					:    List[Any]          =   test_model(x + 2			)

      UpperCamelCase__					:    Optional[int]          =   PreForwardHook()
      add_hook_to_module(lowerCamelCase__							,	lowerCamelCase__			)
      UpperCamelCase__					:    List[Any]          =   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
      UpperCamelCase__					:    Optional[Any]          =   PreForwardHook()
      add_hook_to_module(lowerCamelCase__							,	lowerCamelCase__			)
      UpperCamelCase__					:    List[str]          =   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
      UpperCamelCase__					:    Tuple          =   SequentialHook(PreForwardHook()							,	PreForwardHook()			)
      add_hook_to_module(lowerCamelCase__							,	lowerCamelCase__			)

      UpperCamelCase__					:    Any          =   test_model(lowerCamelCase__			)
      assert torch.allclose(lowerCamelCase__							,	lowerCamelCase__							,	atol=1E-5			)


     def      UpperCAmelCase__	(      self							:     List[Any]			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   ModelForTest()
      UpperCamelCase__					:    str          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    Any          =   test_model(lowerCamelCase__			)

      UpperCamelCase__					:    int          =   PostForwardHook()
      add_hook_to_module(lowerCamelCase__							,	lowerCamelCase__			)
      UpperCamelCase__					:    Dict          =   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
      UpperCamelCase__					:    Dict          =   PostForwardHook()
      add_hook_to_module(lowerCamelCase__							,	lowerCamelCase__			)
      UpperCamelCase__					:    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
      UpperCamelCase__					:    List[Any]          =   SequentialHook(PostForwardHook()							,	PostForwardHook()			)
      add_hook_to_module(lowerCamelCase__							,	lowerCamelCase__			)

      UpperCamelCase__					:    Tuple          =   test_model(lowerCamelCase__			)
      assert torch.allclose(lowerCamelCase__							,	output + 2							,	atol=1E-5			)


     def      UpperCAmelCase__	(      self							:     List[Any]			)  ->      Dict:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   ModelForTest()
      UpperCamelCase__					:    Tuple          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    Tuple          =   test_model(lowerCamelCase__			)

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

      UpperCamelCase__					:    Optional[int]          =   True
      UpperCamelCase__					:    Optional[int]          =   test_model(lowerCamelCase__			)
      self.assertFalse(outputa.requires_grad			)


     @require_multi_gpu
     def      UpperCAmelCase__	(      self							:     Any			)  ->      Dict:





      '''simple docstring'''






      UpperCamelCase__					:    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
      UpperCamelCase__					:    Tuple          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    str          =   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__			)			)
      UpperCamelCase__					:    Tuple          =   torch.randn(2							,	3			).to(0			)
      UpperCamelCase__					:    Optional[int]          =   model(lowerCamelCase__			)
      self.assertEqual(output.device							,	torch.device(0			)			)


     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      Union[str, Any]:





      '''simple docstring'''






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

      UpperCamelCase__					:    Dict          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    List[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'''			)			)

      # Now test with buffers included in the offload
      UpperCamelCase__					:    Optional[Any]          =   {
          '''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'''			)			)

      UpperCamelCase__					:    List[Any]          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    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      UpperCAmelCase__	(      self							:     str			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[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
      UpperCamelCase__					:    Any          =   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
      UpperCamelCase__					:    str          =   torch.device(lowerCamelCase__			)
      self.assertEqual(model.batchnorm.running_mean.device							,	lowerCamelCase__			)

      UpperCamelCase__					:    int          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    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__							,	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'''			)			)

      UpperCamelCase__					:    int          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    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      UpperCAmelCase__	(      self							:     int			)  ->      List[Any]:





      '''simple docstring'''






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

      UpperCamelCase__					:    List[str]          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    Any          =   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'''			)			)

      UpperCamelCase__					:    Union[str, Any]          =   torch.randn(2							,	3			)
      UpperCamelCase__					:    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'''			)			)


