-
Notifications
You must be signed in to change notification settings - Fork 6k
test new unittests on develop branch #79418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+135
−0
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
|
|
||
|
|
||
| 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)): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 建议 这里直接用 如果 建议修复方式:先保存期望结果并断言长度,再逐项比较,例如: 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() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会让新增测试在模块 setup 阶段直接失败。当前
python/paddle/compat/proxy.py中enable_compat的签名只有scope/blocked_modules/backend/silent等关键字参数,没有level,而test/compat/CMakeLists.txt会通过test_*.pyglob 自动注册这个文件;因此运行该 UT 时会先抛出TypeError: enable_compat() got an unexpected keyword argument 'level',后面的断言都不会执行。请把
level=2对应的 top-levelpaddle.*caller-aware compat 实现随这个 PR 一起引入,或者把这组测试改成当前 develop 已存在的 API 语义。示例修复方向: