

import collections
import json
import os
import re
from typing import TYPE_CHECKING, List, Optional, Tuple

import numpy as np

from ...tokenization_utils_fast import PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
       from transformers.pipelines.conversational import Conversation


snake_case_			  = logging.get_logger(__name__)

snake_case_			  = {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''}

snake_case_			  = {
    '''vocab_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''',
    },
    '''emoji_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''',
    },
}

snake_case_			  = {
    '''abeja/gpt-neox-japanese-2.7b''': 2_048,
}

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    str	,     SCREAMING_SNAKE_CASE_		:    List[Any]			):



     '''simple docstring'''





     with open(SCREAMING_SNAKE_CASE_	,     'r'	,     encoding='utf-8'			) as f:
          lowercase__    :   Tuple      =       json.loads(f.read()			)

     lowercase__    :   Dict      =       collections.OrderedDict()
     lowercase__    :   List[str]      =       collections.OrderedDict()
     lowercase__    :   List[str]      =       collections.OrderedDict()
     with open(SCREAMING_SNAKE_CASE_	,     'r'	,     encoding='utf-8'			) as f:
          lowercase__    :   Optional[Any]      =       f.readlines()
     lowercase__    :   List[Any]      =       [[t.rstrip('\n'			)] if (t == ',' or ',' not in t) else t.rstrip('\n'			).split(','			) for t in token]
     for idx, b in enumerate(SCREAMING_SNAKE_CASE_			):
          lowercase__    :   Dict      =       b
          lowercase__    :   str      =       idx
          for wd in b:
               lowercase__    :   Optional[int]      =       idx

     return vocab, raw_vocab, ids_to_tokens, emoji


class       SCREAMING_SNAKE_CASE__					(__snake_case     ):
    __lowerCamelCase						:       Tuple														=     VOCAB_FILES_NAMES
    __lowerCamelCase						:       Tuple														=     PRETRAINED_VOCAB_FILES_MAP
    __lowerCamelCase						:       Tuple														=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __lowerCamelCase						:       List[Any]														=     ["""input_ids""", """attention_mask"""]





    def __init__(      self						,   a						,   a						,   a="<|endoftext|>"						,   a="<|endoftext|>"						,   a="<|startoftext|>"						,   a="<|endoftext|>"						,   a=False						,   **a						,   ):
         super().__init__(
             unk_token=a						,   pad_token=a						,   bos_token=a						,   eos_token=a						,   do_clean_text=a						,   **a						,   )
         if not os.path.isfile(a):
              raise ValueError(
                  f"""Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained"""
                  ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`')
         if not os.path.isfile(a):
              raise ValueError(
                  f"""Can't find a emoji file at path '{emoji_file}'. To load the emoji information from a Google"""
                  ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`')
         lowercase__    :   str      =       do_clean_text
         lowercase__      , lowercase__      , lowercase__      , lowercase__    :   Tuple      =       load_vocab_and_emoji(a						,   a)
         lowercase__    :   Dict      =       SubWordJapaneseTokenizer(
             vocab=self.vocab						,   ids_to_tokens=self.ids_to_tokens						,   emoji=self.emoji)





    @property
    def       snake_case_				(      self):
         # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab
         return len(self.raw_vocab)





    def       snake_case_				(      self):
         return dict(self.raw_vocab						,   **self.added_tokens_encoder)





    def       snake_case_				(      self						,   a):
         return self.subword_tokenizer.tokenize(a						,   clean=self.do_clean_text)





    def       snake_case_				(      self						,   a):
         return self.vocab.get(a						,   self.vocab.get(self.unk_token))





    def       snake_case_				(      self						,   a):
         return self.subword_tokenizer.convert_id_to_token(a)





    def       snake_case_				(      self						,   a):
         lowercase__    :   int      =       ''.join(a).strip()
         return out_string





    def       snake_case_				(      self						,   a):
         lowercase__    :   Union[str, Any]      =       []
         for is_user, text in conversation.iter_texts():
              input_ids.extend(self.encode(a						,   add_special_tokens=a) + [self.eos_token_id])

         if len(a) > self.model_max_length:
              lowercase__    :   Tuple      =       input_ids[-self.model_max_length :]
         return input_ids





    def       snake_case_				(      self						,   a						,   a = None):
         lowercase__    :   Optional[int]      =       0
         if os.path.isdir(a):
              lowercase__    :   str      =       os.path.join(
                  a						,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])
              lowercase__    :   Optional[Any]      =       os.path.join(
                  a						,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'])
         else:
              lowercase__    :   Union[str, Any]      =       (
                  (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file']
              )
              lowercase__    :   str      =       (
                  (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file']
              )
         with open(a						,   'w'						,   encoding='utf-8') as writer:
              for token_index, token in self.ids_to_tokens.items():
                   if index != token_index:
                        logger.warning(
                            f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."""
                            ' Please check that the vocabulary is not corrupted!')
                        lowercase__    :   Union[str, Any]      =       token_index
                   writer.write(','.join(a) + '\n')
                   index += 1
         with open(a						,   'w'						,   encoding='utf-8') as writer:
              json.dump(self.emoji						,   a)
         return vocab_file, emoji_file



class       SCREAMING_SNAKE_CASE__					(__snake_case     ):





    def __init__(      self						,   a						,   a						,   a):
         lowercase__    :   Union[str, Any]      =       vocab  # same as swe
         lowercase__    :   str      =       ids_to_tokens  # same as bpe
         lowercase__    :   int      =       emoji
         lowercase__    :   Any      =       np.max([len(a) for w in self.vocab.keys()])
         lowercase__    :   int      =       re.compile(r'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)')
         lowercase__    :   Tuple      =       re.compile(r'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*')
         lowercase__    :   str      =       re.compile(r'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}')
         lowercase__    :   Dict      =       re.compile(
             r'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*')
         lowercase__    :   Dict      =       re.compile(
             r'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*')
         lowercase__    :   Union[str, Any]      =       re.compile(
             r'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*')
         lowercase__    :   Optional[Any]      =       '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿'
         lowercase__    :   List[Any]      =       '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟'
         lowercase__    :   Dict      =       str.maketrans({k: '<BLOCK>' for k in keisen + blocks})





    def __len__(      self):
         return len(self.ids_to_tokens)





    def       snake_case_				(      self						,   a):
         lowercase__    :   Optional[Any]      =       self.content_repattera.sub('<URL>'						,   a)
         lowercase__    :   List[str]      =       self.content_repattera.sub('<EMAIL>'						,   a)
         lowercase__    :   List[Any]      =       self.content_repattera.sub('<TEL>'						,   a)
         lowercase__    :   Optional[Any]      =       self.content_repattera.sub('<DATE>'						,   a)
         lowercase__    :   Optional[int]      =       self.content_repattera.sub('<DATE>'						,   a)
         lowercase__    :   Dict      =       self.content_repattera.sub('<PRICE>'						,   a)
         lowercase__    :   List[str]      =       content.translate(self.content_transa)
         while "<BLOCK><BLOCK>" in content:
              lowercase__    :   Optional[Any]      =       content.replace('<BLOCK><BLOCK>'						,   '<BLOCK>')
         return content





    def       snake_case_				(      self						,   a						,   a=False):
         lowercase__    :   str      =       text.replace(' '						,   '<SP>')
         lowercase__    :   Any      =       text.replace('　'						,   '<SP>')
         lowercase__    :   List[str]      =       text.replace('\r\n'						,   '<BR>')
         lowercase__    :   List[Any]      =       text.replace('\n'						,   '<BR>')
         lowercase__    :   List[str]      =       text.replace('\r'						,   '<BR>')
         lowercase__    :   int      =       text.replace('\t'						,   '<TAB>')
         lowercase__    :   List[Any]      =       text.replace('—'						,   'ー')
         lowercase__    :   Union[str, Any]      =       text.replace('−'						,   'ー')
         for k, v in self.emoji["emoji"].items():
              if k in text:
                   lowercase__    :   Optional[Any]      =       text.replace(a						,   a)
         if clean:
              lowercase__    :   Tuple      =       self.clean_text(a)

         def check_simbol(a):
              lowercase__    :   Dict      =       x.encode()
              if len(a) == 1 and len(a) == 2:
                   lowercase__    :   Union[str, Any]      =       (int(e[0]) << 8) + int(e[1])
                   if (
                       (c >= 0xC_2A1 and c <= 0xC_2BF)
                       or (c >= 0xC_780 and c <= 0xC_783)
                       or (c >= 0xC_AB9 and c <= 0xC_BBF)
                       or (c >= 0xC_C80 and c <= 0xC_DA2)
                   ):
                        return True
              return False

         def checkuae(a):
              lowercase__    :   Dict      =       x.encode()
              if len(a) == 1 and len(a) == 3:
                   lowercase__    :   str      =       (int(e[0]) << 16) + (int(e[1]) << 8) + int(e[2])
                   if c >= 0xE28_080 and c <= 0xE2B_07F:
                        return True
              return False

         lowercase__    :   Optional[Any]      =       0
         lowercase__    :   Union[str, Any]      =       []
         while pos < len(a):
              lowercase__    :   str      =       min(len(a)						,   pos + self.maxlen + 1) if text[pos] == '<' else pos + 3
              lowercase__    :   Any      =       []  # (token_id, token, pos)
              for e in range(a						,   a						,   -1):
                   lowercase__    :   Optional[Any]      =       text[pos:e]
                   if wd in self.vocab:
                        if wd[0] == "<" and len(a) > 2:
                             lowercase__    :   Dict      =       [(self.vocab[wd], wd, e)]
                             break
                        else:
                             candidates.append((self.vocab[wd], wd, e))
              if len(a) > 0:
                   # the smallest token_id is adopted
                   lowercase__      , lowercase__      , lowercase__    :   str      =       sorted(a						,   key=lambda a: x[0])[0]
                   result.append(a)
                   lowercase__    :   str      =       e
              else:
                   lowercase__    :   str      =       pos + 1
                   lowercase__    :   Union[str, Any]      =       text[pos:end]
                   if check_simbol(a):
                        result.append('<KIGOU>')
                   elif checkuae(a):
                        result.append('<U2000U2BFF>')
                   else:
                        for i in wd.encode('utf-8'):
                             result.append('<|byte%d|>' % i)
                   lowercase__    :   str      =       end
         return result





    def       snake_case_				(      self						,   a						,   a="\n"):
         lowercase__    :   Union[str, Any]      =       []
         lowercase__    :   List[Any]      =       []
         lowercase__    :   Any      =       self.ids_to_tokens[index][0]
         if word[:6] == "<|byte" and word[-2:] == "|>":
              byte_tokens.append(int(word[6:-2]))
         else:
              if len(a) > 0:
                   words.append(bytearray(a).decode('utf-8'						,   errors='replace'))
                   lowercase__    :   Optional[int]      =       []
              if word[:7] == "<|emoji" and word[-2:] == "|>":
                   words.append(self.emoji['emoji_inv'][word])
              elif word == "<SP>":
                   words.append(' ')
              elif word == "<BR>":
                   words.append(a)
              elif word == "<TAB>":
                   words.append('\t')
              elif word == "<BLOCK>":
                   words.append('▀')
              elif word == "<KIGOU>":
                   words.append('ǀ')
              elif word == "<U2000U2BFF>":
                   words.append('‖')
              else:
                   words.append(a)
         if len(a) > 0:
              words.append(bytearray(a).decode('utf-8'						,   errors='replace'))
         lowercase__    :   str      =       ''.join(a)
         return text




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


snake_case_			  = logging.getLogger(__name__)

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

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Tuple			):



     '''simple docstring'''





     lowercase__    :   str      =       parser.add_argument_group('quant_trainer arguments'			)
     group.add_argument('--wprec'	,     type=SCREAMING_SNAKE_CASE_	,     default=8	,     help='weight precision'			)
     group.add_argument('--aprec'	,     type=SCREAMING_SNAKE_CASE_	,     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=SCREAMING_SNAKE_CASE_	,     nargs='+'	,     help='disable quantizers by keyword'			)
     group.add_argument('--quant-disable-layer-module'	,     type=SCREAMING_SNAKE_CASE_	,     help='disable quantizers by keyword under layer.'			)
     group.add_argument('--quant-enable-layer-module'	,     type=SCREAMING_SNAKE_CASE_	,     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=SCREAMING_SNAKE_CASE_	,     type=SCREAMING_SNAKE_CASE_	,     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=SCREAMING_SNAKE_CASE_	,     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 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    List[Any]			):



     '''simple docstring'''





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

     lowercase__    :   Any      =       QuantDescriptor(num_bits=args.aprec	,     calib_method=SCREAMING_SNAKE_CASE_			)
     lowercase__    :   List[Any]      =       QuantDescriptor(num_bits=args.wprec	,     axis=(None if args.quant_per_tensor else (0,))			)
     quant_nn.QuantLinear.set_default_quant_desc_input(SCREAMING_SNAKE_CASE_			)
     quant_nn.QuantLinear.set_default_quant_desc_weight(SCREAMING_SNAKE_CASE_			)

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[Any]	,     SCREAMING_SNAKE_CASE_		:    List[str]	,     SCREAMING_SNAKE_CASE_		:    Union[str, Any]=False	,     SCREAMING_SNAKE_CASE_		:    Any=False			):



     '''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(SCREAMING_SNAKE_CASE_	,     ['embeddings']	,     which='weight'	,     _disabled=SCREAMING_SNAKE_CASE_			)

          if args.quant_disable:
               set_quantizer_by_name(SCREAMING_SNAKE_CASE_	,     ['']	,     _disabled=SCREAMING_SNAKE_CASE_			)

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

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

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

          if args.recalibrate_weights:
               recalibrate_weights(SCREAMING_SNAKE_CASE_			)

          if args.fuse_qkv:
               fuse_qkv(SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_			)

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

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

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Dict			):



     '''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 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[Any]	,     SCREAMING_SNAKE_CASE_		:    Union[str, Any]			):



     '''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(SCREAMING_SNAKE_CASE_			)

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Any	,     SCREAMING_SNAKE_CASE_		:    Optional[Any]			):



     '''simple docstring'''





     def fusea(SCREAMING_SNAKE_CASE_		:    Any	,     SCREAMING_SNAKE_CASE_		:    str	,     SCREAMING_SNAKE_CASE_		:    List[str]			):
          for mod in [qq, qk, qv]:
               if not hasattr(SCREAMING_SNAKE_CASE_	,     '_amax'			):
                    print('          WARNING: NO AMAX BUFFER'			)
                    return
          lowercase__    :   List[Any]      =       qq._amax.detach().item()
          lowercase__    :   Optional[int]      =       qk._amax.detach().item()
          lowercase__    :   List[str]      =       qv._amax.detach().item()

          lowercase__    :   Tuple      =       max(SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_			)
          qq._amax.fill_(SCREAMING_SNAKE_CASE_			)
          qk._amax.fill_(SCREAMING_SNAKE_CASE_			)
          qv._amax.fill_(SCREAMING_SNAKE_CASE_			)
          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 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    List[Any]	,     SCREAMING_SNAKE_CASE_		:    Optional[int]			):



     '''simple docstring'''





     for name, mod in model.named_modules():
          if name.endswith('.output.dense'			) and not name.endswith('attention.output.dense'			):
               lowercase__    :   Any      =       mod._input_quantizer._amax.data.detach().item()
               mod._input_quantizer._amax.data.detach().clamp_(max=SCREAMING_SNAKE_CASE_			)
               lowercase__    :   List[str]      =       mod._input_quantizer._amax.data.detach().item()
               logger.info(f"""CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}"""			)

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[int]			):



     '''simple docstring'''





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

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Union[str, Any]			):



     '''simple docstring'''





     for name, mod in model.named_modules():
          if hasattr(SCREAMING_SNAKE_CASE_	,     '_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)
               lowercase__    :   Dict      =       set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis			)
               lowercase__    :   Union[str, Any]      =       set(range(len(mod.weight.size()			)			)			) - axis_set
               lowercase__    :   str      =       pytorch_quantization.utils.reduce_amax(mod.weight	,     axis=SCREAMING_SNAKE_CASE_	,     keepdims=SCREAMING_SNAKE_CASE_			).detach()
               logger.info(f"""RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}"""			)
               lowercase__    :   Union[str, Any]      =       amax

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Dict	,     SCREAMING_SNAKE_CASE_		:    Optional[Any]=25	,     SCREAMING_SNAKE_CASE_		:    Any=180	,     SCREAMING_SNAKE_CASE_		:    Optional[int]=None			):



     '''simple docstring'''





     if ignore is None:
          lowercase__    :   Tuple      =       []
     elif not isinstance(SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_			):
          lowercase__    :   str      =       [ignore]

     lowercase__    :   Optional[Any]      =       0
     for name, mod in model.named_modules():
          if not hasattr(SCREAMING_SNAKE_CASE_	,     'weight'			):
               continue
          lowercase__    :   Optional[Any]      =       max(SCREAMING_SNAKE_CASE_	,     len(SCREAMING_SNAKE_CASE_			)			)

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

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    int			):



     '''simple docstring'''





     lowercase__    :   List[str]      =       0
     for name, mod in model.named_modules():
          if isinstance(SCREAMING_SNAKE_CASE_	,     pytorch_quantization.nn.TensorQuantizer			):
               print(f"""{name:80} {mod}"""			)
               count += 1
     print(f"""{count} TensorQuantizers found in model"""			)

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[Any]	,     SCREAMING_SNAKE_CASE_		:    int	,     SCREAMING_SNAKE_CASE_		:    str	,     SCREAMING_SNAKE_CASE_		:    List[str]	,     SCREAMING_SNAKE_CASE_		:    Optional[int]			):



     '''simple docstring'''





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

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    List[Any]	,     SCREAMING_SNAKE_CASE_		:    Optional[int]	,     SCREAMING_SNAKE_CASE_		:    List[Any]="both"	,     **SCREAMING_SNAKE_CASE_		:    Tuple			):



     '''simple docstring'''





     lowercase__    :   Optional[int]      =       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(SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_	,     '_input_quantizer'	,     SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_			)
          if which in ["weight", "both"]:
               set_quantizer(SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_	,     '_weight_quantizer'	,     SCREAMING_SNAKE_CASE_	,     SCREAMING_SNAKE_CASE_			)
     logger.info(SCREAMING_SNAKE_CASE_			)

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[Any]	,     SCREAMING_SNAKE_CASE_		:    Dict	,     **SCREAMING_SNAKE_CASE_		:    List[str]			):



     '''simple docstring'''





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

