Skip to content

[API Compatibility] align the loss apis --part#78811

Open
Manfredss wants to merge 11 commits into
PaddlePaddle:developfrom
Manfredss:ApiEnhance_loss
Open

[API Compatibility] align the loss apis --part#78811
Manfredss wants to merge 11 commits into
PaddlePaddle:developfrom
Manfredss:ApiEnhance_loss

Conversation

@Manfredss
Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

New features

Description

align the loss apis in nn.layer and nn.functional

是否引起精度变化

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Apr 27, 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.

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.

这些没对齐的关键是 https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/utils/decorator_utils.py#L801

Pytorch支持size_average、reduce的用法,用PaConvert先测试看看。

np.testing.assert_allclose(fetches[0], fetches[i], rtol=1e-5)


class TestL1LossAPI(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.

测下这些用法:size_average、reduce

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 27, 2026

Codecov Report

❌ Patch coverage is 97.19626% with 3 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@1ec99a3). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/utils/decorator_utils.py 95.58% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #78811   +/-   ##
==========================================
  Coverage           ?   97.19%           
==========================================
  Files              ?        3           
  Lines              ?      107           
  Branches           ?        0           
==========================================
  Hits               ?      104           
  Misses             ?        3           
  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.

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Comment thread python/paddle/utils/decorator_utils.py Outdated
@@ -865,37 +989,87 @@ def legacy_reduction_special_decorator(
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.

这些装饰器体系有些复杂了,尽可能把这些装饰器合起来,代码复用:

  1. 别名装饰器通过配置来解决,每个API只能有一个装饰器
  2. func和layer的装饰器能否合并?

_consume_legacy_positional_args(cls_name, args, kwargs)
)
):
raise_deprecated_error(cls_name, reduce_val, size_avg_val)
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 Apr 28, 2026

Choose a reason for hiding this comment

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

看一看怎么在之前的基础上进行最小改动:只在之前的基础上设置下suggested,将raise_deprecated_error换个名字。

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss
Copy link
Copy Markdown
Contributor Author

Manfredss commented May 2, 2026

Paddle 与 PyTorch 在 loss API 上的不对齐主要分 5 类:

  • (1) 已废弃的 size_average/reduce 参数 PyTorch 仍接受并翻译为 reduction=,Paddle 之前直接报错;
  • (2) PyTorch 的位置布局比 Paddle 多 size_average/reduce 两个位置,且后续参数顺序也不同(如 CrossEntropyLossreduction 之后还插了 soft_label/axis/use_softmax);
  • (3) 参数名差异(target vs labelbeta vs deltaeps vs epsilonanchor vs input 等);
  • (4) API名差异(multilabel_* vs multi_label_*);
  • (5) smooth_l1_loss 默认公式不同(PyTorch 是非 Huber,Paddle is_huber=True 是 Huber)。

想法是把第 (1)(2)(3) 类一次性折叠到一个机制里:在 decorator_utils.py 中维护一张 PYTORCH_LOSS_LAYOUT 表,记录每个 loss API 位置布局;legacy_reduction_decorator 在入口处检查 size_average 槽是否为 bool/None,若是则判定为 PyTorch 风格调用,按 PyTorch 名字把所有位置参数转成 kwargs,再 pop 掉 size_average/reduce 翻译成 reduction= 并 raise DeprecationWarning;否则视作 Paddle 内部调用(如 layer.forwardfunctional),原样透传不动。这样位置歧义、内部调用冲突、参数重命名(交给下游的 param_one_alias)三个问题在同一处统一解决

第 (4) 类靠 paddle.nn.functional.__init__ 里 4 行模块级别名解决;第 (5) 类用一个 11 行的 smooth_l1_beta_compat 装饰器:检测到 beta 时同时把 is_huber 默认设成 False,保留 Paddle 原有 delta 调用方的 Huber 默认。配套地,把 PoissonNLLLossTripletMarginLoss 两个 layer 上 @param_one_alias(["epsilon", "eps"])@legacy_reduction_decorator 的顺序对调(前者改到内层), 这样位置参数到关键字参数转换出来的 eps 才能被 param_one_alias 接力改名为 epsilon

@zhwesky2010
Copy link
Copy Markdown
Contributor

zhwesky2010 commented May 7, 2026

Paddle 与 PyTorch 在 loss API 上的不对齐主要分 5 类:

  • (1) 已废弃的 size_average/reduce 参数 PyTorch 仍接受并翻译为 reduction=,Paddle 之前直接报错;
  • (2) PyTorch 的位置布局比 Paddle 多 size_average/reduce 两个位置,且后续参数顺序也不同(如 CrossEntropyLossreduction 之后还插了 soft_label/axis/use_softmax);
  • (3) 参数名差异(target vs labelbeta vs deltaeps vs epsilonanchor vs input 等);
  • (4) API名差异(multilabel_* vs multi_label_*);
  • (5) smooth_l1_loss 默认公式不同(PyTorch 是非 Huber,Paddle is_huber=True 是 Huber)。

想法是把第 (1)(2)(3) 类一次性折叠到一个机制里:在 decorator_utils.py 中维护一张 PYTORCH_LOSS_LAYOUT 表,记录每个 loss API 位置布局;legacy_reduction_decorator 在入口处检查 size_average 槽是否为 bool/None,若是则判定为 PyTorch 风格调用,按 PyTorch 名字把所有位置参数转成 kwargs,再 pop 掉 size_average/reduce 翻译成 reduction= 并 raise DeprecationWarning;否则视作 Paddle 内部调用(如 layer.forwardfunctional),原样透传不动。这样位置歧义、内部调用冲突、参数重命名(交给下游的 param_one_alias)三个问题在同一处统一解决

第 (4) 类靠 paddle.nn.functional.__init__ 里 4 行模块级别名解决;第 (5) 类用一个 11 行的 smooth_l1_beta_compat 装饰器:检测到 beta 时同时把 is_huber 默认设成 False,保留 Paddle 原有 delta 调用方的 Huber 默认。配套地,把 PoissonNLLLossTripletMarginLoss 两个 layer 上 @param_one_alias(["epsilon", "eps"])@legacy_reduction_decorator 的顺序对调(前者改到内层), 这样位置参数到关键字参数转换出来的 eps 才能被 param_one_alias 接力改名为 epsilon

方案基本没问题,可以再简化下:

  • 第1~3类,给每个loss API维护一个别名表,也记录到LEGACY_POS中。对于关键字用法,则pop size_average/reduce,设置reduction,并修改别名。对于位置用法,由于目前的别名都是对应到了相同位置,应该别名不用做任何处理,而size_average/reduce直接去除,后面的参数位置参数也能对上,所以别名只需要删除size_average/reduce应该就可以?试试有无反例
  • 第4类,处理好multi_label_的差异,然后新增API别名
  • 第5类,单独处理,单独一个装饰器,参考index_add_decortor,看能否通过关键字与位置类型来区分出两者不同签名

…h<->Paddle 参数别名 合并到一张 ``LEGACY_POS`` 表中,由 ``legacy_reduction_decorator`` 一次性完成位置 -> 关键字、关键字别名重命名、``size_average/reduce`` → ``reduction`` 的全部转换;同时移除,loss API 上多余的 ``@param_one_alias`` / ``@param_two_alias`` / ``@ParamAliasDecorator`` 装饰器
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

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.

减少下注释吧。风格参考:
https://github.com/pytorch/pytorch/blob/main/CLAUDE.md#coding-style-guidelines

代码自身即为注释

Comment thread python/paddle/utils/decorator_utils.py Outdated
return decorator


# PyTorch positional layout for each loss API. Each entry maps the API
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.

注释可以不用写太多(只写必要的注释),从代码可读性与简洁的角度来设计代码,参考

https://github.com/pytorch/pytorch/blob/main/CLAUDE.md#coding-style-guidelines

Comment thread python/paddle/utils/decorator_utils.py Outdated
return len(use_args) > idx and use_args[idx] in allowed


def legacy_reduction_decorator(fn=None, *, is_method=True):
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.

这个实现看起来也挺复杂的,那还是用第一版实现吧:

不用维护_SA0_RD1这些信息了,每个loss API维护一个args_list+kwargs_change,直接按args_list匹配然后pop掉size_average/reduce,设置reduction,修改别名即可。

相当于paconvert的逻辑搬过来

Comment thread python/paddle/nn/layer/loss.py Outdated
"""

@legacy_reduction_decorator
@param_one_alias(["epsilon", "eps"])
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只允许一个装饰器

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