Fuyu Finetuning Example - #26997
Conversation
|
The issue I am getting right now is that I am unable to pad the image patches correct it seems for batch training. The image processor doesn't seem to be doing it correct. |
|
I thought it would be as simple as modifying variable_size var to false: https://github.com/huggingface/transformers/blob/main/src/transformers/models/fuyu/processing_fuyu.py#L508C1-L508C37 And that seems to patch the image patches correct, but the |
Yeah, the current image processor seems only outputs batch-size=1 result |
|
Heyya @ArthurZucker ! In that case, should I close this PR or should this PR be the first of multi-modal model training examples? Seems like the space is getting super interesting and so it might be nice for people to see example scripts in the official repo for huggingface |
|
Hi Is this working now? |
|
This is working for me, but only with batch size 1. Using 4-bit Lora training this does fit on a 24GB GPU. Using higher than batch 1 gives the error I reported in: #27255 |
|
Hi @ncoop57! It was mentioned before not sure if this PR should be merged here but it's a great resource. We don't have official training scripts, but we updated the def collate_fn(examples):
texts = [e[text_column_name] for e in examples]
images = [e[image_column_name] for e in examples]
output = processor(
text=texts,
images=images,
padding="max_length",
truncation=True
)
first_non_padded = (output["input_ids"] != tokenizer.pad_token_id).nonzero(as_tuple=False)
first_non_padded_indices = first_non_padded[:, 1].view(-1, 1)
labels = torch.full_like(output["input_ids"], -100)
for i, index in enumerate(first_non_padded_indices):
labels[i, index:] = output["input_ids"][i, index:]
output["labels"] = labels
return outputlet me know what you think! |
|
@molbap @ArthurZucker I would be pro adding a finetuning script for Fuyu to our examples because we yet have an example for this modality / task. |
|
Super happy to see more excitement around this example script! Thanks for the loss pointer, I'm looking into it. I had the same error as @Carolinabanana mentioned but only when using the Trainer object, not when doing it myself. I did try to account for padding/image patches because I don't think we want to train on the prediction of the image patch indices, but I did forget to take into account the left padding! |
|
The script doesn't support VQA, does it? |
|
Can it fit in a 80GB card with bfloat16 for tuning? I faced a CUDA memory issue when running |
|
Can anyone provide a train script? Thanks! |
|
Hi @ncoop57 Any update on the script and readiness for PR review? Already looks very good! Let us know if you need any help adding it to the library |
|
AH sadly haven't had a chance to look back into this. Luckily Ill be off all next week so gonna add this to my list to complete and will reach out with help that I need, ty! |
|
Thanks @ncoop57 ! I would love an example training script to try finetuning Fuyu-8B for a domain specific image captioning dataset |
|
@amyeroberts think I got it fully in and ready for review. @molbap if you have some time, I'd love for you to checkout how I am handling the left padding: def collate_fn(examples):
texts = [e[text_column_name] for e in examples]
images = [e[image_column_name] for e in examples]
output = processor(
text=texts, images=images, padding="max_length", truncation=True
)
position = (output["input_ids"] == tokenizer.vocab["<s>"]).nonzero(
as_tuple=True
)[0][
0
] # This gets the index of the first '1' in the tensor by passing the left padding
output["labels"] = torch.full_like(
output["input_ids"], -100
) # This creates a tensor filled with -100
output["labels"][position:] = output["input_ids"][position:]
return outputLet me know what else is needed to get this merged in 🤓 |
|
I wasn't able to get the Trainer working so right now this is just with a standard training loop and uses accelerate to handle all the FSDP stuff |
|
@ncoop57 Great! For the quality checks, make sure toe run |
|
@amyeroberts looks like I got all the CI stuff working 🤓 lemme know if any other stuff are needed |
|
maybe one tweak would be renaming the folder to "visual-language-modeling"? |
|
@amyeroberts any updates on this? |
There was a problem hiding this comment.
Thanks for all the work adding this!
Overall looking good. Made some general comments on the structure. There's some places which have been copied across from other scripts but need adapting to this one.
Before we can publish this script, we'll need a demonstration of a model which has been trained for a few epochs and pushed to the hub successfully. If/when you have thant, could you share in the PR description?
| @@ -0,0 +1,51 @@ | |||
| <!--- | |||
| Copyright 2020 The HuggingFace Team and Nathan Cooper. All rights reserved. | |||
There was a problem hiding this comment.
| Copyright 2020 The HuggingFace Team and Nathan Cooper. All rights reserved. | |
| Copyright 2024 The HuggingFace Team and Nathan Cooper. All rights reserved. |
| @@ -0,0 +1,712 @@ | |||
| #!/usr/bin/env python | |||
| # coding=utf-8 | |||
| # Copyright 2020 The HuggingFace Inc. team. All rights reserved. | |||
There was a problem hiding this comment.
| # Copyright 2020 The HuggingFace Inc. team. All rights reserved. | |
| # Copyright 2024 The HuggingFace Inc. team. All rights reserved. |
There was a problem hiding this comment.
Lets undo the style changes here - we prefer to keep things on one line as much as possible.
For some of the lines, adding an extra comma on the last element will cause the split i.e. (a, b, c,) will be split across three lines whereas (a, b, c) won't
| @@ -0,0 +1,778 @@ | |||
| #!/usr/bin/env python | |||
| # coding=utf-8 | |||
| # Copyright 2021 The HuggingFace Inc. team and Nathan Cooper. All rights reserved. | |||
There was a problem hiding this comment.
| # Copyright 2021 The HuggingFace Inc. team and Nathan Cooper. All rights reserved. | |
| # Copyright 2024 The HuggingFace Inc. team and Nathan Cooper. All rights reserved. |
| Fine-tuning the library models for causal language modeling (GPT, GPT-2, CTRL, ...) | ||
| on a text file or a dataset without using HuggingFace Trainer. | ||
|
|
||
| Here is the full list of checkpoints on the hub that can be fine-tuned by this script: | ||
| https://huggingface.co/models?filter=text-generation |
| valid_images = [] | ||
| for i in range(0, len(examples[image_column_name])): | ||
| try: | ||
| processor(text="test", images=[examples[image_column_name][i]]) |
There was a problem hiding this comment.
Why pass them through the processor here? Trying to open them with Image.open should be enough to check. A failure here doesn't mean the image itself is problematic.
| for i in range(0, len(examples[image_column_name])): | ||
| try: | ||
| processor(text="test", images=[examples[image_column_name][i]]) |
There was a problem hiding this comment.
- range automatically starts on the 0th index
- You can iterate directly on the dataset
| for i in range(0, len(examples[image_column_name])): | |
| try: | |
| processor(text="test", images=[examples[image_column_name][i]]) | |
| for image in examples[image_column_name]: | |
| try: | |
| Image.open(image) |
| batched=True, | ||
| ) | ||
| # resize images | ||
| resize = Resize((224, 224)) |
There was a problem hiding this comment.
The size here should be taken from the image processor config:
size_dict = processor.image_processor.size
size = (size_dict["height"], size_dict["width"])
resize = Resize(size)There was a problem hiding this comment.
Actually figured out the processor already handles resizing, so need to do something like:
processor = FuyuProcessor.from_pretrained(args.model_name_or_path, size = {"height": 480, "width": 480})
There was a problem hiding this comment.
Yes, but it doesn't do augmentation. I'd recommend not using the processor to process the images.
| batched=True, | ||
| ) | ||
| # resize images | ||
| resize = Resize((224, 224)) |
There was a problem hiding this comment.
Why is this the only transformation applied? I'd expect some augmentation in a training script. You can look at the image classification script as an example.
| ) | ||
|
|
||
| if args.block_size is None: | ||
| block_size = tokenizer.model_max_length |
There was a problem hiding this comment.
This should use the processor's tokenizer
|
Awesome, thanks for the review and recommendations! 🤓 I've been working on finetuning using the https://huggingface.co/datasets/HuggingFaceM4/WebSight dataset, so once I have the model trained I'll make sure to add it here and to the README! |
|
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
What does this PR do?
This PR is about contributing an example for finetuning the recent Adept Fuyu model architecture. It adds the ability to calculate the loss for the model as well as add a new folder for image-text examples. It is currently in DRAFT model as it still has a bug due to the data collator padding and an easy way of performing evaluation. I'm opening the PR in this state so that I can get help.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@molbap would love some help since you initially created the model!
Models:
Library: