Skip to content

fix ulysses #5501

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

Merged
merged 1 commit into from
Aug 22, 2025
Merged

fix ulysses #5501

merged 1 commit into from
Aug 22, 2025

Conversation

tastelikefeet
Copy link
Collaborator

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Write the detail information belongs to this PR.

Experiment results

Paste your experiment result here(if needed).

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @tastelikefeet, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug within the Ulysses sequence parallel training module. The core change involves modifying how causal masks are generated and applied. Specifically, it ensures that input embeddings and cache positions are correctly managed for sequence parallel operations, which is crucial for the proper functioning of the training process.

Highlights

  • Causal Mask Adjustment: A new create_causal_mask function has been implemented within ulysses.py to correctly reshape input_embeds and adjust cache_position for sequence parallel processing.
  • Masking Utility Override: The original masking_utils.create_causal_mask is now temporarily stored and then replaced with the new, modified version to ensure the correct mask generation logic is applied during Ulysses training.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a patch to create_causal_mask in ulysses.py to better handle sequence parallelism. While the core logic of adjusting input_embeds and cache_position is sound, I've identified a critical issue with the monkey-patching implementation that could lead to infinite recursion. Additionally, I've suggested a performance optimization to use torch.empty instead of torch.ones for creating a dummy tensor, as its values are not used. The suggested code change addresses both of these points.

Comment on lines +183 to +193
def create_causal_mask(config, input_embeds, attention_mask, cache_position, *args, **kwargs):
input_embeds = torch.ones(
(input_embeds.shape[0], input_embeds.shape[1] * self.sp_world_size, input_embeds.shape[2]),
dtype=input_embeds.dtype,
device=input_embeds.device)
cache_position = torch.arange(0, input_embeds.shape[1], device=input_embeds.device)
return masking_utils.origin_create_causal_mask(config, input_embeds, attention_mask, cache_position,
*args, **kwargs)

masking_utils.origin_create_causal_mask = masking_utils.create_causal_mask
masking_utils.create_causal_mask = create_causal_mask
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This monkey-patch has two potential issues:

  1. Idempotency: The patch is not idempotent. If init_sequence_parallel is called multiple times (e.g., from different Ulysses instances), it will wrap create_causal_mask repeatedly, leading to masking_utils.origin_create_causal_mask pointing to an already-patched function. This would cause an infinite recursion, which is a critical bug.
  2. Efficiency: torch.ones is used to create a dummy input_embeds tensor. However, the underlying create_causal_mask function only needs the tensor's metadata (shape, dtype, device), not its values. Using torch.empty is more efficient as it avoids unnecessary memory initialization.

I've combined fixes for both issues in the suggestion below.

Suggested change
def create_causal_mask(config, input_embeds, attention_mask, cache_position, *args, **kwargs):
input_embeds = torch.ones(
(input_embeds.shape[0], input_embeds.shape[1] * self.sp_world_size, input_embeds.shape[2]),
dtype=input_embeds.dtype,
device=input_embeds.device)
cache_position = torch.arange(0, input_embeds.shape[1], device=input_embeds.device)
return masking_utils.origin_create_causal_mask(config, input_embeds, attention_mask, cache_position,
*args, **kwargs)
masking_utils.origin_create_causal_mask = masking_utils.create_causal_mask
masking_utils.create_causal_mask = create_causal_mask
if not hasattr(masking_utils, 'origin_create_causal_mask'):
def create_causal_mask(config, input_embeds, attention_mask, cache_position, *args, **kwargs):
input_embeds = torch.empty(
(input_embeds.shape[0], input_embeds.shape[1] * self.sp_world_size, input_embeds.shape[2]),
dtype=input_embeds.dtype,
device=input_embeds.device)
cache_position = torch.arange(0, input_embeds.shape[1], device=input_embeds.device)
return masking_utils.origin_create_causal_mask(config, input_embeds, attention_mask, cache_position,
*args, **kwargs)
masking_utils.origin_create_causal_mask = masking_utils.create_causal_mask
masking_utils.create_causal_mask = create_causal_mask

@tastelikefeet tastelikefeet merged commit cbf7d25 into modelscope:main Aug 22, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants