Skip to content

[Relax] Fix divide-by-zero in reshape pattern detection#19958

Open
guan404ming wants to merge 1 commit into
apache:mainfrom
guan404ming:fix-17745-reshape-zero-dim
Open

[Relax] Fix divide-by-zero in reshape pattern detection#19958
guan404ming wants to merge 1 commit into
apache:mainfrom
guan404ming:fix-17745-reshape-zero-dim

Conversation

@guan404ming

@guan404ming guan404ming commented Jul 6, 2026

Copy link
Copy Markdown
Member

Why

Fixes #17745. has_reshape_pattern builds an inverse index map that divides by each iter extent, so a zero-extent iter crashed with divide-by-zero.

How

  • Skip the fused-var check when any block iter has zero extent; such blocks touch no elements, so they are not reshape patterns.
  • Added test_reshape_pattern_zero_extent in tests/python/relax/test_analysis.py.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request prevents division-by-zero crashes during reshape pattern analysis by skipping checks on blocks with zero-extent iterators, and adds a unit test to verify this behavior. The reviewer suggested using the analyzer's CanProveEqual method instead of is_zero to robustly handle symbolic zero extents as well as constant ones.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +483 to +484
bool has_zero_extent = std::any_of(block->iter_vars.begin(), block->iter_vars.end(),
[](const IterVar& v) { return is_zero(v->dom->extent); });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using is_zero only checks if the extent is a constant integer 0. If the extent is a symbolic expression that can be proven to be 0 by the analyzer, is_zero will return false, which could still lead to a division-by-zero crash during the subsequent inverse index map simplification.

Since the analyzer ana_ is already available, we can use this->ana_->CanProveEqual(v->dom->extent, 0) to robustly handle both constant and symbolic zero extents.

Suggested change
bool has_zero_extent = std::any_of(block->iter_vars.begin(), block->iter_vars.end(),
[](const IterVar& v) { return is_zero(v->dom->extent); });
bool has_zero_extent = std::any_of(block->iter_vars.begin(), block->iter_vars.end(),
[this](const IterVar& v) { return this->ana_->CanProveEqual(v->dom->extent, 0); });

@guan404ming guan404ming force-pushed the fix-17745-reshape-zero-dim branch from 9f3ab2a to 141d4fb Compare July 6, 2026 12:55
@guan404ming guan404ming force-pushed the fix-17745-reshape-zero-dim branch from 141d4fb to c0fc7be Compare July 7, 2026 03:02
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.

[Bug] Constant folding cannot process onnx model correctly: InternalError: Check failed: pb->value != 0 (0 vs. 0) : Divide by zero

1 participant