





"""simple docstring"""

import logging
import re

import pytorch_quantization
import pytorch_quantization.nn as quant_nn
import torch
from pytorch_quantization import calib
from pytorch_quantization.tensor_quant import QuantDescriptor


__UpperCAmelCase        =logging.getLogger(__name__)

__UpperCAmelCase        =50  # max width of layer names
__UpperCAmelCase        =70  # max width of quantizer names



def        __a							(			A )       ->						Optional[Any]:



   '''simple docstring'''



   A__											=					parser.add_argument_group("quant_trainer arguments" )
   group.add_argument("--wprec" ,	type=__A ,	default=8 ,	help="weight precision" )
   group.add_argument("--aprec" ,	type=__A ,	default=8 ,	help="activation precision" )
   group.add_argument("--quant-per-tensor" ,	action="store_true" ,	help="per tensor weight scaling" )
   group.add_argument("--quant-disable" ,	action="store_true" ,	help="disable all quantizers" )
   group.add_argument("--quant-disable-embeddings" ,	action="store_true" ,	help="disable all embeddings quantizers" )
   group.add_argument("--quant-disable-keyword" ,	type=__A ,	nargs="+" ,	help="disable quantizers by keyword" )
   group.add_argument("--quant-disable-layer-module" ,	type=__A ,	help="disable quantizers by keyword under layer." )
   group.add_argument("--quant-enable-layer-module" ,	type=__A ,	help="enable quantizers by keyword under layer" )
   group.add_argument("--calibrator" ,	default="max" ,	help="which quantization range calibrator to use" )
   group.add_argument("--percentile" ,	default=__A ,	type=__A ,	help="percentile for PercentileCalibrator" )
   group.add_argument("--fuse-qkv" ,	action="store_true" ,	help="use the same scale factor for qkv" )
   group.add_argument("--clip-gelu" ,	metavar="N" ,	type=__A ,	help="clip gelu output maximum value to N" )
   group.add_argument(
       "--recalibrate-weights" ,	action="store_true" ,	help=(
           "recalibrate weight amaxes by taking the max of the weights."
           " amaxes will be computed with the current quantization granularity (axis)."
       ) ,	)



def        __a							(			A )       ->						Optional[int]:



   '''simple docstring'''



   if args.calibrator == "max":
      A__											=					'''max'''
   elif args.calibrator == "percentile":
      if args.percentile is None:
         raise ValueError("Specify --percentile when using percentile calibrator" )
      A__											=					'''histogram'''
   elif args.calibrator == "mse":
      A__											=					'''histogram'''
   else:
      raise ValueError(f"""Invalid calibrator {args.calibrator}""" )

   A__											=					QuantDescriptor(num_bits=args.aprec ,	calib_method=__A )
   A__											=					QuantDescriptor(num_bits=args.wprec ,	axis=(None if args.quant_per_tensor else (0,)) )
   quant_nn.QuantLinear.set_default_quant_desc_input(__A )
   quant_nn.QuantLinear.set_default_quant_desc_weight(__A )



def        __a							(			A ,	A ,	A=False ,	A=False )       ->						List[Any]:



   '''simple docstring'''



   logger.info("Configuring Model for Quantization" )
   logger.info(f"""using quantization package {pytorch_quantization.__file__}""" )

   if not calib:
      if args.quant_disable_embeddings:
         set_quantizer_by_name(__A ,	["embeddings"] ,	which="weight" ,	_disabled=__A )

      if args.quant_disable:
         set_quantizer_by_name(__A ,	[""] ,	_disabled=__A )

      if args.quant_disable_keyword:
         set_quantizer_by_name(__A ,	args.quant_disable_keyword ,	_disabled=__A )

      if args.quant_disable_layer_module:
         set_quantizer_by_name(__A ,	[R"layer.\d+." + args.quant_disable_layer_module] ,	_disabled=__A )

      if args.quant_enable_layer_module:
         set_quantizer_by_name(__A ,	[R"layer.\d+." + args.quant_enable_layer_module] ,	_disabled=__A )

      if args.recalibrate_weights:
         recalibrate_weights(__A )

      if args.fuse_qkv:
         fuse_qkv(__A ,	__A )

   if args.clip_gelu:
      clip_gelu(__A ,	args.clip_gelu )

   # if args.local_rank in [-1, 0] and not calib:
   print_quant_summary(__A )



def        __a							(			A )       ->						Tuple:



   '''simple docstring'''



   logger.info("Enabling Calibration" )
   for name, module in model.named_modules():
      if name.endswith("_quantizer" ):
         if module._calibrator is not None:
            module.disable_quant()
            module.enable_calib()
         else:
            module.disable()
         logger.info(f"""{name:80}: {module}""" )



def        __a							(			A ,	A )       ->						Optional[int]:



   '''simple docstring'''



   logger.info("Loading calibrated amax" )
   for name, module in model.named_modules():
      if name.endswith("_quantizer" ):
         if module._calibrator is not None:
            if isinstance(module._calibrator ,	calib.MaxCalibrator ):
               module.load_calib_amax()
            else:
               module.load_calib_amax("percentile" ,	percentile=args.percentile )
            module.enable_quant()
            module.disable_calib()
         else:
            module.enable()
   model.cuda()
   print_quant_summary(__A )



def        __a							(			A ,	A )       ->						Union[str, Any]:



   '''simple docstring'''



   def fusea(A ,	A ,	A ):
      for mod in [qq, qk, qv]:
         if not hasattr(__A ,	"_amax" ):
            print("          WARNING: NO AMAX BUFFER" )
            return
      A__											=					qq._amax.detach().item()
      A__											=					qk._amax.detach().item()
      A__											=					qv._amax.detach().item()

      A__											=					max(__A ,	__A ,	__A )
      qq._amax.fill_(__A )
      qk._amax.fill_(__A )
      qv._amax.fill_(__A )
      logger.info(f"""          q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}""" )

   for name, mod in model.named_modules():
      if name.endswith(".attention.self" ):
         logger.info(f"""FUSE_QKV: {name:{name_width}}""" )
         fusea(mod.matmul_q_input_quantizer ,	mod.matmul_k_input_quantizer ,	mod.matmul_v_input_quantizer )
         if args.quant_per_tensor:
            fusea(mod.query._weight_quantizer ,	mod.key._weight_quantizer ,	mod.value._weight_quantizer )



def        __a							(			A ,	A )       ->						Optional[int]:



   '''simple docstring'''



   for name, mod in model.named_modules():
      if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ):
         A__											=					mod._input_quantizer._amax.data.detach().item()
         mod._input_quantizer._amax.data.detach().clamp_(max=__A )
         A__											=					mod._input_quantizer._amax.data.detach().item()
         logger.info(f"""CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}""" )



def        __a							(			A )       ->						List[Any]:



   '''simple docstring'''



   for name, mod in model.named_modules():
      if hasattr(__A ,	"_weight_quantizer" ) and mod._weight_quantizer.axis is not None:
         A__											=					mod.weight.shape[0]
         A__											=					mod._weight_quantizer._amax.detach()
         A__											=					torch.ones(__A ,	dtype=amax.dtype ,	device=amax.device ) * amax
         print(f"""expanding {name} {amax} -> {mod._weight_quantizer._amax}""" )



def        __a							(			A )       ->						Any:



   '''simple docstring'''



   for name, mod in model.named_modules():
      if hasattr(__A ,	"_weight_quantizer" ):
         if not hasattr(mod.weight_quantizer ,	"_amax" ):
            print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" )
            continue

         # determine which axes to reduce across
         # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3)
         A__											=					set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis )
         A__											=					set(range(len(mod.weight.size() ) ) ) - axis_set
         A__											=					pytorch_quantization.utils.reduce_amax(mod.weight ,	axis=__A ,	keepdims=__A ).detach()
         logger.info(f"""RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}""" )
         A__											=					amax



def        __a							(			A ,	A=25 ,	A=180 ,	A=None )       ->						Optional[Any]:



   '''simple docstring'''



   if ignore is None:
      A__											=					[]
   elif not isinstance(__A ,	__A ):
      A__											=					[ignore]

   A__											=					0
   for name, mod in model.named_modules():
      if not hasattr(__A ,	"weight" ):
         continue
      A__											=					max(__A ,	len(__A ) )

   for name, mod in model.named_modules():
      A__											=					getattr(__A ,	"_input_quantizer" ,	__A )
      A__											=					getattr(__A ,	"_weight_quantizer" ,	__A )
      if not hasattr(__A ,	"weight" ):
         continue
      if type(__A ) in ignore:
         continue
      if [True for s in ignore if type(__A ) is str and s in name]:
         continue
      A__											=					f"""Act:{input_q.extra_repr()}"""
      A__											=					f"""Wgt:{weight_q.extra_repr()}"""
      A__											=					f"""{name:{name_width}} {act_str} {wgt_str}"""
      if len(__A ) <= line_width:
         logger.info(__A )
      else:
         logger.info(f"""{name:{name_width}} {act_str}""" )
         logger.info(f"""{"  ":{name_width}} {wgt_str}""" )



def        __a							(			A )       ->						Dict:



   '''simple docstring'''



   A__											=					0
   for name, mod in model.named_modules():
      if isinstance(__A ,	pytorch_quantization.nn.TensorQuantizer ):
         print(f"""{name:80} {mod}""" )
         count += 1
   print(f"""{count} TensorQuantizers found in model""" )



def        __a							(			A ,	A ,	A ,	A ,	A )       ->						List[Any]:



   '''simple docstring'''



   A__											=					getattr(__A ,	__A ,	__A )
   if quantizer_mod is not None:
      assert hasattr(__A ,	__A )
      setattr(__A ,	__A ,	__A )
   else:
      logger.warning(f"""{name} has no {quantizer}""" )



def        __a							(			A ,	A ,	A="both" ,	**A )       ->						Dict:



   '''simple docstring'''



   A__											=					f"""Warning: changing {which} quantizers of {name:{qname_width}}"""
   for k, v in kwargs.items():
      s += f""" {k}={v}"""
      if which in ["input", "both"]:
         set_quantizer(__A ,	__A ,	"_input_quantizer" ,	__A ,	__A )
      if which in ["weight", "both"]:
         set_quantizer(__A ,	__A ,	"_weight_quantizer" ,	__A ,	__A )
   logger.info(__A )



def        __a							(			A ,	A ,	**A )       ->						List[Any]:



   '''simple docstring'''



   for name, mod in model.named_modules():
      if hasattr(__A ,	"_input_quantizer" ) or hasattr(__A ,	"_weight_quantizer" ):
         for n in names:
            if re.search(__A ,	__A ):
               set_quantizers(__A ,	__A ,	**__A )
      elif name.endswith("_quantizer" ):
         for n in names:
            if re.search(__A ,	__A ):
               A__											=					f"""Warning: changing {name:{name_width}}"""
               for k, v in kwargs.items():
                  s += f""" {k}={v}"""
                  setattr(__A ,	__A ,	__A )
               logger.info(__A )







'''simple docstring'''



import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
      import torch
class UpperCAmelCase_       (     __A					):




      """simple docstring"""




      UpperCamelCase_             =      '''char'''
      UpperCamelCase_             =      '''bpe'''
      UpperCamelCase_             =      '''wp'''





SCREAMING_SNAKE_CASE							=						(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)
class UpperCAmelCase_       (     __A					):




      """simple docstring"""




      UpperCamelCase_             =      ['''image_processor''', '''char_tokenizer''']
      UpperCamelCase_             =      '''ViTImageProcessor'''
      UpperCamelCase_             =      '''MgpstrTokenizer'''






      def __init__(			self      :		int  ,   UpperCAmelCase      :		Tuple=None  ,   UpperCAmelCase      :		Dict=None  ,   **UpperCAmelCase      :		Any		)	->	List[str]:


       '''simple docstring'''

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

       lowercase       : Optional[Any]						=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`.'''		)

       lowercase       : Any						=tokenizer
       lowercase       : Dict						=AutoTokenizer.from_pretrained('''gpt2'''		)
       lowercase       : Optional[int]						=AutoTokenizer.from_pretrained('''bert-base-uncased'''		)

       super().__init__(UpperCAmelCase  ,   UpperCAmelCase		)






      def __call__(			self      :		List[Any]  ,   UpperCAmelCase      :		Union[str, Any]=None  ,   UpperCAmelCase      :		List[str]=None  ,   UpperCAmelCase      :		Dict=None  ,   **UpperCAmelCase      :		Union[str, Any]		)	->	int:


       '''simple docstring'''

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

       if images is not None:
        lowercase       : str						=self.image_processor(UpperCAmelCase  ,   return_tensors=UpperCAmelCase  ,   **UpperCAmelCase		)
       if text is not None:
        lowercase       : Any						=self.char_tokenizer(UpperCAmelCase  ,   return_tensors=UpperCAmelCase  ,   **UpperCAmelCase		)

       if text is None:
        return inputs
       elif images is None:
        return encodings
       else:
        lowercase       : List[str]						=encodings['''input_ids''']
        return inputs






      def 					A__   (			self      :		List[Any]  ,   UpperCAmelCase      :		Optional[Any]		)	->	Optional[Any]:


       '''simple docstring'''

       lowercase	,		lowercase	,		lowercase       : Union[str, Any]						=sequences
       lowercase       : Dict						=char_preds.size(0		)

       lowercase	,		lowercase       : Any						=self._decode_helper(UpperCAmelCase  ,   '''char'''		)
       lowercase	,		lowercase       : int						=self._decode_helper(UpperCAmelCase  ,   '''bpe'''		)
       lowercase	,		lowercase       : Optional[Any]						=self._decode_helper(UpperCAmelCase  ,   '''wp'''		)

       lowercase       : Dict						=[]
       lowercase       : List[Any]						=[]
       for i in range(UpperCAmelCase		):
        lowercase       : List[str]						=[char_scores[i], bpe_scores[i], wp_scores[i]]
        lowercase       : Union[str, Any]						=[char_strs[i], bpe_strs[i], wp_strs[i]]
        lowercase       : List[str]						=scores.index(max(UpperCAmelCase		)		)
        final_strs.append(strs[max_score_index]		)
        final_scores.append(scores[max_score_index]		)

       lowercase       : List[Any]						={}
       lowercase       : Optional[Any]						=final_strs
       lowercase       : Optional[Any]						=final_scores
       lowercase       : str						=char_strs
       lowercase       : Tuple						=bpe_strs
       lowercase       : List[str]						=wp_strs
       return out






      def 					A__   (			self      :		str  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		Union[str, Any]		)	->	Optional[int]:


       '''simple docstring'''

       if format == DecodeType.CHARACTER:
        lowercase       : Union[str, Any]						=self.char_decode
        lowercase       : List[str]						=1
        lowercase       : Tuple						='''[s]'''
       elif format == DecodeType.BPE:
        lowercase       : Union[str, Any]						=self.bpe_decode
        lowercase       : Optional[int]						=2
        lowercase       : Optional[Any]						='''#'''
       elif format == DecodeType.WORDPIECE:
        lowercase       : List[str]						=self.wp_decode
        lowercase       : Union[str, Any]						=102
        lowercase       : int						='''[SEP]'''
       else:
        raise ValueError(f'Format {format} is not supported.'		)

       lowercase	,		lowercase       : Union[str, Any]						=[], []
       lowercase       : Optional[int]						=pred_logits.size(0		)
       lowercase       : List[str]						=pred_logits.size(1		)
       lowercase	,		lowercase       : Optional[int]						=pred_logits.topk(1  ,   dim=-1  ,   largest=UpperCAmelCase  ,   sorted=UpperCAmelCase		)
       lowercase       : Tuple						=preds_index.view(-1  ,   UpperCAmelCase		)[:, 1:]
       lowercase       : Any						=decoder(UpperCAmelCase		)
       lowercase	,		lowercase       : List[str]						=torch.nn.functional.softmax(UpperCAmelCase  ,   dim=2		).max(dim=2		)
       lowercase       : List[Any]						=preds_max_prob[:, 1:]

       for index in range(UpperCAmelCase		):
        lowercase       : List[Any]						=preds_str[index].find(UpperCAmelCase		)
        lowercase       : Any						=preds_str[index][:pred_eos]
        lowercase       : Any						=preds_index[index].cpu().tolist()
        lowercase       : Optional[int]						=pred_index.index(UpperCAmelCase		) if eos_token in pred_index else -1
        lowercase       : int						=preds_max_prob[index][: pred_eos_index + 1]
        lowercase       : Tuple						=pred_max_prob.cumprod(dim=0		)[-1] if pred_max_prob.nelement() != 0 else 0.0
        dec_strs.append(UpperCAmelCase		)
        conf_scores.append(UpperCAmelCase		)

       return dec_strs, conf_scores






      def 					A__   (			self      :		List[str]  ,   UpperCAmelCase      :		Optional[int]		)	->	Dict:


       '''simple docstring'''

       lowercase       : Union[str, Any]						=[seq.replace(''' '''  ,   ''''''		) for seq in self.char_tokenizer.batch_decode(UpperCAmelCase		)]
       return decode_strs






      def 					A__   (			self      :		Optional[int]  ,   UpperCAmelCase      :		Optional[Any]		)	->	str:


       '''simple docstring'''

       return self.bpe_tokenizer.batch_decode(UpperCAmelCase		)






      def 					A__   (			self      :		Tuple  ,   UpperCAmelCase      :		Dict		)	->	List[str]:


       '''simple docstring'''

       lowercase       : str						=[seq.replace(''' '''  ,   ''''''		) for seq in self.wp_tokenizer.batch_decode(UpperCAmelCase		)]
       return decode_strs



