Skip to content

Conversation

DanielSun11
Copy link
Contributor

@DanielSun11 DanielSun11 commented Jul 31, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

添加paddle.nn.utils.vector_to_parameters对vector的元素总个数多于所有parameters的元素个数之和这一情况的支持。

  • 当vector中所有的元素个数等于( == )parameters中所有元素之和时使用paddle.split实现。
  • 当vector中所有的元素个数小于 ( < ) parameters中所有元素之和时,报错 。
  • 当vector中所有的元素个数多于( > )parameters中所有元素之和时,仿照pytorch使用slice机制实现 。

pytorch 的实现:

def vector_to_parameters(vec: torch.Tensor, parameters: Iterable[torch.Tensor]) -> None:
    r"""Copy slices of a vector into an iterable of parameters.

    Args:
        vec (Tensor): a single vector representing the parameters of a model.
        parameters (Iterable[Tensor]): an iterable of Tensors that are the
            parameters of a model.
    """
    # Ensure vec of type Tensor
    if not isinstance(vec, torch.Tensor):
        raise TypeError(f"expected torch.Tensor, but got: {torch.typename(vec)}")
    # Flag for the device where the parameter is located
    param_device = None

    # Pointer for slicing the vector for each parameter
    pointer = 0
    for param in parameters:
        # Ensure the parameters are located in the same device
        param_device = _check_param_device(param, param_device)

        # The length of the parameter
        num_param = param.numel()
        # Slice the vector, reshape it, and replace the old data of the parameter
        param.data = vec[pointer : pointer + num_param].view_as(param).data

        # Increment the pointer
        pointer += num_param

pcard-67164

Copy link

paddle-bot bot commented Jul 31, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@3a7d7f4). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #74348   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?        12           
  Branches           ?         0           
===========================================
  Hits               ?        12           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wanghuancoder wanghuancoder merged commit f871114 into PaddlePaddle:develop Aug 1, 2025
98 of 109 checks passed
@DanielSun11 DanielSun11 deleted the fix_vec2params branch September 18, 2025 06:26
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.

3 participants