





import os
import unittest
from tempfile import TemporaryDirectory

import torch
import torch.nn as nn

from accelerate.utils import (
    OffloadedWeightsLoader,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
)





class 				A_				(      nn.Module	):







       """simple docstring"""





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







       def    __UpperCAmelCase (    self	:	str						,__A	:	Union[str, Any]  )						->					Any:
         return self.lineara(self.batchnorm(self.lineara(__A  )  )  )





class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	List[str]  )						->					Dict:
         _lowercase							= ModelForTest()
         with TemporaryDirectory() as tmp_dir:
           offload_state_dict(__A						,model.state_dict()  )
           _lowercase							= os.path.join(__A						,'index.json'  )
           self.assertTrue(os.path.isfile(__A  )  )
           # TODO: add tests on what is inside the index

           for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]:
             _lowercase							= os.path.join(__A						,F"""{key}.dat"""  )
             self.assertTrue(os.path.isfile(__A  )  )
             # TODO: add tests on the fact weights are properly loaded







       def    __UpperCAmelCase (    self	:	int  )						->					List[Any]:
         _lowercase							= [torch.floataa, torch.floataa, torch.bfloataa]

         for dtype in dtypes:
           _lowercase							= torch.randn(2						,3						,dtype=__A  )
           with TemporaryDirectory() as tmp_dir:
             _lowercase							= offload_weight(__A						,'weight'						,__A						,{}  )
             _lowercase							= os.path.join(__A						,'weight.dat'  )
             self.assertTrue(os.path.isfile(__A  )  )
             self.assertDictEqual(__A						,{'weight': {'shape': [2, 3], 'dtype': str(__A  ).split('.'  )[1]}}  )

             _lowercase							= load_offloaded_weight(__A						,index['weight']  )
             self.assertTrue(torch.equal(__A						,__A  )  )







       def    __UpperCAmelCase (    self	:	str  )						->					List[str]:
         _lowercase							= ModelForTest()
         _lowercase							= model.state_dict()
         _lowercase							= {k: v for k, v in state_dict.items() if 'linear2' not in k}
         _lowercase							= {k: v for k, v in state_dict.items() if 'linear2' in k}

         with TemporaryDirectory() as tmp_dir:
           offload_state_dict(__A						,__A  )
           _lowercase							= OffloadedWeightsLoader(state_dict=__A						,save_folder=__A  )

           # Every key is there with the right value
           self.assertEqual(sorted(__A  )						,sorted(state_dict.keys()  )  )
           for key, param in state_dict.items():
             self.assertTrue(torch.allclose(__A						,weight_map[key]  )  )

         _lowercase							= {k: v for k, v in state_dict.items() if 'weight' in k}
         _lowercase							= {k: v for k, v in state_dict.items() if 'weight' not in k}

         with TemporaryDirectory() as tmp_dir:
           offload_state_dict(__A						,__A  )
           _lowercase							= OffloadedWeightsLoader(state_dict=__A						,save_folder=__A  )

           # Every key is there with the right value
           self.assertEqual(sorted(__A  )						,sorted(state_dict.keys()  )  )
           for key, param in state_dict.items():
             self.assertTrue(torch.allclose(__A						,weight_map[key]  )  )

         with TemporaryDirectory() as tmp_dir:
           offload_state_dict(__A						,__A  )
           # Duplicates are removed
           _lowercase							= OffloadedWeightsLoader(state_dict=__A						,save_folder=__A  )

           # Every key is there with the right value
           self.assertEqual(sorted(__A  )						,sorted(state_dict.keys()  )  )
           for key, param in state_dict.items():
             self.assertTrue(torch.allclose(__A						,weight_map[key]  )  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Dict:
         _lowercase							= {'a.1': 0, 'a.10': 1, 'a.2': 2}
         _lowercase							= extract_submodules_state_dict(__A						,['a.1', 'a.2']  )
         self.assertDictEqual(__A						,{'a.1': 0, 'a.2': 2}  )

         _lowercase							= {'a.1.a': 0, 'a.10.a': 1, 'a.2.a': 2}
         _lowercase							= extract_submodules_state_dict(__A						,['a.1', 'a.2']  )
         self.assertDictEqual(__A						,{'a.1.a': 0, 'a.2.a': 2}  )






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
      import torch

      from transformers.generation import DisjunctiveConstraint





@require_torch
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					List[Any]:
         # For consistency across different places the DisjunctiveConstraint is called,
         # dc.token_ids is a list of integers. It is also initialized only by integers.

         _lowercase							= [[1, 2, 4], [1, 2, 3, 4]]
         _lowercase							= DisjunctiveConstraint(__A  )
         self.assertTrue(isinstance(dc.token_ids						,__A  )  )

         with self.assertRaises(__A  ):
           DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]  )  )

         with self.assertRaises(__A  ):
           DisjunctiveConstraint([torch.LongTensor([1, 2, 4]  ), torch.LongTensor([1, 2, 3, 4, 5]  )]  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					str:
         # We can't have constraints that are complete subsets of another. This leads to a preverse
         # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
         # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
         # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
         # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
         _lowercase							= [[1, 2], [1, 2, 3, 4]]

         with self.assertRaises(__A  ):
           DisjunctiveConstraint(__A  )  # fails here







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[Any]:
         _lowercase							= [[1, 2, 3], [1, 2, 4]]

         _lowercase							= DisjunctiveConstraint(__A  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(1  )
         _lowercase							= stepped is True and completed is False and reset is False
         self.assertTrue(__A  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(2  )
         _lowercase							= stepped is True and completed is False and reset is False
         self.assertTrue(__A  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1, 2]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(3  )
         _lowercase							= stepped is True and completed is True and reset is False
         self.assertTrue(__A  )
         self.assertTrue(dc.completed  )  # Completed!
         self.assertTrue(dc.current_seq == [1, 2, 3]  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					Any:
         _lowercase							= [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

         _lowercase							= DisjunctiveConstraint(__A  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(1  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(2  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1, 2]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(4  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1, 2, 4]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(5  )
         self.assertTrue(dc.completed  )  # Completed!
         self.assertTrue(dc.current_seq == [1, 2, 4, 5]  )

         dc.reset()

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(1  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.remaining() == 3  )
         self.assertTrue(dc.current_seq == [1]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(2  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.remaining() == 2  )
         self.assertTrue(dc.current_seq == [1, 2]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(5  )
         self.assertTrue(dc.completed  )  # Completed!
         self.assertTrue(dc.remaining() == 0  )
         self.assertTrue(dc.current_seq == [1, 2, 5]  )