Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions test/compat/test_compat_internal_composites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""``enable_compat(level=2)`` is active for the whole module (a real user session).
Composite paddle APIs that internally call the aliased top-level names must keep
NATIVE behavior: caller-aware dispatch keeps paddle-internal callers on native, so
level=2 only changes the outward ``paddle.*`` surface. Internal call chains covered:

- vsplit / hsplit / dsplit / chunk -> paddle.split(num_or_sections=, axis=)
- quantile -> paddle.sort(x, axis)
- nan_to_num -> paddle.equal -> paddle.where
- histogram_bin_edges -> paddle.min / paddle.max
- F.nll_loss (ignore_index, mean) -> paddle.equal

Inputs are fixed (no RNG) and ops are lightweight so the file stays well under the
newly-added-UT CI budget (ctest --repeat-until-fail 3 --timeout 15).
"""

import unittest

import numpy as np

import paddle
import paddle.nn.functional as F


def setUpModule():
paddle.enable_compat(level=2)

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.

P0 优先级:P0

这里会让新增测试在模块 setup 阶段直接失败。当前 python/paddle/compat/proxy.pyenable_compat 的签名只有 scope/blocked_modules/backend/silent 等关键字参数,没有 level,而 test/compat/CMakeLists.txt 会通过 test_*.py glob 自动注册这个文件;因此运行该 UT 时会先抛出 TypeError: enable_compat() got an unexpected keyword argument 'level',后面的断言都不会执行。

请把 level=2 对应的 top-level paddle.* caller-aware compat 实现随这个 PR 一起引入,或者把这组测试改成当前 develop 已存在的 API 语义。示例修复方向:

# 方案一:先在实现侧支持该测试依赖的 API,并保证 level=2 会切换外部 paddle.* 表面
def enable_compat(
    *,
    level: int | None = None,
    scope: _ScopeType = None,
    blocked_modules: _ScopeType = None,
    backend: Literal["torch"] = "torch",
    silent: bool = False,
) -> None:
    ...

# 方案二:如果本 PR 只验证当前 develop,移除 level=2 假设,改用现有 paddle.compat.* / torch proxy 入口重写断言。

This comment was marked as outdated.



def tearDownModule():
paddle.disable_compat()


class TestCompatIsActuallyOn(unittest.TestCase):
"""Guard against a vacuous pass: this module is an external caller, so the
torch-aligned surface must be in effect here while the composites stay native."""

def test_external_surface_is_torch_style(self):
t = paddle.to_tensor([[3.0, 1.0, 2.0]])
self.assertIsInstance(
paddle.split(t, 1, dim=0), tuple
) # torch: chunk size
self.assertTrue(hasattr(paddle.sort(t, dim=-1), "values"))
with self.assertRaises(TypeError):
paddle.max(t, axis=1) # native kwarg rejected externally


class TestSplitFamilyStaysNative(unittest.TestCase):
"""vsplit/hsplit/dsplit/chunk internally call paddle.split with native
num_or_sections=/axis=; a compat-split leak would reinterpret those args."""

def test_vsplit(self):
x = np.arange(48, dtype="float32").reshape([4, 4, 3])
outs = paddle.vsplit(paddle.to_tensor(x), 2)
for o, r in zip(outs, np.array_split(x, 2, axis=0)):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 建议 这里直接用 zip(outs, np.array_split(...)) 会截断到较短序列,缺少对返回分片数量的校验。

如果 paddle.vsplit 少返回或多返回分片,只要已有前缀内容碰巧一致,这个测试仍可能通过;hsplitdsplit 里也有同样模式。

建议修复方式:先保存期望结果并断言长度,再逐项比较,例如:

expected = np.array_split(x, 2, axis=0)
self.assertEqual(len(outs), len(expected))
for o, r in zip(outs, expected):
    np.testing.assert_array_equal(o.numpy(), r)

np.testing.assert_array_equal(o.numpy(), r)

def test_hsplit(self):
x = np.arange(24, dtype="float32").reshape([4, 6])
outs = paddle.hsplit(paddle.to_tensor(x), 3)
for o, r in zip(outs, np.array_split(x, 3, axis=1)):
np.testing.assert_array_equal(o.numpy(), r)

def test_dsplit(self):
x = np.arange(48, dtype="float32").reshape([2, 4, 6])
outs = paddle.dsplit(paddle.to_tensor(x), 2)
for o, r in zip(outs, np.array_split(x, 2, axis=2)):
np.testing.assert_array_equal(o.numpy(), r)

def test_chunk(self):
# chunk: `chunks` is the chunk COUNT; a compat-split leak would read 3 as
# per-chunk size and fail the count/shape checks.
x = np.arange(18, dtype="float32").reshape([6, 3])
outs = paddle.chunk(paddle.to_tensor(x), 3, axis=0)
self.assertEqual(len(outs), 3)
for o, r in zip(outs, np.split(x, 3, axis=0)):
np.testing.assert_array_equal(o.numpy(), r)


class TestReduceAndCompareStayNative(unittest.TestCase):
def test_quantile_uses_native_sort(self):
# quantile internally calls paddle.sort(x, axis); a compat-sort leak would
# hand it a (values, indices) namedtuple instead of a tensor.
x = np.array(
[
[0.2, 0.7, 0.1, 0.4],
[1.0, 0.3, 0.8, 0.5],
[0.6, 0.9, 0.0, 0.25],
],
dtype="float32",
)
got = paddle.quantile(paddle.to_tensor(x), 0.35, axis=1)
np.testing.assert_allclose(
got.numpy(), np.quantile(x, 0.35, axis=1), rtol=1e-5
)

def test_nan_to_num_uses_native_equal(self):
# paddle.equal feeds paddle.where; compat.equal returns a python bool.
x = np.array([1.0, np.nan, np.inf, -np.inf, -2.5], dtype="float32")
got = paddle.nan_to_num(paddle.to_tensor(x), nan=0.5)
np.testing.assert_allclose(got.numpy(), np.nan_to_num(x, nan=0.5))

def test_histogram_bin_edges_uses_native_min_max(self):
x = np.array([0.0, 1.5, 3.0, 4.5, 6.0], dtype="float32")
got = paddle.histogram_bin_edges(paddle.to_tensor(x), bins=4)
np.testing.assert_allclose(
got.numpy(), np.histogram_bin_edges(x, bins=4), rtol=1e-6
)

def test_nll_loss_uses_native_equal(self):
# reduction='mean' + ignore_index takes the paddle.equal(count, 0.) path.
prob = np.array(
[
[0.70, 0.10, 0.10, 0.10],
[0.20, 0.50, 0.20, 0.10],
[0.10, 0.20, 0.60, 0.10],
[0.25, 0.25, 0.25, 0.25],
[0.10, 0.20, 0.20, 0.50],
],
dtype="float32",
)
logp = np.log(prob)
label = np.array([0, 1, 2, 1, 3], dtype="int64")
got = F.nll_loss(
paddle.to_tensor(logp),
paddle.to_tensor(label),
ignore_index=1,
reduction="mean",
)
keep = label != 1
ref = -logp[np.arange(5)[keep], label[keep]].sum() / keep.sum()
np.testing.assert_allclose(got.item(), ref, rtol=1e-5)


if __name__ == "__main__":
unittest.main()
Loading