Skip to content

Conversation

@CJack812
Copy link
Contributor

PR type

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

PR information

问题描述

swift/llm/template/base.py 中调用 self.loss_scale 时,没有将额外的参数传递给底层的 get_loss_scale 函数,导致用户无法通过数据集样本自定义参数来灵活控制 loss_scale 行为。

修改内容

文件: swift/llm/template/base.py

修改前:

res_context_list, loss_scale_list = self.loss_scale(res_context_list, res_context_types, inputs.messages)

修改后:

res_context_list, loss_scale_list = self.loss_scale(res_context_list, res_context_types, inputs.messages,
                                                            **inputs.extra_kwargs)

class LossScale

__call__ 方法 函数签名原本就支持额外的 **kwargs 参数:

    def __call__(self, context_list: List[str], context_types: List[ContextType], messages: Messages,
                 **kwargs) -> Tuple[List[str], List[float]]:

但在template base.py的调用处没有利用这一特性,导致额外的参数无法传递

用户现在可以在数据集的样本中定义 extra_kwargs,通过这些参数来自定义loss_scale

无破坏性修改:完全向后兼容,不影响现有代码的正常运行
支持高级用法:例如:
基于样本难度动态调整损失权重
根据任务类型应用不同的缩放策略

Experiment results

已在qwen3-omni模型的megatron sft训练和qwen2.5-omni模型的deepspeed sft 训练中试验了该功能。具体为:在样本中增加extra_kwargs ,自定义loss_scale根据这些extra_kwargs 调整 loss_scale , 模型性能提升。

该修改为框架基础设施优化,不直接影响模型性能指标,但显著提升了框架的灵活性和可定制性,为用户实现更精细化的训练控制提供了可能。

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @CJack812, 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!

此拉取请求通过在 swift/llm/template/base.py 文件中,向 self.loss_scale 方法调用中添加 **inputs.extra_kwargs 参数,显著增强了框架的灵活性和可定制性。这一改动使得用户能够通过数据集样本中的 extra_kwargs 来自定义 loss_scale 行为,从而实现更精细化的训练控制,例如根据样本特性动态调整损失权重。此项优化是基础设施层面的改进,旨在提升框架的通用性和扩展性,且完全向后兼容。

Highlights

  • 增强 loss_scale 方法的灵活性: 修复了 swift/llm/template/base.pyself.loss_scale 方法调用时未传递 inputs.extra_kwargs 的问题,现在允许用户通过数据集样本自定义参数来灵活控制 loss_scale 行为。
  • 支持高级用法: 用户现在可以在数据集的样本中定义 extra_kwargs,从而实现基于样本难度动态调整损失权重或根据任务类型应用不同缩放策略等高级用法。
  • 无破坏性修改: 此修改是无破坏性的,完全向后兼容,不影响现有代码的正常运行。
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
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

您好,感谢您的贡献。这个 PR 的目标是允许用户通过 extra_kwargs 自定义 loss_scale 的行为,这是一个非常有用的功能增强,可以提高框架的灵活性。

您在 swift/llm/template/base.py 中所做的修改是正确的第一步。然而,我发现为了让这个功能完全生效,可能还需要在 swift/plugin/loss_scale/loss_scale.py 中进行相应的修改。我在代码中留下了具体的审查意见。请查看并考虑是否需要将相关改动一并提交。

@Jintao-Huang
Copy link
Collaborator

非常好的修改😊

@Jintao-Huang Jintao-Huang merged commit fffd293 into modelscope:main Oct 16, 2025
2 of 4 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