2024-07-26 14:29:09 +00:00
# ruff: noqa: F821
# the above line disables the `undefined-name` rule for the model type variables
2023-01-20 11:24:39 +00:00
import torch
2024-05-22 14:22:57 +00:00
import enum
2024-05-14 10:33:18 +00:00
import os
2023-01-20 11:24:39 +00:00
2023-03-24 13:02:14 +00:00
from loguru import logger
2023-06-01 10:07:41 +00:00
from transformers . configuration_utils import PretrainedConfig
2023-03-27 07:23:22 +00:00
from transformers . models . auto import modeling_auto
2024-05-14 10:33:18 +00:00
from huggingface_hub import hf_hub_download , HfApi
2024-07-24 19:32:14 +00:00
from typing import Optional , List , Dict
2024-02-26 18:49:28 +00:00
from pathlib import Path
2023-01-31 17:53:56 +00:00
2023-12-11 11:46:30 +00:00
from text_generation_server . utils . speculate import get_speculate , set_speculate
2023-03-07 17:52:22 +00:00
from text_generation_server . models . model import Model
2024-07-05 08:29:56 +00:00
from text_generation_server . models . causal_lm import CausalLM , CausalLMBatchKeysLast
from text_generation_server . models . custom_modeling . opt_modeling import OPTForCausalLM
from text_generation_server . models . custom_modeling . mpt_modeling import (
MPTForCausalLM ,
)
2024-07-05 14:07:48 +00:00
from text_generation_server . models . bloom import BloomCausalLMBatch
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . bloom_modeling import (
BloomForCausalLM ,
)
2023-03-07 17:52:22 +00:00
from text_generation_server . models . seq2seq_lm import Seq2SeqLM
2024-07-05 08:29:56 +00:00
from text_generation_server . models . galactica import GalacticaCausalLMBatch
from text_generation_server . models . custom_modeling . neox_modeling import (
GPTNeoxForCausalLM ,
)
from text_generation_server . models . custom_modeling . phi_modeling import (
PhiConfig ,
PhiForCausalLM ,
)
from text_generation_server . models . custom_modeling . t5_modeling import (
T5ForConditionalGeneration ,
)
2023-01-20 11:24:39 +00:00
2024-07-24 19:32:14 +00:00
from text_generation_server . utils . adapter import (
AdapterParameters ,
build_layer_weight_lookup ,
load_and_merge_adapters ,
AdapterInfo ,
)
from text_generation_server . adapters . lora import LoraWeights
2024-06-10 07:09:50 +00:00
from text_generation_server . utils . import_utils import SYSTEM
2024-07-20 17:02:04 +00:00
from text_generation_server . utils . log import log_master
2024-06-10 07:09:50 +00:00
2023-06-19 07:53:45 +00:00
# The flag below controls whether to allow TF32 on matmul. This flag defaults to False
# in PyTorch 1.12 and later.
torch . backends . cuda . matmul . allow_tf32 = True
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch . backends . cudnn . allow_tf32 = True
# Disable gradients
torch . set_grad_enabled ( False )
__all__ = [
" Model " ,
" CausalLM " ,
" Seq2SeqLM " ,
2024-07-24 19:32:14 +00:00
" get_model_with_lora_adapters " ,
2023-06-19 07:53:45 +00:00
]
2023-07-18 14:21:18 +00:00
FLASH_ATT_ERROR_MESSAGE = " {} requires Flash Attention enabled models. "
2023-06-19 07:53:45 +00:00
2023-07-18 14:21:18 +00:00
FLASH_ATTENTION = True
2024-02-29 15:44:20 +00:00
2023-03-24 13:02:14 +00:00
try :
2024-07-01 21:28:00 +00:00
from text_generation_server . models . flash_causal_lm import FlashCausalLM
2024-07-05 08:29:56 +00:00
from text_generation_server . models . vlm_causal_lm import VlmCausalLM
2024-07-19 15:23:20 +00:00
from text_generation_server . models . custom_modeling . flash_deepseek_v2_modeling import (
FlashDeepseekV2ForCausalLM ,
DeepseekV2Config ,
)
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . flash_llama_modeling import (
FlashLlamaForCausalLM ,
2023-07-18 14:21:18 +00:00
)
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . flash_cohere_modeling import (
FlashCohereForCausalLM ,
2024-02-28 14:50:31 +00:00
)
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . flash_gemma_modeling import (
FlashGemmaForCausalLM ,
2024-03-22 16:59:25 +00:00
)
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . flash_gemma2_modeling import (
FlashGemma2ForCausalLM ,
2024-02-21 13:15:22 +00:00
)
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . flash_dbrx_modeling import (
FlashDbrxForCausalLM ,
DbrxConfig ,
)
from text_generation_server . models . custom_modeling . flash_rw_modeling import (
RWConfig ,
FlashRWForCausalLM ,
)
from text_generation_server . models . custom_modeling . flash_neox_modeling import (
FlashGPTNeoXForCausalLM ,
2024-06-27 14:04:20 +00:00
)
Pali gemma modeling (#1895)
This PR adds paligemma modeling code
Blog post: https://huggingface.co/blog/paligemma
Transformers PR: https://github.com/huggingface/transformers/pull/30814
install the latest changes and run with
```bash
# get the weights
# text-generation-server download-weights gv-hf/PaliGemma-base-224px-hf
# run TGI
text-generation-launcher --model-id gv-hf/PaliGemma-base-224px-hf
```
basic example sending various requests
```python
from huggingface_hub import InferenceClient
client = InferenceClient("http://127.0.0.1:3000")
images = [
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png",
]
prompts = [
"What animal is in this image?",
"Name three colors in this image.",
"What are 10 colors in this image?",
"Where is the cow standing?",
"answer en Where is the cow standing?",
"Is there a bird in the image?",
"Is ther a cow in the image?",
"Is there a rabbit in the image?",
"how many birds are in the image?",
"how many rabbits are in the image?",
]
for img in images:
print(f"\nImage: {img.split('/')[-1]}")
for prompt in prompts:
inputs = f"{prompt}\n"
json_data = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 30,
"do_sample": False,
},
}
generated_output = client.text_generation(prompt, max_new_tokens=30, stream=False)
print([f"{prompt}\n{generated_output}"])
```
---------
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
2024-05-16 04:58:47 +00:00
from text_generation_server . models . pali_gemma import (
2024-07-05 08:29:56 +00:00
PaliGemmaBatch ,
Pali gemma modeling (#1895)
This PR adds paligemma modeling code
Blog post: https://huggingface.co/blog/paligemma
Transformers PR: https://github.com/huggingface/transformers/pull/30814
install the latest changes and run with
```bash
# get the weights
# text-generation-server download-weights gv-hf/PaliGemma-base-224px-hf
# run TGI
text-generation-launcher --model-id gv-hf/PaliGemma-base-224px-hf
```
basic example sending various requests
```python
from huggingface_hub import InferenceClient
client = InferenceClient("http://127.0.0.1:3000")
images = [
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png",
]
prompts = [
"What animal is in this image?",
"Name three colors in this image.",
"What are 10 colors in this image?",
"Where is the cow standing?",
"answer en Where is the cow standing?",
"Is there a bird in the image?",
"Is ther a cow in the image?",
"Is there a rabbit in the image?",
"how many birds are in the image?",
"how many rabbits are in the image?",
]
for img in images:
print(f"\nImage: {img.split('/')[-1]}")
for prompt in prompts:
inputs = f"{prompt}\n"
json_data = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 30,
"do_sample": False,
},
}
generated_output = client.text_generation(prompt, max_new_tokens=30, stream=False)
print([f"{prompt}\n{generated_output}"])
```
---------
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
2024-05-16 04:58:47 +00:00
)
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . flash_pali_gemma_modeling import (
PaliGemmaForConditionalGeneration ,
)
from text_generation_server . models . custom_modeling . flash_phi_modeling import (
FlashPhiForCausalLM ,
2023-04-19 10:51:11 +00:00
)
2023-08-17 12:38:49 +00:00
from text_generation_server . models . idefics import IDEFICSSharded
2024-07-05 08:29:56 +00:00
from text_generation_server . models . custom_modeling . llava_next import (
LlavaNextForConditionalGeneration ,
)
from text_generation_server . models . custom_modeling . flash_santacoder_modeling import (
FlashSantacoderForCausalLM ,
)
from text_generation_server . models . custom_modeling . flash_starcoder2_modeling import (
FlashStarcoder2ForCausalLM ,
)
from text_generation_server . models . custom_modeling . flash_qwen2_modeling import (
Qwen2ForCausalLM ,
)
from text_generation_server . models . custom_modeling . flash_mistral_modeling import (
FlashMistralForCausalLM ,
)
from text_generation_server . models . custom_modeling . flash_mixtral_modeling import (
FlashMixtralForCausalLM ,
)
from text_generation_server . models . custom_modeling . flash_gpt2_modeling import (
FlashGPT2ForCausalLM ,
)
from text_generation_server . models . custom_modeling . idefics2 import (
Idefics2ForConditionalGeneration ,
)
2024-05-31 15:57:01 +00:00
from text_generation_server . layers . attention import SUPPORTS_WINDOWING
2023-07-18 14:21:18 +00:00
except ImportError as e :
2024-07-20 17:02:04 +00:00
log_master ( logger . warning , f " Could not import Flash Attention enabled models: { e } " )
2024-05-31 15:57:01 +00:00
SUPPORTS_WINDOWING = False
2023-04-03 17:06:42 +00:00
FLASH_ATTENTION = False
2023-03-24 13:02:14 +00:00
2023-04-03 17:06:42 +00:00
if FLASH_ATTENTION :
2024-07-01 21:28:00 +00:00
__all__ . append ( FlashCausalLM )
2023-08-17 12:38:49 +00:00
__all__ . append ( IDEFICSSharded )
2023-12-11 13:43:40 +00:00
2024-02-08 09:19:45 +00:00
MAMBA_AVAILABLE = True
try :
from text_generation_server . models . mamba import Mamba
except ImportError as e :
2024-07-20 17:02:04 +00:00
log_master ( logger . warning , f " Could not import Mamba: { e } " )
2024-02-08 09:19:45 +00:00
MAMBA_AVAILABLE = False
if MAMBA_AVAILABLE :
__all__ . append ( Mamba )
2023-12-11 13:43:40 +00:00
2024-02-08 17:41:25 +00:00
2024-05-22 14:22:57 +00:00
class ModelType ( enum . Enum ) :
2024-07-19 15:23:20 +00:00
DEEPSEEK_V2 = {
" type " : " deepseek_v2 " ,
" name " : " Deepseek V2 " ,
" url " : " https://huggingface.co/deepseek-ai/DeepSeek-V2 " ,
}
2024-05-22 14:22:57 +00:00
IDEFICS2 = {
" type " : " idefics2 " ,
" name " : " Idefics 2 " ,
" url " : " https://huggingface.co/HuggingFaceM4/idefics2-8b " ,
" multimodal " : True ,
}
LLAVA_NEXT = {
" type " : " llava_next " ,
" name " : " Llava Next (1.6) " ,
" url " : " https://huggingface.co/llava-hf/llava-v1.6-vicuna-13b-hf " ,
" multimodal " : True ,
}
LLAMA = {
" type " : " llama " ,
" name " : " Llama " ,
" url " : " https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct " ,
}
PHI3 = {
" type " : " phi3 " ,
" name " : " Phi 3 " ,
" url " : " https://huggingface.co/microsoft/Phi-3-mini-4k-instruct " ,
}
GEMMA = {
" type " : " gemma " ,
" name " : " Gemma " ,
" url " : " https://huggingface.co/google/gemma-7b " ,
}
2024-07-05 08:29:56 +00:00
PALIGEMMA = {
" type " : " paligemma " ,
" name " : " PaliGemma " ,
" url " : " https://huggingface.co/google/paligemma-3b-pt-224 " ,
}
2024-06-27 14:04:20 +00:00
GEMMA2 = {
" type " : " gemma2 " ,
" name " : " Gemma2 " ,
" url " : " https://huggingface.co/google/gemma2-9b " ,
}
2024-05-22 14:22:57 +00:00
COHERE = {
" type " : " cohere " ,
" name " : " Cohere " ,
" url " : " https://huggingface.co/CohereForAI/c4ai-command-r-plus " ,
}
DBRX = {
" type " : " dbrx " ,
" name " : " Dbrx " ,
" url " : " https://huggingface.co/databricks/dbrx-instruct " ,
}
MAMBA = {
" type " : " ssm " ,
" name " : " Mamba " ,
" url " : " https://huggingface.co/state-spaces/mamba-2.8b-slimpj " ,
}
MISTRAL = {
" type " : " mistral " ,
" name " : " Mistral " ,
" url " : " https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2 " ,
}
MIXTRAL = {
" type " : " mixtral " ,
" name " : " Mixtral " ,
" url " : " https://huggingface.co/mistralai/Mixtral-8x22B-Instruct-v0.1 " ,
}
GPT_BIGCODE = {
" type " : " gpt_bigcode " ,
" name " : " Gpt Bigcode " ,
" url " : " https://huggingface.co/bigcode/gpt_bigcode-santacoder " ,
}
PHI = {
" type " : " phi " ,
" name " : " Phi " ,
" url " : " https://huggingface.co/microsoft/phi-1_5 " ,
}
BAICHUAN = {
" type " : " baichuan " ,
" name " : " Baichuan " ,
" url " : " https://huggingface.co/baichuan-inc/Baichuan2-7B-Chat " ,
}
FALCON = {
" type " : " falcon " ,
" name " : " Falcon " ,
" url " : " https://huggingface.co/tiiuae/falcon-7b-instruct " ,
}
STARCODER2 = {
" type " : " starcoder2 " ,
" name " : " StarCoder 2 " ,
" url " : " https://huggingface.co/bigcode/starcoder2-15b-instruct-v0.1 " ,
}
QWEN2 = {
" type " : " qwen2 " ,
" name " : " Qwen 2 " ,
2024-06-14 09:59:33 +00:00
" url " : " https://huggingface.co/collections/Qwen/qwen2-6659360b33528ced941e557f " ,
2024-05-22 14:22:57 +00:00
}
OPT = {
" type " : " opt " ,
" name " : " Opt " ,
" url " : " https://huggingface.co/facebook/opt-6.7b " ,
}
T5 = {
" type " : " t5 " ,
" name " : " T5 " ,
" url " : " https://huggingface.co/google/flan-t5-xxl " ,
}
GALACTICA = {
" type " : " galactica " ,
" name " : " Galactica " ,
" url " : " https://huggingface.co/facebook/galactica-120b " ,
}
SANTACODER = {
" type " : " santacoder " ,
" name " : " SantaCoder " ,
" url " : " https://huggingface.co/bigcode/santacoder " ,
}
BLOOM = {
" type " : " bloom " ,
" name " : " Bloom " ,
" url " : " https://huggingface.co/bigscience/bloom-560m " ,
}
MPT = {
" type " : " mpt " ,
" name " : " Mpt " ,
" url " : " https://huggingface.co/mosaicml/mpt-7b-instruct " ,
}
GPT2 = {
" type " : " gpt2 " ,
" name " : " Gpt2 " ,
" url " : " https://huggingface.co/openai-community/gpt2 " ,
}
GPT_NEOX = {
" type " : " gpt_neox " ,
" name " : " Gpt Neox " ,
" url " : " https://huggingface.co/EleutherAI/gpt-neox-20b " ,
}
IDEFICS = {
" type " : " idefics " ,
" name " : " Idefics " ,
" url " : " https://huggingface.co/HuggingFaceM4/idefics-9b " ,
" multimodal " : True ,
}
__GLOBALS = locals ( )
for data in ModelType :
__GLOBALS [ data . name ] = data . value [ " type " ]
2023-01-31 17:53:56 +00:00
def get_model (
2023-05-23 18:40:39 +00:00
model_id : str ,
2024-06-25 18:46:27 +00:00
lora_adapter_ids : Optional [ List [ str ] ] ,
2023-05-23 18:40:39 +00:00
revision : Optional [ str ] ,
sharded : bool ,
quantize : Optional [ str ] ,
2023-12-11 11:46:30 +00:00
speculate : Optional [ int ] ,
2023-06-30 18:30:09 +00:00
dtype : Optional [ str ] ,
2023-05-23 18:40:39 +00:00
trust_remote_code : bool ,
2024-06-10 07:09:50 +00:00
max_input_tokens : int ,
2023-01-31 17:53:56 +00:00
) - > Model :
2024-05-31 15:57:01 +00:00
global FLASH_ATTENTION
2024-07-22 15:51:32 +00:00
config_dict , _ = PretrainedConfig . get_config_dict (
model_id , revision = revision , trust_remote_code = trust_remote_code
)
model_type = config_dict . get ( " model_type " , None )
quantization_config = config_dict . get ( " quantization_config " , None )
if quantization_config is not None and quantize is None :
method = quantization_config . get ( " quant_method " , None )
if method in { " gptq " , " awq " , " exl2 " } :
log_master ( logger . info , f " Auto selecting quantization method { method } " )
quantize = method
elif method == " fbgemm_fp8 " :
log_master ( logger . info , " Auto selecting quantization method fp8 " )
quantize = " fp8 "
else :
log_master ( logger . warning , f " Unknown quantization method { method } " )
2023-06-30 18:30:09 +00:00
if dtype is None :
2024-06-05 08:14:40 +00:00
if quantize in [ " awq " , " exl2 " , " gptq " , " marlin " ] :
2024-05-27 12:41:28 +00:00
# These quantizers only work with float16 params.
dtype = torch . float16
2024-07-20 17:02:04 +00:00
elif quantize == " fp8 " :
2024-07-22 15:51:32 +00:00
from text_generation_server . layers . fp8 import FBGEMM_DYN_AVAILABLE
2024-07-20 17:02:04 +00:00
2024-07-22 15:51:32 +00:00
if FBGEMM_DYN_AVAILABLE :
2024-07-20 17:02:04 +00:00
# fbgemm kernels are fp8xfp8->bf16
dtype = torch . bfloat16
2024-05-27 12:41:28 +00:00
else :
# Keep it as default for now and let
# every model resolve their own default dtype.
dtype = None
2023-06-30 18:30:09 +00:00
elif dtype == " float16 " :
dtype = torch . float16
elif dtype == " bfloat16 " :
dtype = torch . bfloat16
else :
raise RuntimeError ( f " Unknown dtype { dtype } " )
2023-12-11 11:46:30 +00:00
if speculate is not None :
set_speculate ( speculate )
else :
set_speculate ( 0 )
2024-05-14 10:33:18 +00:00
speculator = None
2023-12-11 11:46:30 +00:00
if " medusa_num_heads " in config_dict :
2024-02-26 18:49:28 +00:00
medusa_model_id = model_id
medusa_revision = revision
2023-12-11 11:46:30 +00:00
model_id = config_dict [ " base_model_name_or_path " ]
revision = " main "
speculate_medusa = config_dict [ " medusa_num_heads " ]
if speculate is not None :
if speculate > speculate_medusa :
2023-12-11 13:49:52 +00:00
raise RuntimeError (
2024-04-12 14:24:45 +00:00
f " Speculate is set to ` { speculate } ` but this medusa models only has ` { speculate_medusa } ` heads, please make them match "
2023-12-11 13:49:52 +00:00
)
2023-12-11 11:46:30 +00:00
else :
set_speculate ( speculate )
else :
set_speculate ( speculate_medusa )
config_dict , _ = PretrainedConfig . get_config_dict (
model_id , revision = revision , trust_remote_code = trust_remote_code
)
2024-05-14 10:33:18 +00:00
# Reload model type from parent.
model_type = config_dict . get ( " model_type " , None )
2024-02-26 18:49:28 +00:00
is_local = Path ( medusa_model_id ) . exists ( )
if not is_local :
medusa_config = hf_hub_download (
medusa_model_id , revision = medusa_revision , filename = " config.json "
)
hf_hub_download (
medusa_model_id ,
revision = medusa_revision ,
filename = " medusa_lm_head.safetensors " ,
)
2024-05-14 10:33:18 +00:00
speculator = {
" path " : Path ( medusa_config ) . parent ,
" model_paths " : [ " medusa_lm_head.safetensors " ] ,
}
2024-02-26 18:49:28 +00:00
else :
2024-05-14 10:33:18 +00:00
speculator = {
" path " : Path ( medusa_model_id ) ,
" model_paths " : [ " medusa_lm_head.safetensors " ] ,
}
2024-02-26 18:49:28 +00:00
2023-12-11 11:46:30 +00:00
method = " medusa "
2024-05-14 10:33:18 +00:00
elif model_type == " mlp_speculator " :
mlp_model_id = model_id
mlp_revision = revision
model_id = config_dict [ " base_model_name_or_path " ]
revision = " main "
speculate_mlp = config_dict [ " n_predict " ]
if speculate is not None :
if speculate > speculate_mlp :
raise RuntimeError (
f " Speculate is set to ` { speculate } ` but this mlp_speculator models only has ` { speculate_mlp } ` heads, please make them match "
)
else :
set_speculate ( speculate )
else :
set_speculate ( speculate_mlp )
config_dict , _ = PretrainedConfig . get_config_dict (
model_id , revision = revision , trust_remote_code = trust_remote_code
)
# Reload model type from parent.
model_type = config_dict . get ( " model_type " , None )
is_local = Path ( mlp_model_id ) . exists ( )
extension = " .safetensors "
if not is_local :
mlp_speculator_config = hf_hub_download (
mlp_model_id , revision = mlp_revision , filename = " config.json "
)
api = HfApi ( )
info = api . model_info ( mlp_model_id , revision = mlp_revision )
filenames = [
s . rfilename
for s in info . siblings
if s . rfilename . endswith ( extension )
and len ( s . rfilename . split ( " / " ) ) == 1
and " arguments " not in s . rfilename
and " args " not in s . rfilename
and " training " not in s . rfilename
]
for filename in filenames :
hf_hub_download (
mlp_model_id ,
revision = mlp_revision ,
filename = filename ,
)
speculator = {
" path " : Path ( mlp_speculator_config ) . parent ,
" model_paths " : filenames ,
}
else :
speculator = Path ( mlp_model_id )
filenames = [ p for p in os . listdir ( speculator ) if p . endswith ( extension ) ]
speculator = { " path " : speculator , " model_paths " : filenames }
method = " mlp_speculator "
2023-12-11 11:46:30 +00:00
else :
method = " n-gram "
speculate = get_speculate ( )
if speculate > 0 :
2024-07-20 17:02:04 +00:00
log_master (
logger . info , f " Using speculation { method } with { speculate } input ids. "
)
2023-12-11 11:46:30 +00:00
2024-02-08 09:19:45 +00:00
if model_type is None :
# TODO: fix how we determine model type for Mamba
if " ssm_cfg " in config_dict :
# *only happens in Mamba case
model_type = " ssm "
else :
raise RuntimeError (
f " Could not determine model type for { model_id } revision { revision } "
)
2024-05-28 09:51:31 +00:00
if quantize == " exl2 " and sharded :
raise RuntimeError (
" Sharding is currently not supported with `exl2` quantization "
)
2024-05-31 15:57:01 +00:00
sliding_window = config_dict . get ( " sliding_window " , - 1 )
2024-06-10 07:09:50 +00:00
if (
( sliding_window is not None and sliding_window != - 1 )
and not SUPPORTS_WINDOWING
and max_input_tokens > sliding_window
) :
raise ValueError (
f " The backend { SYSTEM } does not support sliding window attention that is used by the model type { model_type } . To use this model nonetheless with the { SYSTEM } backend, please launch TGI with the argument `--max-input-tokens` smaller than sliding_window= { sliding_window } (got here max_input_tokens= { max_input_tokens } ). "
2024-05-31 15:57:01 +00:00
)
2024-05-28 09:51:31 +00:00
2024-07-19 15:23:20 +00:00
if model_type == DEEPSEEK_V2 :
if FLASH_ATTENTION :
head_size = max (
config_dict . get ( " qk_nope_dim " , 128 )
+ config_dict . get ( " qk_rope_dim " , 64 ) ,
config_dict . get ( " v_head_dim " , 128 ) ,
)
return FlashCausalLM (
model_id = model_id ,
model_class = FlashDeepseekV2ForCausalLM ,
revision = revision ,
quantize = quantize ,
speculator = speculator ,
default_dtype = torch . bfloat16 ,
dtype = dtype ,
trust_remote_code = trust_remote_code ,
lora_adapter_ids = lora_adapter_ids ,
config_class = DeepseekV2Config ,
head_size = head_size ,
)
elif sharded :
raise NotImplementedError (
FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Deepseek V2 " )
)
else :
return CausalLM . fallback (
model_id ,
revision ,
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
elif model_type == MAMBA :
2024-02-08 09:19:45 +00:00
return Mamba (
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-08 09:19:45 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2023-01-31 17:53:56 +00:00
2024-02-28 11:07:08 +00:00
if model_id . startswith ( " facebook/galactica " ) :
2024-07-05 08:29:56 +00:00
return CausalLM (
model_id = model_id ,
# Yes galactica is just an OPT model.
model_class = OPTForCausalLM ,
revision = revision ,
2024-02-28 11:07:08 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-28 11:07:08 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
batch_class = GalacticaCausalLMBatch ,
2024-02-28 11:07:08 +00:00
)
2024-02-26 18:49:28 +00:00
if (
2024-05-22 14:22:57 +00:00
model_type == GPT_BIGCODE
or model_type == GPT2
2024-02-26 18:49:28 +00:00
and model_id . startswith ( " bigcode/ " )
) :
2023-06-08 12:51:52 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashSantacoderForCausalLM ,
revision = revision ,
2023-05-23 18:40:39 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
aliases = { " transformer.wte.weight " : [ " lm_head.weight " ] } ,
num_kv_heads = 1 ,
2023-05-23 18:40:39 +00:00
)
2023-06-08 12:51:52 +00:00
elif sharded :
raise NotImplementedError (
FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Santacoder " )
)
2023-05-15 08:35:20 +00:00
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
model_id = model_id ,
revision = revision ,
2023-05-23 18:40:39 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
)
2023-05-15 08:35:20 +00:00
2024-05-22 14:22:57 +00:00
if model_type == BLOOM :
2024-07-05 08:29:56 +00:00
return CausalLM (
model_id = model_id ,
model_class = BloomForCausalLM ,
revision = revision ,
2023-06-30 18:30:09 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 14:07:48 +00:00
batch_class = BloomCausalLMBatch ,
2023-06-08 12:51:52 +00:00
)
2024-05-22 14:22:57 +00:00
elif model_type == MPT :
2024-07-05 08:29:56 +00:00
return CausalLM (
model_id = model_id ,
model_class = MPTForCausalLM ,
revision = revision ,
2023-09-27 10:22:09 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-09-27 10:22:09 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
batch_class = CausalLMBatchKeysLast ,
2023-07-03 11:01:46 +00:00
)
2024-05-22 14:22:57 +00:00
elif model_type == GPT2 :
2024-05-15 11:31:22 +00:00
if FLASH_ATTENTION :
2024-05-23 12:39:38 +00:00
try :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashGPT2ForCausalLM ,
revision = revision ,
2024-05-23 12:39:38 +00:00
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-05-23 12:39:38 +00:00
)
except RuntimeError as e :
# Lots of legacy models with various weight names.
2024-07-20 17:02:04 +00:00
log_master ( logger . warning , f " Couldn ' t load flash gpt2 variant: { e } " )
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-05-23 12:39:38 +00:00
model_id ,
revision ,
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-05-15 11:31:22 +00:00
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded GPT-2 " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-05-15 11:31:22 +00:00
model_id ,
revision ,
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
elif model_type == GPT_NEOX :
2023-06-08 12:51:52 +00:00
if FLASH_ATTENTION :
2024-07-19 12:42:19 +00:00
from text_generation_server . models . custom_modeling . flash_neox_modeling import (
GPTNeoXConfig ,
)
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashGPTNeoXForCausalLM ,
revision = revision ,
2023-06-08 12:51:52 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-06-08 12:51:52 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-07-19 12:42:19 +00:00
config_class = GPTNeoXConfig ,
2023-06-08 12:51:52 +00:00
)
elif sharded :
2024-07-05 08:29:56 +00:00
return CausalLM (
model_id = model_id ,
model_class = GPTNeoxForCausalLM ,
revision = revision ,
2023-05-23 18:40:39 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
)
2023-01-31 17:53:56 +00:00
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2023-05-23 18:40:39 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
)
2024-01-26 18:04:57 +00:00
2024-05-22 14:22:57 +00:00
elif model_type == PHI :
2024-01-25 14:37:53 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashPhiForCausalLM ,
revision = revision ,
2024-01-25 14:37:53 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-01-25 14:37:53 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-01-25 14:37:53 +00:00
)
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-01-25 14:37:53 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-01-25 14:37:53 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
elif model_type == " phi-msft " :
if FLASH_ATTENTION :
2024-01-26 18:04:57 +00:00
raise NotImplementedError (
" Legacy phi-msft is not supported with Flash Attention "
)
2024-01-25 14:37:53 +00:00
else :
2024-07-05 08:29:56 +00:00
return CausalLM (
model_id = model_id ,
model_class = PhiForCausalLM ,
config_class = PhiConfig ,
revision = revision ,
2024-01-25 14:37:53 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-01-25 14:37:53 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2023-02-14 12:02:16 +00:00
2024-05-22 14:22:57 +00:00
elif model_type == LLAMA or model_type == BAICHUAN or model_type == PHI3 :
2024-07-26 14:29:09 +00:00
print ( f " >>> model_type: { model_type } " )
2023-06-08 12:51:52 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashLlamaForCausalLM ,
revision = revision ,
2023-05-23 18:40:39 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
2024-06-25 18:46:27 +00:00
lora_adapter_ids = lora_adapter_ids ,
2023-05-23 18:40:39 +00:00
)
2023-06-08 12:51:52 +00:00
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Llama " ) )
2022-10-28 17:24:00 +00:00
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2023-05-23 18:40:39 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
if model_type == GEMMA :
2024-02-21 13:15:22 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashGemmaForCausalLM ,
revision = revision ,
2024-02-21 13:15:22 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-21 13:15:22 +00:00
dtype = dtype ,
2024-07-05 08:29:56 +00:00
# Works better for these models
default_dtype = torch . bfloat16 ,
2024-02-21 13:15:22 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-02-21 13:15:22 +00:00
)
elif sharded :
2024-02-28 14:50:31 +00:00
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Gemma " ) )
2024-02-21 13:15:22 +00:00
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-02-21 13:15:22 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-21 13:15:22 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-06-27 14:04:20 +00:00
elif model_type == GEMMA2 :
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashGemma2ForCausalLM ,
revision = revision ,
2024-06-27 14:04:20 +00:00
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
2024-07-05 08:29:56 +00:00
# Works better for these models
default_dtype = torch . bfloat16 ,
2024-06-27 14:04:20 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-06-27 14:04:20 +00:00
)
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Gemma2 " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-06-27 14:04:20 +00:00
model_id ,
revision ,
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2023-02-14 12:02:16 +00:00
2024-05-22 14:22:57 +00:00
if model_type == COHERE :
2024-03-22 16:59:25 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashCohereForCausalLM ,
revision = revision ,
2024-03-22 16:59:25 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-03-22 16:59:25 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-03-22 16:59:25 +00:00
)
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Cohere " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-03-22 16:59:25 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-03-22 16:59:25 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
if model_type == DBRX :
2024-03-29 17:49:36 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashDbrxForCausalLM ,
revision = revision ,
2024-03-29 17:49:36 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-03-29 17:49:36 +00:00
dtype = dtype ,
2024-07-05 08:29:56 +00:00
# Dbrx works better in bfloat16.
default_dtype = torch . bfloat16 ,
2024-03-29 17:49:36 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
config_class = DbrxConfig ,
2024-03-29 17:49:36 +00:00
)
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded DBRX " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-03-29 17:49:36 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-03-29 17:49:36 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
if model_type in [ " RefinedWeb " , " RefinedWebModel " , FALCON ] :
2023-05-30 16:25:19 +00:00
if sharded :
if FLASH_ATTENTION :
2023-07-27 16:38:57 +00:00
if config_dict . get ( " alibi " , False ) :
2023-05-30 16:25:19 +00:00
raise NotImplementedError ( " sharded is not supported for this model " )
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashRWForCausalLM ,
revision = revision ,
2023-05-30 16:25:19 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2024-07-05 08:29:56 +00:00
aliases = {
" lm_head.weight " : [ " transformer.word_embeddings.weight " ] ,
" transformer.word_embeddings.weight " : [ " lm_head.weight " ] ,
} ,
2023-05-30 16:25:19 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
config_class = RWConfig ,
2023-05-30 16:25:19 +00:00
)
2024-07-26 14:29:09 +00:00
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Falcon " ) )
2023-05-30 16:25:19 +00:00
else :
2023-06-01 10:07:41 +00:00
if FLASH_ATTENTION and not config_dict . get ( " alibi " , False ) :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashRWForCausalLM ,
revision = revision ,
2023-05-30 16:25:19 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2024-07-08 11:22:38 +00:00
aliases = {
" lm_head.weight " : [ " transformer.word_embeddings.weight " ] ,
" transformer.word_embeddings.weight " : [ " lm_head.weight " ] ,
} ,
2023-05-30 16:25:19 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
config_class = RWConfig ,
2023-05-30 16:25:19 +00:00
)
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2023-05-30 16:25:19 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-30 16:25:19 +00:00
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
if model_type == MISTRAL :
2024-05-31 15:57:01 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashMistralForCausalLM ,
revision = revision ,
2023-09-28 07:55:47 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-09-28 07:55:47 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2023-09-28 07:55:47 +00:00
)
2024-02-28 14:50:31 +00:00
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Mistral " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-02-28 14:50:31 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-28 14:50:31 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2023-12-11 13:43:40 +00:00
2024-05-22 14:22:57 +00:00
if model_type == MIXTRAL :
2024-05-31 15:57:01 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashMixtralForCausalLM ,
revision = revision ,
2023-12-11 13:43:40 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-12-11 13:43:40 +00:00
dtype = dtype ,
2024-02-28 11:07:08 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-02-28 11:07:08 +00:00
)
2024-02-28 14:50:31 +00:00
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Mixtral " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-02-28 14:50:31 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-28 14:50:31 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
if model_type == STARCODER2 :
2024-05-31 15:57:01 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = FlashStarcoder2ForCausalLM ,
revision = revision ,
2024-02-28 11:07:08 +00:00
quantize = quantize ,
2024-07-05 08:29:56 +00:00
speculator = speculator ,
2024-02-28 11:07:08 +00:00
dtype = dtype ,
2024-02-28 14:50:31 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-02-28 14:50:31 +00:00
)
elif sharded :
raise NotImplementedError (
FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Starcoder2 " )
)
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-02-28 14:50:31 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-28 14:50:31 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2024-05-22 14:22:57 +00:00
if model_type == QWEN2 :
2024-06-10 07:09:50 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return FlashCausalLM (
model_id = model_id ,
model_class = Qwen2ForCausalLM ,
revision = revision ,
2024-02-28 14:50:31 +00:00
quantize = quantize ,
2024-07-05 08:29:56 +00:00
speculator = speculator ,
2024-02-28 14:50:31 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
2024-02-28 14:50:31 +00:00
)
elif sharded :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Sharded Qwen2 " ) )
else :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2024-02-28 14:50:31 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-02-28 14:50:31 +00:00
dtype = dtype ,
2023-12-11 13:43:40 +00:00
trust_remote_code = trust_remote_code ,
)
2023-09-28 07:55:47 +00:00
2024-05-22 14:22:57 +00:00
if model_type == OPT :
2024-07-05 08:29:56 +00:00
return CausalLM (
model_id = model_id ,
model_class = OPTForCausalLM ,
revision = revision ,
2023-06-30 18:30:09 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2023-06-08 12:51:52 +00:00
)
2023-04-11 17:16:41 +00:00
2024-05-22 14:22:57 +00:00
if model_type == T5 :
2024-07-05 08:29:56 +00:00
return Seq2SeqLM (
model_id = model_id ,
model_class = T5ForConditionalGeneration ,
revision = revision ,
2023-06-20 09:06:10 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-06-20 09:06:10 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
aliases = {
" shared.weight " : [
" encoder.embed_tokens.weight " ,
" decoder.embed_tokens.weight " ,
]
} ,
2023-06-20 09:06:10 +00:00
)
2024-05-22 14:22:57 +00:00
if model_type == IDEFICS :
2023-08-17 12:38:49 +00:00
if FLASH_ATTENTION :
2023-09-27 10:22:09 +00:00
return IDEFICSSharded (
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-09-27 10:22:09 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
2023-08-17 12:38:49 +00:00
else :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Idefics " ) )
2024-05-22 14:22:57 +00:00
if model_type == IDEFICS2 :
2024-04-23 21:04:44 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return VlmCausalLM (
model_id = model_id ,
model_class = Idefics2ForConditionalGeneration ,
revision = revision ,
2024-04-23 21:04:44 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2024-04-23 21:04:44 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
# XXX: Extremely important to cap resolution in order to limit
# VRAM usage.
processor_kwargs = { " size " : { " longest_edge " : 448 , " shortest_edge " : 378 } } ,
2024-04-23 21:04:44 +00:00
)
else :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Idefics " ) )
2024-07-05 08:29:56 +00:00
if model_type == PALIGEMMA :
Pali gemma modeling (#1895)
This PR adds paligemma modeling code
Blog post: https://huggingface.co/blog/paligemma
Transformers PR: https://github.com/huggingface/transformers/pull/30814
install the latest changes and run with
```bash
# get the weights
# text-generation-server download-weights gv-hf/PaliGemma-base-224px-hf
# run TGI
text-generation-launcher --model-id gv-hf/PaliGemma-base-224px-hf
```
basic example sending various requests
```python
from huggingface_hub import InferenceClient
client = InferenceClient("http://127.0.0.1:3000")
images = [
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png",
]
prompts = [
"What animal is in this image?",
"Name three colors in this image.",
"What are 10 colors in this image?",
"Where is the cow standing?",
"answer en Where is the cow standing?",
"Is there a bird in the image?",
"Is ther a cow in the image?",
"Is there a rabbit in the image?",
"how many birds are in the image?",
"how many rabbits are in the image?",
]
for img in images:
print(f"\nImage: {img.split('/')[-1]}")
for prompt in prompts:
inputs = f"{prompt}\n"
json_data = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 30,
"do_sample": False,
},
}
generated_output = client.text_generation(prompt, max_new_tokens=30, stream=False)
print([f"{prompt}\n{generated_output}"])
```
---------
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
2024-05-16 04:58:47 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return VlmCausalLM (
model_id = model_id ,
model_class = PaliGemmaForConditionalGeneration ,
revision = revision ,
Pali gemma modeling (#1895)
This PR adds paligemma modeling code
Blog post: https://huggingface.co/blog/paligemma
Transformers PR: https://github.com/huggingface/transformers/pull/30814
install the latest changes and run with
```bash
# get the weights
# text-generation-server download-weights gv-hf/PaliGemma-base-224px-hf
# run TGI
text-generation-launcher --model-id gv-hf/PaliGemma-base-224px-hf
```
basic example sending various requests
```python
from huggingface_hub import InferenceClient
client = InferenceClient("http://127.0.0.1:3000")
images = [
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png",
]
prompts = [
"What animal is in this image?",
"Name three colors in this image.",
"What are 10 colors in this image?",
"Where is the cow standing?",
"answer en Where is the cow standing?",
"Is there a bird in the image?",
"Is ther a cow in the image?",
"Is there a rabbit in the image?",
"how many birds are in the image?",
"how many rabbits are in the image?",
]
for img in images:
print(f"\nImage: {img.split('/')[-1]}")
for prompt in prompts:
inputs = f"{prompt}\n"
json_data = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 30,
"do_sample": False,
},
}
generated_output = client.text_generation(prompt, max_new_tokens=30, stream=False)
print([f"{prompt}\n{generated_output}"])
```
---------
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
2024-05-16 04:58:47 +00:00
quantize = quantize ,
speculator = speculator ,
dtype = dtype ,
2024-07-05 08:29:56 +00:00
# Works better for these models
default_dtype = torch . bfloat16 ,
Pali gemma modeling (#1895)
This PR adds paligemma modeling code
Blog post: https://huggingface.co/blog/paligemma
Transformers PR: https://github.com/huggingface/transformers/pull/30814
install the latest changes and run with
```bash
# get the weights
# text-generation-server download-weights gv-hf/PaliGemma-base-224px-hf
# run TGI
text-generation-launcher --model-id gv-hf/PaliGemma-base-224px-hf
```
basic example sending various requests
```python
from huggingface_hub import InferenceClient
client = InferenceClient("http://127.0.0.1:3000")
images = [
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png",
]
prompts = [
"What animal is in this image?",
"Name three colors in this image.",
"What are 10 colors in this image?",
"Where is the cow standing?",
"answer en Where is the cow standing?",
"Is there a bird in the image?",
"Is ther a cow in the image?",
"Is there a rabbit in the image?",
"how many birds are in the image?",
"how many rabbits are in the image?",
]
for img in images:
print(f"\nImage: {img.split('/')[-1]}")
for prompt in prompts:
inputs = f"{prompt}\n"
json_data = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 30,
"do_sample": False,
},
}
generated_output = client.text_generation(prompt, max_new_tokens=30, stream=False)
print([f"{prompt}\n{generated_output}"])
```
---------
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
2024-05-16 04:58:47 +00:00
trust_remote_code = trust_remote_code ,
2024-07-05 08:29:56 +00:00
lora_adapter_ids = lora_adapter_ids ,
batch_class = PaliGemmaBatch ,
Pali gemma modeling (#1895)
This PR adds paligemma modeling code
Blog post: https://huggingface.co/blog/paligemma
Transformers PR: https://github.com/huggingface/transformers/pull/30814
install the latest changes and run with
```bash
# get the weights
# text-generation-server download-weights gv-hf/PaliGemma-base-224px-hf
# run TGI
text-generation-launcher --model-id gv-hf/PaliGemma-base-224px-hf
```
basic example sending various requests
```python
from huggingface_hub import InferenceClient
client = InferenceClient("http://127.0.0.1:3000")
images = [
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png",
]
prompts = [
"What animal is in this image?",
"Name three colors in this image.",
"What are 10 colors in this image?",
"Where is the cow standing?",
"answer en Where is the cow standing?",
"Is there a bird in the image?",
"Is ther a cow in the image?",
"Is there a rabbit in the image?",
"how many birds are in the image?",
"how many rabbits are in the image?",
]
for img in images:
print(f"\nImage: {img.split('/')[-1]}")
for prompt in prompts:
inputs = f"{prompt}\n"
json_data = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 30,
"do_sample": False,
},
}
generated_output = client.text_generation(prompt, max_new_tokens=30, stream=False)
print([f"{prompt}\n{generated_output}"])
```
---------
Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
2024-05-16 04:58:47 +00:00
)
else :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " Idefics " ) )
2023-02-14 12:02:16 +00:00
2024-05-22 14:22:57 +00:00
if model_type == LLAVA_NEXT :
Adding Llava-Next (Llava 1.6) with full support. (#1709)
# What does this PR do?
- Changed all models to extract `embed_tokens` in order to enable llava
to separately call the embeddings and the core model layers.
- Added VlmCausalLM to inherit from FlashMistral in order to be
maximally supported. The only added logics sits on top and parses images
into pixel values, preallocates input_ids space for the image
embeddings, and passes them for the model.
- Added Clip for the vision tower.
- Didn't add flash for the vision tower since there's no padding anyway.
- Added heuristic (potentially incomplete) to calculate number of
features *before* calculating the clip patches (allows for easier logic
reuse of the LLM under the hood).
Still needs to be done:
- [x] Implement the image parsing in the controller side, to avoid
downloading n times per TP shard and also refusing requests too large
early and avoid issues where the truncation actually truncates the
image.
- [ ] Make sure it works with quantization properly.
- [x] Make sure it works with TP>1
<!--
Congratulations! You've made it this far! You're not quite done yet
though.
Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.
Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.
Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## Who can review?
Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
2024-04-09 19:32:00 +00:00
if FLASH_ATTENTION :
2024-07-05 08:29:56 +00:00
return VlmCausalLM (
model_class = LlavaNextForConditionalGeneration ,
model_id = model_id ,
revision = revision ,
Adding Llava-Next (Llava 1.6) with full support. (#1709)
# What does this PR do?
- Changed all models to extract `embed_tokens` in order to enable llava
to separately call the embeddings and the core model layers.
- Added VlmCausalLM to inherit from FlashMistral in order to be
maximally supported. The only added logics sits on top and parses images
into pixel values, preallocates input_ids space for the image
embeddings, and passes them for the model.
- Added Clip for the vision tower.
- Didn't add flash for the vision tower since there's no padding anyway.
- Added heuristic (potentially incomplete) to calculate number of
features *before* calculating the clip patches (allows for easier logic
reuse of the LLM under the hood).
Still needs to be done:
- [x] Implement the image parsing in the controller side, to avoid
downloading n times per TP shard and also refusing requests too large
early and avoid issues where the truncation actually truncates the
image.
- [ ] Make sure it works with quantization properly.
- [x] Make sure it works with TP>1
<!--
Congratulations! You've made it this far! You're not quite done yet
though.
Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.
Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.
Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## Who can review?
Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
2024-04-09 19:32:00 +00:00
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
Adding Llava-Next (Llava 1.6) with full support. (#1709)
# What does this PR do?
- Changed all models to extract `embed_tokens` in order to enable llava
to separately call the embeddings and the core model layers.
- Added VlmCausalLM to inherit from FlashMistral in order to be
maximally supported. The only added logics sits on top and parses images
into pixel values, preallocates input_ids space for the image
embeddings, and passes them for the model.
- Added Clip for the vision tower.
- Didn't add flash for the vision tower since there's no padding anyway.
- Added heuristic (potentially incomplete) to calculate number of
features *before* calculating the clip patches (allows for easier logic
reuse of the LLM under the hood).
Still needs to be done:
- [x] Implement the image parsing in the controller side, to avoid
downloading n times per TP shard and also refusing requests too large
early and avoid issues where the truncation actually truncates the
image.
- [ ] Make sure it works with quantization properly.
- [x] Make sure it works with TP>1
<!--
Congratulations! You've made it this far! You're not quite done yet
though.
Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.
Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.
Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## Who can review?
Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
2024-04-09 19:32:00 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
)
else :
raise NotImplementedError ( FLASH_ATT_ERROR_MESSAGE . format ( " LlavaNext " ) )
2023-02-14 12:02:16 +00:00
if sharded :
2023-12-15 11:52:24 +00:00
raise NotImplementedError ( " sharded is not supported for AutoModel " )
feat(server): Add inference support for GPTQ (llama + falcon tested) + Quantization script (#438)
Let's start discussing implementation.
- Need to expose the quantization scripts (either included here or add
doc on how to use https://github.com/qwopqwop200/GPTQ-for-LLaMa)
- Make sure GPTQ works for multiple models (priority to Falcon).
Currently it means that every place we use `get_{tensor|sharded}` to
check for quantization.
My idea is to reintegrate as much as possible into `utils/layer.py` by
expanding `load_multi` to be a bit more generic.
This might require some thinking, but ultimately the
`qweight,qzeros,scales,g_idx` should be in a single place, and
independant of bias presence.
# What does this PR do?
<!--
Congratulations! You've made it this far! You're not quite done yet
though.
Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.
Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.
Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## Who can review?
Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
---------
Co-authored-by: Ubuntu <ubuntu@ip-172-31-41-161.ec2.internal>
Co-authored-by: OlivierDehaene <olivier@huggingface.co>
2023-06-26 10:27:01 +00:00
if quantize == " gptq " :
2023-12-15 11:52:24 +00:00
raise NotImplementedError (
feat(server): Add inference support for GPTQ (llama + falcon tested) + Quantization script (#438)
Let's start discussing implementation.
- Need to expose the quantization scripts (either included here or add
doc on how to use https://github.com/qwopqwop200/GPTQ-for-LLaMa)
- Make sure GPTQ works for multiple models (priority to Falcon).
Currently it means that every place we use `get_{tensor|sharded}` to
check for quantization.
My idea is to reintegrate as much as possible into `utils/layer.py` by
expanding `load_multi` to be a bit more generic.
This might require some thinking, but ultimately the
`qweight,qzeros,scales,g_idx` should be in a single place, and
independant of bias presence.
# What does this PR do?
<!--
Congratulations! You've made it this far! You're not quite done yet
though.
Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.
Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.
Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## Who can review?
Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
---------
Co-authored-by: Ubuntu <ubuntu@ip-172-31-41-161.ec2.internal>
Co-authored-by: OlivierDehaene <olivier@huggingface.co>
2023-06-26 10:27:01 +00:00
" gptq quantization is not supported for AutoModel, you can try to quantize it with `text-generation-server quantize ORIGINAL_MODEL_ID NEW_MODEL_ID` "
)
Add AWQ quantization inference support (#1019) (#1054)
# Add AWQ quantization inference support
Fixes
https://github.com/huggingface/text-generation-inference/issues/781
This PR (partially) adds support for AWQ quantization for inference.
More information on AWQ [here](https://arxiv.org/abs/2306.00978). In
general, AWQ is faster and more accurate than GPTQ, which is currently
supported by TGI.
This PR installs 4-bit GEMM custom CUDA kernels released by AWQ authors
(in `requirements.txt`, just one line change).
Quick way to test this PR would be bring up TGI as follows:
```
text-generation-server download-weights abhinavkulkarni/codellama-CodeLlama-7b-Python-hf-w4-g128-awq
text-generation-launcher \
--huggingface-hub-cache ~/.cache/huggingface/hub/ \
--model-id abhinavkulkarni/codellama-CodeLlama-7b-Python-hf-w4-g128-awq \
--trust-remote-code --port 8080 \
--max-input-length 2048 --max-total-tokens 4096 --max-batch-prefill-tokens 4096 \
--quantize awq
```
Please note:
* This PR was tested with FlashAttention v2 and vLLM.
* This PR adds support for AWQ inference, not quantizing the models.
That needs to be done outside of TGI, instructions
[here](https://github.com/mit-han-lab/llm-awq/tree/f084f40bd996f3cf3a0633c1ad7d9d476c318aaa).
* This PR only adds support for `FlashLlama` models for now.
* Multi-GPU setup has not been tested.
* No integration tests have been added so far, will add later if
maintainers are interested in this change.
* This PR can be tested on any of the models released
[here](https://huggingface.co/abhinavkulkarni?sort_models=downloads#models).
Please refer to the linked issue for benchmarks for
[abhinavkulkarni/meta-llama-Llama-2-7b-chat-hf-w4-g128-awq](https://huggingface.co/abhinavkulkarni/meta-llama-Llama-2-7b-chat-hf-w4-g128-awq)
vs
[TheBloke/Llama-2-7b-Chat-GPTQ](https://huggingface.co/TheBloke/Llama-2-7b-Chat-GPTQ).
Please note, AWQ has released faster (and in case of Llama, fused)
kernels for 4-bit GEMM, currently at the top of the `main` branch at
https://github.com/mit-han-lab/llm-awq, but this PR uses an older commit
that has been tested to work. We can switch to latest commit later on.
## Who can review?
@OlivierDehaene OR @Narsil
---------
# What does this PR do?
<!--
Congratulations! You've made it this far! You're not quite done yet
though.
Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.
Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.
Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## Who can review?
Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
---------
Co-authored-by: Abhinav M Kulkarni <abhinavkulkarni@gmail.com>
Co-authored-by: Abhinav Kulkarni <abhinav@concentric.ai>
2023-09-25 13:31:27 +00:00
if quantize == " awq " :
2023-12-15 11:52:24 +00:00
raise NotImplementedError ( " awq quantization is not supported for AutoModel " )
2023-08-03 21:00:59 +00:00
elif ( quantize == " bitsandbytes-fp4 " ) or ( quantize == " bitsandbytes-nf4 " ) :
2023-12-15 11:52:24 +00:00
raise NotImplementedError ( " 4bit quantization is not supported for AutoModel " )
2023-12-11 13:49:52 +00:00
elif quantize == " eetq " :
2023-12-15 11:52:24 +00:00
raise NotImplementedError ( " Eetq quantization is not supported for AutoModel " )
2024-05-28 09:51:31 +00:00
elif quantize == " exl2 " :
raise NotImplementedError ( " exl2 quantization is not supported for AutoModel " )
2023-03-27 07:23:22 +00:00
if model_type in modeling_auto . MODEL_FOR_CAUSAL_LM_MAPPING_NAMES :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2023-06-30 18:30:09 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2023-05-23 18:40:39 +00:00
)
2023-03-27 07:23:22 +00:00
if model_type in modeling_auto . MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES :
2024-07-05 08:29:56 +00:00
return Seq2SeqLM . fallback (
2023-06-30 18:30:09 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
trust_remote_code = trust_remote_code ,
2023-05-23 18:40:39 +00:00
)
2023-06-01 10:07:41 +00:00
auto_map = config_dict . get ( " auto_map " , None )
2023-05-23 18:40:39 +00:00
if trust_remote_code and auto_map is not None :
if " AutoModelForCausalLM " in auto_map . keys ( ) :
2024-07-05 08:29:56 +00:00
return CausalLM . fallback (
2023-05-23 18:40:39 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
)
2023-05-26 10:31:47 +00:00
if " AutoModelForSeq2SeqLM " in auto_map . keys ( ) :
2024-07-05 08:29:56 +00:00
return Seq2SeqLM . fallback (
2023-05-23 18:40:39 +00:00
model_id ,
revision ,
quantize = quantize ,
2024-05-14 10:33:18 +00:00
speculator = speculator ,
2023-06-30 18:30:09 +00:00
dtype = dtype ,
2023-05-23 18:40:39 +00:00
trust_remote_code = trust_remote_code ,
)
2023-03-27 07:23:22 +00:00
raise ValueError ( f " Unsupported model type { model_type } " )
2024-07-24 19:32:14 +00:00
# get_model_with_lora_adapters wraps the internal get_model function and adds support for loading adapters
# this provides a post model loading hook to load adapters into the model after the model has been loaded
def get_model_with_lora_adapters (
model_id : str ,
lora_adapters : Optional [ List [ AdapterInfo ] ] ,
revision : Optional [ str ] ,
sharded : bool ,
quantize : Optional [ str ] ,
speculate : Optional [ int ] ,
dtype : Optional [ str ] ,
trust_remote_code : bool ,
max_input_tokens : int ,
adapter_to_index : Dict [ str , int ] ,
) :
lora_adapter_ids = [ adapter . id for adapter in lora_adapters ]
model = get_model (
model_id ,
lora_adapter_ids ,
revision ,
sharded ,
quantize ,
speculate ,
dtype ,
trust_remote_code ,
max_input_tokens ,
)
if len ( lora_adapters ) > 0 :
target_to_layer = build_layer_weight_lookup ( model . model )
for index , adapter in enumerate ( lora_adapters ) :
# The AdapterParameters object allows for merging multiple adapters into a single adapter.
# At the moment, we only support loading a single adapter into the model, but we keep the
# AdapterParameters object for easier extension in the future.
adapter_parameters = AdapterParameters (
adapter_info = [ adapter ] ,
# when merging multiple adapters we can weight them differently
# if this is not set, all adapters will be weighted equally
# see: text_generation_server.utils.merges.strategies for impl
weights = None ,
merge_strategy = 0 ,
density = 1.0 ,
majority_sign_method = 0 ,
)
adapter_index = index + 1
adapter_to_index [ adapter . id ] = adapter_index
logger . info (
f " Loading adapter weights into model: { ' , ' . join ( [ adapter . id for adapter in adapter_parameters . adapter_info ] ) } "
)
weight_names = tuple ( [ v [ 0 ] for v in target_to_layer . values ( ) ] )
(
module_map ,
adapter_config ,
adapter_weight_names ,
adapter_tokenizer ,
) = load_and_merge_adapters (
model . model_id ,
adapter_parameters ,
adapter_index ,
weight_names ,
False ,
)
unused_weight_names = adapter_weight_names . copy ( )
adapter_layers = [
" q_proj " ,
" k_proj " ,
" v_proj " ,
" o_proj " ,
" gate_proj " ,
" up_proj " ,
" down_proj " ,
]
for layer_name in adapter_layers :
nlayers = (
1 if layer_name == " lm_head " else len ( model . model . model . layers )
)
adapter_weights = LoraWeights . prepare_weights (
config = adapter_config ,
module_map = module_map ,
layer_type = layer_name ,
unused_weight_names = unused_weight_names ,
nlayers = nlayers ,
dtype = model . dtype ,
world_size = model . world_size ,
process_group = model . process_group ,
target_to_layer = target_to_layer ,
)
if adapter_weights is None :
continue
model . layer_to_adapter_weights [ layer_name ] . add_adapter (
adapter_index , adapter_weights
)
if len ( unused_weight_names ) > 0 :
logger . warning (
f " { ' , ' . join ( adapter_parameters . adapter_ids ) } unused adapter weights: { unused_weight_names } "
)
if adapter_tokenizer is not None :
model . tokenizers . add_tokenizer ( adapter_index , adapter_tokenizer )
model . loaded_adapters . add ( adapter_index )
return model