This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Absorb non-MM OpenAI dialog parsing into generic input parsing #1248
Merged
Conversation
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
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchchat/1248
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 29f5204 with merge base edaa15c ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Jack-Khuu
commented
Oct 1, 2024
Comment on lines
+783
to
+832
| assert ( | ||
| max_new_tokens is not None | ||
| ), "max_new_tokens must be specified for Flamingo models" | ||
| assert isinstance( | ||
| prompt, str | ||
| ), "(Currently) prompt must be a str for Flamingo models" | ||
|
|
||
| is_multimodal = images is not None | ||
| content = [{"type": "text", "content": prompt}] | ||
| is_multimodal = images is not None | ||
| content = [{"type": "text", "content": prompt}] | ||
|
|
||
| if is_multimodal: | ||
| content = [{"type": "image", "content": images[0]}] + content | ||
| if is_multimodal: | ||
| content = [{"type": "image", "content": images[0]}] + content | ||
|
|
||
| messages = [ | ||
| Message( | ||
| role="user", | ||
| content=content, | ||
| eot=True, | ||
| ), | ||
| Message(role="assistant", content=""), | ||
| ] | ||
| messages = [ | ||
| Message( | ||
| role="user", | ||
| content=content, | ||
| eot=True, | ||
| ), | ||
| Message(role="assistant", content=""), | ||
| ] | ||
|
|
||
| transform = llama3_2_vision_transform(str(self.tokenizer_args.tokenizer_path)) | ||
| transform = llama3_2_vision_transform(str(self.tokenizer_args.tokenizer_path)) | ||
|
|
||
| device = torch.device(device=self.builder_args.device) | ||
| device = torch.device(device=self.builder_args.device) | ||
|
|
||
| with device, set_default_dtype(self.dtype): | ||
| data = transform({"messages": messages}, inference=True) | ||
| with device, set_default_dtype(self.dtype): | ||
| data = transform({"messages": messages}, inference=True) | ||
|
|
||
| if is_multimodal: | ||
| batch = padded_collate_tiled_images_and_mask( | ||
| [data], pad_direction="left", pad_max_images=1 | ||
| ) | ||
| encoded = batch.pop("tokens").to(device).view(-1) | ||
| seq_len = encoded.size(0) | ||
| batch["encoder_mask"] = batch["encoder_mask"][:, :seq_len] | ||
| batch["encoder_input"]["images"] = batch["encoder_input"]["images"].to(self.dtype) | ||
| else: | ||
| encoded = torch.tensor( | ||
| data["tokens"], device=device | ||
| ).view(-1) | ||
| seq_len = encoded.size(0) | ||
| batch = {} | ||
|
|
||
| total_response_length = seq_len + max_new_tokens | ||
| batch["causal_mask"] = torch.tril( | ||
| torch.ones( | ||
| size=(total_response_length, total_response_length), | ||
| dtype=torch.bool, | ||
| ) | ||
| ) | ||
| else: | ||
| encoded = self.encode_tokens( | ||
| prompt, bos=True, device=self.builder_args.device | ||
| if is_multimodal: | ||
| batch = padded_collate_tiled_images_and_mask( | ||
| [data], pad_direction="left", pad_max_images=1 | ||
| ) | ||
| encoded = batch.pop("tokens").to(device).view(-1) | ||
| seq_len = encoded.size(0) | ||
| batch["encoder_mask"] = batch["encoder_mask"][:, :seq_len] | ||
| batch["encoder_input"]["images"] = batch["encoder_input"]["images"].to( | ||
| self.dtype | ||
| ) | ||
| else: | ||
| encoded = torch.tensor(data["tokens"], device=device).view(-1) | ||
| seq_len = encoded.size(0) | ||
| batch = {} | ||
|
|
||
| total_response_length = seq_len + max_new_tokens | ||
| batch["causal_mask"] = torch.tril( | ||
| torch.ones( | ||
| size=(total_response_length, total_response_length), | ||
| dtype=torch.bool, | ||
| ) |
Contributor
Author
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.
Lint and white space
byjlw
approved these changes
Oct 1, 2024
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Refactor the OpenAI parsing logic for non-MM dialog into
_gen_model_inputof generate.pyTested via Browser
Then test in browser with 1B (Text only - Saw it can do multiturn) and 11B (Multimodal - No multiturn as expected)