docstr: load quantized model from local disk
code: def from_quantized(
      cls,
      model_name_or_path: Optional[str],
      model_basename: Optional[str] = None,
      device_map: Optional[
          Union[str, Dict[str, Union[int, str]]]
      ] = None,
      max_memory: Optional[dict] = None,
      device: Optional[Union[str, int]] = None,
      low_cpu_mem_usage: bool = False,
      torch_dtype: Optional[torch.dtype] = None,
      use_safetensors: bool = False,
      trust_remote_code: bool = False,
      **kwargs,
  ):
      """load quantized model from local disk"""
  
      # Parameters related to loading from Hugging Face Hub
      cache_dir = kwargs.pop("cache_dir", None)
      force_download = kwargs.pop("force_download", False)
      resume_download = kwargs.pop("resume_download", False)
      proxies = kwargs.pop("proxies", None)
      local_files_only = kwargs.pop("local_files_only", False)
      use_auth_token = kwargs.pop("use_auth_token", None)
      revision = kwargs.pop("revision", None)
      subfolder = kwargs.pop("subfolder", "")
      commit_hash = kwargs.pop("_commit_hash", None)
  
      cached_file_kwargs = {
          "cache_dir": cache_dir,
          "force_download": force_download,
          "proxies": proxies,
          "resume_download": resume_download,
          "local_files_only": local_files_only,
          "use_auth_token": use_auth_token,
          "revision": revision,
          "subfolder": subfolder,
          "_raise_exceptions_for_missing_entries": False,
          "_commit_hash": commit_hash,
      }
  
      # == step1: prepare configs and file names == #
      config = AutoConfig.from_pretrained(
          model_name_or_path,
          trust_remote_code=trust_remote_code,
          **cached_file_kwargs,
      )
  
      if config.model_type not in SUPPORTED_MODELS:
          raise TypeError(f"{config.model_type} isn't supported yet.")
  
      extensions = []
      if use_safetensors:
          extensions.append(".safetensors")
      else:
          extensions += [".bin", ".pt"]
  
      model_name_or_path = str(model_name_or_path)
      is_local = isdir(model_name_or_path)
  
      resolved_archive_file = None
      if is_local:
          model_save_name = join(model_name_or_path, model_basename)
          for ext in extensions:
              if isfile(model_save_name + ext):
                  resolved_archive_file = model_save_name + ext
                  break
      else:  # remote
          for ext in extensions:
              resolved_archive_file = cached_file(
                  model_name_or_path,
                  model_basename + ext,
                  **cached_file_kwargs,
              )
              if resolved_archive_file is not None:
                  break
  
      if (
          resolved_archive_file is None
      ):  # Could not find a model file to use
          raise FileNotFoundError(
              f"Could not find model in {model_name_or_path}"
          )
  
      model_save_name = resolved_archive_file
  
      # == step2: convert model to quantized-model (replace Linear) == #
      def skip(*args, **kwargs):
          pass
  
      torch.nn.init.kaiming_uniform_ = skip
      torch.nn.init.uniform_ = skip
      torch.nn.init.normal_ = skip
  
      transformers.modeling_utils._init_weights = False
  
      init_contexts = [no_init_weights()]
      if low_cpu_mem_usage:
          init_contexts.append(
              accelerate.init_empty_weights(include_buffers=True)
          )
  
      with ContextManagers(init_contexts):
          model = AutoModelForCausalLM.from_config(
              config,
              trust_remote_code=trust_remote_code,
              torch_dtype=torch_dtype,
          )
          cls.create_quantized_model(model)
          model.tie_weights()
  
      # == step3: load checkpoint to quantized-model == #
      accelerate.utils.modeling.load_checkpoint_in_model(
          model,
          checkpoint=model_save_name,
          offload_state_dict=True,
          offload_buffers=True,
      )
  
      # == step4: set seqlen == #
      model_config = model.config.to_dict()
      seq_len_keys = [
          "max_position_embeddings",
          "seq_length",
          "n_positions",
      ]
      if any([k in model_config for k in seq_len_keys]):
          for key in seq_len_keys:
              if key in model_config:
                  model.seqlen = model_config[key]
                  break
      else:
          warnings.warn(
              "can't get model's sequence length from model config, will set to 4096."
          )
          model.seqlen = 4096
  
      return cls(
          model,
          True,
      )
  
imports: import os
  import warnings
  from abc import abstractmethod
  from functools import partial
  from os.path import isdir, isfile, join
  from typing import Dict, List, Optional, Union
  import numpy
  import torch
  import torch.nn
  import transformers
  from safetensors.torch import save_file
  from tqdm import tqdm
  from transformers import AutoConfig, AutoModelForCausalLM, PreTrainedModel
  from transformers.modeling_utils import no_init_weights
  from transformers.utils.generic import ContextManagers
  from transformers.utils.hub import PushToHubMixin, cached_file
  from colossalai.inference.kv_cache.batch_infer_state import BatchInferState, MemoryManager
label_desc: Use image node_id 17 for calling the BaseSmoothForCausalLM obj's underlying member method code with example usage: obj.from_quantized(cls, model_name_or_path, model_basename, device_map, max_memory, device, low_cpu_mem_usage, torch_dtype, use_safetensors, trust_remote_code) and returns: cls