Skip to content

Conversation

@cangtianhuang
Copy link
Contributor

@cangtianhuang cangtianhuang commented Jul 11, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

Modification:

  1. Change the parameter k signature from int to int64_t. Preposition PR:
  1. Fix paddle/fluid/framework/infershape_utils.ccpaddle/fluid/framework/operator.cc to support int to int64_t , referring to the handling of vector<int> : https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/framework/infershape_utils.cc#L825

Correctness Test:

x_np = np.random.random(4294967295) - 0.5 k = 4200000000 x = paddle.to_tensor(x_np, dtype="float32") result = paddle.kthvalue(x, k) x = torch.tensor(x_np, dtype=torch.float32) result = torch.kthvalue(x, k)
image

Now paddle.kthvalue supports int64_t for the parameter k, and computes faster than torch. Paddle return the first index of the k-th value, whereas torch return it randomly.

Compatibility Test:

To test whether the models of old/new IR can be loaded before and after modification (using paddle daily build to save the models)

import paddle pir_path = "models/kthvalue_pir" legacy_ir_path = "models/kthvalue_legacy_ir" class KthValueModel(paddle.nn.Layer): def __init__(self): super().__init__() def forward(self, x): values, indices = paddle.kthvalue(x, 2) return values, indices model = KthValueModel() # paddle.jit.save( # model, # pir_path, # input_spec=[ # paddle.static.InputSpec(shape=[None, None], dtype='float32', name='x'), # ], # ) # with paddle.pir_utils.DygraphOldIrGuard(): # paddle.jit.save( # model, # legacy_ir_path, # input_spec=[ # paddle.static.InputSpec( # shape=[None, None], dtype='float32', name='x' # ), # ], # ) x = paddle.randn([4, 6], dtype='float32') load_model_pir = paddle.jit.load(pir_path) out = load_model_pir(x) print(out) with paddle.pir_utils.DygraphOldIrGuard(): load_model_legacy_ir = paddle.jit.load(legacy_ir_path) out_legacy_ir = load_model_legacy_ir(x) print(out_legacy_ir) 

new IR report error:
image

It can be addressed for Release version by changing pir patch version:

old IR pass:
image

Pcard-85711

@paddle-bot
Copy link

paddle-bot bot commented Jul 11, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

lshpku
lshpku previously approved these changes Jul 16, 2025
wanghuancoder
wanghuancoder previously approved these changes Jul 16, 2025
Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM 这个PR再找黄济懿、陈志扬帮忙看看

changeyoung98
changeyoung98 previously approved these changes Jul 16, 2025
@huangjiyi
Copy link
Member

另外 Static-Check 的 approval 问题需要解决一下,旧 IR 的 Op 定义是不希望改变的,同样参考 #72639 里在 static_ops.yaml, static_backward.yaml, ops_exclude.yamllp_pool 的改动

@cangtianhuang
Copy link
Contributor Author

另外 Static-Check 的 approval 问题需要解决一下,旧 IR 的 Op 定义是不希望改变的,同样参考 #72639 里在 static_ops.yaml, static_backward.yaml, ops_exclude.yamllp_pool 的改动

done

huangjiyi
huangjiyi previously approved these changes Jul 16, 2025
Copy link
Member

@huangjiyi huangjiyi left a comment

Choose a reason for hiding this comment

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

LGTM

changeyoung98
changeyoung98 previously approved these changes Jul 16, 2025
@cangtianhuang cangtianhuang dismissed stale reviews from changeyoung98 and huangjiyi via de94393 July 16, 2025 07:21
@cangtianhuang cangtianhuang marked this pull request as draft July 16, 2025 09:27
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 82.60870% with 4 lines in your changes missing coverage. Please review.

Please upload report for BASE (develop@b8b4c24). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddle/fluid/framework/infershape_utils.cc 83.33% 2 Missing ⚠️
paddle/fluid/framework/operator.cc 81.81% 2 Missing ⚠️

❌ Your patch status has failed because the patch coverage (82.60%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@ Coverage Diff @@ ## develop #74002 +/- ## ========================================== Coverage ? 82.60% ========================================== Files ? 2 Lines ? 23 Branches ? 0 ========================================== Hits ? 19 Misses ? 4 Partials ? 0 

☔ View full report in Codecov by Sentry.
📢 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.
@cangtianhuang cangtianhuang marked this pull request as ready for review July 21, 2025 06:48
Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@huangjiyi huangjiyi left a comment

Choose a reason for hiding this comment

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

LGTM

@cangtianhuang
Copy link
Contributor Author

/re-run all-failed

@wanghuancoder wanghuancoder merged commit 19e0454 into PaddlePaddle:develop Jul 21, 2025
76 of 79 checks passed
@cangtianhuang cangtianhuang deleted the fix-kthvalue branch July 26, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment