Skip to content

[SOT][FasterGuard] ShapeMatchGuard support dynamic shape #72564

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

gouzil
Copy link
Member

@gouzil gouzil commented Apr 30, 2025

PR Category

Performance Optimization

PR Types

Performance

Description

ShapeMatchGuard 支持动态 shape

Copy link

paddle-bot bot commented Apr 30, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Apr 30, 2025
@gouzil gouzil requested a review from Copilot April 30, 2025 08:38
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the ShapeMatchGuard to support dynamic shapes for performance optimizations.

  • Refactors the constructor to accept a vector of py::object and converts it to int64_t values.
  • Modifies the shape-checking logic in the guard implementation.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
paddle/fluid/pybind/sot/guards.h Adjusted constructor and changed internal type usage.
paddle/fluid/pybind/sot/guards.cc Updated shape checking logic to validate non-positive dimensions.

@@ -141,7 +141,7 @@ bool ShapeMatchGuard::check(PyObject* value) {
return false;
}
for (size_t i = 0; i < shape.size(); ++i) {
if (expected_[i] && shape[i] != *expected_[i]) {
if (shape[i] <= 0 || shape[i] != expected_[i]) {
Copy link
Preview

Copilot AI Apr 30, 2025

Choose a reason for hiding this comment

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

The updated condition checks for non-positive values and fails dynamic shapes that could be represented by a negative or zero value. To support dynamic shapes, consider revising the condition to allow for dynamic dimensions (e.g., checking if expected_[i] is set to a dynamic indicator such as -1 before comparing equality).

Suggested change
if (shape[i] <= 0 || shape[i] != expected_[i]) {
if (shape[i] <= 0 || (expected_[i] != -1 && shape[i] != expected_[i])) {

Copilot uses AI. Check for mistakes.

@gouzil gouzil requested a review from Copilot May 6, 2025 08:43
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds dynamic shape support to the ShapeMatchGuard, improving performance by allowing flexible shape validation.

  • Change the ShapeMatchGuard constructor to accept a vector of py::object and represent dynamic dimensions with a sentinel value.
  • Replace the std::optional<int64_t> storage with std::vector<int64_t>, using -1 as the indicator for dynamic dimensions.
  • Update the shape-check logic in guards.cc accordingly.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
paddle/fluid/pybind/sot/guards.h Updated constructor and internal representation to support dynamic shape
paddle/fluid/pybind/sot/guards.cc Modified shape checking logic to accommodate dynamic shape representation

if (py::isinstance<py::int_>(s)) {
expected_.push_back(s.cast<int64_t>());
} else {
expected_.push_back(-1);
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider replacing the magic number -1 with a named constant (e.g., kDynamicDimension) to improve code readability and maintainability.

Suggested change
expected_.push_back(-1);
expected_.push_back(kDynamicDimension);

Copilot uses AI. Check for mistakes.

@@ -141,7 +141,8 @@ bool ShapeMatchGuard::check(PyObject* value) {
return false;
}
for (size_t i = 0; i < shape.size(); ++i) {
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

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

[nitpick] Adding a brief comment clarifying that -1 is used as a sentinel for dynamic dimensions would help maintainers understand the control flow.

Suggested change
for (size_t i = 0; i < shape.size(); ++i) {
for (size_t i = 0; i < shape.size(); ++i) {
// -1 is used as a sentinel value for dynamic dimensions, allowing any positive size to match.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant