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)

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

def get_loss_scale(self, context: str, context_type: ContextType, is_last_round: bool, **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 , 模型性能提升。

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

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!

This pull request introduces a significant feature enhancement by allowing extra_kwargs to be passed to the loss_scale method. This change provides greater flexibility and customizability for loss weighting strategies within the framework, empowering users to fine-tune training controls based on specific dataset sample characteristics. The modification is designed to be non-breaking and has been validated in various training setups, improving the framework's overall adaptability.

Highlights

  • Enhanced Loss Scale Customization: The loss_scale method call in swift/llm/template/base.py now correctly passes inputs.extra_kwargs, enabling users to dynamically adjust loss weights based on dataset sample parameters.
  • Non-Breaking Change: This modification is fully backward compatible and does not affect the normal operation of existing code, ensuring a smooth integration.
  • Advanced Usage Support: Users can now leverage extra_kwargs to implement sophisticated loss scaling strategies, such as dynamic adjustments based on sample difficulty or different scaling policies per task type.
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 旨在通过将 inputs.extra_kwargs 传递给 loss_scale 方法来增强框架的灵活性。这是一个很好的功能增强。

我在代码中发现了一个潜在问题:虽然您在调用 self.loss_scale 时传递了额外的参数,但 LossScale 类中的 __call__ 方法似乎没有将这些参数继续传递给底层的 get_loss_scale 方法,这可能导致新功能无法按预期工作。我在具体的代码行上留下了详细的评论和修改建议。

请您检查一下,以确保 extra_kwargs 能够被完全利用。

Comment on lines +1149 to +1150
res_context_list, loss_scale_list = self.loss_scale(res_context_list, res_context_types, inputs.messages,
**inputs.extra_kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

感谢您的贡献!您在 _swift_encode 中正确地将 inputs.extra_kwargs 传递给了 self.loss_scale

但是,LossScale.__call__ 方法 (位于 swift/plugin/loss_scale/loss_scale.py) 似乎没有将这些额外的关键字参数 (**kwargs) 传递给 get_loss_scale 方法。

为了让您的修改完全生效,LossScale.__call__ 中的 get_loss_scale 调用点也需要更新。

目前的实现:

# swift/plugin/loss_scale/loss_scale.py:73
new_context, loss_scale = self.get_loss_scale(context, context_type, is_last_round, query=query)

建议修改为:

# swift/plugin/loss_scale/loss_scale.py:73
new_context, loss_scale = self.get_loss_scale(context, context_type, is_last_round, query=query, **kwargs)

这样才能确保 extra_kwargs 能够被各个 LossScale 子类中的 get_loss_scale 方法接收和使用。请您检查并补充这一修改。

@CJack812 CJack812 closed this Oct 15, 2025
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.

1 participant