[API Compatibility] Align torch.distributions.constraints.positive_definite.check and torch.distributions.categorical.Categorical#79279
Conversation
CI报告基于以下代码生成(30分钟更新一次): 1 Required任务 : 41/48 通过
2 失败详情🔴 CI-H / Coverage build — 环境问题(置信度: 高)分析器: 通用分析(fallback)
关键日志:
Build 步骤根据 flashattn submodule hash 拼出 BCE BOS 缓存 URL,并在 HTTP 状态不是 200 时直接 修复建议:
关联变更: 未发现与 PR 修改文件直接相关;PR 修改集中在 🔴 Coverage / Coverage build — PR问题(置信度: 高)分析器: 通用分析(fallback)
关键日志:
该 job 的 修复建议:
关联变更: PR 新增 |
| return paddle.log(self.probs(value), name=name) | ||
|
|
||
|
|
||
| class _TorchCategorical(distribution.Distribution): |
There was a problem hiding this comment.
这个为啥要这么加一个API,能否兼容设计
就没这个改法
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #79279 +/- ##
==========================================
Coverage ? 97.29%
==========================================
Files ? 5
Lines ? 74
Branches ? 0
==========================================
Hits ? 72
Misses ? 2
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
/re-run all-failed |
| def __init__( | ||
| self, | ||
| logits: _CategoricalBoundary, | ||
| logits: _CategoricalBoundary | None = None, |
There was a problem hiding this comment.
这个如果第2个参数都是同一种类型,则无法兼容性设计
…finite.check and torch.distributions.categorical.Categorical
zhwesky2010
left a comment
There was a problem hiding this comment.
torch测试结果都验证对齐了吧,贴下截图
| return paddle.log(self.probs(value), name=name) | ||
|
|
||
|
|
||
| class _TorchCategorical(distribution.Distribution): |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from paddle.distribution.constraint import * # noqa: F403 |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from paddle.distribution.categorical import _TorchCategorical as Categorical |
|
@Le-soleile CI没有通过 |
|
/re-run all-failed |
|
/re-run all-failed |
| def __getattr__(name): | ||
| if name == "paddle_triton": | ||
| return paddle_triton_fun() | ||
| if name == "distributions": |
There was a problem hiding this comment.
应该是形成了循环,导入顺序有问题
| from .uniform import Uniform | ||
|
|
||
| constraints = constraint | ||
| _sys.modules[__name__ + '.constraints'] = constraints |
| from paddle.tensor import multinomial | ||
|
|
||
|
|
||
| class _IntegerInterval(constraint.Constraint): |
There was a problem hiding this comment.
这个也存在兼容性问题,需要放到compat目录下?
| from .uniform import Uniform | ||
|
|
||
| constraints = constraint | ||
| _sys.modules['paddle.distribution.constraints'] = constraints |
| 'Binomial', | ||
| 'Poisson', | ||
| 'StudentT', | ||
| 'constraint', |
| from paddle.tensor import multinomial | ||
|
|
||
|
|
||
| class _IntegerInterval(constraint.Constraint): |
There was a problem hiding this comment.
这个没有历史后向兼容性问题,用compat的目的是为了避免后向兼容问题。
paddle里面没这个,不会有历史兼容问题,为何要放到compat目录里?
| def __getattr__(name): | ||
| if name == "paddle_triton": | ||
| return paddle_triton_fun() | ||
| if name == "distributions": |
There was a problem hiding this comment.
importlib放这个分支里来,这个有缓存吗,需要缓存一下,第二次就不要importlib.import_module了
| 'Binomial', | ||
| 'Poisson', | ||
| 'StudentT', | ||
| 'constraints', |
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-06-19 01:02:23 Asia/Shanghai
📋 Review 摘要
PR 概述:为 torch 兼容路径补充 Categorical 行为,并调整 distributions constraints/positive_definite 检查。
变更范围:python/paddle/compat/distributions/、python/paddle/distribution/、相关 legacy tests
影响面 Tag:Python API Compatibility
问题
| 级别 | 文件 | 概述 |
|---|---|---|
| 🔴 Bug | python/paddle/compat/distributions/categorical.py:24 |
torch.distributions.categorical.Categorical 子模块路径仍会落到旧 Paddle 实现,probs / validate_args 兼容语义不可用 |
历史 Findings 修复情况
| Finding | 问题 | 状态 |
|---|---|---|
| F1 | 复数正定矩阵需要按 Hermitian 检查 | |
| F2 | validate_args=True 在兼容类中未完整生效 |
🔄 部分修复 |
| F3 | 静态图动态方阵维度下非方阵输入仍可能进入矩阵分支 | |
| F4 | constraints 仅为包属性,子模块导入路径未支持 |
📝 PR 规范检查
PR 描述结构符合模板,是否引起精度变化 已填写为 否。标题当前使用 [API Compatibility],该 Tag 不在 Paddle PR 模板枚举中;建议改为:
标题建议(可直接复制):
[User Experience] Align torch.distributions Categorical and positive_definite APIs
总体评价
新增 Categorical 主入口覆盖了大部分 probs / logits 行为,但 torch proxy 的子模块导入还没有接到该兼容实现。历史未解决项仍需要继续处理,尤其是 constraints 子模块导入和 positive_definite 的复数/静态图场景。
| from paddle.tensor import multinomial | ||
|
|
||
|
|
||
| class Categorical(distribution.Distribution): |
There was a problem hiding this comment.
🔴 Bug torch.distributions.categorical.Categorical 仍会落到旧的 Paddle Categorical。
enable_compat() 注册 torch proxy override 时只扫描 paddle.compat.* 中声明了 __all__ 的模块;当前只有 paddle.compat.distributions 包级别暴露了 Categorical,这个 categorical 子模块本身没有 __all__,所以 from torch.distributions.categorical import Categorical 会按 proxy 规则加载 paddle.distributions.categorical.Categorical,拿到旧构造函数 (logits, name=None),probs= / validate_args= 语义仍不可用。
建议修复方式:
在本模块增加 __all__ = ['Categorical'](必要时也把 categorical 加到包级 __all__),并补一个 paddle.enable_compat() 下的 from torch.distributions.categorical import Categorical 用例,确认 probs/logits 参数走到 compat 实现。




PR Category
User Experience
PR Types
New features
Description
对齐 Categorical 的 PyTorch 构造和核心行为:
- 支持 probs / logits / validate_args 参数语义。
- 保证 probs 和 logits 只能二选一。
- probs 按最后一维归一化。
- logits 使用 logsumexp 归一化。
- 对齐 probs、logits、mode、sample、log_prob、entropy 行为。
- 对 probs -> logits 增加 clamp,避免零概率场景下和 PyTorch 行为不一致。
- - -
对齐 constraints.positive_definite.check 调用路径:
- 新增 constraints 复数路径支持。
- 保持原正定判断逻辑。
- 修复静态图下 bool(Tensor) 判断不可用的问题,改为张量逻辑组合。
是否引起精度变化
否
😺