Skip to content

[API Compatibility] Support legacy reduction args for BCE/MSE/multi_margin/nll_loss#78574

Open
Aidenwu0209 wants to merge 4 commits intoPaddlePaddle:developfrom
Aidenwu0209:api-compat-loss-change-prefix
Open

[API Compatibility] Support legacy reduction args for BCE/MSE/multi_margin/nll_loss#78574
Aidenwu0209 wants to merge 4 commits intoPaddlePaddle:developfrom
Aidenwu0209:api-compat-loss-change-prefix

Conversation

@Aidenwu0209
Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

New features

Description

Complete the functional-loss alignment needed for PaConvert prefix-only conversion on these APIs:

  • paddle.nn.functional.binary_cross_entropy
  • paddle.nn.functional.mse_loss
  • paddle.nn.functional.multi_margin_loss
  • paddle.nn.functional.nll_loss

This PR:

  • adds PyTorch legacy size_average / reduce compatibility while keeping Paddle's historical positional reduction calls available
  • keeps target alias usage aligned for these functional losses
  • adds dygraph/static regression tests for legacy args and positional call forms

Related issue: #76301 (No.154 / No.186 / No.187 / No.188)

????????

?

@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Apr 3, 2026

你的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
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 8 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@70e3d77). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/nn/functional/loss.py 77.77% 8 Missing ⚠️

❌ Your patch status has failed because the patch coverage (77.77%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #78574   +/-   ##
==========================================
  Coverage           ?   77.77%           
==========================================
  Files              ?        1           
  Lines              ?       36           
  Branches           ?        0           
==========================================
  Hits               ?       28           
  Misses             ?        8           
  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.

kIgnoreIndex = -100


def _resolve_legacy_reduction_args(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这个你修改原来的legacy_reduction_decorator、legacy_reduction_special_decorator,原本的代码上是warning,你改成直接设置size_average/reduce

return size_average, ignore_index, reduce, reduction


def dice_loss(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

只加装饰器,不改API签名,这两个废弃参数避免直接加到API上。

@@ -299,6 +300,157 @@ def test_BCELoss_target_alias_static(self):
)


class TestBCELossLegacyReductionArgs(unittest.TestCase):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

legacy_reduction_decorator、legacy_reduction_special_decorator涉及的十几个API都同等情况处理size_average/reduce测试,都加下

is_method = len(param_names) > 0 and param_names[0] == 'self'

@functools.wraps(init_func)
def wrapper(*args: _InputT.args, **kwargs: _InputT.kwargs) -> _RetT:
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 Apr 7, 2026

Choose a reason for hiding this comment

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

不要改这么多,人工开发找到最简最优的方案写法

直接把这里的raise_deprecated_error展开,并设置kwarg['reduce']=suggested 就可以了吧?

Image


wrapper.__signature__ = inspect.signature(init_func)
return wrapper
return _legacy_reduction_wrapper(init_func, special=True)
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 Apr 7, 2026

Choose a reason for hiding this comment

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

special的作用是



@legacy_reduction_decorator
@param_one_alias(["label", "target"])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这些别名装饰器融合到legacy_reduction_decorator里,每个API只配置一个装饰器

Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

这个代码是人写的还是AI写的?看起来把简单的事情复杂化了。

单测你可以用AI,但是要符合单测规范:

https://github.com/PaddlePaddle/docs/blob/develop/docs/dev_guides/coding_agent/api_compatibility/python-decorator.md#step-5-%E6%B7%BB%E5%8A%A0%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B

其他代码需要人先想清楚方案,并给出一定的约束规则,不是直接把需求丢进去让AI自由发挥。

这里其实可以复用之前很多逻辑,但是有大量的代码重构,有两个疑问:

  1. 这里的方案思路是?
  2. 选择不复用代码的原因是?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants