



"""simple docstring"""
import json
import os
import shutil
import tempfile
from unittest import TestCase

from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow
from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available


if is_torch_available() and is_datasets_available() and is_faiss_available():
  from transformers.models.rag.configuration_rag import RagConfig
  from transformers.models.rag.tokenization_rag import RagTokenizer
@require_faiss
@require_torch
class 							_UpperCAmelCase							( a      ):





     '''simple docstring'''



     def  __lowerCAmelCase	(					self		)							->					Optional[Any]:
         _UpperCAmelCase							:  Tuple											=    tempfile.mkdtemp()
         _UpperCAmelCase							:  int											=    8

         # DPR tok
         _UpperCAmelCase							:  Union[str, Any]											=    [
             '''[UNK]''',
             '''[CLS]''',
             '''[SEP]''',
             '''[PAD]''',
             '''[MASK]''',
             '''want''',
             '''##want''',
             '''##ed''',
             '''wa''',
             '''un''',
             '''runn''',
             '''##ing''',
             ''',''',
             '''low''',
             '''lowest''',
         ]
         _UpperCAmelCase							:  str											=    os.path.join(self.tmpdirname       ,	'''dpr_tokenizer'''		)
         os.makedirs(A       ,	exist_ok=A		)
         _UpperCAmelCase							:  Any											=    os.path.join(A       ,	DPR_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]		)		)

         # BART tok
         _UpperCAmelCase							:  Optional[int]											=    [
             '''l''',
             '''o''',
             '''w''',
             '''e''',
             '''r''',
             '''s''',
             '''t''',
             '''i''',
             '''d''',
             '''n''',
             '''\u0120''',
             '''\u0120l''',
             '''\u0120n''',
             '''\u0120lo''',
             '''\u0120low''',
             '''er''',
             '''\u0120lowest''',
             '''\u0120newer''',
             '''\u0120wider''',
             '''<unk>''',
         ]
         _UpperCAmelCase							:  List[Any]											=    dict(zip(A       ,	range(len(A		)		)		)		)
         _UpperCAmelCase							:  int											=    ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
         _UpperCAmelCase							:  str											=    {'''unk_token''': '''<unk>'''}

         _UpperCAmelCase							:  List[str]											=    os.path.join(self.tmpdirname       ,	'''bart_tokenizer'''		)
         os.makedirs(A       ,	exist_ok=A		)
         _UpperCAmelCase							:  Union[str, Any]											=    os.path.join(A       ,	BART_VOCAB_FILES_NAMES['''vocab_file''']		)
         _UpperCAmelCase							:  Tuple											=    os.path.join(A       ,	BART_VOCAB_FILES_NAMES['''merges_file''']		)
         with open(self.vocab_file       ,	'''w'''       ,	encoding='''utf-8'''		) as fp:
             fp.write(json.dumps(A		) + '''\n'''		)
         with open(self.merges_file       ,	'''w'''       ,	encoding='''utf-8'''		) as fp:
             fp.write('''\n'''.join(A		)		)





     def  __lowerCAmelCase	(					self		)							->					DPRQuestionEncoderTokenizer:
         return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname       ,	'''dpr_tokenizer'''		)		)





     def  __lowerCAmelCase	(					self		)							->					BartTokenizer:
         return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname       ,	'''bart_tokenizer'''		)		)





     def  __lowerCAmelCase	(					self		)							->					Any:
         shutil.rmtree(self.tmpdirname		)





     @require_tokenizers
     def  __lowerCAmelCase	(					self		)							->					str:
         _UpperCAmelCase							:  Optional[int]											=    os.path.join(self.tmpdirname       ,	'''rag_tokenizer'''		)
         _UpperCAmelCase							:  Tuple											=    RagConfig(question_encoder=DPRConfig().to_dict()       ,	generator=BartConfig().to_dict()		)
         _UpperCAmelCase							:  int											=    RagTokenizer(question_encoder=self.get_dpr_tokenizer()       ,	generator=self.get_bart_tokenizer()		)
         rag_config.save_pretrained(A		)
         rag_tokenizer.save_pretrained(A		)
         _UpperCAmelCase							:  List[Any]											=    RagTokenizer.from_pretrained(A       ,	config=A		)
         self.assertIsInstance(new_rag_tokenizer.question_encoder       ,	A		)
         self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab()       ,	rag_tokenizer.question_encoder.get_vocab()		)
         self.assertIsInstance(new_rag_tokenizer.generator       ,	A		)
         self.assertEqual(new_rag_tokenizer.generator.get_vocab()       ,	rag_tokenizer.generator.get_vocab()		)





     @slow
     def  __lowerCAmelCase	(					self		)							->					Optional[Any]:
         _UpperCAmelCase							:  Optional[Any]											=    RagTokenizer.from_pretrained('''facebook/rag-token-nq'''		)
         _UpperCAmelCase							:  Any											=    [
             '''who got the first nobel prize in physics''',
             '''when is the next deadpool movie being released''',
             '''which mode is used for short wave broadcast service''',
             '''who is the owner of reading football club''',
             '''when is the next scandal episode coming out''',
             '''when is the last time the philadelphia won the superbowl''',
             '''what is the most current adobe flash player version''',
             '''how many episodes are there in dragon ball z''',
             '''what is the first step in the evolution of the eye''',
             '''where is gall bladder situated in human body''',
             '''what is the main mineral in lithium batteries''',
             '''who is the president of usa right now''',
             '''where do the greasers live in the outsiders''',
             '''panda is a national animal of which country''',
             '''what is the name of manchester united stadium''',
         ]
         _UpperCAmelCase							:  Any											=    tokenizer(A		)
         self.assertIsNotNone(A		)





     @slow
     def  __lowerCAmelCase	(					self		)							->					List[Any]:
         _UpperCAmelCase							:  Optional[int]											=    RagTokenizer.from_pretrained('''facebook/rag-sequence-nq'''		)
         _UpperCAmelCase							:  Union[str, Any]											=    [
             '''who got the first nobel prize in physics''',
             '''when is the next deadpool movie being released''',
             '''which mode is used for short wave broadcast service''',
             '''who is the owner of reading football club''',
             '''when is the next scandal episode coming out''',
             '''when is the last time the philadelphia won the superbowl''',
             '''what is the most current adobe flash player version''',
             '''how many episodes are there in dragon ball z''',
             '''what is the first step in the evolution of the eye''',
             '''where is gall bladder situated in human body''',
             '''what is the main mineral in lithium batteries''',
             '''who is the president of usa right now''',
             '''where do the greasers live in the outsiders''',
             '''panda is a national animal of which country''',
             '''what is the name of manchester united stadium''',
         ]
         _UpperCAmelCase							:  List[Any]											=    tokenizer(A		)
         self.assertIsNotNone(A		)







"""simple docstring"""
import inspect
import logging
import os
import random
import shutil
import tempfile
import unittest

import pytest
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset

from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_cuda
from accelerate.utils import ProjectConfiguration, set_seed


_lowerCAmelCase    :Tuple     =					logging.getLogger(__name__)





def  lowerCamelCase_						(UpperCamelCase__		:		List[Any]=2     ,       UpperCamelCase__		:		List[Any]=3     ,       UpperCamelCase__		:		List[Any]=16     ,       UpperCamelCase__		:		int = 10     ,       UpperCamelCase__		:		int = 2							):

    def get_dataset(UpperCamelCase__		:		List[str]							):
        _UpperCAmelCase							:  Optional[Any]											=    torch.randn(batch_size * n_batches     ,       1							)
        return TensorDataset(UpperCamelCase__     ,       a * x + b + 0.1 * torch.randn(batch_size * n_batches     ,       1							)							)

    _UpperCAmelCase							:  Optional[Any]											=    get_dataset(UpperCamelCase__							)
    _UpperCAmelCase							:  Optional[Any]											=    get_dataset(UpperCamelCase__							)
    _UpperCAmelCase							:  List[str]											=    DataLoader(UpperCamelCase__     ,       shuffle=UpperCamelCase__     ,       batch_size=UpperCamelCase__     ,       num_workers=4							)
    _UpperCAmelCase							:  List[str]											=    DataLoader(UpperCamelCase__     ,       shuffle=UpperCamelCase__     ,       batch_size=UpperCamelCase__     ,       num_workers=4							)
    return (train_dataloader, valid_dataloader)





def  lowerCamelCase_						(UpperCamelCase__		:		Optional[int]     ,       UpperCamelCase__		:		List[Any]     ,       UpperCamelCase__		:		int     ,       UpperCamelCase__		:		Any     ,       UpperCamelCase__		:		Optional[Any]     ,       UpperCamelCase__		:		Tuple=None							):
    _UpperCAmelCase							:  Tuple											=    []
    for epoch in range(UpperCamelCase__							):
        # Train quickly
        model.train()
        for batch in dataloader:
            _UpperCAmelCase							,				_UpperCAmelCase							:  Dict											=    batch
            _UpperCAmelCase							:  int											=    model(UpperCamelCase__							)
            _UpperCAmelCase							:  Dict											=    torch.nn.functional.mse_loss(UpperCamelCase__     ,       UpperCamelCase__							)
            accelerator.backward(UpperCamelCase__							)
            optimizer.step()
            optimizer.zero_grad()
        rands.append(random.random()							)  # Introduce some randomness
        if scheduler is not None:
            scheduler.step()
    return rands
class 							_UpperCAmelCase							( nn.Module      ):





     '''simple docstring'''



     def __init__(					self		)							->					List[Any]:
         super().__init__()
         _UpperCAmelCase							:  List[Any]											=    nn.Parameter(torch.randn(1		)		)
         _UpperCAmelCase							:  int											=    nn.Parameter(torch.randn(1		)		)





     def  __lowerCAmelCase	(					self       ,	A		)							->					Tuple:
         return x * self.a + self.b



class 							_UpperCAmelCase							( unittest.TestCase      ):





     '''simple docstring'''



     def  __lowerCAmelCase	(					self		)							->					Any:
         with tempfile.TemporaryDirectory() as tmpdir:
             set_seed(4_2		)
             _UpperCAmelCase							:  int											=    DummyModel()
             _UpperCAmelCase							:  str											=    torch.optim.Adam(params=model.parameters()       ,	lr=1E-3		)
             _UpperCAmelCase							,				_UpperCAmelCase							:  List[Any]											=    dummy_dataloaders()
             _UpperCAmelCase							:  Any											=    ProjectConfiguration(total_limit=1       ,	project_dir=A       ,	automatic_checkpoint_naming=A		)
             # Train baseline
             _UpperCAmelCase							:  Union[str, Any]											=    Accelerator(project_config=A		)
             _UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							:  int											=    accelerator.prepare(
                 A       ,	A       ,	A       ,	A		)
             # Save initial
             accelerator.save_state()

             # Save second state
             accelerator.save_state()
             self.assertEqual(len(os.listdir(accelerator.project_dir		)		)       ,	1		)





     def  __lowerCAmelCase	(					self		)							->					List[str]:
         with tempfile.TemporaryDirectory() as tmpdir:
             set_seed(4_2		)
             _UpperCAmelCase							:  Optional[Any]											=    DummyModel()
             _UpperCAmelCase							:  int											=    torch.optim.Adam(params=model.parameters()       ,	lr=1E-3		)
             _UpperCAmelCase							,				_UpperCAmelCase							:  Dict											=    dummy_dataloaders()
             # Train baseline
             _UpperCAmelCase							:  Optional[int]											=    Accelerator()
             _UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							:  List[str]											=    accelerator.prepare(
                 A       ,	A       ,	A       ,	A		)
             # Save initial
             _UpperCAmelCase							:  Union[str, Any]											=    os.path.join(A       ,	'''initial'''		)
             accelerator.save_state(A		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Optional[Any]											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  str											=    optimizer.state_dict()
             _UpperCAmelCase							:  Tuple											=    train(3       ,	A       ,	A       ,	A       ,	A		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Dict											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  List[Any]											=    optimizer.state_dict()

             # Train partially
             set_seed(4_2		)
             _UpperCAmelCase							:  Dict											=    DummyModel()
             _UpperCAmelCase							:  Optional[Any]											=    torch.optim.Adam(params=model.parameters()       ,	lr=1E-3		)
             _UpperCAmelCase							,				_UpperCAmelCase							:  Union[str, Any]											=    dummy_dataloaders()
             _UpperCAmelCase							:  Tuple											=    Accelerator()
             _UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							:  str											=    accelerator.prepare(
                 A       ,	A       ,	A       ,	A		)
             accelerator.load_state(A		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Union[str, Any]											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  List[str]											=    optimizer.state_dict()
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)

             _UpperCAmelCase							:  Union[str, Any]											=    train(2       ,	A       ,	A       ,	A       ,	A		)
             # Save everything
             _UpperCAmelCase							:  List[str]											=    os.path.join(A       ,	'''checkpoint'''		)
             accelerator.save_state(A		)

             # Load everything back in and make sure all states work
             accelerator.load_state(A		)
             test_rands += train(1       ,	A       ,	A       ,	A       ,	A		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Dict											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  Dict											=    optimizer.state_dict()
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)





     def  __lowerCAmelCase	(					self		)							->					int:
         with tempfile.TemporaryDirectory() as tmpdir:
             set_seed(4_2		)
             _UpperCAmelCase							:  List[Any]											=    DummyModel()
             _UpperCAmelCase							:  List[str]											=    torch.optim.Adam(params=model.parameters()       ,	lr=1E-3		)
             _UpperCAmelCase							,				_UpperCAmelCase							:  List[Any]											=    dummy_dataloaders()
             _UpperCAmelCase							:  List[str]											=    ProjectConfiguration(automatic_checkpoint_naming=A		)

             # Train baseline
             _UpperCAmelCase							:  str											=    Accelerator(project_dir=A       ,	project_config=A		)
             _UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							:  Any											=    accelerator.prepare(
                 A       ,	A       ,	A       ,	A		)
             # Save initial
             accelerator.save_state()
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Union[str, Any]											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  Dict											=    optimizer.state_dict()
             _UpperCAmelCase							:  int											=    train(3       ,	A       ,	A       ,	A       ,	A		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Union[str, Any]											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  Union[str, Any]											=    optimizer.state_dict()

             # Train partially
             set_seed(4_2		)
             _UpperCAmelCase							:  List[Any]											=    DummyModel()
             _UpperCAmelCase							:  Union[str, Any]											=    torch.optim.Adam(params=model.parameters()       ,	lr=1E-3		)
             _UpperCAmelCase							,				_UpperCAmelCase							:  Any											=    dummy_dataloaders()
             _UpperCAmelCase							:  List[str]											=    ProjectConfiguration(iteration=1       ,	automatic_checkpoint_naming=A		)
             _UpperCAmelCase							:  Tuple											=    Accelerator(project_dir=A       ,	project_config=A		)
             _UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							:  int											=    accelerator.prepare(
                 A       ,	A       ,	A       ,	A		)
             accelerator.load_state(os.path.join(A       ,	'''checkpoints'''       ,	'''checkpoint_0'''		)		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  Dict											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  str											=    optimizer.state_dict()
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)

             _UpperCAmelCase							:  List[str]											=    train(2       ,	A       ,	A       ,	A       ,	A		)
             # Save everything
             accelerator.save_state()

             # Load everything back in and make sure all states work
             accelerator.load_state(os.path.join(A       ,	'''checkpoints'''       ,	'''checkpoint_1'''		)		)
             test_rands += train(1       ,	A       ,	A       ,	A       ,	A		)
             ((_UpperCAmelCase)							,				(_UpperCAmelCase))							:  List[str]											=    model.a.item(), model.b.item()
             _UpperCAmelCase							:  Tuple											=    optimizer.state_dict()
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)
             self.assertEqual(A       ,	A		)





     def  __lowerCAmelCase	(					self		)							->					Dict:
         _UpperCAmelCase							:  List[Any]											=    torch.tensor([1, 2, 3]		)
         _UpperCAmelCase							:  List[str]											=    torch.tensor([2, 3, 4]		)
         _UpperCAmelCase							:  Optional[int]											=    DummyModel()
         _UpperCAmelCase							:  Dict											=    torch.optim.Adam(net.parameters()		)
         _UpperCAmelCase							:  Optional[int]											=    Accelerator()
         with self.assertRaises(A		) as ve:
             accelerator.register_for_checkpointing(A       ,	A       ,	A       ,	A		)
         _UpperCAmelCase							:  Dict											=    str(ve.exception		)
         self.assertTrue('''Item at index 0''' in message		)
         self.assertTrue('''Item at index 1''' in message		)
         self.assertFalse('''Item at index 2''' in message		)
         self.assertFalse('''Item at index 3''' in message		)





     def  __lowerCAmelCase	(					self		)							->					Tuple:
         with tempfile.TemporaryDirectory() as tmpdir:
             set_seed(4_2		)
             _UpperCAmelCase							:  Tuple											=    DummyModel()
             _UpperCAmelCase							:  List[Any]											=    torch.optim.Adam(params=model.parameters()       ,	lr=1E-3		)
             _UpperCAmelCase							:  Optional[int]											=    torch.optim.lr_scheduler.StepLR(A       ,	step_size=1       ,	gamma=0.99		)
             _UpperCAmelCase							,				_UpperCAmelCase							:  str											=    dummy_dataloaders()
             _UpperCAmelCase							:  List[str]											=    ProjectConfiguration(automatic_checkpoint_naming=A		)
             # Train baseline
             _UpperCAmelCase							:  int											=    Accelerator(project_dir=A       ,	project_config=A		)
             _UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							,				_UpperCAmelCase							:  str											=    accelerator.prepare(
                 A       ,	A       ,	A       ,	A       ,	A		)
             # Save initial
             accelerator.save_state()
             _UpperCAmelCase							:  List[str]											=    scheduler.state_dict()
             train(3       ,	A       ,	A       ,	A       ,	A       ,	A		)
             self.assertNotEqual(A       ,	scheduler.state_dict()		)

             # Load everything back in and make sure all states work
             accelerator.load_state(os.path.join(A       ,	'''checkpoints'''       ,	'''checkpoint_0'''		)		)
             self.assertEqual(A       ,	scheduler.state_dict()		)





     def  __lowerCAmelCase	(					self		)							->					Optional[Any]:
         with tempfile.TemporaryDirectory() as tmpdir:
             set_seed(4_2		)
             _UpperCAmelCase							:  int											=    DummyModel()
             _UpperCAmelCase							:  str											=    ProjectConfiguration(automatic_checkpoint_naming=A       ,	total_limit=2		)
             # Train baseline
             _UpperCAmelCase							:  Union[str, Any]											=    Accelerator(project_dir=A       ,	project_config=A		)
             _UpperCAmelCase							:  Optional[Any]											=    accelerator.prepare(A		)
             # Save 3 states:
             for _ in range(1_1		):
                 accelerator.save_state()
             self.assertTrue(not os.path.exists(os.path.join(A       ,	'''checkpoints'''       ,	'''checkpoint_0'''		)		)		)
             self.assertTrue(os.path.exists(os.path.join(A       ,	'''checkpoints'''       ,	'''checkpoint_9'''		)		)		)
             self.assertTrue(os.path.exists(os.path.join(A       ,	'''checkpoints'''       ,	'''checkpoint_10'''		)		)		)





     @require_cuda
     def  __lowerCAmelCase	(					self		)							->					Dict:
         _UpperCAmelCase							:  str											=    ['''torchrun''', f'--nproc_per_node={torch.cuda.device_count()}', inspect.getfile(self.__class__		)]
         execute_subprocess_async(A       ,	env=os.environ.copy()		)


if __name__ == "__main__":
  _lowerCAmelCase    :Dict     =					'/tmp/accelerate/state_checkpointing'
  _lowerCAmelCase    :Any     =					DummyModel()
  _lowerCAmelCase    :Tuple     =					torch.optim.Adam(params=model.parameters(), lr=1E-3)
  _lowerCAmelCase    :Dict     =					torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.99)
  _lowerCAmelCase,_lowerCAmelCase    :Any     =					dummy_dataloaders()
  _lowerCAmelCase    :Tuple     =					ProjectConfiguration(automatic_checkpoint_naming=True)
  # Train baseline
  _lowerCAmelCase    :Optional[Any]     =					Accelerator(project_dir=savedir, project_config=project_config, mixed_precision='no')
  if accelerator.process_index == 0:
    if os.path.exists(savedir):
      shutil.rmtree(savedir)
    os.makedirs(savedir)
  _lowerCAmelCase,_lowerCAmelCase,_lowerCAmelCase,_lowerCAmelCase,_lowerCAmelCase    :str     =					accelerator.prepare(
      model, optimizer, train_dataloader, valid_dataloader, scheduler
  )
  _lowerCAmelCase,_lowerCAmelCase    :List[Any]     =					accelerator.prepare(model, optimizer)
  train(3, model, train_dataloader, optimizer, accelerator, scheduler)
  # Check that the intial optimizer is loaded on the GPU
  for group in optimizer.param_groups:
    _lowerCAmelCase    :int     =					group['params'][0].device
    break
  assert param_device.type == accelerator.device.type
  _lowerCAmelCase    :Dict     =					model.cpu()
  accelerator.wait_for_everyone()
  accelerator.save_state()
  accelerator.wait_for_everyone()

  # Check CPU state
  accelerator.load_state(os.path.join(savedir, 'checkpoints', 'checkpoint_0'), map_location='cpu')
  for group in optimizer.param_groups:
    _lowerCAmelCase    :List[Any]     =					group['params'][0].device
    break
  assert (
      param_device.type == torch.device('cpu').type
  ), f"Loaded optimizer states did not match, expected to be loaded on the CPU but got {param_device}"

  # Check device state
  model.to(accelerator.device)
  accelerator.load_state(os.path.join(savedir, 'checkpoints', 'checkpoint_0'), map_location='on_device')
  for group in optimizer.param_groups:
    _lowerCAmelCase    :Union[str, Any]     =					group['params'][0].device
    break
  assert (
      param_device.type == accelerator.device.type
  ), f"Loaded optimizer states did not match, expected to be loaded on {accelerator.device} but got {param_device}"

  # Check error
  with pytest.raises(TypeError, match='Unsupported optimizer map location passed'):
    accelerator.load_state(os.path.join(savedir, 'checkpoints', 'checkpoint_0'), map_location='invalid')
  accelerator.wait_for_everyone()
  if accelerator.process_index == 0:
    shutil.rmtree(savedir)
  accelerator.wait_for_everyone()


