2024-02-28 07:50:31 -07:00
|
|
|
import math
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.distributed
|
|
|
|
|
|
|
|
from opentelemetry import trace
|
2024-04-26 11:19:08 -06:00
|
|
|
from transformers import AutoTokenizer, AutoConfig
|
2024-02-28 07:50:31 -07:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from text_generation_server.models.flash_mistral import (
|
|
|
|
BaseFlashMistral,
|
|
|
|
set_sliding_window,
|
|
|
|
)
|
|
|
|
from text_generation_server.models.custom_modeling.flash_qwen2_modeling import (
|
|
|
|
Qwen2ForCausalLM,
|
|
|
|
)
|
|
|
|
from text_generation_server.utils import (
|
|
|
|
initialize_torch_distributed,
|
|
|
|
weight_files,
|
|
|
|
Weights,
|
|
|
|
)
|
2024-07-01 06:32:54 -06:00
|
|
|
from text_generation_server.utils.import_utils import SYSTEM
|
2024-02-28 07:50:31 -07:00
|
|
|
|
|
|
|
tracer = trace.get_tracer(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FlashQwen2(BaseFlashMistral):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
model_id: str,
|
|
|
|
revision: Optional[str] = None,
|
|
|
|
quantize: Optional[str] = None,
|
2024-05-14 04:33:18 -06:00
|
|
|
speculator: Optional[str] = None,
|
2024-02-28 07:50:31 -07:00
|
|
|
dtype: Optional[torch.dtype] = None,
|
|
|
|
trust_remote_code: bool = False,
|
|
|
|
):
|
|
|
|
self.process_group, rank, world_size = initialize_torch_distributed()
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
device = torch.device(f"cuda:{rank}")
|
|
|
|
dtype = torch.float16 if dtype is None else dtype
|
2024-07-01 06:32:54 -06:00
|
|
|
elif SYSTEM == "ipex":
|
|
|
|
if hasattr(torch, "xpu") and torch.xpu.is_available():
|
|
|
|
device = torch.device(f"xpu:{rank}")
|
|
|
|
dtype = torch.float16 if dtype is None else dtype
|
|
|
|
else:
|
|
|
|
device = torch.device("cpu")
|
|
|
|
dtype = torch.bfloat16 if dtype is None else dtype
|
2024-02-28 07:50:31 -07:00
|
|
|
else:
|
|
|
|
raise NotImplementedError("FlashQwen2 is only available on GPU")
|
|
|
|
|
2024-04-26 11:19:08 -06:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
2024-02-28 07:50:31 -07:00
|
|
|
model_id,
|
|
|
|
revision=revision,
|
|
|
|
padding_side="left",
|
|
|
|
truncation_side="left",
|
|
|
|
trust_remote_code=trust_remote_code,
|
|
|
|
)
|
|
|
|
|
2024-04-26 11:19:08 -06:00
|
|
|
config = AutoConfig.from_pretrained(
|
2024-02-28 07:50:31 -07:00
|
|
|
model_id, revision=revision, trust_remote_code=trust_remote_code
|
|
|
|
)
|
|
|
|
config.quantize = quantize
|
2024-05-14 04:33:18 -06:00
|
|
|
config.speculator = speculator
|
2024-02-28 07:50:31 -07:00
|
|
|
|
|
|
|
# Set context windows
|
|
|
|
if config.sliding_window is not None:
|
2024-06-05 04:18:38 -06:00
|
|
|
set_sliding_window(config.sliding_window)
|
2024-02-28 07:50:31 -07:00
|
|
|
|
|
|
|
torch.distributed.barrier(group=self.process_group)
|
|
|
|
|
|
|
|
filenames = weight_files(model_id, revision=revision, extension=".safetensors")
|
|
|
|
weights = Weights(filenames, device, dtype, process_group=self.process_group)
|
2024-06-14 01:45:42 -06:00
|
|
|
if config.quantize in ["gptq", "awq", "marlin"]:
|
2024-02-28 07:50:31 -07:00
|
|
|
weights._set_gptq_params(model_id, revision)
|
|
|
|
|
|
|
|
model = Qwen2ForCausalLM(config, weights)
|
|
|
|
|
|
|
|
self.cuda_graphs = {}
|
|
|
|
|
|
|
|
torch.distributed.barrier(group=self.process_group)
|
|
|
|
super(BaseFlashMistral, self).__init__(
|
2024-06-25 12:46:27 -06:00
|
|
|
model_id=model_id,
|
2024-02-28 07:50:31 -07:00
|
|
|
model=model,
|
|
|
|
tokenizer=tokenizer,
|
|
|
|
num_layers=len(model.model.layers),
|
|
|
|
num_kv_heads=model.model.num_key_value_heads,
|
|
|
|
head_size=model.model.head_size,
|
|
|
|
dtype=dtype,
|
|
|
|
device=device,
|
|
|
|
rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
sliding_window=config.sliding_window,
|
|
|
|
)
|