




import gc
import importlib.metadata
import tempfile
import unittest

from packaging import version

from transformers import (
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForSeqaSeqLM,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    BitsAndBytesConfig,
    pipeline,
)
from transformers.testing_utils import (
    is_torch_available,
    require_accelerate,
    require_bitsandbytes,
    require_torch,
    require_torch_gpu,
    require_torch_multi_gpu,
    slow,
)

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	List[str]					):


      '''simple docstring'''



      if model.config.model_type == "gpt2":
            return model.transformer.h[0].mlp.c_fc
      return model.transformer.h[0].mlp.dense_ah_to_h


if is_torch_available():
 import torch
 import torch.nn as nn







 class   A(						nn.Module       ):



  '''simple docstring'''


  def __init__(    self :  Union[str, Any]						,							A_ :  nn.Module						,							A_ :  int  )       ->     str:

        """simple docstring"""






        super().__init__()
        lowerCamelCase_							=   module
        lowerCamelCase_							=   nn.Sequential(
            nn.Linear(module.in_features						,							A_						,							bias=A_  )						,							nn.Linear(A_						,							module.out_features						,							bias=A_  )						,							)
        lowerCamelCase_							=   (2.0 / (5 * min(module.in_features						,							module.out_features  ))) ** 0.5
        nn.init.normal_(self.adapter[0].weight						,							std=A_  )
        nn.init.zeros_(self.adapter[1].weight  )
        self.adapter.to(module.weight.device  )





  def     a__			(    self :  List[Any]						,							A_ :  Any						,							*A_ :  Optional[int]						,							**A_ :  Optional[Any]  )       ->     Optional[int]:

        """simple docstring"""






        return self.module(A_						,							*A_						,							**A_  ) + self.adapter(A_  )







@require_bitsandbytes
@require_accelerate
@require_torch
@require_torch_gpu
@slow
class   A(						unittest.TestCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						'''bigscience/bloom-1b7'''

 # Constant values
 UpperCamelCase						       =						2.109_6595_5269_2574

 UpperCamelCase						       =						'''Hello my name is'''
 UpperCamelCase						       =						set()
 EXPECTED_OUTPUTS.add('''Hello my name is John and I am a professional photographer. I'''       )
 EXPECTED_OUTPUTS.add('''Hello my name is John.\nI am a friend of your father.\n'''       )
 EXPECTED_OUTPUTS.add('''Hello my name is John Doe, I am a student at the University'''       )
 UpperCamelCase						       =						10

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

       """simple docstring"""






       lowerCamelCase_							=   AutoTokenizer.from_pretrained(self.model_name  )







class   A(						UpperCamelCase       ):



 '''simple docstring'''


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

       """simple docstring"""






       super().setUp()

       # Models and tokenizer
       lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(
           self.model_name						,							torch_dtype=torch.floataa						,							device_map='auto'  )
       lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(self.model_name						,							load_in_abit=A_						,							device_map='auto'  )

 def     a__			(    self :  Tuple  )       ->     Optional[int]:

       """simple docstring"""






       del self.model_fpaa
       del self.model_abit

       gc.collect()
       torch.cuda.empty_cache()

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

       """simple docstring"""






       lowerCamelCase_							=   self.model_abit.config

       self.assertTrue(hasattr(A_						,							'quantization_config'  )  )

       lowerCamelCase_							=   config.to_dict()
       lowerCamelCase_							=   config.to_diff_dict()

       lowerCamelCase_							=   config.to_json_string()

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

       """simple docstring"""






       from bitsandbytes.nn import Paramsabit

       lowerCamelCase_							=   self.model_fpaa.get_memory_footprint()
       lowerCamelCase_							=   self.model_abit.get_memory_footprint()

       self.assertAlmostEqual(mem_fpaa / mem_abit						,							self.EXPECTED_RELATIVE_DIFFERENCE  )
       lowerCamelCase_							=   get_some_linear_layer(self.model_abit  )
       self.assertTrue(linear.weight.__class__ == Paramsabit  )

 def     a__			(    self :  Tuple  )       ->     List[Any]:

       """simple docstring"""






       from transformers import TaPreTrainedModel

       self.model_fpaa.get_memory_footprint()
       self.model_abit.get_memory_footprint()

       for name, module in self.model_abit.named_modules():
             if isinstance(A_						,							torch.nn.Linear  ):
                   if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules:
                         # 4-bit parameters are packed in uint8 variables
                         self.assertTrue(module.weight.dtype == torch.uinta  )

 def     a__			(    self :  str  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  )
       lowerCamelCase_							=   self.model_abit.generate(input_ids=encoded_input['input_ids'].to(0  )						,							max_new_tokens=10  )

       self.assertIn(self.tokenizer.decode(output_sequences[0]						,							skip_special_tokens=A_  )						,							self.EXPECTED_OUTPUTS  )

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

       """simple docstring"""






       lowerCamelCase_							=   BitsAndBytesConfig()
       lowerCamelCase_							=   True

       lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(
           self.model_name						,							quantization_config=A_						,							device_map='auto'  )

       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  )
       lowerCamelCase_							=   model_abit_from_config.generate(
           input_ids=encoded_input['input_ids'].to(0  )						,							max_new_tokens=10  )

       self.assertIn(self.tokenizer.decode(output_sequences[0]						,							skip_special_tokens=A_  )						,							self.EXPECTED_OUTPUTS  )

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

       """simple docstring"""






       with self.assertRaises(A_  ), tempfile.TemporaryDirectory() as tmpdirname:
             self.model_abit.save_pretrained(A_  )

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

       """simple docstring"""






       lowerCamelCase_							=   BitsAndBytesConfig()

       with self.assertRaises(A_  ):
             lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(
                 self.model_name						,							quantization_config=A_						,							load_in_abit=A_						,							device_map='auto'						,							bnb_abit_quant_type='nf4'						,							)

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

       """simple docstring"""






       with self.assertRaises(A_  ):
             # Tries with `str`
             self.model_abit.to('cpu'  )

       with self.assertRaises(A_  ):
             # Tries with a `dtype``
             self.model_abit.to(torch.floataa  )

       with self.assertRaises(A_  ):
             # Tries with a `device`
             self.model_abit.to(torch.device('cuda:0'  )  )

       with self.assertRaises(A_  ):
             # Tries with a `device`
             self.model_abit.float()

       with self.assertRaises(A_  ):
             # Tries with a `device`
             self.model_abit.half()

       # Test if we did not break anything
       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  )

       lowerCamelCase_							=   self.model_fpaa.to(torch.floataa  )
       lowerCamelCase_							=   self.model_fpaa.generate(input_ids=encoded_input['input_ids'].to(0  )						,							max_new_tokens=10  )

       # Check this does not throw an error
       lowerCamelCase_							=   self.model_fpaa.to('cpu'  )

       # Check this does not throw an error
       lowerCamelCase_							=   self.model_fpaa.half()

       # Check this does not throw an error
       lowerCamelCase_							=   self.model_fpaa.float()





 def     a__			(    self :  int  )       ->     List[str]:

       """simple docstring"""






       lowerCamelCase_							=   AutoModelForSeqaSeqLM.from_pretrained('t5-small'						,							load_in_abit=A_						,							device_map='auto'  )
       self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa  )







@require_bitsandbytes
@require_accelerate
@require_torch
@require_torch_gpu
@slow
class   A(						unittest.TestCase       ):



 '''simple docstring'''


 @classmethod
 def     a__			(    cls :  str  )       ->     Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_							=   't5-small'
       lowerCamelCase_							=   'google/flan-t5-small'  # flan-t5 uses dense-act instead of dense-relu-dense
       lowerCamelCase_							=   AutoTokenizer.from_pretrained(cls.model_name  )
       lowerCamelCase_							=   'Translate in German: Hello, my dog is cute'

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

       """simple docstring"""






       gc.collect()
       torch.cuda.empty_cache()

 def     a__			(    self :  List[str]  )       ->     str:

       """simple docstring"""






       from transformers import TaForConditionalGeneration

       lowerCamelCase_							=   TaForConditionalGeneration._keep_in_fpaa_modules
       lowerCamelCase_							=   None

       # test with `t5-small`
       lowerCamelCase_							=   TaForConditionalGeneration.from_pretrained(self.model_name						,							load_in_abit=A_						,							device_map='auto'  )
       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  ).to(0  )
       lowerCamelCase_							=   model.generate(**A_  )

       # test with `flan-t5-small`
       lowerCamelCase_							=   TaForConditionalGeneration.from_pretrained(
           self.dense_act_model_name						,							load_in_abit=A_						,							device_map='auto'  )
       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  ).to(0  )
       lowerCamelCase_							=   model.generate(**A_  )
       lowerCamelCase_							=   modules





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

       """simple docstring"""






       import bitsandbytes as bnb

       from transformers import TaForConditionalGeneration

       # test with `t5-small`
       lowerCamelCase_							=   TaForConditionalGeneration.from_pretrained(self.model_name						,							load_in_abit=A_						,							device_map='auto'  )

       # there was a bug with decoders - this test checks that it is fixed
       self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q						,							bnb.nn.Linearabit  )  )

       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  ).to(0  )
       lowerCamelCase_							=   model.generate(**A_  )

       # test with `flan-t5-small`
       lowerCamelCase_							=   TaForConditionalGeneration.from_pretrained(
           self.dense_act_model_name						,							load_in_abit=A_						,							device_map='auto'  )
       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  ).to(0  )
       lowerCamelCase_							=   model.generate(**A_  )







class   A(						UpperCamelCase       ):



 '''simple docstring'''


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

       """simple docstring"""






       super().setUp()
       # model_name
       lowerCamelCase_							=   'bigscience/bloom-560m'
       lowerCamelCase_							=   't5-small'

       # Different types of model

       lowerCamelCase_							=   AutoModel.from_pretrained(self.model_name						,							load_in_abit=A_						,							device_map='auto'  )
       # Sequence classification model
       lowerCamelCase_							=   AutoModelForSequenceClassification.from_pretrained(
           self.model_name						,							load_in_abit=A_						,							device_map='auto'  )
       # CausalLM model
       lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(self.model_name						,							load_in_abit=A_						,							device_map='auto'  )
       # Seq2seq model
       lowerCamelCase_							=   AutoModelForSeqaSeqLM.from_pretrained(
           self.seq_to_seq_name						,							load_in_abit=A_						,							device_map='auto'  )

 def     a__			(    self :  str  )       ->     List[str]:

       """simple docstring"""






       del self.base_model
       del self.sequence_model
       del self.model_abit
       del self.seq_to_seq_model

       gc.collect()
       torch.cuda.empty_cache()





 def     a__			(    self :  Any  )       ->     Any:

       """simple docstring"""






       from bitsandbytes.nn import Paramsabit

       self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit  )

       # Other heads should be nn.Parameter
       self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter  )
       self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter  )
       self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter  )







class   A(						UpperCamelCase       ):



 '''simple docstring'''


 def     a__			(    self :  Tuple  )       ->     Dict:

       """simple docstring"""






       super().setUp()

 def     a__			(    self :  Optional[int]  )       ->     Tuple:

       """simple docstring"""






       del self.pipe

       gc.collect()
       torch.cuda.empty_cache()





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

       """simple docstring"""






       lowerCamelCase_							=   pipeline(
           'text-generation'						,							model=self.model_name						,							model_kwargs={'device_map': 'auto', 'load_in_4bit': True, 'torch_dtype': torch.floataa}						,							max_new_tokens=self.MAX_NEW_TOKENS						,							)

       # Real second forward pass
       lowerCamelCase_							=   self.pipe(self.input_text  )
       self.assertIn(pipeline_output[0]['generated_text']						,							self.EXPECTED_OUTPUTS  )







@require_torch_multi_gpu
class   A(						UpperCamelCase       ):



 '''simple docstring'''


 def     a__			(    self :  int  )       ->     List[str]:

       """simple docstring"""






       super().setUp()





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

       """simple docstring"""






       lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(
           self.model_name						,							load_in_abit=A_						,							device_map='balanced'  )

       # Check correct device map
       self.assertEqual(set(model_parallel.hf_device_map.values()  )						,							{0, 1}  )

       # Check that inference pass works on the model
       lowerCamelCase_							=   self.tokenizer(self.input_text						,							return_tensors='pt'  )

       # Second real batch
       lowerCamelCase_							=   model_parallel.generate(input_ids=encoded_input['input_ids'].to(0  )						,							max_new_tokens=10  )
       self.assertIn(self.tokenizer.decode(output_parallel[0]						,							skip_special_tokens=A_  )						,							self.EXPECTED_OUTPUTS  )







class   A(						UpperCamelCase       ):



 '''simple docstring'''


 def     a__			(    self :  Dict  )       ->     List[str]:

       """simple docstring"""






       lowerCamelCase_							=   'facebook/opt-350m'
       super().setUp()





 def     a__			(    self :  List[str]  )       ->     int:

       """simple docstring"""






       if version.parse(importlib.metadata.version('bitsandbytes'  )  ) < version.parse('0.37.0'  ):
             return

       # Step 1: freeze all parameters
       lowerCamelCase_							=   AutoModelForCausalLM.from_pretrained(self.model_name						,							load_in_abit=A_  )

       self.assertEqual(set(model.hf_device_map.values()  )						,							{torch.cuda.current_device()}  )

       for param in model.parameters():
             lowerCamelCase_							=   False  # freeze the model - train adapters later
             if param.ndim == 1:
                   # cast the small parameters (e.g. layernorm) to fp32 for stability
                   lowerCamelCase_							=   param.data.to(torch.floataa  )

        # Step 2: add adapters
       for _, module in model.named_modules():
             if "OPTAttention" in repr(type(A_  )  ):
                   lowerCamelCase_							=   LoRALayer(module.q_proj						,							rank=16  )
                   lowerCamelCase_							=   LoRALayer(module.k_proj						,							rank=16  )
                   lowerCamelCase_							=   LoRALayer(module.v_proj						,							rank=16  )

        # Step 3: dummy batch
       lowerCamelCase_							=   self.tokenizer('Test batch '						,							return_tensors='pt'  ).to(0  )

       # Step 4: Check if the gradient is not None
       with torch.cuda.amp.autocast():
             lowerCamelCase_							=   model.forward(**A_  )
             out.logits.norm().backward()

       for module in model.modules():
             if isinstance(A_						,							A_  ):
                   self.assertTrue(module.adapter[1].weight.grad is not None  )
                   self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0  )
             elif isinstance(A_						,							nn.Embedding  ):
                   self.assertTrue(module.weight.grad is None  )







class   A(						UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						'''gpt2-xl'''
 UpperCamelCase						       =						3.3191_8548_5415_2187









import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class   A(						UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						['''image_processor''', '''tokenizer''']
 UpperCamelCase						       =						'''OwlViTImageProcessor'''
 UpperCamelCase						       =						('''CLIPTokenizer''', '''CLIPTokenizerFast''')

 def __init__(    self :  Optional[Any]						,							A_ :  Tuple=None						,							A_ :  Tuple=None						,							**A_ :  List[str]  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   None
       if "feature_extractor" in kwargs:
             warnings.warn(
                 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                 ' instead.'						,							A_						,							)
             lowerCamelCase_							=   kwargs.pop('feature_extractor'  )

       lowerCamelCase_							=   image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
             raise ValueError('You need to specify an `image_processor`.'  )
       if tokenizer is None:
             raise ValueError('You need to specify a `tokenizer`.'  )

       super().__init__(A_						,							A_  )

 def __call__(    self :  List[str]						,							A_ :  List[str]=None						,							A_ :  List[Any]=None						,							A_ :  Dict=None						,							A_ :  Tuple="max_length"						,							A_ :  int="np"						,							**A_ :  Union[str, Any]  )       ->     Union[str, Any]:

       """simple docstring"""






       if text is None and query_images is None and images is None:
             raise ValueError(
                 'You have to specify at least one text or query image or image. All three cannot be none.'  )

       if text is not None:
             if isinstance(A_						,							A_  ) or (isinstance(A_						,							A_  ) and not isinstance(text[0]						,							A_  )):
                   lowerCamelCase_							=   [self.tokenizer(A_						,							padding=A_						,							return_tensors=A_						,							**A_  )]

             elif isinstance(A_						,							A_  ) and isinstance(text[0]						,							A_  ):
                   lowerCamelCase_							=   []

                   # Maximum number of queries across batch
                   lowerCamelCase_							=   max([len(A_  ) for t in text]  )

                   # Pad all batch samples to max number of text queries
                   for t in text:
                         if len(A_  ) != max_num_queries:
                               lowerCamelCase_							=   t + [' '] * (max_num_queries - len(A_  ))

                         lowerCamelCase_							=   self.tokenizer(A_						,							padding=A_						,							return_tensors=A_						,							**A_  )
                         encodings.append(A_  )
             else:
                   raise TypeError('Input text should be a string, a list of strings or a nested list of strings'  )

             if return_tensors == "np":
                   lowerCamelCase_							=   np.concatenate([encoding['input_ids'] for encoding in encodings]						,							axis=0  )
                   lowerCamelCase_							=   np.concatenate([encoding['attention_mask'] for encoding in encodings]						,							axis=0  )

             elif return_tensors == "jax" and is_flax_available():
                   import jax.numpy as jnp

                   lowerCamelCase_							=   jnp.concatenate([encoding['input_ids'] for encoding in encodings]						,							axis=0  )
                   lowerCamelCase_							=   jnp.concatenate([encoding['attention_mask'] for encoding in encodings]						,							axis=0  )

             elif return_tensors == "pt" and is_torch_available():
                   import torch

                   lowerCamelCase_							=   torch.cat([encoding['input_ids'] for encoding in encodings]						,							dim=0  )
                   lowerCamelCase_							=   torch.cat([encoding['attention_mask'] for encoding in encodings]						,							dim=0  )

             elif return_tensors == "tf" and is_tf_available():
                   import tensorflow as tf

                   lowerCamelCase_							=   tf.stack([encoding['input_ids'] for encoding in encodings]						,							axis=0  )
                   lowerCamelCase_							=   tf.stack([encoding['attention_mask'] for encoding in encodings]						,							axis=0  )

             else:
                   raise ValueError('Target return tensor type could not be returned'  )

             lowerCamelCase_							=   BatchEncoding()
             lowerCamelCase_							=   input_ids
             lowerCamelCase_							=   attention_mask

       if query_images is not None:
             lowerCamelCase_							=   BatchEncoding()
             lowerCamelCase_							=   self.image_processor(
                 A_						,							return_tensors=A_						,							**A_  ).pixel_values
             lowerCamelCase_							=   query_pixel_values

       if images is not None:
             lowerCamelCase_							=   self.image_processor(A_						,							return_tensors=A_						,							**A_  )

       if text is not None and images is not None:
             lowerCamelCase_							=   image_features.pixel_values
             return encoding
       elif query_images is not None and images is not None:
             lowerCamelCase_							=   image_features.pixel_values
             return encoding
       elif text is not None or query_images is not None:
             return encoding
       else:
             return BatchEncoding(data=dict(**A_  )						,							tensor_type=A_  )

 def     a__			(    self :  Tuple						,							*A_ :  Dict						,							**A_ :  Dict  )       ->     Any:

       """simple docstring"""






       return self.image_processor.post_process(*A_						,							**A_  )

 def     a__			(    self :  List[str]						,							*A_ :  Any						,							**A_ :  List[Any]  )       ->     Optional[int]:

       """simple docstring"""






       return self.image_processor.post_process_object_detection(*A_						,							**A_  )

 def     a__			(    self :  Any						,							*A_ :  str						,							**A_ :  List[Any]  )       ->     Any:

       """simple docstring"""






       return self.image_processor.post_process_image_guided_detection(*A_						,							**A_  )

 def     a__			(    self :  Union[str, Any]						,							*A_ :  Any						,							**A_ :  Union[str, Any]  )       ->     str:

       """simple docstring"""






       return self.tokenizer.batch_decode(*A_						,							**A_  )

 def     a__			(    self :  Optional[int]						,							*A_ :  List[Any]						,							**A_ :  int  )       ->     int:

       """simple docstring"""






       return self.tokenizer.decode(*A_						,							**A_  )

 @property
 def     a__			(    self :  List[Any]  )       ->     List[Any]:

       """simple docstring"""






       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'						,							A_						,							)
       return self.image_processor_class





 @property
 def     a__			(    self :  str  )       ->     List[str]:

       """simple docstring"""






       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'						,							A_						,							)
       return self.image_processor



