Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[liger-kernel] support more model & gspo #6338

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
hjh0119 merged 5 commits into modelscope:main from hjh0119:more-liger
Oct 29, 2025
Merged

Conversation

@hjh0119
Copy link
Collaborator

@hjh0119 hjh0119 commented Oct 29, 2025

  • Support newly added models in Liger-Kernel
  • Add support for GSPO with Liger loss

Copy link
Contributor

Summary of Changes

Hello @hjh0119, 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 significantly enhances the integration of liger-kernel within the swift framework. It broadens the compatibility of liger-kernel with a diverse set of large language models and refines the implementation of GSPO with liger loss, allowing for more advanced importance sampling configurations while ensuring proper versioning and user guidance for optimal performance.

Highlights

  • Expanded Model Support: Liger-Kernel now supports a wider range of Large Language Models (LLMs), including various new Gemma, Qwen, GLM, InternVL, Llama, LLaVA, and PaliGemma variants, enhancing its applicability across diverse architectures.
  • Enhanced GSPO with Liger Loss: The Generalized Stochastic Policy Optimization (GSPO) implementation with Liger loss has been refined to support more flexible importance sampling levels, including 'sequence_token' and 'sequence', with appropriate version checks for liger-kernel.
  • Liger-Kernel Version Compatibility: Introduced a version check for the liger-kernel library to ensure compatibility with new features and provide clear guidance to users on upgrading their liger-kernel installation when necessary.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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
Collaborator Author

hjh0119 commented Oct 29, 2025

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 adds support for more models in Liger-Kernel and introduces GSPO with Liger loss. The changes primarily involve updating model type mappings and argument handling. I've identified a duplicated code block and a confusing log message that should be addressed. Additionally, I've suggested a refactoring for better code maintainability.

Comment on lines 20 to 90
def apply_liger(model_type: str):
from liger_kernel.transformers import (apply_liger_kernel_to_llama, apply_liger_kernel_to_mistral,
apply_liger_kernel_to_mixtral, apply_liger_kernel_to_gemma,
apply_liger_kernel_to_qwen2, apply_liger_kernel_to_qwen3,
apply_liger_kernel_to_qwen2_vl, apply_liger_kernel_to_qwen2_5_vl,
apply_liger_kernel_to_phi3, apply_liger_kernel_to_mllama)
from swift.llm import ModelType
if model_type in (ModelType.llama, ModelType.llama3, ModelType.llama3_1, ModelType.llama3_2):
apply_liger_kernel_to_llama()
elif model_type in (ModelType.mistral):
apply_liger_kernel_to_mistral()
elif model_type in (ModelType.mixtral):
apply_liger_kernel_to_mixtral()
elif model_type in (ModelType.gemma, ModelType.gemma2):
apply_liger_kernel_to_gemma()
elif model_type in (ModelType.qwen2, ModelType.qwen2_5):
apply_liger_kernel_to_qwen2()
elif model_type in (ModelType.qwen3):
apply_liger_kernel_to_qwen3()
elif model_type in (ModelType.phi3):
apply_liger_kernel_to_phi3()
elif model_type in (ModelType.llama3_2_vision):
apply_liger_kernel_to_mllama()
elif model_type in (ModelType.qwen2_vl):
apply_liger_kernel_to_qwen2_vl()
elif model_type in (ModelType.qwen2_5_vl):
apply_liger_kernel_to_qwen2_5_vl()
else:
raise ValueError(f'Unsupported liger model_type: {model_type}')
try:
from liger_kernel.transformers import (apply_liger_kernel_to_llama, apply_liger_kernel_to_mistral,
apply_liger_kernel_to_mixtral, apply_liger_kernel_to_gemma,
apply_liger_kernel_to_qwen2, apply_liger_kernel_to_qwen3,
apply_liger_kernel_to_qwen2_vl, apply_liger_kernel_to_qwen2_5_vl,
apply_liger_kernel_to_phi3, apply_liger_kernel_to_mllama)
from swift.llm import ModelType
if model_type in (ModelType.llama, ModelType.llama3, ModelType.llama3_1, ModelType.llama3_2):
apply_liger_kernel_to_llama()
elif model_type in (ModelType.mistral):
apply_liger_kernel_to_mistral()
elif model_type in (ModelType.mixtral):
apply_liger_kernel_to_mixtral()
elif model_type in (ModelType.gemma, ModelType.gemma2):
apply_liger_kernel_to_gemma()
elif model_type in (ModelType.gemma3_text):
from liger_kernel.transformers import apply_liger_kernel_to_gemma3_text
apply_liger_kernel_to_gemma3_text()
elif model_type in (ModelType.gemma3_vision, ModelType.gemma3n):
from liger_kernel.transformers import apply_liger_kernel_to_gemma3
apply_liger_kernel_to_gemma3()
elif model_type in (ModelType.qwen2, ModelType.qwen2_5):
apply_liger_kernel_to_qwen2()
elif model_type in (ModelType.qwen3, ModelType.qwen3_guard, ModelType.qwen3_thinking,
ModelType.qwen3_nothinking, ModelType.qwen3_coder):
apply_liger_kernel_to_qwen3()
elif model_type in (ModelType.qwen3_moe, ModelType.qwen3_moe_thinking, ModelType.qwen3_coder):
from liger_kernel.transformers import apply_liger_kernel_to_qwen3_moe
apply_liger_kernel_to_qwen3_moe()
elif model_type in (ModelType.qwen3_next, ModelType.qwen3_next_thinking):
from liger_kernel.transformers import apply_liger_kernel_to_qwen3_next
apply_liger_kernel_to_qwen3_next()
elif model_type in (ModelType.phi3):
apply_liger_kernel_to_phi3()
elif model_type in (ModelType.llama3_2_vision):
apply_liger_kernel_to_mllama()
elif model_type in (ModelType.qwen2_vl):
apply_liger_kernel_to_qwen2_vl()
elif model_type in (ModelType.qwen2_5_vl, ModelType.qwen3_vl, ModelType.qwen3_moe_vl, ModelType.qvq):
apply_liger_kernel_to_qwen2_5_vl()
elif model_type in (ModelType.glm4, ModelType.glm4_0414, ModelType.glm4_z1_rumination):
from liger_kernel.transformers import apply_liger_kernel_to_glm4
apply_liger_kernel_to_glm4()
elif model_type in (ModelType.glm4v, ModelType.glm4_1v):
from liger_kernel.transformers import apply_liger_kernel_to_glm4v
apply_liger_kernel_to_glm4v()
elif model_type in (ModelType.glm4_5v):
from liger_kernel.transformers import apply_liger_kernel_to_glm4v_moe
apply_liger_kernel_to_glm4v_moe()
elif model_type in (ModelType.internvl_hf, ModelType.internvl_gpt_hf):
from liger_kernel.transformers import apply_liger_kernel_to_internvl
apply_liger_kernel_to_internvl()
elif model_type in (ModelType.llama4):
from liger_kernel.transformers import apply_liger_kernel_to_llama4
apply_liger_kernel_to_llama4()
elif model_type in (ModelType.llava1_5_hf, ModelType.llava_llama3_hf, ModelType.pixtral):
from liger_kernel.transformers import apply_liger_kernel_to_llava
apply_liger_kernel_to_llava()
elif model_type in (ModelType.llama3_2_vision):
from liger_kernel.transformers import apply_liger_kernel_to_mllama
apply_liger_kernel_to_mllama()
elif model_type in (ModelType.paligemma):
from liger_kernel.transformers import apply_liger_kernel_to_paligemma
apply_liger_kernel_to_paligemma()
else:
raise ValueError(f'Unsupported liger model_type: {model_type}')
except ImportError:
raise ImportError('Please upgrade liger-kernel to apply liger kernel to this model '
'by running `pip install -U liger-kernel`')

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot Oct 29, 2025

Choose a reason for hiding this comment

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

medium

This function has become very long and difficult to maintain due to the large number of elif statements. This reduces readability and increases the chance of errors, such as the duplicated block for llama3_2_vision found later in the file.

Consider refactoring this into a dispatch dictionary that maps model types to their corresponding apply_liger_kernel_to_* functions. This would make the code cleaner, more extensible, and easier to read.

Example of the pattern:

from swift.llm import ModelType
from liger_kernel.transformers import (
 apply_liger_kernel_to_llama, 
 # ... other imports
)
LIGER_MAPPING = {
 (ModelType.llama, ModelType.llama3): apply_liger_kernel_to_llama,
 # ... other model types
}
for model_types, func in LIGER_MAPPING.items():
 if model_type in model_types:
 func()
 return
# ... handle special cases or raise error

else:
raise ValueError(f'Unsupported liger model_type: {model_type}')
try:
from liger_kernel.transformers import (apply_liger_kernel_to_llama, apply_liger_kernel_to_mistral,
Copy link
Collaborator

@tastelikefeet tastelikefeet Oct 29, 2025

Choose a reason for hiding this comment

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



def apply_liger(model_type: str):
from liger_kernel.transformers import (apply_liger_kernel_to_llama, apply_liger_kernel_to_mistral,
Copy link
Collaborator

@Jintao-Huang Jintao-Huang Oct 29, 2025

Choose a reason for hiding this comment

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

印象中应该跑不到这个这个函数啊

@hjh0119 hjh0119 merged commit 50dd6f9 into modelscope:main Oct 29, 2025
1 of 2 checks passed
@hjh0119 hjh0119 deleted the more-liger branch October 29, 2025 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Jintao-Huang Jintao-Huang Jintao-Huang approved these changes

@tastelikefeet tastelikefeet tastelikefeet approved these changes

+1 more reviewer

@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /