Skip to content

[API Compatibility] Align torch.distributions.constraints.positive_definite.check and torch.distributions.categorical.Categorical#79279

Open
Le-soleile wants to merge 10 commits into
PaddlePaddle:developfrom
Le-soleile:distri
Open

[API Compatibility] Align torch.distributions.constraints.positive_definite.check and torch.distributions.categorical.Categorical#79279
Le-soleile wants to merge 10 commits into
PaddlePaddle:developfrom
Le-soleile:distri

Conversation

@Le-soleile

@Le-soleile Le-soleile commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

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) 判断不可用的问题,改为张量逻辑组合。

是否引起精度变化

😺

@PaddlePaddle-bot

PaddlePaddle-bot commented Jun 8, 2026

Copy link
Copy Markdown

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-06-19 02:49:35 UTC+08:00

CI报告基于以下代码生成(30分钟更新一次):
PR commit: 0671ba0 | Merge base: 0027223 (branch: develop)


1 Required任务 : 41/48 通过

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
80(0) 80 73 2 0 0 5
任务 错误类型 置信度 日志
CI-H / Coverage build 环境问题 Job
Coverage / Coverage build PR问题 Job

2 失败详情

🔴 CI-H / Coverage build — 环境问题(置信度: 高)

分析器: 通用分析(fallback)
失败用例: 无,Coverage build 在构建准备阶段失败

用例 错误摘要
Build step flash-attention 预编译缓存 URL 不存在,脚本主动退出

关键日志:

flashattn_version:1f3e4bb9aaa0dbefa96181bf6026e810f3ebecc1
url:https://xly-devops.bj.bcebos.com/gpups/flash-attention/cu90/flashattn_libs_1f3e4bb9aaa0dbefa96181bf6026e810f3ebecc1.tar
flashattn cache not found, please contact umiswing
##[error]Process completed with exit code 7.
  • 根因摘要: flash-attention 预编译缓存缺失

Build 步骤根据 flashattn submodule hash 拼出 BCE BOS 缓存 URL,并在 HTTP 状态不是 200 时直接 exit 7。PR diff 只包含 Python API/测试变更,未修改 flash-attention submodule 或 CI 下载脚本,因此该失败与本 PR 代码变更无直接关系。

修复建议:

  1. 环境问题,请 rerun;若仍失败,需要维护者补齐 flashattn_libs_1f3e4bb9aaa0dbefa96181bf6026e810f3ebecc1.tar 缓存或修复缓存地址。

关联变更: 未发现与 PR 修改文件直接相关;PR 修改集中在 python/paddle/compat/distributions/*python/paddle/distribution/constraint.py 和兼容性测试。

🔴 Coverage / Coverage build — PR问题(置信度: 高)

分析器: 通用分析(fallback)
失败用例: 无,构建成功后在 coverage build size 审批门禁失败

用例 错误摘要
Check coverage build size requires approval step release paddlepaddle coverage build size 增长超过 3G,且当前 PR 未获得指定 RD approval

关键日志:

current pr 79279 got approvals: FALSE
This PR make the release paddlepaddle coverage build size growth exceeds 3 G, please explain why your PR exceeds 3G to ext_ppee@baidu.com and in PR description.
Then you must have one RD (swgu98 (Recommend) or luotao1 or risemeup1) approval for this PR
##[error]Process completed with exit code 6.
  • 根因摘要: coverage 构建产物增量超 3G 且缺少 RD approval

该 job 的 BuildCheck added unittests 已通过,失败发生在 size approval 门禁。日志明确说明当前 PR 79279 的 release paddlepaddle coverage build size growth 超过 3G,且 got approvals: FALSE,因此需要按门禁要求补充说明并取得指定 RD 审批。

修复建议:

  1. 在 PR 描述中解释 coverage build size 超过 3G 的原因,并联系 ext_ppee@baidu.com
  2. 获取 swgu98(推荐)、luotao1risemeup1 中任一 RD approval;若 size 增长非预期,优先排查新增兼容模块/测试是否引入异常构建产物。

关联变更: PR 新增 python/paddle/compat/distributions/categorical.pypython/paddle/compat/distributions/__init__.py,并在 test/legacy_test/test_api_compatibility_part3.py 增加大量兼容性测试;日志未指向单个源码错误,而是指向 coverage size 审批门禁。

@paddle-bot paddle-bot Bot added the contributor External developers label Jun 8, 2026
return paddle.log(self.probs(value), name=name)


class _TorchCategorical(distribution.Distribution):

@zhwesky2010 zhwesky2010 Jun 9, 2026

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,能否兼容设计

就没这个改法

阅读 https://github.com/PaddlePaddle/docs/blob/develop/docs/dev_guides/coding_agent/api_compatibility/.claude/skills/api-change-decider/SKILL.md 找解决方案

@codecov-commenter

codecov-commenter commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.29730% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@0027223). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/compat/__init__.py 75.00% 1 Missing ⚠️
python/paddle/compat/distributions/categorical.py 98.24% 1 Missing ⚠️
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.
📢 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.

@Le-soleile

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

PaddlePaddle-bot

This comment was marked as outdated.

PaddlePaddle-bot

This comment was marked as outdated.

def __init__(
self,
logits: _CategoricalBoundary,
logits: _CategoricalBoundary | None = None,

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.

这个如果第2个参数都是同一种类型,则无法兼容性设计

…finite.check and torch.distributions.categorical.Categorical

@zhwesky2010 zhwesky2010 left a comment

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.

torch测试结果都验证对齐了吧,贴下截图

return paddle.log(self.probs(value), name=name)


class _TorchCategorical(distribution.Distribution):

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.

这个不要这么命令,直接按正常命名

# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.distribution.constraint import * # noqa: F403

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.

这个是路径别名吗,有简单实现方式吗


from __future__ import annotations

from paddle.distribution.categorical import _TorchCategorical as Categorical

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.

正常取名字

@zhwesky2010

Copy link
Copy Markdown
Contributor

@Le-soleile CI没有通过

PaddlePaddle-bot

This comment was marked as outdated.

@Le-soleile

Copy link
Copy Markdown
Contributor Author
image

PaddlePaddle-bot

This comment was marked as outdated.

@Le-soleile

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Le-soleile

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

PaddlePaddle-bot

This comment was marked as outdated.

def __getattr__(name):
if name == "paddle_triton":
return paddle_triton_fun()
if name == "distributions":

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.

这个为什么要整这个操作,正常导入不行吗

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

正常导入会有报错
image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

应该是形成了循环,导入顺序有问题

Comment thread python/paddle/distribution/__init__.py Outdated
from .uniform import Uniform

constraints = constraint
_sys.modules[__name__ + '.constraints'] = constraints

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.

这个__name__展开吧

PaddlePaddle-bot

This comment was marked as outdated.

PaddlePaddle-bot

This comment was marked as outdated.

PaddlePaddle-bot

This comment was marked as outdated.

Comment thread python/paddle/compat/__init__.py
from paddle.tensor import multinomial


class _IntegerInterval(constraint.Constraint):

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.

这个也存在兼容性问题,需要放到compat目录下?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

image 这个就为了支持support,torch里面有_IntegerInterval 私有实现,paddle里面没有这个 image

Comment thread python/paddle/distribution/__init__.py Outdated
from .uniform import Uniform

constraints = constraint
_sys.modules['paddle.distribution.constraints'] = constraints

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.

这个还需要吗做路径别名吗,这个是原路径吧

Comment thread python/paddle/distribution/__init__.py Outdated
'Binomial',
'Poisson',
'StudentT',
'constraint',

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.

导出constraints就行

from paddle.tensor import multinomial


class _IntegerInterval(constraint.Constraint):

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.

这个没有历史后向兼容性问题,用compat的目的是为了避免后向兼容问题。

paddle里面没这个,不会有历史兼容问题,为何要放到compat目录里?

def __getattr__(name):
if name == "paddle_triton":
return paddle_triton_fun()
if name == "distributions":

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.

importlib放这个分支里来,这个有缓存吗,需要缓存一下,第二次就不要importlib.import_module了

PaddlePaddle-bot

This comment was marked as outdated.

Comment thread python/paddle/distribution/__init__.py Outdated
'Binomial',
'Poisson',
'StudentT',
'constraints',

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.

调用路径不用写到__all__里

PaddlePaddle-bot

This comment was marked as outdated.

zhwesky2010
zhwesky2010 previously approved these changes Jun 18, 2026

@zhwesky2010 zhwesky2010 left a comment

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.

LGTM

@PaddlePaddle-bot PaddlePaddle-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 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
影响面 TagPython 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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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 实现。

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.

4 participants