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

[Feature] Support multi-image training and chatting for LLaVA #459

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 4 # per_device
Expand Down Expand Up @@ -96,6 +97,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 8 # per_device
Expand Down Expand Up @@ -115,6 +116,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
data_path = data_root + 'LLaVA-Pretrain/blip_laion_cc_sbu_558k.json'
image_folder = data_root + 'LLaVA-Pretrain/images'
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 32 # per_device
Expand Down Expand Up @@ -102,6 +103,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=False)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 8 # per_device
Expand Down Expand Up @@ -95,6 +96,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -115,6 +116,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
data_path = data_root + 'LLaVA-Pretrain/blip_laion_cc_sbu_558k.json'
image_folder = data_root + 'LLaVA-Pretrain/images'
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 32 # per_device
Expand Down Expand Up @@ -102,6 +103,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=False)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.internlm_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -95,6 +96,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.internlm_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -115,6 +116,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
data_path = data_root + 'LLaVA-Pretrain/blip_laion_cc_sbu_558k.json'
image_folder = data_root + 'LLaVA-Pretrain/images'
prompt_template = PROMPT_TEMPLATE.internlm_chat
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 32 # per_device
Expand Down Expand Up @@ -102,6 +103,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=False)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.vicuna
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -95,6 +96,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.vicuna
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -115,6 +116,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
data_path = data_root + 'LLaVA-Pretrain/blip_laion_cc_sbu_558k.json'
image_folder = data_root + 'LLaVA-Pretrain/images'
prompt_template = PROMPT_TEMPLATE.vicuna
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 32 # per_device
Expand Down Expand Up @@ -102,6 +103,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=False)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.vicuna
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -95,6 +96,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
data_path = data_root + 'LLaVA-Instruct-150K/llava_v1_5_mix665k.json'
image_folder = data_root + 'llava_images'
prompt_template = PROMPT_TEMPLATE.vicuna
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 16 # per_device
Expand Down Expand Up @@ -115,6 +116,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=True)

train_dataloader = dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
data_path = data_root + 'LLaVA-Pretrain/blip_laion_cc_sbu_558k.json'
image_folder = data_root + 'LLaVA-Pretrain/images'
prompt_template = PROMPT_TEMPLATE.vicuna
max_length = int(2048 - (336 / 14)**2)
max_length = 2048
per_image_length = int(336 / 14)**2 # inferred from the visual encoder

# Scheduler & Optimizer
batch_size = 32 # per_device
Expand Down Expand Up @@ -102,6 +103,7 @@
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
per_image_length=per_image_length,
pad_image_to_square=False)

train_dataloader = dict(
Expand Down
19 changes: 5 additions & 14 deletions xtuner/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@
warnings.simplefilter(action='ignore', category=FutureWarning)

__all__ = [
'process_hf_dataset',
'ConcatDataset',
'MOSSSFTDataset',
'process_ms_dataset',
'LLaVADataset',
'expand2square',
'decode_base64_to_image',
'load_image',
'process_ms_dataset',
'process_hf_dataset', 'ConcatDataset', 'MOSSSFTDataset',
'process_ms_dataset', 'LLaVADataset', 'expand2square',
'decode_base64_to_image', 'load_image', 'process_ms_dataset',
'load_intern_repo_tokenized_dataset',
'load_intern_repo_untokenized_dataset',
'build_packed_dataset',
'RefCOCOJsonDataset',
'RefCOCOJsonEvalDataset',
'InvRefCOCOJsonDataset',
'load_intern_repo_untokenized_dataset', 'build_packed_dataset',
'RefCOCOJsonDataset', 'RefCOCOJsonEvalDataset', 'InvRefCOCOJsonDataset'
]
5 changes: 4 additions & 1 deletion xtuner/dataset/collate_fns/defalut_collate_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def default_collate_fn(instances: Sequence[Dict],
indexes.append(torch.LongTensor(example['indexes']))

if has_image:
pixel_values.append(example['pixel_values'])
if isinstance(example['pixel_values'], list):
pixel_values.extend(example['pixel_values'])
else:
pixel_values.append(example['pixel_values'])

if len(instances) > 1:
input_ids = pad_sequence(
Expand Down