Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds VLM Training support to SFTTrainer + VSFT script #1518

Merged
merged 23 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
precommit
  • Loading branch information
edbeeching committed Apr 10, 2024
commit 2bec041fe796cd0ce60b2611e079ba67767ca64d
14 changes: 7 additions & 7 deletions examples/scripts/vsft.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@
################
# Model, Tokenizer & Processor
################

LLAVA_CHAT_TEMPLATE = """{% for message in messages %}{% if message['role'] == 'user' %}USER:{% else %}ASSISTANT:{% endif %}{% for item in message['content'] %}{% if item['type'] == 'text' %}{{ item['text'] }}{% elif item['type'] == 'image' %}<image>{% endif %}{% endfor %}{{'\n'}}{% endfor %}"""
edbeeching marked this conversation as resolved.
Show resolved Hide resolved

torch_dtype = (
model_config.torch_dtype
if model_config.torch_dtype in ["auto", None]
Expand All @@ -122,7 +122,7 @@
# tokenizer.pad_token = tokenizer.eos_token
edbeeching marked this conversation as resolved.
Show resolved Hide resolved
tokenizer.chat_template = LLAVA_CHAT_TEMPLATE
processor = AutoProcessor.from_pretrained(model_config.model_name_or_path)
processor.tokenizer = tokenizer
processor.tokenizer = tokenizer

model = LlavaForConditionalGeneration.from_pretrained(model_config.model_name_or_path, **model_kwargs)

Expand All @@ -133,7 +133,7 @@
class LLavaDataCollator:
edbeeching marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, processor):
self.processor = processor

def __call__(self, examples):
texts = []
images = []
Expand All @@ -155,7 +155,7 @@ def __call__(self, examples):
batch["labels"] = labels

return batch

data_collator = LLavaDataCollator(processor)

################
Expand Down Expand Up @@ -184,12 +184,12 @@ def __call__(self, examples):
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
dataset_text_field="text", # need a dummy field
dataset_text_field="text", # need a dummy field
tokenizer=tokenizer,
peft_config=get_peft_config(model_config),
callbacks=[RichProgressCallback] if TRL_USE_RICH else None,
data_collator=data_collator,
dataset_kwargs={"skip_prepare_dataset": True}
dataset_kwargs={"skip_prepare_dataset": True},
)

trainer.train()
Expand Down
4 changes: 3 additions & 1 deletion trl/trainer/sft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ def _prepare_dataset(
raise ValueError("The dataset should not be None")

# check if torch dataset / dataloader and do nothing
if skip_prepare_dataset or isinstance(dataset, (torch.utils.data.IterableDataset, torch.utils.data.Dataset, ConstantLengthDataset)):
if skip_prepare_dataset or isinstance(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an integration test for this case under the existing SFTTrainer tests, along with an example training a tiny random llava model

dataset, (torch.utils.data.IterableDataset, torch.utils.data.Dataset, ConstantLengthDataset)
):
return dataset

if not packing:
Expand Down
Loading