[Dynamic Shape] Use SizeVar instead of Var when convert Any in the GetShape function #8555
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background and Motivation
This is the PR for this RFC
Currently, TVM uses
Any
to represent an unknown dimension when the input has a dynamic shape. When building the schedule for a Relay primitive function, theAnyNode
will be converted to aVar
named"any_dim"
. However, an element in a shape array cannot be a negative number, and useVar
to represent shape array elements will cause redundant boundary check because we cannot deduce the sign of aVar
. For example, given a simple network with only a softmax operation likeThe corresponding tir looks like (here we show a part of the whole function since it is too long)
If you look carefully, you will find some unnecessary if-conditions like
floormod(floordiv(((blockIdx.x*512) + threadIdx.x), any_dim_2), any_dim_1) < any_dim_1
. This is becausefloormod
can have negative inputs. For example,y < floodmod(x, y) <= 0
ify < 0
. Since we do not know the sign ofany_dim_1
and the output offloodmod
will be greater thanany_dim_1
ifany_dim_1 < 0
, a redundant if-condition will be added here.Solution
I think currently we do not have the need for negative shape arrays. Therefore, perhaps using
SizeVar
instead ofVar
here is a better choice.