2023-08-17 06:38:49 -06:00
|
|
|
import torch
|
|
|
|
import torch.distributed
|
|
|
|
|
|
|
|
from typing import List, Optional, Tuple
|
|
|
|
|
|
|
|
from transformers import (
|
|
|
|
AutoTokenizer,
|
|
|
|
AutoConfig,
|
|
|
|
AutoProcessor,
|
|
|
|
)
|
|
|
|
|
|
|
|
from text_generation_server.models.custom_modeling.idefics_config import IdeficsConfig
|
|
|
|
from text_generation_server.models.custom_modeling.idefics_processing import (
|
|
|
|
IdeficsProcessor,
|
|
|
|
)
|
|
|
|
from transformers import LlamaTokenizerFast
|
|
|
|
from text_generation_server.models.custom_modeling.idefics_modeling import (
|
|
|
|
IdeficsForVisionText2Text,
|
|
|
|
)
|
|
|
|
from text_generation_server.models.idefics_causal_lm import IdeficsCausalLM
|
|
|
|
from text_generation_server.utils import (
|
|
|
|
initialize_torch_distributed,
|
|
|
|
weight_files,
|
|
|
|
Weights,
|
|
|
|
)
|
2024-07-09 12:04:03 -06:00
|
|
|
from text_generation_server.utils.quantization import get_loader
|
2023-08-17 06:38:49 -06:00
|
|
|
|
|
|
|
|
|
|
|
class IDEFICSSharded(IdeficsCausalLM):
|
|
|
|
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,
|
2023-08-17 06:38:49 -06: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}")
|
|
|
|
# 9b seems to work correctly enough in float16, but 80b seems
|
|
|
|
# to be really saturating for f16.
|
2024-02-29 05:16:34 -07:00
|
|
|
dtype = torch.float16 if dtype is None else dtype
|
2023-08-17 06:38:49 -06:00
|
|
|
else:
|
|
|
|
device = torch.device("cpu")
|
2023-09-19 09:19:28 -06:00
|
|
|
dtype = torch.float32 if dtype is None else dtype
|
2023-08-17 06:38:49 -06:00
|
|
|
self.device, self.dtype = device, dtype
|
|
|
|
|
|
|
|
config = IdeficsConfig.from_pretrained(
|
|
|
|
model_id,
|
|
|
|
revision=revision,
|
|
|
|
trust_remote_code=trust_remote_code,
|
|
|
|
)
|
|
|
|
config.quantize = quantize
|
2024-05-14 04:33:18 -06:00
|
|
|
config.speculator = speculator
|
2023-08-17 06:38:49 -06:00
|
|
|
config.vision_config.quantize = quantize
|
|
|
|
|
|
|
|
tokenizer = LlamaTokenizerFast.from_pretrained(
|
|
|
|
model_id,
|
|
|
|
revision=revision,
|
|
|
|
padding_side="left",
|
|
|
|
truncation_side="left",
|
|
|
|
trust_remote_code=trust_remote_code,
|
|
|
|
)
|
|
|
|
self.processor = IdeficsProcessor.from_pretrained(
|
|
|
|
model_id,
|
|
|
|
revision=revision,
|
|
|
|
padding_side="left",
|
|
|
|
truncation_side="left",
|
|
|
|
trust_remote_code=trust_remote_code,
|
|
|
|
)
|
|
|
|
|
2024-07-09 12:04:03 -06:00
|
|
|
weights_loader = get_loader(
|
|
|
|
quantize=quantize, model_id=model_id, revision=revision
|
|
|
|
)
|
2023-08-17 06:38:49 -06:00
|
|
|
torch.distributed.barrier(group=self.process_group)
|
|
|
|
filenames = weight_files(model_id, revision=revision, extension=".safetensors")
|
|
|
|
weights = Weights(
|
|
|
|
filenames,
|
|
|
|
device=device,
|
|
|
|
dtype=dtype,
|
|
|
|
process_group=self.process_group,
|
2024-07-09 12:04:03 -06:00
|
|
|
weights_loader=weights_loader,
|
2023-08-17 06:38:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
model = IdeficsForVisionText2Text(config, weights)
|
|
|
|
|
|
|
|
torch.distributed.barrier(group=self.process_group)
|
|
|
|
super(IdeficsCausalLM, self).__init__(
|
2024-06-25 12:46:27 -06:00
|
|
|
model_id=model_id,
|
2023-08-17 06:38:49 -06:00
|
|
|
model=model,
|
|
|
|
tokenizer=tokenizer,
|
|
|
|
requires_padding=True,
|
|
|
|
dtype=dtype,
|
|
|
|
device=device,
|
|
|
|
rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
)
|