Skip to content

[paddle-adapt] model_optimizations: 690 PASS with torch.sort compat patch#24

Merged
BingooYang merged 1 commit into
PFCCLab:0.6from
BingooYang:adapt/model_optimizations
May 19, 2026
Merged

[paddle-adapt] model_optimizations: 690 PASS with torch.sort compat patch#24
BingooYang merged 1 commit into
PFCCLab:0.6from
BingooYang:adapt/model_optimizations

Conversation

@BingooYang
Copy link
Copy Markdown

📌 Description

Adapt tests/model_optimizations for Paddle compat mode.

The key fix is a monkey-patch for torch.sort in tests/model_optimizations/conftest.py, which addresses two Paddle compat issues:

  1. Paddle compat torch.sort returns values-only Tensor (not a (values, indices) namedtuple like PyTorch), causing ValueError: not enough values to unpack (expected 2, got 1)
  2. Paddle compat torch.sort does not accept dim= keyword argument (uses axis= internally), causing TypeError: paddle.sort() received unexpected keyword argument dim

The patch introduces a _SortResult class wrapping (values, indices) where indices are computed via argsort with stable=True to preserve correct bfloat16 tie-breaking semantics.

Result: 690 passed, 4164 skipped (SM architecture env-level), 0 failed

🔍 Related Issues

Part of the FlashInfer Paddle compat adaptation effort.

Changes

  • tests/model_optimizations/conftest.py (new): Paddle compat monkey-patches §44-§48 (assert_close bfloat16, torch.equal, tensor.multiply, clamp_min/max) + §51 (torch.sort)
  • scripts/paddle_all_test_cases.sh: Added tests/model_optimizations/ entry

🚀 Pull Request Checklist

✅ Pre-commit Checks

  • Installed pre-commit hooks
  • pre-commit run --all-files (all hooks passed)

🧪 Tests

  • tests/model_optimizations: 690 passed, 4164 skipped, 0 failed
  • Regression: tests/utils/test_topk.py 1335 PASS, tests/grouped_mm 4 PASS

Reviewer Notes

Key insight: torch.sort must use stable=True in argsort for bfloat16 cases. Without stable=True, many tied values in bfloat16 are broken differently than PyTorch, causing ~210 new failures. The fix must live in conftest.py (not inline in test files) to apply the stable sort globally.

Refs: MISMATCH_EXPERIMENT §51

…atch

- §51: torch.sort in Paddle compat returns values-only Tensor (no indices namedtuple);
  patch wraps result in _SortResult(values, indices) with stable=True argsort for
  correct bfloat16 tie-breaking semantics
- §51: torch.sort does not accept dim= kwarg in Paddle compat;
  patch accepts dim= and passes it as positional arg to bypass Paddle TypeError
- No existing test files modified; all fixes isolated in tests/model_optimizations/conftest.py
- Regression: tests/utils/test_topk.py 1335 PASS, tests/grouped_mm 4 PASS all still OK
- Result: 690 passed, 4164 skipped (env-level SM architecture constraints), 0 failed

Refs: MISMATCH_EXPERIMENT §51
@BingooYang BingooYang merged commit 2880251 into PFCCLab:0.6 May 19, 2026
2 checks passed
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.

1 participant