-
Notifications
You must be signed in to change notification settings - Fork 223
DFlash VLM training support with SGLang backend #505
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
Open
Mandy3311
wants to merge
4
commits into
sgl-project:main
Choose a base branch
from
Mandy3311:feat/dflash-vlm-sglang
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b9f2a1a
feat: DFlash VLM training support with SGLang backend
Mandy3311 d1ce873
fix: resolve CI failures (ported from sgl-project/SpecForge#509)
c8cf479
address reviewer comments.
Mandy3311 7a85748
removed unused variables and commented-out code.
Mandy3311 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,8 +202,6 @@ def preprocess_vlm_conversations( | |
| - pixel_values: List of pixel values for images in the examples. | ||
| - image_grid_thw: List of image grid tensors. | ||
| """ | ||
| system_prompt = chat_template.system_prompt | ||
|
|
||
| # prepare result | ||
| results = { | ||
| "input_ids": [], | ||
|
|
@@ -213,36 +211,71 @@ def preprocess_vlm_conversations( | |
| "image_grid_thw": [], | ||
| } | ||
|
|
||
| # Note: currently, we assume that each example has only one image | ||
| for i, image in enumerate(examples["image"]): | ||
| for i, images in enumerate(examples["images"]): | ||
| source = examples["conversations"][i] | ||
| messages = [{"role": "system", "content": system_prompt}] | ||
| messages = [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few unused variables — please do a cleanup pass (e.g. system_prompt, mrope_interleaved). |
||
| if not source: | ||
| # if the source is None, skip it | ||
| continue | ||
|
|
||
| if not images: | ||
| text_messages = [] | ||
| convroles = ["user", "assistant"] | ||
| for j, sentence in enumerate(source): | ||
| role = sentence["role"] | ||
| assert role == convroles[j % 2], f"unexpected role {role}" | ||
| text_messages.append({"role": role, "content": sentence["content"]}) | ||
| conversation = processor.apply_chat_template( | ||
| text_messages, | ||
| tokenize=False, | ||
| add_generation_prompt=False, | ||
| ) | ||
| encoding = processor( | ||
| text=[conversation], | ||
| max_length=max_length, | ||
| truncation=True, | ||
| return_tensors="pt", | ||
| return_offsets_mapping=True, | ||
| add_special_tokens=False, | ||
| ) | ||
|
|
||
| input_ids = encoding.input_ids[0] | ||
| offsets = encoding.offset_mapping[0] | ||
|
|
||
| # get conversation with image info for loss mask generation | ||
| decoded_conversation = processor.tokenizer.decode( | ||
| encoding.input_ids[0], skip_special_tokens=False | ||
| ) | ||
|
|
||
| # Apply loss mask | ||
| loss_mask = _apply_loss_mask_from_chat_template( | ||
| decoded_conversation, offsets, chat_template | ||
| ) | ||
| results["input_ids"].append(input_ids[None, :]) | ||
| results["loss_mask"].append(loss_mask[None, :]) | ||
| results["attention_mask"].append(torch.ones_like(loss_mask)[None, :]) | ||
| results["pixel_values"].append(torch.empty(0, 0).float()) | ||
| results["image_grid_thw"].append([]) | ||
| continue | ||
|
|
||
| if source[0]["role"] != "user": | ||
| # if the first message is not from user, skip it | ||
| source = source[1:] | ||
|
|
||
| convroles = ["user", "assistant"] | ||
| has_added_images = False | ||
| for j, sentence in enumerate(source): | ||
| role = sentence["role"] | ||
| assert role == convroles[j % 2], f"unexpected role {role}" | ||
| if role == "user": | ||
| # if the message is from user and has image, process the image | ||
| messages.append( | ||
| { | ||
| "role": role, | ||
| "content": [ | ||
| { | ||
| "type": "image", | ||
| "image": image, | ||
| }, | ||
| {"type": "text", "text": sentence["content"]}, | ||
| ], | ||
| } | ||
| ) | ||
| # Insert all images into the first user message | ||
| if not has_added_images: | ||
| content = [{"type": "image", "image": img} for img in images] | ||
| content.append({"type": "text", "text": sentence["content"]}) | ||
| messages.append({"role": role, "content": content}) | ||
| has_added_images = True | ||
| else: | ||
| messages.append({"role": role, "content": sentence["content"]}) | ||
| else: | ||
| messages.append({"role": role, "content": sentence["content"]}) | ||
|
|
||
|
|
@@ -273,7 +306,7 @@ def preprocess_vlm_conversations( | |
| input_ids = encoding.input_ids[0] | ||
| offsets = encoding.offset_mapping[0] | ||
| pixel_values = encoding.pixel_values | ||
| image_grid_thw = encoding.image_grid_thw[0] | ||
| image_grid_thw = encoding.image_grid_thw # shape: (num_images, 3) | ||
|
|
||
| # get conversation with image info for loss mask generation | ||
| decoded_conversation = processor.tokenizer.decode( | ||
|
|
@@ -289,7 +322,7 @@ def preprocess_vlm_conversations( | |
| results["loss_mask"].append(loss_mask[None, :]) | ||
| results["attention_mask"].append(torch.ones_like(loss_mask)[None, :]) | ||
| results["pixel_values"].append(pixel_values) | ||
| results["image_grid_thw"].append(image_grid_thw[None, :]) | ||
| results["image_grid_thw"].append(image_grid_thw) | ||
| return results | ||
|
|
||
|
|
||
|
|
@@ -390,7 +423,7 @@ def preprocess_function(examples): | |
| # Parse tools: handle JSON strings from safe_conversations_generator | ||
| tools = [] | ||
| for tool_item in tools_raw: | ||
| if isinstance(tool_item, (str, list)): | ||
| if isinstance(tool_item, str): | ||
| try: | ||
| tools.append(json.loads(tool_item)) | ||
| except json.JSONDecodeError: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
exist_ok=Trueargument passed toAutoProcessor.from_pretraineddoes not appear to be a standard argument for this Hugging Face Transformers method. While it might be ignored iftrust_remote_code=Trueallows for custom arguments in the model's loading code, it's not guaranteed and could lead to unexpected behavior or errors with different models or library versions. It would be safer to remove this argument if it's not strictly required by the Qwen-VL model's custom code.