-
Notifications
You must be signed in to change notification settings - Fork 33.5k
fix(janus): Handle None values in image generation mode #44793
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
Closed
BillionClaw
wants to merge
1
commit into
huggingface:main
from
BillionClaw:clawoss/fix-janus-image-generation-44792
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1285,7 +1285,7 @@ def generate( | |
| input_ids, model_kwargs = self._expand_inputs_for_generation( | ||
| input_ids=input_ids, | ||
| attention_mask=attention_mask, | ||
| expand_size=generation_config.num_return_sequences, | ||
| expand_size=generation_config.num_return_sequences or 1, | ||
| **model_kwargs, | ||
| ) | ||
|
|
||
|
|
@@ -1298,6 +1298,17 @@ def generate( | |
| attention_mask = attention_mask.repeat(2, 1) | ||
| model_kwargs["attention_mask"] = attention_mask | ||
|
|
||
| # Ensure generation_kwargs exists with boi_token_id | ||
|
Member
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. prob this and rest will be resolved after calling |
||
| if not hasattr(generation_config, "generation_kwargs") or generation_config.generation_kwargs is None: | ||
| generation_config.generation_kwargs = {} | ||
| if "boi_token_id" not in generation_config.generation_kwargs: | ||
| # Default boi_token_id - usually the image_token_id from config | ||
| generation_config.generation_kwargs["boi_token_id"] = getattr(self.config, "image_token_id", 0) | ||
|
|
||
| # Ensure pad_token_id is set | ||
| if generation_config.pad_token_id is None: | ||
| generation_config.pad_token_id = getattr(self.config, "pad_token_id", 0) | ||
|
|
||
| # Mask all the tokens that are neither BOS nor BOI with pad token in the unconditional logits. | ||
| mask = (input_tokens[batch_size:, :] != generation_config.bos_token_id) & ( | ||
| input_tokens[batch_size:, :] != generation_config.generation_kwargs["boi_token_id"] | ||
|
|
@@ -1310,12 +1321,18 @@ def generate( | |
|
|
||
| if model_kwargs.get("past_key_values", None) is None: | ||
| # Prepare cache if not provided. | ||
| # Need enough space for: input sequence + num_image_tokens iterations + safety margin | ||
| # The loop runs num_image_tokens times, starting from seq_len position | ||
| max_length = generation_config.max_length | ||
| min_cache_len = seq_len + num_image_tokens + 100 # Ensure enough buffer | ||
| if max_length is None: | ||
| max_length = min_cache_len | ||
| model_kwargs["past_key_values"] = self._prepare_static_cache( | ||
| cache_implementation=generation_config.cache_implementation or "static", | ||
| # batch_size should account for both conditional/unconditional input; hence multiplied by 2. | ||
| batch_size=batch_size * 2, | ||
| # we should have at least a cache len of seq_len + num_image_tokens. | ||
| max_cache_len=max(generation_config.max_length, num_image_tokens + seq_len), | ||
| # we should have at least a cache len of seq_len + num_image_tokens + buffer. | ||
| max_cache_len=max(max_length, min_cache_len), | ||
| model_kwargs=model_kwargs, | ||
| ) | ||
|
|
||
|
|
||
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.
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.
we just need to call
_prepare_generation_configin the very beginning, it is missing