# Copyright: DAMO Academy, Alibaba Group # By Xuan Phi Nguyen at DAMO Academy, Alibaba Group # Description: """ VLLM-based demo script to launch Language chat model for Southeast Asian Languages """ import os import numpy as np import argparse import torch import gradio as gr from typing import Any, Iterator from typing import Iterator, List, Optional, Tuple import filelock import glob import json import time from gradio_client.documentation import document, set_documentation_group from typing import List, Optional, Union, Dict, Tuple from tqdm.auto import tqdm from huggingface_hub import snapshot_download # @@ environments ================ DEBUG = bool(int(os.environ.get("DEBUG", "1"))) # List of languages to block BLOCK_LANGS = str(os.environ.get("BLOCK_LANGS", "")) BLOCK_LANGS = [x.strip() for x in BLOCK_LANGS.strip().split(";")] if len(BLOCK_LANGS.strip()) > 0 else [] # for lang block, wether to block in history too LANG_BLOCK_HISTORY = bool(int(os.environ.get("LANG_BLOCK_HISTORY", "0"))) TENSOR_PARALLEL = int(os.environ.get("TENSOR_PARALLEL", "1")) DTYPE = os.environ.get("DTYPE", "bfloat16") # ! (no debug) whether to download HF_MODEL_NAME and save to MODEL_PATH DOWNLOAD_SNAPSHOT = bool(int(os.environ.get("DOWNLOAD_SNAPSHOT", "0"))) LOG_RESPONSE = bool(int(os.environ.get("LOG_RESPONSE", "0"))) # ! show model path in the demo page, only for internal DISPLAY_MODEL_PATH = bool(int(os.environ.get("DISPLAY_MODEL_PATH", "1"))) # ! uploaded model path, will be downloaded to MODEL_PATH HF_MODEL_NAME = os.environ.get("HF_MODEL_NAME", "DAMO-NLP-SG/seal-13b-chat-a") # ! if model is private, need HF_TOKEN to access the model HF_TOKEN = os.environ.get("HF_TOKEN", None) # ! path where the model is downloaded, either on ./ or persistent disc MODEL_PATH = os.environ.get("MODEL_PATH", "./seal-13b-chat-a") # ! log path LOG_PATH = os.environ.get("LOG_PATH", "").strip() LOG_FILE = None SAVE_LOGS = LOG_PATH is not None and LOG_PATH != '' if SAVE_LOGS: if os.path.exists(LOG_PATH): print(f'LOG_PATH exist: {LOG_PATH}') else: LOG_DIR = os.path.dirname(LOG_PATH) os.makedirs(LOG_DIR, exist_ok=True) # ! get LOG_PATH as aggregated outputs in log GET_LOG_CMD = os.environ.get("GET_LOG_CMD", "").strip() print(f'SAVE_LOGS: {SAVE_LOGS} | {LOG_PATH}') # print(f'GET_LOG_CMD: {GET_LOG_CMD}') # ! !! Whether to delete the folder, ONLY SET THIS IF YOU WANT TO DELETE SAVED MODEL ON PERSISTENT DISC DELETE_FOLDER = os.environ.get("DELETE_FOLDER", "") IS_DELETE_FOLDER = DELETE_FOLDER is not None and os.path.exists(DELETE_FOLDER) print(f'DELETE_FOLDER: {DELETE_FOLDER} | {DOWNLOAD_SNAPSHOT=}') # ! list of keywords to disabled as security measures to comply with local regulation KEYWORDS = os.environ.get("KEYWORDS", "").strip() KEYWORDS = KEYWORDS.split(";") if len(KEYWORDS) > 0 else [] KEYWORDS = [x.lower() for x in KEYWORDS] # gradio config PORT = int(os.environ.get("PORT", "7860")) # how many iterations to yield response STREAM_YIELD_MULTIPLE = int(os.environ.get("STREAM_YIELD_MULTIPLE", "1")) # how many iterations to perform safety check on response STREAM_CHECK_MULTIPLE = int(os.environ.get("STREAM_CHECK_MULTIPLE", "0")) # whether to enable to popup accept user ENABLE_AGREE_POPUP = bool(int(os.environ.get("ENABLE_AGREE_POPUP", "0"))) # self explanatory MAX_TOKENS = int(os.environ.get("MAX_TOKENS", "2048")) TEMPERATURE = float(os.environ.get("TEMPERATURE", "0.1")) FREQUENCE_PENALTY = float(os.environ.get("FREQUENCE_PENALTY", "0.4")) PRESENCE_PENALTY = float(os.environ.get("PRESENCE_PENALTY", "0.0")) gpu_memory_utilization = float(os.environ.get("gpu_memory_utilization", "0.9")) # whether to enable quantization, currently not in use QUANTIZATION = str(os.environ.get("QUANTIZATION", "")) # Batch inference file upload ENABLE_BATCH_INFER = bool(int(os.environ.get("ENABLE_BATCH_INFER", "1"))) BATCH_INFER_MAX_ITEMS = int(os.environ.get("BATCH_INFER_MAX_ITEMS", "100")) BATCH_INFER_MAX_FILE_SIZE = int(os.environ.get("BATCH_INFER_MAX_FILE_SIZE", "500")) BATCH_INFER_MAX_PROMPT_TOKENS = int(os.environ.get("BATCH_INFER_MAX_PROMPT_TOKENS", "4000")) BATCH_INFER_SAVE_TMP_FILE = os.environ.get("BATCH_INFER_SAVE_TMP_FILE", "./tmp/pred.json") # DATA_SET_REPO_PATH = str(os.environ.get("DATA_SET_REPO_PATH", "")) DATA_SET_REPO = None """ Internal instructions of how to configure the DEMO 1. Upload SFT model as a model to huggingface: hugginface/models/seal_13b_a 2. If the model weights is private, set HF_TOKEN= in https://huggingface.co/spaces/????/?????/settings 3. space config env: `HF_MODEL_NAME=SeaLLMs/seal-13b-chat-a` or the underlining model 4. If enable persistent storage: set HF_HOME=/data/.huggingface MODEL_PATH=/data/.huggingface/seal-13b-chat-a if not: MODEL_PATH=./seal-13b-chat-a HF_HOME=/data/.huggingface MODEL_PATH=/data/ckpt/seal-13b-chat-a DELETE_FOLDER=/data/ """ # ============================== print(f'DEBUG mode: {DEBUG}') print(f'Torch version: {torch.__version__}') try: print(f'Torch CUDA version: {torch.version.cuda}') except Exception as e: print(f'Failed to print cuda version: {e}') try: compute_capability = torch.cuda.get_device_capability() print(f'Torch CUDA compute_capability: {compute_capability}') except Exception as e: print(f'Failed to print compute_capability version: {e}') # @@ constants ================ DTYPES = { 'float16': torch.float16, 'bfloat16': torch.bfloat16 } llm = None demo = None BOS_TOKEN = '' EOS_TOKEN = '' B_INST, E_INST = "[INST]", "[/INST]" B_SYS, E_SYS = "<>\n", "\n<>\n\n" # TODO: should Hide the system prompt SYSTEM_PROMPT_1 = """You are a multilingual, helpful, respectful and honest assistant. You are built by DAMO Academy, Alibaba Group. \ Please always answer as helpfully as possible, while being safe. Your \ answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure \ that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not \ correct. If you don't know the answer to a question, please don't share false information. As a multilingual assistant, you must respond and follow instructions in the native language of the user by default, unless told otherwise. \ Your response should adapt to the norms and customs of the respective language and culture. """ SYSTEM_PROMPT_1 = """You are a multilingual, helpful, respectful and honest assistant. You are built by DAMO Academy, Alibaba Group. \ Please always answer as helpfully as possible, while being safe. Your \ answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure \ that your responses are socially unbiased and positive in nature.""" # SYSTEM_PROMPT_1 = """You are a multilingual, helpful, respectful and honest assistant. \ # Please always answer as helpfully as possible, while being safe. Your \ # answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure \ # that your responses are socially unbiased and positive in nature. # If a question does not make any sense, or is not factually coherent, explain why instead of answering something not \ # correct. If you don't know the answer to a question, please don't share false information. # As a multilingual assistant, you must respond and follow instructions in the native language of the user by default, unless told otherwise. \ # Your response should adapt to the norms and customs of the respective language and culture. # """ # ============ CONSTANT ============ # https://github.com/gradio-app/gradio/issues/884 MODEL_NAME = "SeaLLM-13B" MODEL_NAME = str(os.environ.get("MODEL_NAME", "SeaLLM-13B")) MODEL_TITLE = """

SeaLLMs - Large Language Models for Southeast Asia

""" """ Somehow cannot add image here
""" MODEL_DESC = f"""
{MODEL_NAME} - a helpful assistant for Southeast Asian Languages. It supports English 🇬🇧, Vietnamese 🇻🇳, Indonesian 🇮🇩, Thai 🇹🇭, Malay 🇲🇾, Khmer🇰🇭, Lao🇱🇦, Tagalog🇵🇭 and Burmese🇲🇲. Explore our article for more.
NOTE: The chatbot may produce false and harmful content and does not have up-to-date knowledge. By using our service, you are required to agree to our Terms Of Use, which includes not to use our service to generate any harmful, inappropriate or illegal content that violates local and international laws. The service collects user dialogue data for testing and performance improvement, and reserves the right to distribute it under (CC-BY) or similar license. So do not enter any personal information! """.strip() cite_markdown = """ ## Citation If you find our project useful, hope you can star our repo and cite our paper as follows: ``` @article{damonlpsg2023seallm, author = {Xuan-Phi Nguyen*, Wenxuan Zhang*, Xin Li*, Mahani Aljunied*, Qingyu Tan, Liying Cheng, Guanzheng Chen, Yue Deng, Sen Yang, Chaoqun Liu, Hang Zhang, Lidong Bing}, title = {SeaLLMs - Large Language Models for Southeast Asia}, year = 2023, } ``` """ path_markdown = """ #### Model path: {model_path} """ def custom_hf_model_weights_iterator( model_name_or_path: str, cache_dir: Optional[str] = None, use_np_cache: bool = False, ) -> Iterator[Tuple[str, torch.Tensor]]: # ! if use vllm==0.1.4, use this to augment hf_model_weights_iterator loader from vllm.model_executor.weight_utils import Disabledtqdm # Prepare file lock directory to prevent multiple processes from # downloading the same model weights at the same time. lock_dir = cache_dir if cache_dir is not None else "/tmp" lock_file_name = model_name_or_path.replace("/", "-") + ".lock" lock = filelock.FileLock(os.path.join(lock_dir, lock_file_name)) # Download model weights from huggingface. is_local = os.path.isdir(model_name_or_path) if not is_local: with lock: hf_folder = snapshot_download(model_name_or_path, allow_patterns="*.bin", cache_dir=cache_dir, local_files_only=True, tqdm_class=Disabledtqdm) else: hf_folder = model_name_or_path hf_bin_files = [ x for x in glob.glob(os.path.join(hf_folder, "*model*.bin")) if not x.endswith("training_args.bin") ] hf_safetensors_files = [ x for x in glob.glob(os.path.join(hf_folder, "*model*.safetensors")) if not x.endswith("training_args.bin") ] if use_np_cache: # Convert the model weights from torch tensors to numpy arrays for # faster loading. np_folder = os.path.join(hf_folder, "np") os.makedirs(np_folder, exist_ok=True) weight_names_file = os.path.join(np_folder, "weight_names.json") with lock: if not os.path.exists(weight_names_file): weight_names = [] for bin_file in hf_bin_files: state = torch.load(bin_file, map_location="cpu") for name, param in state.items(): param_path = os.path.join(np_folder, name) with open(param_path, "wb") as f: np.save(f, param.cpu().detach().numpy()) weight_names.append(name) with open(weight_names_file, "w") as f: json.dump(weight_names, f) with open(weight_names_file, "r") as f: weight_names = json.load(f) for name in weight_names: param_path = os.path.join(np_folder, name) with open(param_path, "rb") as f: param = np.load(f) yield name, torch.from_numpy(param) else: if len(hf_bin_files) > 0: print(F'Load bin files: {hf_bin_files}') for bin_file in hf_bin_files: state = torch.load(bin_file, map_location="cpu") for name, param in state.items(): yield name, param del state torch.cuda.empty_cache() elif len(hf_safetensors_files) > 0: print(F'Load safetensor files: {hf_safetensors_files}') from safetensors.torch import load_file for safe_file in hf_safetensors_files: # state = torch.load(bin_file, map_location="cpu") state = load_file(safe_file) for name, param in state.items(): yield name, param del state torch.cuda.empty_cache() else: raise ValueError(f'no files available either bin or safe') def convert_pyslice_to_tensor(x: Any) -> torch.Tensor: """convert PySafeSlice object from safetensors to torch.Tensor PySafeSlice object supports indexing, which is done before loading the actual tensor and can reduce the amount of memory being read into the memory. However, it does not support more advanced functionalities like `.view()` or `.t()`. Therefore, if we need to modify the loaded tensor with these more complicated operators, we need to convert to tensor first. """ if not isinstance(x, torch.Tensor): x = x[:] return x def load_padded_tensor_parallel_vocab( param: torch.Tensor, loaded_weight: Any, # `torch.Tensor` or `PySafeSlice` tensor_model_parallel_rank: int, ) -> None: shard_size = param.shape[0] start_idx = tensor_model_parallel_rank * shard_size end_idx = (tensor_model_parallel_rank + 1) * shard_size loaded_weight = loaded_weight[start_idx:end_idx] loaded_weight = convert_pyslice_to_tensor(loaded_weight) param[:loaded_weight.shape[0]].copy_(loaded_weight) def llama_load_weights( self, model_name_or_path: str, cache_dir: Optional[str] = None, use_np_cache: bool = False, load_format: str = "auto", revision: Optional[str] = None ): # if use vllm==0.1.4 from vllm.model_executor.weight_utils import ( load_tensor_parallel_weights ) from vllm.model_executor.parallel_utils.parallel_state import ( get_tensor_model_parallel_rank, get_tensor_model_parallel_world_size) tp_size = get_tensor_model_parallel_world_size() tensor_model_parallel_rank = get_tensor_model_parallel_rank() q_proj_shard_size = (self.config.hidden_size // tp_size) kv_proj_shard_size = (self.config.hidden_size // self.config.num_attention_heads * getattr(self.config, "num_key_value_heads", self.config.num_attention_heads) // tp_size) attention_weight_specs = [ # (weight_name, shard_size, offset) ("q_proj", q_proj_shard_size, 0), ("k_proj", kv_proj_shard_size, q_proj_shard_size), ("v_proj", kv_proj_shard_size, q_proj_shard_size + kv_proj_shard_size), ] state_dict = self.state_dict() need_to_load = len(state_dict) loaded = 0 iterator = custom_hf_model_weights_iterator(model_name_or_path, cache_dir, use_np_cache) for name, loaded_weight in iterator: if "rotary_emb.inv_freq" in name: continue if "embed_tokens" in name or "lm_head" in name: param = state_dict[name] # Consider padding in the vocab size. padded_vocab_size = (param.shape[0] * tp_size) # num_extra_rows = padded_vocab_size - self.config.vocab_size num_extra_rows = padded_vocab_size - loaded_weight.size(0) load_size = loaded_weight.size() extra_rows = torch.empty(num_extra_rows, loaded_weight.shape[1]) extra_rows = extra_rows.to(loaded_weight) loaded_weight = torch.cat([loaded_weight, extra_rows], dim=0) if num_extra_rows > 0: print(f'Add empty to {num_extra_rows} extra row for {name}') print(f'Load: {name} | {padded_vocab_size=} | {self.config.vocab_size=} | {num_extra_rows=} | {param.size()=} | {loaded_weight.size()=} | {load_size=}') is_attention_weight = False for weight_name, shard_size, offset in attention_weight_specs: if weight_name not in name or "qkv_proj" in name: continue param = state_dict[name.replace(weight_name, "qkv_proj")] loaded_weight = loaded_weight[ shard_size * tensor_model_parallel_rank:shard_size * (tensor_model_parallel_rank + 1)] param_slice = param.data[offset:offset + shard_size] assert param_slice.shape == loaded_weight.shape param_slice.copy_(loaded_weight) loaded += 1.0 / 3 is_attention_weight = True break if is_attention_weight: continue # ! qkv_proj is sharded differently if concatenated into qkv # qkv: qqqq kkkk vvvv # lweight: qq0qq1 kk0kk1 vv0vv1 # q_shard_size: hidden_size // tp_size = qq # qkv_s0: qq0_kk0_vv0 # qkv_s1: qq1_kk1_vv1 if "qkv_proj" in name: param = state_dict[name] # loaded_weight qsize = self.config.hidden_size kvsize = self.config.hidden_size // self.config.num_attention_heads * getattr(self.config, "num_key_value_heads", self.config.num_attention_heads) q_offsets = ( q_proj_shard_size * tensor_model_parallel_rank, q_proj_shard_size * (tensor_model_parallel_rank + 1) ) k_offsets = ( qsize + kv_proj_shard_size * tensor_model_parallel_rank, qsize + kv_proj_shard_size * (tensor_model_parallel_rank + 1) ) v_offsets = ( qsize + kvsize + kv_proj_shard_size * tensor_model_parallel_rank, qsize + kvsize + kv_proj_shard_size * (tensor_model_parallel_rank + 1) ) _loaded_weight = torch.cat( [ loaded_weight[q_offsets[0]:q_offsets[1]], loaded_weight[k_offsets[0]:k_offsets[1]], loaded_weight[v_offsets[0]:v_offsets[1]], ], 0 ) assert param.shape == _loaded_weight.shape, f'{param.shape=} != {_loaded_weight.shape=}' param.data.copy_(_loaded_weight) loaded += 1.0 is_attention_weight = True if is_attention_weight: continue is_gate_up_weight = False for stride_id, weight_name in enumerate(["gate_proj", "up_proj"]): if weight_name not in name or "gate_up_proj" in name: continue param = state_dict[name.replace(weight_name, "gate_up_proj")] shard_size = param.shape[0] // 2 loaded_weight = loaded_weight[ shard_size * tensor_model_parallel_rank:shard_size * (tensor_model_parallel_rank + 1)] param_slice = param.data[shard_size * stride_id:shard_size * (stride_id + 1)] assert param_slice.shape == loaded_weight.shape param_slice.copy_(loaded_weight) loaded += 1.0 / 2 is_gate_up_weight = True break if is_gate_up_weight: continue if "gate_up_proj" in name: param = state_dict[name] shard_size = param.shape[0] // 2 intermediate_size = self.config.intermediate_size g_offsets = ( shard_size * tensor_model_parallel_rank, shard_size * (tensor_model_parallel_rank + 1) ) u_offsets = ( intermediate_size + shard_size * tensor_model_parallel_rank, intermediate_size + shard_size * (tensor_model_parallel_rank + 1) ) _loaded_weight = torch.cat( [ loaded_weight[g_offsets[0]:g_offsets[1]], loaded_weight[u_offsets[0]:u_offsets[1]], ], 0 ) assert param.shape == _loaded_weight.shape param.data.copy_(_loaded_weight) loaded += 1.0 is_gate_up_weight = True if is_gate_up_weight: continue param = state_dict[name] load_tensor_parallel_weights(param, loaded_weight, name, self._column_parallel_weights, self._row_parallel_weights, tensor_model_parallel_rank) loaded += 1 if np.abs(loaded - need_to_load) < 0.01: print(f'WARNING: only {loaded} params loaded out of {need_to_load}') else: print(f'Loaded all {loaded} params loaded out of {need_to_load}') def new_llama_load_weights( self, model_name_or_path: str, cache_dir: Optional[str] = None, load_format: str = "auto", revision: Optional[str] = None ): # If use newest vllm, not been thoroughly tested yet. from vllm.model_executor.weight_utils import ( load_tensor_parallel_weights, hf_model_weights_iterator ) from vllm.model_executor.parallel_utils.parallel_state import ( get_tensor_model_parallel_rank, get_tensor_model_parallel_world_size) if self.quant_config is None: weight_suffixes = ["weight"] else: weight_suffixes = self.quant_config.get_tp_tensor_names() column_parallel_weights: List[str] = [] for layer in self._column_parallel_layers: for suffix in weight_suffixes: column_parallel_weights.append(f"{layer}.{suffix}") row_parallel_weights: List[str] = [] for layer in self._row_parallel_layers: for suffix in weight_suffixes: row_parallel_weights.append(f"{layer}.{suffix}") tp_size = get_tensor_model_parallel_world_size() tp_rank = get_tensor_model_parallel_rank() assert tp_size == 1, f'tensorparallel >=2 not allowed. {tp_size}' q_proj_shard_size = (self.config.hidden_size // tp_size) num_kv_heads_replicas = max(1, tp_size // self.config.num_key_value_heads) num_kv_heads_per_gpu = max(1, self.config.num_key_value_heads // tp_size) kv_proj_shard_size = (self.config.hidden_size // self.config.num_attention_heads * num_kv_heads_per_gpu) attention_weight_specs = [ # (weight_name, shard_size, offset) ("q_proj", q_proj_shard_size, 0), ("k_proj", kv_proj_shard_size, q_proj_shard_size), ("v_proj", kv_proj_shard_size, q_proj_shard_size + kv_proj_shard_size), ] state_dict = self.state_dict() need_to_load = len(state_dict) loaded = 0 for name, loaded_weight in hf_model_weights_iterator( model_name_or_path, cache_dir, load_format, revision): if "rotary_emb.inv_freq" in name: continue is_packed = False is_transposed = False if self.quant_config is not None: is_packed = self.quant_config.is_packed(name) is_transposed = self.quant_config.is_transposed(name) if is_transposed: loaded_weight = convert_pyslice_to_tensor(loaded_weight) loaded_weight = loaded_weight.T is_attention_weight = False for weight_name, shard_size, offset in attention_weight_specs: if weight_name not in name or "qkv_proj" in name: continue param = state_dict[name.replace(weight_name, "qkv_proj")] if is_transposed: param = param.T if is_packed: shard_size //= self.quant_config.pack_factor offset //= self.quant_config.pack_factor if weight_name in ["k_proj", "v_proj"]: shard_id = tp_rank // num_kv_heads_replicas else: shard_id = tp_rank loaded_weight = loaded_weight[shard_size * shard_id:shard_size * (shard_id + 1)] param_slice = param.data[offset:offset + shard_size] assert param_slice.shape == loaded_weight.shape param_slice.copy_(loaded_weight) loaded += 1.0 / 3 is_attention_weight = True break if is_attention_weight: continue # TODO: need to figure out to do sharding with qkv_proj fused is_gate_up_weight = False for stride_id, weight_name in enumerate(["gate_proj", "up_proj"]): if weight_name not in name or "gate_up_proj" in name: continue param = state_dict[name.replace(weight_name, "gate_up_proj")] if is_transposed: param = param.T shard_size = param.shape[0] // 2 loaded_weight = loaded_weight[shard_size * tp_rank:shard_size * (tp_rank + 1)] param_slice = param.data[shard_size * stride_id:shard_size * (stride_id + 1)] assert param_slice.shape == loaded_weight.shape param_slice.copy_(loaded_weight) loaded += 1.0 / 2 is_gate_up_weight = True break if is_gate_up_weight: continue # TODO: need to figure out to do sharding with gate_up_proj fused param = state_dict[name] if is_transposed: param = param.T if "embed_tokens" in name or "lm_head" in name: load_padded_tensor_parallel_vocab(param, loaded_weight, tp_rank) loaded += 1 continue load_tensor_parallel_weights(param, loaded_weight, name, column_parallel_weights, row_parallel_weights, tp_rank) loaded += 1 if np.abs(loaded - need_to_load) < 0.01: print(f'WARNING: only {loaded} params loaded out of {need_to_load}') else: print(f'Loaded all {loaded} params loaded out of {need_to_load}') # Reassign LlamaForCausalLM.load_weights with llama_load_weights if not DEBUG: try: import vllm from vllm.model_executor.model_loader import _MODEL_REGISTRY from vllm.model_executor.models import LlamaForCausalLM _MODEL_REGISTRY['FasterLlamaForCausalLM'] = LlamaForCausalLM if vllm.__version__ == "0.1.4": LlamaForCausalLM.load_weights = llama_load_weights else: LlamaForCausalLM.load_weights = new_llama_load_weights if DTYPE == "bfloat16": try: compute_capability = torch.cuda.get_device_capability() if compute_capability[0] < 8: gpu_name = torch.cuda.get_device_name() print( "Bfloat16 is only supported on GPUs with compute capability " f"of at least 8.0. Your {gpu_name} GPU has compute capability " f"{compute_capability[0]}.{compute_capability[1]}. --> Move to FLOAT16") DTYPE = "float16" except Exception as e: print(f'Unable to obtain compute_capability: {e}') except Exception as e: print(f'Failing import and reconfigure VLLM: {str(e)}') # ! ================================================================== set_documentation_group("component") RES_PRINTED = False def llama_chat_sys_input_seq_constructor(text, sys_prompt=SYSTEM_PROMPT_1, bos_token=BOS_TOKEN, eos_token=EOS_TOKEN): return f"{bos_token}{B_INST} {B_SYS} {sys_prompt} {E_SYS} {text} {E_INST}" def llama_chat_multiturn_sys_input_seq_constructor( message: str, history: List[Tuple[str, str]], sys_prompt=SYSTEM_PROMPT_1, bos_token=BOS_TOKEN, eos_token=EOS_TOKEN, include_end_instruct=True, ): """ ``` [INST] B_SYS SytemPrompt E_SYS Prompt [/INST] Answer [INST] Prompt [/INST] Answer [INST] Prompt [/INST] ``` """ text = '' end_instr = f" {E_INST}" if include_end_instruct else "" for i, (prompt, res) in enumerate(history): if i == 0: text += f"{bos_token}{B_INST} {B_SYS} {sys_prompt} {E_SYS} {prompt}{end_instr}" else: text += f"{bos_token}{B_INST} {prompt}{end_instr}" if res is not None: text += f" {res} {eos_token} " if len(history) == 0 or text.strip() == '': text = f"{bos_token}{B_INST} {B_SYS} {sys_prompt} {E_SYS} {message}{end_instr}" else: text += f"{bos_token}{B_INST} {message}{end_instr}" return text @document() class ChatBot(gr.Chatbot): def _postprocess_chat_messages( self, chat_message ): x = super()._postprocess_chat_messages(chat_message) # if isinstance(x, str): # x = x.strip().replace("\n", "
") return x from gradio.components import Button from gradio.events import Dependency, EventListenerMethod # replace events so that submit button is disabled during generation, if stop_btn not found # this prevent weird behavior def _setup_stop_events( self, event_triggers: list[EventListenerMethod], event_to_cancel: Dependency ) -> None: from gradio.components import State event_triggers = event_triggers if isinstance(event_triggers, (list, tuple)) else [event_triggers] if self.stop_btn and self.is_generator: if self.submit_btn: for event_trigger in event_triggers: event_trigger( lambda: ( Button(visible=False), Button(visible=True), ), None, [self.submit_btn, self.stop_btn], api_name=False, queue=False, ) event_to_cancel.then( lambda: (Button(visible=True), Button(visible=False)), None, [self.submit_btn, self.stop_btn], api_name=False, queue=False, ) else: for event_trigger in event_triggers: event_trigger( lambda: Button(visible=True), None, [self.stop_btn], api_name=False, queue=False, ) event_to_cancel.then( lambda: Button(visible=False), None, [self.stop_btn], api_name=False, queue=False, ) self.stop_btn.click( None, None, None, cancels=event_to_cancel, api_name=False, ) else: if self.submit_btn: for event_trigger in event_triggers: event_trigger( lambda: Button(interactive=False), None, [self.submit_btn], api_name=False, queue=False, ) event_to_cancel.then( lambda: Button(interactive=True), None, [self.submit_btn], api_name=False, queue=False, ) # upon clear, cancel the submit event as well if self.clear_btn: self.clear_btn.click( lambda: ([], [], None, Button(interactive=True)), None, [self.chatbot, self.chatbot_state, self.saved_input, self.submit_btn], queue=False, api_name=False, cancels=event_to_cancel, ) # TODO: reconfigure clear button as stop and clear button def _setup_events(self) -> None: from gradio.components import State has_on = False try: from gradio.events import Dependency, EventListenerMethod, on has_on = True except ImportError as ie: has_on = False submit_fn = self._stream_fn if self.is_generator else self._submit_fn def update_time(c_time, chatbot_state): # if chatbot_state is empty, register a new conversaion with the current timestamp # assert len(chatbot_state) > 0, f'empty chatbot state' if len(chatbot_state) <= 1: return gr.Number(value=time.time(), label='current_time', visible=False), chatbot_state # elif len(chatbot_state) == 1: # # assert chatbot_state[-1][-1] is None, f'invalid [[message, None]] , got {chatbot_state}' # return gr.Number(value=time.time(), label='current_time', visible=False), chatbot_state else: return c_time, chatbot_state if has_on: # new version submit_triggers = ( [self.textbox.submit, self.submit_btn.click] if self.submit_btn else [self.textbox.submit] ) submit_event = ( on( submit_triggers, self._clear_and_save_textbox, [self.textbox], [self.textbox, self.saved_input], api_name=False, queue=False, ) .then( self._display_input, [self.saved_input, self.chatbot_state], [self.chatbot, self.chatbot_state], api_name=False, queue=False, ) .then( update_time, [self.additional_inputs[-1], self.chatbot_state], [self.additional_inputs[-1], self.chatbot_state], api_name=False, queue=False, ) .then( submit_fn, [self.saved_input, self.chatbot_state] + self.additional_inputs, [self.chatbot, self.chatbot_state], api_name=False, ) ) self._setup_stop_events(submit_triggers, submit_event) else: raise ValueError(f'Better install new gradio version than 3.44.0') if self.retry_btn: retry_event = ( self.retry_btn.click( self._delete_prev_fn, [self.chatbot_state], [self.chatbot, self.saved_input, self.chatbot_state], api_name=False, queue=False, ) .then( self._display_input, [self.saved_input, self.chatbot_state], [self.chatbot, self.chatbot_state], api_name=False, queue=False, ) .then( submit_fn, [self.saved_input, self.chatbot_state] + self.additional_inputs, [self.chatbot, self.chatbot_state], api_name=False, ) ) self._setup_stop_events([self.retry_btn.click], retry_event) if self.undo_btn: self.undo_btn.click( self._delete_prev_fn, [self.chatbot_state], [self.chatbot, self.saved_input, self.chatbot_state], api_name=False, queue=False, ).then( lambda x: x, [self.saved_input], [self.textbox], api_name=False, queue=False, ) # Reconfigure clear_btn to stop and clear text box # if self.clear_btn: # self.clear_btn.click( # lambda: ([], [], None), # None, # [self.chatbot, self.chatbot_state, self.saved_input], # queue=False, # api_name=False, # cancels=submit_event, # ) def _display_input( self, message: str, history: list[list[str | None]] ) -> tuple[list[list[str | None]], list[list[str | None]]]: if message is not None and message.strip() != "": history.append([message, None]) return history, history # replace gr.ChatInterface._setup_stop_events = _setup_stop_events gr.ChatInterface._setup_events = _setup_events gr.ChatInterface._display_input = _display_input @document() class CustomTabbedInterface(gr.Blocks): def __init__( self, interface_list: list[gr.Interface], tab_names: Optional[list[str]] = None, title: Optional[str] = None, description: Optional[str] = None, theme: Optional[gr.Theme] = None, analytics_enabled: Optional[bool] = None, css: Optional[str] = None, ): """ Parameters: interface_list: a list of interfaces to be rendered in tabs. tab_names: a list of tab names. If None, the tab names will be "Tab 1", "Tab 2", etc. title: a title for the interface; if provided, appears above the input and output components in large font. Also used as the tab title when opened in a browser window. analytics_enabled: whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable or default to True. css: custom css or path to custom css file to apply to entire Blocks Returns: a Gradio Tabbed Interface for the given interfaces """ super().__init__( title=title or "Gradio", theme=theme, analytics_enabled=analytics_enabled, mode="tabbed_interface", css=css, ) self.description = description if tab_names is None: tab_names = [f"Tab {i}" for i in range(len(interface_list))] with self: if title: gr.Markdown( f"

{title}

" ) if description: gr.Markdown(description) with gr.Tabs(): for interface, tab_name in zip(interface_list, tab_names): with gr.Tab(label=tab_name): interface.render() # def vllm_abort(self: Any): # sh = self.llm_engine.scheduler # for g in (sh.waiting + sh.running + sh.swapped): # sh.abort_seq_group(g.request_id) # from vllm.sequence import SequenceStatus # scheduler = self.llm_engine.scheduler # for state_queue in [scheduler.waiting, scheduler.running, scheduler.swapped]: # for seq_group in state_queue: # # if seq_group.request_id == request_id: # # Remove the sequence group from the state queue. # state_queue.remove(seq_group) # for seq in seq_group.seqs: # if seq.is_finished(): # continue # scheduler.free_seq(seq, SequenceStatus.FINISHED_ABORTED) def vllm_abort(self): sh = self.llm_engine.scheduler for g in (sh.waiting + sh.running + sh.swapped): sh.abort_seq_group(g.request_id) from vllm.sequence import SequenceStatus scheduler = self.llm_engine.scheduler for state_queue in [scheduler.waiting, scheduler.running, scheduler.swapped]: for seq_group in state_queue: # if seq_group.request_id == request_id: # Remove the sequence group from the state queue. state_queue.remove(seq_group) for seq in seq_group.seqs: if seq.is_finished(): continue scheduler.free_seq(seq, SequenceStatus.FINISHED_ABORTED) def _vllm_run_engine(self: Any, use_tqdm: bool = False) -> Dict[str, Any]: from vllm.outputs import RequestOutput # Initialize tqdm. if use_tqdm: num_requests = self.llm_engine.get_num_unfinished_requests() pbar = tqdm(total=num_requests, desc="Processed prompts") # Run the engine. outputs: Dict[str, RequestOutput] = {} while self.llm_engine.has_unfinished_requests(): step_outputs = self.llm_engine.step() for output in step_outputs: outputs[output.request_id] = output if len(outputs) > 0: yield outputs def vllm_generate_stream( self: Any, prompts: Optional[Union[str, List[str]]] = None, sampling_params: Optional[Any] = None, prompt_token_ids: Optional[List[List[int]]] = None, use_tqdm: bool = False, ) -> Dict[str, Any]: """Generates the completions for the input prompts. NOTE: This class automatically batches the given prompts, considering the memory constraint. For the best performance, put all of your prompts into a single list and pass it to this method. Args: prompts: A list of prompts to generate completions for. sampling_params: The sampling parameters for text generation. If None, we use the default sampling parameters. prompt_token_ids: A list of token IDs for the prompts. If None, we use the tokenizer to convert the prompts to token IDs. use_tqdm: Whether to use tqdm to display the progress bar. Returns: A list of `RequestOutput` objects containing the generated completions in the same order as the input prompts. """ from vllm import LLM, SamplingParams if prompts is None and prompt_token_ids is None: raise ValueError("Either prompts or prompt_token_ids must be " "provided.") if isinstance(prompts, str): # Convert a single prompt to a list. prompts = [prompts] if prompts is not None and prompt_token_ids is not None: if len(prompts) != len(prompt_token_ids): raise ValueError("The lengths of prompts and prompt_token_ids " "must be the same.") if sampling_params is None: # Use default sampling params. sampling_params = SamplingParams() # Add requests to the engine. if prompts is not None: num_requests = len(prompts) else: num_requests = len(prompt_token_ids) for i in range(num_requests): prompt = prompts[i] if prompts is not None else None if prompt_token_ids is None: token_ids = None else: token_ids = prompt_token_ids[i] self._add_request(prompt, sampling_params, token_ids) # return self._run_engine(use_tqdm) yield from _vllm_run_engine(self, use_tqdm) # ! avoid saying LANG_BLOCK_MESSAGE = """Sorry, the language you have asked is currently not supported. If you have questions in other supported languages, I'll be glad to help. \ Please also consider clearing the chat box for a better experience.""" KEYWORD_BLOCK_MESSAGE = "Sorry, I cannot fulfill your request. If you have any unrelated question, I'll be glad to help." def _detect_lang(text): # Disable language that may have safety risk from langdetect import detect as detect_lang dlang = None try: dlang = detect_lang(text) except Exception as e: # print(f'Error: {e}') if "No features in text." in str(e): return "en" else: return "zh" return dlang def block_lang( message: str, history: List[Tuple[str, str]] = None, ) -> str: # relieve history base block if len(BLOCK_LANGS) == 0: return False if LANG_BLOCK_HISTORY and history is not None and any((LANG_BLOCK_MESSAGE in x[1].strip()) for x in history): return True else: _lang = _detect_lang(message) if _lang in BLOCK_LANGS: print(f'Detect blocked {_lang}: {message}') return True else: return False def safety_check(text, history=None, ) -> Optional[str]: """ Despite our effort in safety tuning and red teaming, our models may still generate harmful or illegal content. This provides an additional security measure to enhance safety and compliance with local regulations. """ if len(KEYWORDS) > 0 and any(x in text.lower() for x in KEYWORDS): return KEYWORD_BLOCK_MESSAGE if len(BLOCK_LANGS) > 0: if block_lang(text, history): return LANG_BLOCK_MESSAGE return None TURN_TEMPLATE = "<|im_start|>{role}\n{content}" TURN_PREFIX = "<|im_start|>{role}\n" def chatml_chat_convo_format(conversations, add_assistant_prefix: bool, default_system=SYSTEM_PROMPT_1): if conversations[0]['role'] != 'system': conversations = [{"role": "system", "content": default_system}] + conversations text = '' for turn_id, turn in enumerate(conversations): prompt = TURN_TEMPLATE.format(role=turn['role'], content=turn['content']) text += prompt if add_assistant_prefix: prompt = TURN_PREFIX.format(role='assistant') text += prompt return text def chatml_format(message, history=None, system_prompt=None): conversations = [] system_prompt = system_prompt or "You are a helpful assistant." if history is not None and len(history) > 0: for i, (prompt, res) in enumerate(history): conversations.append({"role": "user", "content": prompt.strip()}) conversations.append({"role": "assistant", "content": res.strip()}) conversations.append({"role": "user", "content": message.strip()}) return chatml_chat_convo_format(conversations, True, default_system=system_prompt) def chat_response_stream_multiturn( message: str, history: List[Tuple[str, str]], temperature: float, max_tokens: int, frequency_penalty: float, presence_penalty: float, current_time: Optional[float] = None, system_prompt: Optional[str] = SYSTEM_PROMPT_1 ) -> str: global LOG_FILE, LOG_PATH from vllm import LLM, SamplingParams """Build multi turn [INST] B_SYS SytemPrompt E_SYS Prompt [/INST] Answer [INST] Prompt [/INST] Answer [INST] Prompt [/INST] message is incoming prompt history don't have the current messauge """ global llm, RES_PRINTED assert llm is not None assert system_prompt.strip() != '', f'system prompt is empty' tokenizer = llm.get_tokenizer() # force removing all vllm_abort(llm) temperature = float(temperature) frequency_penalty = float(frequency_penalty) max_tokens = int(max_tokens) message = message.strip() if GET_LOG_CMD != "" and message.strip() == GET_LOG_CMD: print_log_file() yield "Finish printed log. Please clear the chatbox now." return if len(message) == 0: raise gr.Error("The message cannot be empty!") message_safety = safety_check(message, history=history) if message_safety is not None: yield message_safety return # history will be appended with message later on # full_prompt = llama_chat_multiturn_sys_input_seq_constructor( # message, history, sys_prompt=system_prompt # ) full_prompt = chatml_format(message.strip(), history=history, system_prompt=system_prompt) # print(full_prompt) if len(tokenizer.encode(full_prompt, add_special_tokens=False)) >= 4050: raise gr.Error(f"Conversation or prompt is too long, please clear the chatbox or try shorter input.") sampling_params = SamplingParams( temperature=temperature, max_tokens=max_tokens, frequency_penalty=frequency_penalty, presence_penalty=presence_penalty, # stop=['', '', '<>', '<>', '[INST]', '[/INST]'], stop=['', '', '<|im_start|>', '<|im_end|>'], ) cur_out = None for j, gen in enumerate(vllm_generate_stream(llm, full_prompt, sampling_params)): if cur_out is not None and (STREAM_YIELD_MULTIPLE < 1 or j % STREAM_YIELD_MULTIPLE == 0) and j > 0: # cur_out = cur_out.replace("\\n", "\n") # optionally check safety, and respond if STREAM_CHECK_MULTIPLE > 0 and j % STREAM_CHECK_MULTIPLE == 0: message_safety = safety_check(cur_out, history=None) if message_safety is not None: yield message_safety return yield cur_out assert len(gen) == 1, f'{gen}' item = next(iter(gen.values())) cur_out = item.outputs[0].text #cur_out = "Our system is under maintenance, will be back soon!" if j >= max_tokens - 2: gr.Warning(f'The response hits limit of {max_tokens} tokens. Consider increase the max tokens parameter in the Additional Inputs.') # TODO: use current_time to register conversations, accoriding history and cur_out history_str = format_conversation(history + [[message, cur_out]]) print(f'@@@@@@@@@@\n{history_str}\n##########\n') maybe_log_conv_file(current_time, history, message, cur_out, temperature=temperature, frequency_penalty=frequency_penalty) if cur_out is not None and "\\n" in cur_out: print(f'double slash-n in cur_out:\n{cur_out}') cur_out = cur_out.replace("\\n", "\n") if cur_out is not None: yield cur_out message_safety = safety_check(cur_out, history=None) if message_safety is not None: yield message_safety return def maybe_log_conv_file(current_time, history, message, response, **kwargs): global LOG_FILE if LOG_FILE is not None: my_history = history + [[message, response]] obj = { 'key': str(current_time), 'history': my_history } for k, v in kwargs.items(): obj[k] = v log_ = json.dumps(obj, ensure_ascii=False) LOG_FILE.write(log_ + "\n") LOG_FILE.flush() print(f'Wrote {obj["key"]} to {LOG_PATH}') def format_conversation(history): _str = '\n'.join([ ( f'<<>> {h[0]}\n' f'<<>> {h[1]}' ) for h in history ]) return _str def aggregate_convos(): from datetime import datetime global LOG_FILE, DATA_SET_REPO_PATH, SAVE_LOGS assert os.path.exists(LOG_PATH), f'{LOG_PATH} not found' convos = None irregular_count = 1 with open(LOG_PATH, 'r', encoding='utf-8') as f: convos = {} for i, l in enumerate(f): if l: item = json.loads(l) key = item['key'] try: key = float(key) except Exception as e: key = -1 if key > 0.0: item_key = datetime.fromtimestamp(key).strftime("%Y-%m-%d %H:%M:%S") else: key = item_key = f'e{irregular_count}' irregular_count += 1 item['key'] = item_key convos[key] = item return convos def maybe_upload_to_dataset(): from datetime import datetime global LOG_FILE, DATA_SET_REPO_PATH, SAVE_LOGS if SAVE_LOGS and os.path.exists(LOG_PATH) and DATA_SET_REPO_PATH != "": convos = aggregate_convos() AGG_LOG_PATH = LOG_PATH + ".agg.json" with open(AGG_LOG_PATH, 'w', encoding='utf-8') as fo: json.dump(convos, fo, indent=4, ensure_ascii=False) print(f'Saved aggregated json to {AGG_LOG_PATH}') try: from huggingface_hub import upload_file print(f'upload {AGG_LOG_PATH} to {DATA_SET_REPO_PATH}') upload_file( path_or_fileobj=AGG_LOG_PATH, path_in_repo=os.path.basename(AGG_LOG_PATH), repo_id=DATA_SET_REPO_PATH, token=HF_TOKEN, repo_type="dataset", create_pr=True ) except Exception as e: print(f'Failed to save to repo: {DATA_SET_REPO_PATH}|{str(e)}') def print_log_file(): global LOG_FILE, LOG_PATH if SAVE_LOGS and os.path.exists(LOG_PATH): with open(LOG_PATH, 'r', encoding='utf-8') as f: convos = aggregate_convos() print(f'Printing log from {LOG_PATH}') items = list(convos.items()) for k, v in items[-10:]: history = v.pop('history') print(f'######--{v}--#####') _str = format_conversation(history) print(_str) maybe_upload_to_dataset() def debug_chat_response_echo( message: str, history: List[Tuple[str, str]], temperature: float = 0.0, max_tokens: int = 4096, frequency_penalty: float = 0.4, presence_penalty: float = 0.0, current_time: Optional[float] = None, system_prompt: str = SYSTEM_PROMPT_1, ) -> str: global LOG_FILE import time time.sleep(0.5) if message.strip() == GET_LOG_CMD: print_log_file() yield "Finish printed log." return for i in range(len(message)): yield f"repeat: {current_time} {message[:i + 1]}" cur_out = f"repeat: {current_time} {message}" maybe_log_conv_file(current_time, history, message, cur_out, temperature=temperature, frequency_penalty=frequency_penalty) def check_model_path(model_path) -> str: assert os.path.exists(model_path), f'{model_path} not found' ckpt_info = "None" if os.path.isdir(model_path): if os.path.exists(f'{model_path}/info.txt'): with open(f'{model_path}/info.txt', 'r') as f: ckpt_info = f.read() print(f'Checkpoint info:\n{ckpt_info}\n-----') else: print(f'info.txt not found in {model_path}') print(f'model path dir: {list(os.listdir(model_path))}') return ckpt_info def maybe_delete_folder(): if IS_DELETE_FOLDER and DOWNLOAD_SNAPSHOT: import shutil print(f'DELETE ALL FILES IN {DELETE_FOLDER}') for filename in os.listdir(DELETE_FOLDER): file_path = os.path.join(DELETE_FOLDER, filename) try: if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print('Failed to delete %s. Reason: %s' % (file_path, e)) AGREE_POP_SCRIPTS = """ async () => { alert("To use our service, you are required to agree to the following terms:\\nYou must not use our service to generate any harmful, unethical or illegal content that violates local and international laws, including but not limited to hate speech, violence and deception.\\nThe service may collect user dialogue data for performance improvement, and reserves the right to distribute it under CC-BY or similar license. So do not enter any personal information!"); } """ def debug_file_function( files: Union[str, List[str]], prompt_mode: str, temperature: float, max_tokens: int, frequency_penalty: float, presence_penalty: float, stop_strings: str = "[STOP],,", current_time: Optional[float] = None, ): """This is only for debug purpose""" files = files if isinstance(files, list) else [files] print(files) filenames = [f.name for f in files] all_items = [] for fname in filenames: print(f'Reading {fname}') with open(fname, 'r', encoding='utf-8') as f: items = json.load(f) assert isinstance(items, list), f'invalid items from {fname} not list' all_items.extend(items) print(all_items) print(f'{prompt_mode} / {temperature} / {max_tokens}, {frequency_penalty}, {presence_penalty}') save_path = "./test.json" with open(save_path, 'w', encoding='utf-8') as f: json.dump(all_items, f, indent=4, ensure_ascii=False) for x in all_items: x['response'] = "Return response" print_items = all_items[:1] # print_json = json.dumps(print_items, indent=4, ensure_ascii=False) return save_path, print_items def validate_file_item(filename, index, item: Dict[str, str]): """ check safety for items in files """ message = item['prompt'].strip() if len(message) == 0: raise gr.Error(f'Prompt {index} empty') message_safety = safety_check(message, history=None) if message_safety is not None: raise gr.Error(f'Prompt {index} invalid: {message_safety}') tokenizer = llm.get_tokenizer() if llm is not None else None if tokenizer is None or len(tokenizer.encode(message, add_special_tokens=False)) >= BATCH_INFER_MAX_PROMPT_TOKENS: raise gr.Error(f"Prompt {index} too long, should be less than {BATCH_INFER_MAX_PROMPT_TOKENS} tokens") def read_validate_json_files(files: Union[str, List[str]]): files = files if isinstance(files, list) else [files] filenames = [f.name for f in files] all_items = [] for fname in filenames: # check each files print(f'Reading {fname}') with open(fname, 'r', encoding='utf-8') as f: items = json.load(f) assert isinstance(items, list), f'Data {fname} not list' assert all(isinstance(x, dict) for x in items), f'item in input file not list' assert all("prompt" in x for x in items), f'key prompt should be in dict item of input file' for i, x in enumerate(items): validate_file_item(fname, i, x) all_items.extend(items) if len(all_items) > BATCH_INFER_MAX_ITEMS: raise gr.Error(f"Num samples {len(all_items)} > {BATCH_INFER_MAX_ITEMS} allowed.") return all_items, filenames def remove_gradio_cache(exclude_names=None): """remove gradio cache to avoid flooding""" import shutil for root, dirs, files in os.walk('/tmp/gradio/'): for f in files: # if not any(f in ef for ef in except_files): if exclude_names is None or not any(ef in f for ef in exclude_names): print(f'Remove: {f}') os.unlink(os.path.join(root, f)) # for d in dirs: # # if not any(d in ef for ef in except_files): # if exclude_names is None or not any(ef in d for ef in exclude_names): # print(f'Remove d: {d}') # shutil.rmtree(os.path.join(root, d)) def maybe_upload_batch_set(pred_json_path): global LOG_FILE, DATA_SET_REPO_PATH, SAVE_LOGS if SAVE_LOGS and DATA_SET_REPO_PATH != "": try: from huggingface_hub import upload_file path_in_repo = "misc/" + os.path.basename(pred_json_path).replace(".json", f'.{time.time()}.json') print(f'upload {pred_json_path} to {DATA_SET_REPO_PATH}//{path_in_repo}') upload_file( path_or_fileobj=pred_json_path, path_in_repo=path_in_repo, repo_id=DATA_SET_REPO_PATH, token=HF_TOKEN, repo_type="dataset", create_pr=True ) except Exception as e: print(f'Failed to save to repo: {DATA_SET_REPO_PATH}|{str(e)}') def free_form_prompt(prompt, history=None, system_prompt=None): return prompt def batch_inference( files: Union[str, List[str]], prompt_mode: str, temperature: float, max_tokens: int, frequency_penalty: float, presence_penalty: float, stop_strings: str = "[STOP],,,<|im_start|>", current_time: Optional[float] = None, system_prompt: Optional[str] = SYSTEM_PROMPT_1 ): """ Handle file upload batch inference """ global LOG_FILE, LOG_PATH, DEBUG, llm, RES_PRINTED if DEBUG: return debug_file_function( files, prompt_mode, temperature, max_tokens, presence_penalty, stop_strings, current_time) from vllm import LLM, SamplingParams assert llm is not None # assert system_prompt.strip() != '', f'system prompt is empty' stop_strings = [x.strip() for x in stop_strings.strip().split(",")] tokenizer = llm.get_tokenizer() # force removing all # NOTE: need to make sure all cached items are removed!!!!!!!!! vllm_abort(llm) temperature = float(temperature) frequency_penalty = float(frequency_penalty) max_tokens = int(max_tokens) all_items, filenames = read_validate_json_files(files) # remove all items in /tmp/gradio/ remove_gradio_cache(exclude_names=['upload_chat.json', 'upload_few_shot.json']) if prompt_mode == 'chat': prompt_format_fn = chatml_format elif prompt_mode == 'few-shot': from functools import partial # prompt_format_fn = partial( # chatml_format, include_end_instruct=False # ) prompt_format_fn = free_form_prompt else: raise gr.Error(f'Wrong mode {prompt_mode}') full_prompts = [ prompt_format_fn( x['prompt'], [], sys_prompt=system_prompt ) for i, x in enumerate(all_items) ] print(f'{full_prompts[0]}\n') if any(len(tokenizer.encode(x, add_special_tokens=False)) >= 4090 for x in full_prompts): raise gr.Error(f"Some prompt is too long!") stop_seq = list(set(['', '', '<>', '<>', '[INST]', '[/INST]'] + stop_strings)) sampling_params = SamplingParams( temperature=temperature, max_tokens=max_tokens, frequency_penalty=frequency_penalty, presence_penalty=presence_penalty, stop=stop_seq ) generated = llm.generate(full_prompts, sampling_params, use_tqdm=False) responses = [g.outputs[0].text for g in generated] #responses = ["Our system is under maintenance, will be back soon!" for g in generated] if len(responses) != len(all_items): raise gr.Error(f'inconsistent lengths {len(responses)} != {len(all_items)}') for res, item in zip(responses, all_items): item['response'] = res save_path = BATCH_INFER_SAVE_TMP_FILE os.makedirs(os.path.dirname(save_path), exist_ok=True) with open(save_path, 'w', encoding='utf-8') as f: json.dump(all_items, f, indent=4, ensure_ascii=False) # You need to upload save_path as a new timestamp file. maybe_upload_batch_set(save_path) print_items = all_items[:2] # print_json = json.dumps(print_items, indent=4, ensure_ascii=False) return save_path, print_items # BATCH_INFER_MAX_ITEMS FILE_UPLOAD_DESCRIPTION = f"""Upload JSON file as list of dict with < {BATCH_INFER_MAX_ITEMS} items, \ each item has `prompt` key. We put guardrails to enhance safety, so do not input any harmful content or personal information! Re-upload the file after every submit. See the examples below. ``` [ {{"id": 0, "prompt": "Hello world"}} , {{"id": 1, "prompt": "Hi there?"}}] ``` """ CHAT_EXAMPLES = [ ["Hãy giải thích thuyết tương đối rộng."], ["Tolong bantu saya menulis email ke lembaga pemerintah untuk mencari dukungan finansial untuk penelitian AI."], ["ຂໍແຈ້ງ 5 ສະຖານທີ່ທ່ອງທ່ຽວໃນນະຄອນຫຼວງວຽງຈັນ"], ] # performance items def launch(): global demo, llm, DEBUG, LOG_FILE model_desc = MODEL_DESC model_path = MODEL_PATH model_title = MODEL_TITLE hf_model_name = HF_MODEL_NAME tensor_parallel = TENSOR_PARALLEL assert tensor_parallel > 0 , f'{tensor_parallel} invalid' dtype = DTYPE sys_prompt = SYSTEM_PROMPT_1 max_tokens = MAX_TOKENS temperature = TEMPERATURE frequence_penalty = FREQUENCE_PENALTY presence_penalty = PRESENCE_PENALTY ckpt_info = "None" print( f'Launch config: ' f'\n| model_title=`{model_title}` ' f'\n| max_tokens={max_tokens} ' f'\n| dtype={dtype} ' f'\n| tensor_parallel={tensor_parallel} ' f'\n| IS_DELETE_FOLDER={IS_DELETE_FOLDER} ' f'\n| STREAM_YIELD_MULTIPLE={STREAM_YIELD_MULTIPLE} ' f'\n| STREAM_CHECK_MULTIPLE={STREAM_CHECK_MULTIPLE} ' f'\n| DISPLAY_MODEL_PATH={DISPLAY_MODEL_PATH} ' f'\n| LANG_BLOCK_HISTORY={LANG_BLOCK_HISTORY} ' f'\n| frequence_penalty={frequence_penalty} ' f'\n| presence_penalty={presence_penalty} ' f'\n| temperature={temperature} ' # f'\n| hf_model_name={hf_model_name} ' f'\n| model_path={model_path} ' f'\n| DOWNLOAD_SNAPSHOT={DOWNLOAD_SNAPSHOT} ' f'\n| gpu_memory_utilization={gpu_memory_utilization} ' f'\n| LOG_PATH={LOG_PATH} | SAVE_LOGS={SAVE_LOGS} ' f'\n| Desc={model_desc}' ) if DEBUG: model_desc += "\n
!!!!! This is in debug mode, responses will copy original" response_fn = debug_chat_response_echo print(f'Creating in DEBUG MODE') if SAVE_LOGS: LOG_FILE = open(LOG_PATH, 'a', encoding='utf-8') else: # ! load the model maybe_delete_folder() if DOWNLOAD_SNAPSHOT: print(f'Downloading from HF_MODEL_NAME={hf_model_name} -> {model_path}') if HF_TOKEN is not None: print(f'Load with HF_TOKEN: {HF_TOKEN}') snapshot_download(hf_model_name, local_dir=model_path, use_auth_token=True, token=HF_TOKEN) else: snapshot_download(hf_model_name, local_dir=model_path) import vllm from vllm import LLM print(F'VLLM: {vllm.__version__}') ckpt_info = check_model_path(model_path) print(f'Load path: {model_path} | {ckpt_info}') if QUANTIZATION == 'awq': print(F'Load model in int4 quantization') llm = LLM(model=model_path, dtype="float16", tensor_parallel_size=tensor_parallel, gpu_memory_utilization=gpu_memory_utilization, quantization="awq", max_model_len=8192) else: llm = LLM(model=model_path, dtype=dtype, tensor_parallel_size=tensor_parallel, gpu_memory_utilization=gpu_memory_utilization, max_model_len=8192) try: print(llm.llm_engine.workers[0].model) except Exception as e: print(f'Cannot print model worker: {e}') try: llm.llm_engine.scheduler_config.max_model_len = 8192 llm.llm_engine.scheduler_config.max_num_batched_tokens = 8192 # llm.llm_engine.tokenizer.add_special_tokens = False except Exception as e: print(f'Cannot set parameters: {e}') print(f'Use system prompt:\n{sys_prompt}') response_fn = chat_response_stream_multiturn print(F'respond: {response_fn}') if SAVE_LOGS: LOG_FILE = open(LOG_PATH, 'a', encoding='utf-8') if ENABLE_BATCH_INFER: demo_file_upload = gr.Interface( batch_inference, inputs=[ gr.File(file_count='single', file_types=['json']), gr.Radio(["chat", "few-shot"], value='chat', label="Chat or Few-shot mode", info="Chat's output more user-friendly, Few-shot's output more consistent with few-shot patterns."), gr.Number(value=temperature, label='Temperature', info="Higher -> more random"), gr.Number(value=max_tokens, label='Max tokens', info='Increase if want more generation'), gr.Number(value=frequence_penalty, label='Frequency penalty', info='> 0 encourage new tokens over repeated tokens'), gr.Number(value=presence_penalty, label='Presence penalty', info='> 0 encourage new tokens, < 0 encourage existing tokens'), gr.Textbox(value="[STOP],[END],,", label='Stop strings', info='Comma-separated string to stop generation only in FEW-SHOT mode', lines=1), gr.Number(value=0, label='current_time', visible=False), ], outputs=[ # "file", gr.File(label="Generated file"), # "json" gr.JSON(label='Example outputs (display 2 samples)') ], description=FILE_UPLOAD_DESCRIPTION, allow_flagging=False, examples=[ ["upload_chat.json", "chat", 0.2, 1024, 0.5, 0, "[STOP],[END],,"], ["upload_few_shot.json", "few-shot", 0.2, 128, 0.5, 0, "[STOP],[END],,,\\n"] ], cache_examples=False, ) demo_chat = gr.ChatInterface( response_fn, chatbot=ChatBot( label=MODEL_NAME, bubble_full_width=False, latex_delimiters=[ { "left": "$", "right": "$", "display": False}, { "left": "$$", "right": "$$", "display": True}, ], show_copy_button=True, ), textbox=gr.Textbox(placeholder='Type message', lines=4, max_lines=128, min_width=200), submit_btn=gr.Button(value='Submit', variant="primary", scale=0), # ! consider preventing the stop button # stop_btn=None, # title=f"{model_title}", # description=f"{model_desc}", additional_inputs=[ gr.Number(value=temperature, label='Temperature (higher -> more random)'), gr.Number(value=max_tokens, label='Max generated tokens (increase if want more generation)'), gr.Number(value=frequence_penalty, label='Frequency penalty (> 0 encourage new tokens over repeated tokens)'), gr.Number(value=presence_penalty, label='Presence penalty (> 0 encourage new tokens, < 0 encourage existing tokens)'), gr.Number(value=0, label='current_time', visible=False), # ! Remove the system prompt textbox to avoid jailbreaking # gr.Textbox(value=sys_prompt, label='System prompt', lines=8) ], examples=CHAT_EXAMPLES, cache_examples=False ) descriptions = model_desc if DISPLAY_MODEL_PATH: descriptions += f"
{path_markdown.format(model_path=model_path)}" demo = CustomTabbedInterface( interface_list=[demo_chat, demo_file_upload], tab_names=["Chat Interface", "Batch Inference"], title=f"{model_title}", description=descriptions, ) demo.title = MODEL_NAME callback = None with demo: if DATA_SET_REPO_PATH != "": try: from performance_plot import attach_plot_to_demo attach_plot_to_demo(demo) except Exception as e: print(f'Fail to load DEMO plot: {str(e)}') gr.Markdown(cite_markdown) # if DISPLAY_MODEL_PATH: # gr.Markdown(path_markdown.format(model_path=model_path)) if ENABLE_AGREE_POPUP: demo.load(None, None, None, _js=AGREE_POP_SCRIPTS) demo.queue(api_open=False) demo.launch(server_port=PORT, show_api=False) else: descriptions = model_desc if DISPLAY_MODEL_PATH: descriptions += f"
{path_markdown.format(model_path=model_path)}" demo = gr.ChatInterface( response_fn, chatbot=ChatBot( label=MODEL_NAME, bubble_full_width=False, latex_delimiters=[ { "left": "$", "right": "$", "display": False}, { "left": "$$", "right": "$$", "display": True}, ], show_copy_button=True, ), textbox=gr.Textbox(placeholder='Type message', lines=4, max_lines=128, min_width=200), submit_btn=gr.Button(value='Submit', variant="primary", scale=0), # ! consider preventing the stop button # stop_btn=None, title=f"{model_title}", description=descriptions, additional_inputs=[ gr.Number(value=temperature, label='Temperature (higher -> more random)'), gr.Number(value=max_tokens, label='Max generated tokens (increase if want more generation)'), gr.Number(value=frequence_penalty, label='Frequency penalty (> 0 encourage new tokens over repeated tokens)'), gr.Number(value=presence_penalty, label='Presence penalty (> 0 encourage new tokens, < 0 encourage existing tokens)'), gr.Number(value=0, label='current_time', visible=False), # ! Remove the system prompt textbox to avoid jailbreaking # gr.Textbox(value=sys_prompt, label='System prompt', lines=8) ], examples=CHAT_EXAMPLES, cache_examples=False ) demo.title = MODEL_NAME with demo: gr.Markdown(cite_markdown) if DISPLAY_MODEL_PATH: gr.Markdown(path_markdown.format(model_path=model_path)) if ENABLE_AGREE_POPUP: demo.load(None, None, None, _js=AGREE_POP_SCRIPTS) demo.queue(api_open=False) demo.launch(server_port=PORT, show_api=False, allowed_paths=["seal_logo.png"]) # def main(): # launch() if __name__ == "__main__": launch()