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
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion paddle/fluid/pybind/sot/guards.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

if (expected_[i] && shape[i] != *expected_[i]) {
if (expected_[i] == -1 && shape[i] >= 1) continue;
if (shape[i] != expected_[i]) {
return false;
}
}
Expand Down
15 changes: 7 additions & 8 deletions paddle/fluid/pybind/sot/guards.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,13 @@ class DtypeMatchGuard : public GuardBase {

class ShapeMatchGuard : public GuardBase {
public:
explicit ShapeMatchGuard(const std::vector<std::optional<int64_t>>& shape)
: expected_(shape) {}

explicit ShapeMatchGuard(const std::vector<py::object>& shape) {
expected_.resize(shape.size());
for (size_t i = 0; i < shape.size(); ++i) {
if (py::isinstance<py::int_>(shape[i]) && shape[i].cast<int64_t>() > 0) {
expected_[i] = std::make_optional(shape[i].cast<int64_t>());
expected_.reserve(shape.size());
for (const auto& s : shape) {
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.

}
}
}
Expand All @@ -172,7 +171,7 @@ class ShapeMatchGuard : public GuardBase {
std::string get_guard_name() const override { return "ShapeMatchGuard"; }

private:
std::vector<std::optional<int64_t>> expected_;
std::vector<int64_t> expected_;
};

class AttributeMatchGuard : public GuardBase {
Expand Down
Loading