|
from typing import List, Optional, Union |
|
|
|
from transformers import PreTrainedTokenizer |
|
from typing import List, Tuple |
|
|
|
from transformers.feature_extraction_utils import BatchFeature |
|
from transformers.image_utils import ImageInput |
|
from transformers.processing_utils import ProcessorMixin |
|
from transformers.tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy |
|
from transformers.utils import TensorType |
|
import torch |
|
|
|
def tokenizer_image_token(prompt, tokenizer, image_token_index=-200, return_tensors=None): |
|
if "<image_0>" in prompt: |
|
image_token_pattern = re.compile(r"<image_(\d+)>") |
|
prompt_chunks = re.split(r'<image_[0-9]+>',prompt) |
|
|
|
image_tags = image_token_pattern.findall(prompt) |
|
|
|
input_ids = [] |
|
for i, chunk in enumerate(prompt_chunks): |
|
input_ids.extend(tokenizer(chunk).input_ids) |
|
if i < len(image_tags): |
|
|
|
input_ids.append(-200) |
|
else: |
|
prompt_chunks = [tokenizer(chunk).input_ids for chunk in prompt.split('<image>')] |
|
def insert_separator(X, sep): |
|
return [ele for sublist in zip(X, [sep]*len(X)) for ele in sublist][:-1] |
|
|
|
input_ids = [] |
|
offset = 0 |
|
if len(prompt_chunks) > 0 and len(prompt_chunks[0]) > 0 and prompt_chunks[0][0] == tokenizer.bos_token_id: |
|
offset = 1 |
|
input_ids.append(prompt_chunks[0][0]) |
|
|
|
for x in insert_separator(prompt_chunks, [image_token_index] * (offset + 1)): |
|
input_ids.extend(x[offset:]) |
|
|
|
if return_tensors is not None: |
|
if return_tensors == 'pt': |
|
return torch.tensor(input_ids, dtype=torch.long) |
|
else: |
|
raise ValueError(f'Unsupported tensor type: {return_tensors}') |
|
|
|
return input_ids |
|
|
|
|
|
def make_context( |
|
tokenizer: PreTrainedTokenizer, |
|
query: str, |
|
history: List[Tuple[str, str]] = None, |
|
system: str = "", |
|
max_window_size: int = 6144, |
|
chat_format: str = "chatml", |
|
): |
|
if history is None: |
|
history = [] |
|
|
|
if chat_format == "chatml": |
|
im_start, im_end = "<|im_start|>", "<|im_end|>" |
|
im_start_tokens = [151644] |
|
im_end_tokens = [151645] |
|
nl_tokens = tokenizer.encode("\n") |
|
|
|
def _tokenize_str(role, content): |
|
if "<image>" in content: |
|
return f"{role}\n{content}", tokenizer.encode( |
|
role |
|
) + nl_tokens + tokenizer_image_token( |
|
content, tokenizer, -200 |
|
) |
|
else: |
|
return f"{role}\n{content}", tokenizer.encode( |
|
role |
|
) + nl_tokens + tokenizer.encode(content) |
|
|
|
def _tokenize_str2(role, content): |
|
return f"{role}\n{content}", tokenizer.encode( |
|
role, |
|
) + nl_tokens + tokenizer.encode(content) |
|
|
|
system_text, system_tokens_part = _tokenize_str("system", system) |
|
system_tokens = im_start_tokens + system_tokens_part + im_end_tokens |
|
|
|
raw_text = "" |
|
context_tokens = [] |
|
|
|
for turn_query, turn_response in reversed(history): |
|
query_text, query_tokens_part = _tokenize_str("user", turn_query) |
|
query_tokens = im_start_tokens + query_tokens_part + im_end_tokens |
|
response_text, response_tokens_part = _tokenize_str( |
|
"assistant", turn_response |
|
) |
|
response_tokens = im_start_tokens + response_tokens_part + im_end_tokens |
|
|
|
next_context_tokens = nl_tokens + query_tokens + nl_tokens + response_tokens |
|
prev_chat = ( |
|
f"\n{im_start}{query_text}{im_end}\n{im_start}{response_text}{im_end}" |
|
) |
|
|
|
current_context_size = ( |
|
len(system_tokens) + len(next_context_tokens) + len(context_tokens) |
|
) |
|
if current_context_size < max_window_size: |
|
context_tokens = next_context_tokens + context_tokens |
|
raw_text = prev_chat + raw_text |
|
else: |
|
break |
|
|
|
context_tokens = system_tokens + context_tokens |
|
raw_text = f"{im_start}{system_text}{im_end}" + raw_text |
|
context_tokens += ( |
|
nl_tokens |
|
+ im_start_tokens |
|
+ _tokenize_str("user", query)[1] |
|
+ im_end_tokens |
|
+ nl_tokens |
|
+ im_start_tokens |
|
+ tokenizer.encode("assistant") |
|
+ nl_tokens |
|
) |
|
raw_text += f"\n{im_start}user\n{query}{im_end}\n{im_start}assistant\n" |
|
|
|
elif chat_format == "raw": |
|
raw_text = query |
|
context_tokens = tokenizer.encode(raw_text) |
|
else: |
|
raise NotImplementedError(f"Unknown chat format {chat_format!r}") |
|
|
|
return raw_text, context_tokens |
|
|
|
def split_tensor(A, B): |
|
split_tensors = [] |
|
start_idx = 0 |
|
|
|
for i, size in enumerate(B.tolist()): |
|
split_tensor = A[i, :size, :, :, :] |
|
split_tensors.append(split_tensor) |
|
|
|
return split_tensors |
|
|
|
class OmChatProcessor(ProcessorMixin): |
|
r""" |
|
Constructs a OmChat processor which wraps a OmChat image processor and a LLaMa tokenizer into a single processor. |
|
|
|
[`OmChatProcessor`] offers all the functionalities of [`OmChatImageProcessor`] and [`LlamaTokenizerFast`]. See the |
|
[`~OmChatProcessor.__call__`] and [`~OmChatProcessor.decode`] for more information. |
|
|
|
Args: |
|
image_processor ([`OmChatImageProcessor`], *optional*): |
|
The image processor is a required input. |
|
tokenizer ([`LlamaTokenizerFast`], *optional*): |
|
The tokenizer is a required input. |
|
chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages |
|
in a chat into a tokenizable string. |
|
""" |
|
|
|
attributes = ["image_processor", "tokenizer"] |
|
valid_kwargs = ["chat_template"] |
|
image_processor_class = "AutoImageProcessor" |
|
tokenizer_class = "AutoTokenizer" |
|
|
|
def __init__(self, image_processor=None, tokenizer=None, **kwargs): |
|
super().__init__(image_processor, tokenizer) |
|
|
|
def __call__( |
|
self, |
|
text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], |
|
system_prompt: str = "You are a helpful assistant.", |
|
images: ImageInput = None, |
|
padding: Union[bool, str, PaddingStrategy] = False, |
|
truncation: Union[bool, str, TruncationStrategy] = None, |
|
max_length: Optional[int] = None, |
|
return_tensors: Optional[Union[str, TensorType]] = TensorType.PYTORCH, |
|
) -> BatchFeature: |
|
""" |
|
Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text` |
|
and `kwargs` arguments to LlamaTokenizerFast's [`~LlamaTokenizerFast.__call__`] if `text` is not `None` to encode |
|
the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to |
|
OmChatImageProcessor's [`~OmChatImageProcessor.__call__`] if `images` is not `None`. Please refer to the doctsring |
|
of the above two methods for more information. |
|
|
|
Args: |
|
text (`str`, `List[str]`, `List[List[str]]`): |
|
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings |
|
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set |
|
`is_split_into_words=True` (to lift the ambiguity with a batch of sequences). |
|
system_prompt ('str'): |
|
the initial system prompt (i.e., You are a helpful assistant.) |
|
images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): |
|
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch |
|
tensor. Both channels-first and channels-last formats are supported. |
|
padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): |
|
Select a strategy to pad the returned sequences (according to the model's padding side and padding |
|
index) among: |
|
- `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single |
|
sequence if provided). |
|
- `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum |
|
acceptable input length for the model if that argument is not provided. |
|
- `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different |
|
lengths). |
|
max_length (`int`, *optional*): |
|
Maximum length of the returned list and optionally padding length (see above). |
|
truncation (`bool`, *optional*): |
|
Activates truncation to cut input sequences longer than `max_length` to `max_length`. |
|
return_tensors (`str` or [`~utils.TensorType`], *optional*): |
|
If set, will return tensors of a particular framework. Acceptable values are: |
|
|
|
- `'tf'`: Return TensorFlow `tf.constant` objects. |
|
- `'pt'`: Return PyTorch `torch.Tensor` objects. |
|
- `'np'`: Return NumPy `np.ndarray` objects. |
|
- `'jax'`: Return JAX `jnp.ndarray` objects. |
|
|
|
Returns: |
|
[`BatchFeature`]: A [`BatchFeature`] with the following fields: |
|
|
|
- **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`. |
|
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when |
|
`return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not |
|
`None`). |
|
- **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`. |
|
""" |
|
|
|
if images is not None: |
|
image_inputs = self.image_processor(images, return_tensors=return_tensors) |
|
new_images = [] |
|
new_texts = [] |
|
img = image_inputs["pixel_values"] |
|
num_patches = image_inputs["num_patches"] |
|
img = split_tensor(img, num_patches) |
|
if len(img) == 1: |
|
n = num_patches.tolist()[0] |
|
inp, context_tokens = make_context( |
|
self.tokenizer, |
|
"<image>\n"+"\n".join(["patch:<image>"]*(n-1)) +"\n"+ text.replace("<image>", ""), |
|
None, |
|
system_prompt, |
|
) |
|
|
|
else: |
|
texts = text.split("<image>") |
|
final =texts[0] |
|
for i, n in enumerate(num_patches.tolist()): |
|
final+= "\n<image>\n"+"\n".join(["patch:<image>"]*(n-1))+"\n" |
|
if i+1 < len(texts): |
|
final += texts[i+1] |
|
inp, context_tokens = make_context(self.tokenizer, final, None, system_prompt) |
|
text_inputs = {"input_ids": torch.tensor([context_tokens])} |
|
image_inputs = {"images":torch.cat(img, dim=0)} |
|
return BatchFeature(data={**text_inputs, **image_inputs}) |
|
else: |
|
image_inputs = {"images":None} |
|
inp, context_tokens = make_context( |
|
self.tokenizer, |
|
text.replace("<image>", "").strip(), |
|
None, |
|
"You are a helpful assistant.", |
|
) |
|
text_inputs = {"input_ids": torch.tensor([context_tokens])} |
|
|
|
return BatchFeature(data={**text_inputs}) |
|
|
|
|
|
def batch_decode(self, *args, **kwargs): |
|
""" |
|
This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please |
|
refer to the docstring of this method for more information. |
|
""" |
|
return self.tokenizer.batch_decode(*args, **kwargs) |
|
|
|
|
|
def decode(self, *args, **kwargs): |
|
""" |
|
This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to |
|
the docstring of this method for more information. |
|
""" |
|
return self.tokenizer.decode(*args, **kwargs) |
|
|
|
@property |
|
|
|
def model_input_names(self): |
|
tokenizer_input_names = self.tokenizer.model_input_names |
|
image_processor_input_names = self.image_processor.model_input_names |
|
return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names)) |
|
|