Skip to content

Conversation

zhtmike
Copy link
Collaborator

@zhtmike zhtmike commented Sep 11, 2025

Relies on Mbart #1195

What does this PR do?

Fixes # (issue)
In MS2.6/2.7, when there is a tied weights scenario, the load_param_into_net API may produce unexpected results during the weight loading stage. To address this, we switched to using the load_state_dict API, which more closely aligns with PyTorch's behavior.

Here is an example that demonstrates the buggy result caused by using the load_param_into_net API with tied weights.

import mindspore as ms
import mindspore.mint as mint


class Model(ms.nn.Cell):
    def __init__(self):
        super().__init__()
        self.layer1 = mint.nn.Linear(64, 64, bias=False)
        self.layer2 = mint.nn.Linear(64, 64, bias=False)
        self.layer1.weight = self.layer2.weight

# way 1
model = Model()
print(dict(model.parameters_and_names()).keys())  # layer 1 weight existed, layer 2 weight missing
model.load_state_dict({"layer1.weight": mint.ones((64, 64))}, strict=False) # can be loaded
print(model.layer2.weight.value()) # weight is correct

# way 2
model = Model()
print(dict(model.parameters_and_names()).keys())  # layer 1 weight existed, layer 2 weight missing
ms.load_param_into_net(model, {"layer1.weight": ms.Parameter(mint.ones((64, 64)))}, strict_load=False)
print(model.layer2.weight.value()) # weight is NOT correct

Adds # (feature)
Add Donut & Flava model

Donut

>>> from datasets import load_dataset
>>> from mindone.transformers import AutoProcessor, AutoModelForVision2Seq

>>> processor = AutoProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa", revision="refs/pr/23")
>>> model = AutoModelForVision2Seq.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa", revision="refs/pr/23)

>>> dataset = load_dataset("hf-internal-testing/example-documents", split="test")
>>> image = dataset[0]["image"]
>>> question = "What time is the coffee break?"
>>> task_prompt = f"<s_docvqa><s_question>{question}</s_question><s_answer>"
>>> inputs = processor(image, task_prompt, return_tensors="ms")

>>> outputs = model.generate(
...     input_ids=inputs.input_ids,
...     pixel_values=inputs.pixel_values,
...     max_length=512
... )
>>> answer = processor.decode(outputs[0], skip_special_tokens=True)
>>> print(answer)
"What time is the coffee break? 11-14 to 11:39 a.m."
version mode model precision task s/step weight load(s)
ms2.7.0 pynative VisionEncoderDecoderModel fp32 VQA 0.02 13

Flava

>>> from PIL import Image
>>> import requests
>>> from mindone.transformers import AutoProcessor, FlavaModel

>>> model = FlavaModel.from_pretrained("facebook/flava-full", revision="refs/pr/6")
>>> processor = AutoProcessor.from_pretrained("facebook/flava-full", revision="refs/pr/6")

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> inputs = processor(text=["a photo of a cat"], images=image, return_tensors="ms", padding=True)

>>> outputs = model(**inputs)

>>> image_embeddings = outputs.image_embeddings
>>> text_embeddings = outputs.text_embeddings
>>> multimodal_embeddings = outputs.multimodal_embeddings

>>> outputs.image_embeddings.shape
(1, 197, 768)

>>> text_embeddings.shape
(1, 7, 768)

>>> multimodal_embeddings.shape
(1, 205, 768)
version mode model precision task s/step) weight load(s)
ms2.7.0 pynative FlavaModel fp32 FeatureExtraction 0.07 15

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@xxx

@zhtmike zhtmike added the feature request Add new features label Sep 11, 2025
@zhtmike zhtmike marked this pull request as ready for review September 18, 2025 08:28
@zhtmike zhtmike requested a review from vigo999 as a code owner September 18, 2025 08:28
@zhtmike zhtmike added the bug Something isn't working label Sep 18, 2025
@zhtmike zhtmike requested a review from wcrzlh September 18, 2025 08:28
cm = silence_mindspore_logger() if is_sharded else nullcontext()
with cm:
ms.load_param_into_net(model_to_load, state_dict, strict_load=True)
model_to_load.load_state_dict(state_dict, strict=False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why change strict to False?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the case of tied weights, there may be extra or missing parameters in the Hugging Face transformer checkpoint. Using strict=True will raise an error, so we follow the same design as the Transformers repo by setting strict=False.

@zhtmike zhtmike added new model add new model to mindone and removed feature request Add new features labels Sep 23, 2025
@zhtmike zhtmike self-assigned this Sep 23, 2025
@zhtmike
Copy link
Collaborator Author

zhtmike commented Sep 23, 2025

/gemini review

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 support for the Donut and Flava models, a significant and valuable addition to the library. The change to use load_state_dict for handling tied weights is also a crucial bug fix. The implementation of the new models appears to be a faithful port from the Hugging Face transformers library. However, I've identified several issues, including two critical bugs in the FlavaForPreTraining model that affect the model's output when return_dict=False. Additionally, there are a few minor typos in docstrings and log messages that should be corrected to improve clarity and usability. Addressing these points will greatly enhance the quality of this contribution.

@vigo999 vigo999 added this to mindone Sep 29, 2025
@vigo999 vigo999 moved this to In Progress in mindone Sep 29, 2025
@vigo999 vigo999 requested a review from zhanghuiyao September 29, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working new model add new model to mindone

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants