Skip to content

Fix: prevent model graph and kwargs mutation in AutoModel.export() /修复 AutoModel 连续导出导致的计算图破坏与字典污染Fixes a critical destructive bug in AutoModel.export().#2835

Open
ottercoconut wants to merge 1 commit intomodelscope:mainfrom
ottercoconut:fix-automodel-export

Conversation

@ottercoconut
Copy link

Fixes a critical destructive bug in AutoModel.export(). Previously, calling export() modified both self.model (by in-place replacing layers with ONNX-specific modules like SANMEncoderExport) and self.kwargs. This caused consecutive export() calls (e.g., exporting FP32 then Int8) to crash with either KeyError: 'model' or AttributeError: 'SANMEncoderExport' object has no attribute 'encoders'.
Implemented a dual deepcopy strategy:

  1. model = copy.deepcopy(self.model): Isolates the graph mutation, ensuring the original PyTorch model remains untouched.
  2. kwargs = copy.deepcopy(self.kwargs): Prevents parameter reference pollution.

修复了 AutoModel.export() 中破坏性的 Bug。原逻辑在导出时会就地(in-place)修改 self.model 的计算图(替换为 SANMEncoderExport 等 ONNX 算子)并污染 self.kwargs。这导致同一脚本连续调用 export() 时必定崩溃,抛出 KeyErrorAttributeError
采用了双重深拷贝策略:

  1. model = copy.deepcopy(self.model):对模型进行深拷贝,隔离 ONNX 算子替换对原计算图的物理破坏。
  2. kwargs = copy.deepcopy(self.kwargs):深拷贝配置字典,防止引用污染。

Reproduction bug/ 复现bug

from funasr import AutoModel
model = AutoModel(model="paraformer-zh", device="cpu")
model.export(quantize=False) # Mutates self.model and self.kwargs
model.export(quantize=True)  # Crashes without this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 resolves a significant issue in the AutoModel.export() method that caused instability and crashes when attempting multiple export operations. By introducing deep copies for both the model and its configuration parameters, the changes ensure that the original objects remain immutable, thereby preventing unintended side effects and allowing for reliable, consecutive calls to the export function.

Highlights

  • Critical Bug Fix: Addressed a critical destructive bug in AutoModel.export() where consecutive calls would crash due to in-place modifications of self.model and self.kwargs.
  • Deep Copy Strategy: Implemented a dual deep copy strategy for both self.model and self.kwargs to prevent mutation of the original objects during the export process.
  • Graph Isolation: Ensured that ONNX-specific module replacements (monkey-patching) only affect a copied version of the model, leaving the original PyTorch graph intact.
  • Parameter Protection: Prevented parameter reference pollution by deep copying self.kwargs, ensuring that deep_update and del operations do not affect the original configuration.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

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 addresses a critical bug in AutoModel.export() that caused crashes on consecutive calls. The fix correctly isolates the model and its configuration using deep copies, which prevents in-place mutations of self.model and self.kwargs. The change also includes a safer way to handle dictionary key deletion, improving robustness. My review includes a minor suggestion to improve code style by removing a redundant import.

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