-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[TOPI] Use branchless boundary index for reflect/replicate pad #19928
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |||||
|
|
||||||
| import tvm | ||||||
| from tvm import te | ||||||
| from tvm.tirx import if_then_else | ||||||
|
|
||||||
| from .. import tag | ||||||
| from ..utils import equal_const_int | ||||||
|
|
@@ -211,15 +210,12 @@ def _pad(*indices): | |||||
|
|
||||||
| orig_idx = idx - before | ||||||
|
|
||||||
| reflected_idx = if_then_else( | ||||||
| orig_idx < 0, | ||||||
| -orig_idx, # reflect from start (no repeat) | ||||||
| if_then_else( | ||||||
| orig_idx >= size, | ||||||
| (2 * size - 2) - orig_idx, # reflect from end | ||||||
| orig_idx, | ||||||
| ), | ||||||
| ) | ||||||
| # Branchless reflect-101 boundary index. This is bit-identical to the | ||||||
| # nested if_then_else form (-orig_idx below 0, (2*size-2)-orig_idx at or | ||||||
| # above size, orig_idx otherwise) over the valid reflect-pad domain, but | ||||||
| # lowers to plain integer arithmetic instead of per-element branches. | ||||||
| m = size - 1 | ||||||
| reflected_idx = m - tvm.tirx.abs(m - tvm.tirx.abs(orig_idx)) | ||||||
| index_tuple.append(reflected_idx) | ||||||
| return data(*index_tuple) | ||||||
|
|
||||||
|
|
@@ -260,15 +256,10 @@ def _pad(*indices): | |||||
| before = pad_before[i] | ||||||
|
|
||||||
| orig_idx = idx - before | ||||||
| clamped_idx = if_then_else( | ||||||
| orig_idx < 0, | ||||||
| tvm.tirx.const(0, "int32"), # replicate first element | ||||||
| if_then_else( | ||||||
| orig_idx >= size, | ||||||
| size - 1, # replicate last element | ||||||
| orig_idx, | ||||||
| ), | ||||||
| ) | ||||||
| # Branchless edge clamp. This is bit-identical to the nested | ||||||
| # if_then_else form (0 below 0, size-1 at or above size, orig_idx | ||||||
| # otherwise) but lowers to min/max instead of per-element branches. | ||||||
| clamped_idx = tvm.tirx.max(tvm.tirx.const(0, "int32"), tvm.tirx.min(size - 1, orig_idx)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a hardcoded
Suggested change
|
||||||
| index_tuple.append(clamped_idx) | ||||||
| return data(*index_tuple) | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
size(which isdata.shape[i]) andorig_idxhave different integer types (e.g.,int32vsint64), the subtractionm - tvm.tirx.abs(orig_idx)can cause a type mismatch error during lowering. To ensure type safety and robustness, we should explicitly castmtoorig_idx.dtypeusingtvm.tirx.Cast.