Skip to content

Validate the reduction argument in paddle.nn.functional.kl_div - #80032

Open
lanqing431 wants to merge 1 commit into
PaddlePaddle:developfrom
lanqing431:fix/kl-div-reduction-validation
Open

lanqing431 wants to merge 1 commit into
PaddlePaddle:developfrom
lanqing431:fix/kl-div-reduction-validation

Conversation

@lanqing431

Copy link
Copy Markdown

PR Category

User Experience

PR Types

Bug fixes

Description

Reject unsupported reduction strings in paddle.nn.functional.kl_div before constructing or executing a loss. The four documented values retain their existing behavior. Validation happens before dispatch, so it applies to eager and static use.

Both new regressions fail against the previous checkout implementation. All 26 cases from the checkout's test_kldiv_loss_op.py passed on Windows using the Paddle 3.3.1 CPU wheel and its matching test harness, with the checkout's kl_div and compatibility decorator loaded. Ruff lint and formatting checks passed.

Fixes #80010.

是否引起精度变化

否,合法 reduction 的计算路径保持不变。

@Paddle-Review-Bot

Paddle-Review-Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Paddle-Bot Review Board (review完成)

序号 位置 优先级 规则来源 状态

可维护性建议(总分 91.8 / 100,只给建议,不影响评审结论)

序号 位置 维度 状态
1 kl_div reduction 类型校验 错误处理一致性 💡
2 kl_div reduction 类型注解 类型安全 💡
各维度得分(仅评本 PR 改动,点击展开)
84 类型安全
2
86 错误处理一致性
1
87 API 表面一致性
88 约定偏离
88 中层优雅度
90 契约一致性
90 底层优雅度
90 测试策略
92 抽象适配度
92 逻辑清晰度
93 AI 生成债务
93 高层优雅度
95 跨模块架构
95 未完成迁移
95 命名质量
95 包组织结构
96 设计一致性
96 初始化耦合
100 依赖健康度
— 鉴权一致性

Powered by Nyanpasu claude with DeepSeek-V4.1-Flash 默认推理级别, please check the suggestions carefully.

True)

"""
check_type(reduction, 'reduction', str, 'kl_div')

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.

💡 可维护性建议(错误处理一致性,不阻塞合入):check_type 在动态图模式下会直接返回(见 python/paddle/base/data_feeder.py 中 check_type 开头的 if in_dygraph_mode(): return),因此这行类型校验实际只在静态图生效。结果是同一个非字符串 reduction(例如 reduction=1)在静态图抛 TypeError、在动态图则落到下一行成员检查抛 ValueError,异常类型随执行模式变化;而同文件其它 loss 基本只用一次成员检查统一抛 ValueError。建议改为与模式无关的显式判断(如 isinstance(reduction, str) 后统一抛 ValueError),或删除该行、仅保留成员检查。


"""
check_type(reduction, 'reduction', str, 'kl_div')
if reduction not in ['none', 'mean', 'sum', 'batchmean']:

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.

💡 可维护性建议(类型安全,不阻塞合入):新增的合法值列表把 'batchmean' 视为合法取值,但函数签名使用的 _ReduceMode(文件头第 46 行定义为 Literal['mean', 'sum', 'none'])并不包含 'batchmean',运行期契约与静态类型注解不一致(类型检查器会把文档示例中的 reduction='batchmean' 判为错误)。建议为 kl_div 单独定义包含 'batchmean' 的类型别名,并让注解与校验共用同一份允许值,避免两处并行定义继续漂移。

This branch has not been deployed

No deployments
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.

kl_div silently treats an unknown reduction as an unreduced result

2 participants