codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license processor class for idefics copied from m4 training packing this function converts 1 0 1 0 0 1 0 0 1 if any of images index are more than numclasses set them to 1 words after the max number of images allowed have been seen don t attend on anything copied from m4 training packing checks if the passed string contains a valid url and nothing else e g if space is included it s immediately invalidated the url if in string return false result urlparsestring return allresult scheme result netloc class ideficsprocessorprocessormixin r constructs a idefics processor which wraps a llama tokenizer and idefics image processor into a single processor ideficsprocessor offers all the functionalities of ideficsimageprocessor and llamatokenizerfast see the docstring of ideficsprocessor call and ideficsprocessor decode for more information args imageprocessor ideficsimageprocessor an instance of ideficsimageprocessor the image processor is a required input tokenizer llamatokenizerfast an instance of llamatokenizerfast the tokenizer is a required input imagesize int optional defaults to 224 image size assuming a square image this method takes batched or nonbatched prompts made of text and images and converts them into prompts that the model was trained on and prepares the image pixel values for the model to process args prompts unionlisttextinput listlisttextinput either a single prompt or a batched list of prompts see the detailed description immediately after the end of the arguments doc section 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 maxlength pad to a maximum length specified with the argument maxlength or to the maximum acceptable input length for the model if that argument is not provided false or donotpad default no padding i e can output a batch with sequences of different lengths maxlength 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 maxlength to maxlength transform callable optional a custom transform function that accepts a single image can be passed for training for example torchvision compose can be used to compose multiple functions if none a preset inferencespecific set of transforms will be applied to the images addeostoken bool optional defaults to false adds eostoken at the end of the final prompt if true addendofutterancetoken bool optional whether to automatically add endofutterance after each prompt s text input unless followed by an image if none the tokenizer will be checked instead and if this token is found in additionalspecialtokens then the value will be true debug bool optional defaults to false true value will help debug prompt generation by dumping useful information returntensors str or tensortype optional defaults to tensortype pytorch the type of tensors to return can be one of tensortype pytorch or pt return a batch of type torch tensor returns a dict with entries inputids attentionmask pixelvalues imageattentionmask which can be directly passed to model generate detailed explanation each entry in prompts is either a text to be passed as is or an image that will be processed an image can be either an image object pil image or a url from which the image can be retrieved when the processor encounters an image it ll inject faketokenaroundimageimagefaketokenaroundimage entry into the prompt example python checkpoint huggingfacem4idefics9b processor autoprocessor frompretrainedcheckpoint url https hips hearstapps comhmgprodimagescutephotosofcatsingrass1593184777 jpg img processor imageprocessor fetchimagesurl0 prompts user img describe this image nassistant an image of two kittens in grass n user https hips hearstapps comhmgprodimagesdogpuns1581708208 jpg describe this image nassistant inputs processorprompts returntensorspt generatedids model generateinputs maxlength100 generatedtext processor batchdecodegeneratedids skipspecialtokenstrue0 in this example the prompts will be converted into suser faketokenaroundimageimagefaketokenaroundimagedescribe this image assistant an image of two kittens in grass user faketokenaroundimageimagefaketokenaroundimagedescribe this image assistant and the two images will be massaged using ideficsimageprocessor call method and placed inside the pixelvalues dict entry of the return value this example also examplifies that images can be passed as objects or as text urls it can be seen that the first image is passed as object and the second one as a url to do training do python imagetransform transforms compose transforms randomresizedcrop w h scale0 9 1 0 interpolationtransforms interpolationmode bicubic transforms totensor transforms normalizemeanself imagemean stdself imagestd inputs processorprompts transformimagetransform returntensorspt in order to help debug prompt generation enable debugtrue which will show you what s happening if the value isn t overriden by the user check if the tokenizer was trained with this token and then use it turn nonbatched prompts into batched the model was trained on samples starting with s an image can either be an image object in the item or the url everything else is a verbatim prompt text we add endofutterancetoken between each subsequent text prompts but not at the last one must be an image obj maxnumimages has to be at least 1 even when there are no images in full language mode we set the image mask to all0s this method forwards all its arguments to llamatokenizerfast s pretrainedtokenizer batchdecode please refer to the docstring of this method for more information this method forwards all its arguments to llamatokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license processor class for idefics copied from m4 training packing this function converts 1 0 1 0 0 1 0 0 1 if any of images index are more than num_classes set them to 1 words after the max number of images allowed have been seen don t attend on anything copied from m4 training packing checks if the passed string contains a valid url and nothing else e g if space is included it s immediately invalidated the url constructs a idefics processor which wraps a llama tokenizer and idefics image processor into a single processor ideficsprocessor offers all the functionalities of ideficsimageprocessor and llamatokenizerfast see the docstring of ideficsprocessor __call__ and ideficsprocessor decode for more information args image_processor ideficsimageprocessor an instance of ideficsimageprocessor the image processor is a required input tokenizer llamatokenizerfast an instance of llamatokenizerfast the tokenizer is a required input image_size int optional defaults to 224 image size assuming a square image this method takes batched or non batched prompts made of text and images and converts them into prompts that the model was trained on and prepares the image pixel values for the model to process args prompts union list textinput list list textinput either a single prompt or a batched list of prompts see the detailed description immediately after the end of the arguments doc section 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 transform callable optional a custom transform function that accepts a single image can be passed for training for example torchvision compose can be used to compose multiple functions if none a preset inference specific set of transforms will be applied to the images add_eos_token bool optional defaults to false adds eos_token at the end of the final prompt if true add_end_of_utterance_token bool optional whether to automatically add end_of_utterance after each prompt s text input unless followed by an image if none the tokenizer will be checked instead and if this token is found in additional_special_tokens then the value will be true debug bool optional defaults to false true value will help debug prompt generation by dumping useful information return_tensors str or tensortype optional defaults to tensortype pytorch the type of tensors to return can be one of tensortype pytorch or pt return a batch of type torch tensor returns a dict with entries input_ids attention_mask pixel_values image_attention_mask which can be directly passed to model generate detailed explanation each entry in prompts is either a text to be passed as is or an image that will be processed an image can be either an image object pil image or a url from which the image can be retrieved when the processor encounters an image it ll inject fake_token_around_image image fake_token_around_image entry into the prompt example python checkpoint huggingfacem4 idefics 9b processor autoprocessor from_pretrained checkpoint url https hips hearstapps com hmg prod images cute photos of cats in grass 1593184777 jpg img processor image_processor fetch_images url 0 prompts user img describe this image nassistant an image of two kittens in grass n user https hips hearstapps com hmg prod images dog puns 1581708208 jpg describe this image nassistant inputs processor prompts return_tensors pt generated_ids model generate inputs max_length 100 generated_text processor batch_decode generated_ids skip_special_tokens true 0 in this example the prompts will be converted into s user fake_token_around_image image fake_token_around_image describe this image assistant an image of two kittens in grass user fake_token_around_image image fake_token_around_image describe this image assistant and the two images will be massaged using ideficsimageprocessor __call__ method and placed inside the pixel_values dict entry of the return value this example also examplifies that images can be passed as objects or as text urls it can be seen that the first image is passed as object and the second one as a url to do training do python image_transform transforms compose transforms randomresizedcrop w h scale 0 9 1 0 interpolation transforms interpolationmode bicubic transforms totensor transforms normalize mean self image_mean std self image_std inputs processor prompts transform image_transform return_tensors pt in order to help debug prompt generation enable debug true which will show you what s happening if the value isn t overriden by the user check if the tokenizer was trained with this token and then use it turn non batched prompts into batched the model was trained on samples starting with s an image can either be an image object in the item or the url everything else is a verbatim prompt text we add end_of_utterance_token between each subsequent text prompts but not at the last one must be an image obj max_num_images has to be at least 1 even when there are no images in full language mode we set the image mask to all 0s this method forwards all its arguments to llamatokenizerfast s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to llamatokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information
from typing import Callable, List, Optional, Union
from urllib.parse import urlparse

from ...feature_extraction_utils import BatchFeature
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TextInput, TruncationStrategy
from ...utils import TensorType, is_torch_available


if is_torch_available():
    import torch


IMAGE_TOKEN = "<image>"



def incremental_to_binary_attention_mask(incremental_mask, num_classes=-1):
    

    
    
    if num_classes != -1:
        incremental_mask[incremental_mask >= num_classes] = -1

    negatives = incremental_mask == -1
    incremental_mask[negatives] = 0
    attn_mask = torch.nn.functional.one_hot(incremental_mask, num_classes=num_classes)
    attn_mask[negatives, :] = 0
    return attn_mask



def image_attention_mask_for_packed_input_ids(input_ids, tokenizer):
    image_attention_mask = torch.full_like(input_ids, fill_value=-1)
    next_image_attention_mask = torch.full_like(input_ids, fill_value=-1)
    image_token_id = tokenizer.convert_tokens_to_ids(IMAGE_TOKEN)
    eod_token_id = tokenizer.eos_token_id
    for batch_idx in range(input_ids.size(0)):
        count = -1
        seen_eod = False
        for idx, token_id in enumerate(input_ids[batch_idx]):
            if token_id == image_token_id:
                count += 1
                image_attention_mask[batch_idx][idx] = count
                seen_eod = False
            else:
                image_attention_mask[batch_idx][idx] = count

            if seen_eod:
                image_attention_mask[batch_idx][idx] = -1

            if token_id == eod_token_id:
                seen_eod = True

    for batch_idx in range(input_ids.size(0)):
        count = -1
        seen_eod = False
        for idx in range(input_ids[batch_idx].size(0) - 1, -1, -1):
            token_id = input_ids[batch_idx][idx]
            if token_id == image_token_id:
                count += 1
                next_image_attention_mask[batch_idx][idx] = count
                seen_eod = False
            else:
                next_image_attention_mask[batch_idx][idx] = count

            if token_id == eod_token_id:
                seen_eod = True

            if seen_eod:
                next_image_attention_mask[batch_idx][idx] = -1

        non_negative_indices = next_image_attention_mask[batch_idx] != -1
        next_image_attention_mask[batch_idx][non_negative_indices] -= count
        next_image_attention_mask[batch_idx][non_negative_indices] *= -1

    return image_attention_mask, next_image_attention_mask


def is_url(string):
    
    if " " in string:
        return False
    result = urlparse(string)
    return all([result.scheme, result.netloc])


class IdeficsProcessor(ProcessorMixin):
    r

    attributes = ["image_processor", "tokenizer"]
    image_processor_class = "IdeficsImageProcessor"
    tokenizer_class = "LlamaTokenizerFast"

    def __init__(self, image_processor, tokenizer=None, image_size=224, add_end_of_utterance_token=None, **kwargs):
        if image_processor is None:
            raise ValueError("You need to specify an `image_processor`.")
        if tokenizer is None:
            raise ValueError("You need to specify a `tokenizer`.")

        super().__init__(image_processor, tokenizer)
        self.current_processor = self.image_processor
        self.image_token_id = tokenizer.convert_tokens_to_ids(IMAGE_TOKEN)

        self.default_image_dims = (
            self.image_processor.image_num_channels,
            self.image_processor.image_size,
            self.image_processor.image_size,
        )

        self.tokenizer_was_trained_with_end_of_utterance_token = (
            True
            if "<end_of_utterance>" in self.tokenizer.special_tokens_map.get("additional_special_tokens", [])
            else False
        )

    def __call__(
        self,
        prompts: Union[List[TextInput], List[List[TextInput]]],
        padding: Union[bool, str, PaddingStrategy] = False,
        truncation: Union[bool, str, TruncationStrategy] = None,
        max_length: Optional[int] = None,
        transform: Callable = None,
        add_eos_token=False,
        add_end_of_utterance_token=None,
        debug=False,
        return_tensors: Optional[Union[str, TensorType]] = TensorType.PYTORCH,
    ) -> BatchEncoding:
        

        
        if add_end_of_utterance_token is None:
            add_end_of_utterance_token = self.tokenizer_was_trained_with_end_of_utterance_token

        
        if not any(isinstance(i, list) for i in prompts):
            prompts = [prompts]

        fake_token = "<fake_token_around_image>"
        image_token = "<image>"
        end_of_utterance_token = "<end_of_utterance>"

        def image_tokens(last_was_image):
            if last_was_image:
                return image_token + fake_token
            else:
                return fake_token + image_token + fake_token

        all_prompts = []
        all_images = []
        for sample in prompts:
            
            full_text = f"{self.tokenizer.bos_token}"

            
            image_objects = []
            last_was_image = False
            last_was_text = False
            for i, item in enumerate(sample):
                if i > 0:
                    last_was_text = True if not last_was_image else False

                if isinstance(item, str):
                    item = item.strip(" ")
                    if is_url(item):
                        image = self.image_processor.fetch_images(item)
                        full_text += image_tokens(last_was_image)
                        image_objects.append(image)
                        last_was_image = True
                    else:
                        
                        if add_end_of_utterance_token and last_was_text:
                            full_text += end_of_utterance_token
                        full_text += item
                        last_was_image = False
                else:
                    
                    full_text += image_tokens(last_was_image)
                    image_objects.append(item)
                    last_was_image = True

            if add_eos_token:
                full_text += self.tokenizer.eos_token

            if debug is True:
                print(f"{full_text=}")

            image_objects = self.image_processor(image_objects, transform=transform)

            all_prompts.append(full_text)
            all_images.append(image_objects)

        text_encoding = self.tokenizer(
            text=all_prompts,
            add_special_tokens=False,
            padding=padding,
            truncation=truncation,
            max_length=max_length,
        )
        all_texts = text_encoding["input_ids"]

        max_seq_len = max(len(x) for x in all_texts)

        
        max_num_images = max(len(x) for x in all_images)
        max_num_images = max(1, max_num_images)

        at_least_one_image = sum(len(x) for x in all_images) > 0
        output_input_ids = []
        output_images = []
        output_attention_masks = []
        for text, images in zip(all_texts, all_images):
            padded_input_ids = [self.tokenizer.pad_token_id] * max_seq_len
            unpadded_seq_len = len(text)
            start = max_seq_len - unpadded_seq_len
            padded_input_ids[start:] = text[:max_seq_len]

            attention_mask = torch.zeros((max_seq_len,), dtype=torch.long)
            attention_mask[start:] = 1

            image_count = padded_input_ids.count(self.image_token_id)
            local_max_num_images = min(image_count, max_num_images)

            current_images = images[:local_max_num_images]

            if len(current_images) > 0:
                padded_image_tensor = torch.zeros(max_num_images, *current_images.size()[1:])
                padded_image_tensor[: current_images.size(0)] = current_images
            else:
                padded_image_tensor = torch.zeros(max_num_images, *self.default_image_dims)

            output_images.append(padded_image_tensor)
            output_input_ids.append(torch.tensor(padded_input_ids))

            output_attention_masks.append(attention_mask)

        output_input_ids = torch.stack(output_input_ids)
        output_images = torch.stack(output_images)
        output_attention_masks = torch.stack(output_attention_masks)

        if at_least_one_image:
            image_attention_mask, _ = image_attention_mask_for_packed_input_ids(output_input_ids, self.tokenizer)
            image_attention_mask = incremental_to_binary_attention_mask(
                image_attention_mask, num_classes=max_num_images
            )
        else:
            
            image_attention_mask = torch.zeros(
                output_input_ids.shape[0], output_input_ids.shape[1], 1, dtype=torch.bool
            )

        return BatchFeature(
            data={
                "input_ids": output_input_ids,
                "attention_mask": output_attention_masks,
                "pixel_values": output_images,
                "image_attention_mask": image_attention_mask,
            }
        )

    def batch_decode(self, *args, **kwargs):
        
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        
        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))