
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 __snake_case		(				nn.Module					):
    def __init__(  self				):



           '''simple docstring'''







           super().__init__()
           lowercase							:					Optional[int]	     =				nn.Linear(3		,4				)
           lowercase							:					Tuple	     =				nn.BatchNormad(4				)
           lowercase							:					int	     =				nn.Linear(4		,5				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case				):



           '''simple docstring'''







           return self.lineara(self.batchnorm(self.lineara(snake_case				)				)				)






class __snake_case		(				unittest.TestCase					):
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					Tuple	     =				ModelForTest()
           with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(snake_case		,model.state_dict()				)
                  lowercase							:					Dict	     =				os.path.join(snake_case		,"""index.json"""				)
                  self.assertTrue(os.path.isfile(snake_case				)				)
                  # TODO: add tests on what is inside the index

                  for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]:
                         lowercase							:					Union[str, Any]	     =				os.path.join(snake_case		,f"{key}.dat"				)
                         self.assertTrue(os.path.isfile(snake_case				)				)
                         # TODO: add tests on the fact weights are properly loaded
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					Optional[Any]	     =				[torch.floataa, torch.floataa, torch.bfloataa]

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

                         lowercase							:					int	     =				load_offloaded_weight(snake_case		,index["""weight"""]				)
                         self.assertTrue(torch.equal(snake_case		,snake_case				)				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					Union[str, Any]	     =				ModelForTest()
           lowercase							:					Tuple	     =				model.state_dict()
           lowercase							:					str	     =				{k: v for k, v in state_dict.items() if """linear2""" not in k}
           lowercase							:					Dict	     =				{k: v for k, v in state_dict.items() if """linear2""" in k}

           with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(snake_case		,snake_case				)
                  lowercase							:					int	     =				OffloadedWeightsLoader(state_dict=snake_case		,save_folder=snake_case				)

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

           lowercase							:					Optional[int]	     =				{k: v for k, v in state_dict.items() if """weight""" in k}
           lowercase							:					Optional[int]	     =				{k: v for k, v in state_dict.items() if """weight""" not in k}

           with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(snake_case		,snake_case				)
                  lowercase							:					List[str]	     =				OffloadedWeightsLoader(state_dict=snake_case		,save_folder=snake_case				)

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

           with TemporaryDirectory() as tmp_dir:
                  offload_state_dict(snake_case		,snake_case				)
                  # Duplicates are removed
                  lowercase							:					Optional[int]	     =				OffloadedWeightsLoader(state_dict=snake_case		,save_folder=snake_case				)

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



           '''simple docstring'''







           lowercase							:					str	     =				{"""a.1""": 0, """a.10""": 1, """a.2""": 2}
           lowercase							:					Tuple	     =				extract_submodules_state_dict(snake_case		,["""a.1""", """a.2"""]				)
           self.assertDictEqual(snake_case		,{"""a.1""": 0, """a.2""": 2}				)

           lowercase							:					Tuple	     =				{"""a.1.a""": 0, """a.10.a""": 1, """a.2.a""": 2}
           lowercase							:					Tuple	     =				extract_submodules_state_dict(snake_case		,["""a.1""", """a.2"""]				)
           self.assertDictEqual(snake_case		,{"""a.1.a""": 0, """a.2.a""": 2}				)


from ...processing_utils import ProcessorMixin

class __snake_case		(				lowerCAmelCase					):
    _a		:						Union[str, Any]=    "WhisperFeatureExtractor"
    _a		:						int=    "WhisperTokenizer"
    def __init__(  self		,snake_case		,snake_case				):



           '''simple docstring'''







           super().__init__(snake_case		,snake_case				)
           lowercase							:					Optional[int]	     =				self.feature_extractor
           lowercase							:					Tuple	     =				False
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case=None		,snake_case=None		,snake_case=True				):



           '''simple docstring'''







           return self.tokenizer.get_decoder_prompt_ids(task=snake_case		,language=snake_case		,no_timestamps=snake_case				)
    def __call__(  self		,*snake_case		,**snake_case				):



           '''simple docstring'''







           if self._in_target_context_manager:
                  return self.current_processor(*snake_case		,**snake_case				)

           lowercase							:					Optional[Any]	     =				kwargs.pop("""audio"""		,snake_case				)
           lowercase							:					str	     =				kwargs.pop("""sampling_rate"""		,snake_case				)
           lowercase							:					Dict	     =				kwargs.pop("""text"""		,snake_case				)
           if len(snake_case				) > 0:
                  lowercase							:					List[Any]	     =				args[0]
                  lowercase							:					Tuple	     =				args[1:]

           if audio is None and text is None:
                  raise ValueError("""You need to specify either an `audio` or `text` input to process."""				)

           if audio is not None:
                  lowercase							:					Any	     =				self.feature_extractor(snake_case		,*snake_case		,sampling_rate=snake_case		,**snake_case				)
           if text is not None:
                  lowercase							:					str	     =				self.tokenizer(snake_case		,**snake_case				)

           if text is None:
                  return inputs

           elif audio is None:
                  return encodings
           else:
                  lowercase							:					List[Any]	     =				encodings["""input_ids"""]
                  return inputs
    def 				_SCREAMING_SNAKE_CASE	(  self		,*snake_case		,**snake_case				):



           '''simple docstring'''







           return self.tokenizer.batch_decode(*snake_case		,**snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,*snake_case		,**snake_case				):



           '''simple docstring'''







           return self.tokenizer.decode(*snake_case		,**snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case="np"				):



           '''simple docstring'''







           return self.tokenizer.get_prompt_ids(snake_case		,return_tensors=snake_case				)
