Skip to content

[Codegen] Add pass to further tile large linalg ops - #19010

Merged
qedawkins merged 2 commits into
iree-org:mainfrom
qedawkins:tile_large_tensors
Nov 8, 2024
Merged

qedawkins merged 2 commits into
iree-org:mainfrom
qedawkins:tile_large_tensors

Conversation

@qedawkins

Copy link
Copy Markdown
Contributor

In certain cases/configurations we can end up with excessively large static linalg.generic ops that will then vectorize and emit code with huge vector sizes. This at a minimum will yield terrible code, and can even crash/hang LLVM when trying to compile such an enormous function.

The implementation here only looks at parallel dims and linalg ops. Large reductions and other vectorizable ops will likely need handling as well, but "vectorizability" isn't really queryable today.

The pass has an options for a maximum vector size to tile to, but the logic here could potentially use some future improvement (e.g. maybe expose a target vector size too to use when we choose to tile).

Also note that in many cases where we hit this is likely to generate poor code because we can often end up with large local/stack allocations, but by the time we get to this point there isn't much else we can do. Better to ask for an allocation than 10k lines of unrolled IR.

@nirvedhmeshram nirvedhmeshram 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.

LGTM, with some minor nits.

@hanhanW hanhanW 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.

Drive by: I don't really like the "solution" that I'm going to suggest, but at least it should make user experience better. There is an option in GenericVectorization that allows you to not vectorize a linalg op when the total elements are greater than the threshold. The performance would be bad for sure, but it does not create huge vectors.

Option<"maxVectorSize", "max-vector-size", "int64_t",
/*default=*/"2147483647",
"Max vector size allowed to avoid creating large vectors.">

In certain cases/configurations we can end up with excessively large
static `linalg.generic` ops that will then vectorize and emit code with
huge vector sizes. This at a minimum will yield terrible code, and can
even crash/hang LLVM when trying to compile such an enormous function.

The implementation here only looks at parallel dims and linalg ops.
Large reductions and other vectorizable ops will likely need handling as
well, but "vectorizability" isn't really queryable today.

The pass has an options for a maximum vector size to tile to, but the
logic here could potentially use some future improvement (e.g. maybe
expose a target vector size too to use when we choose to tile).

Also note that in many cases where we hit this is likely to generate
poor code because we can often end up with large local/stack
allocations, but by the time we get to this point there isn't much else
we can do. Better to ask for an allocation than 10k lines of unrolled
IR.
@qedawkins

Copy link
Copy Markdown
Contributor Author

Drive by: I don't really like the "solution" that I'm going to suggest, but at least it should make user experience better. There is an option in GenericVectorization that allows you to not vectorize a linalg op when the total elements are greater than the threshold. The performance would be bad for sure, but it does not create huge vectors.

Option<"maxVectorSize", "max-vector-size", "int64_t",
/*default=*/"2147483647",
"Max vector size allowed to avoid creating large vectors.">

Hmmm that sounds like it's probably worth enabling as well. This pass should in theory also give us better vectorization of dynamic cases so I'm going to keep it for now. The example that this fixes is already encoded as a pipeline test anyway so we won't regress on the part that matters if we want to change it in the future.

@qedawkins
qedawkins enabled auto-merge (squash) November 8, 2024 13:23
@qedawkins
qedawkins merged commit d76d857 into iree-org:main Nov 8, 2024
@qedawkins
qedawkins deleted the tile_large_tensors branch November 8, 2024 14:10
Groverkss pushed a commit to Groverkss/iree that referenced this pull request Dec 1, 2024
In certain cases/configurations we can end up with excessively large
static `linalg.generic` ops that will then vectorize and emit code with
huge vector sizes. This at a minimum will yield terrible code, and can
even crash/hang LLVM when trying to compile such an enormous function.

The implementation here only looks at parallel dims and linalg ops.
Large reductions and other vectorizable ops will likely need handling as
well, but "vectorizability" isn't really queryable today.

The pass has an options for a maximum vector size to tile to, but the
logic here could potentially use some future improvement (e.g. maybe
expose a target vector size too to use when we choose to tile).

Also note that in many cases where we hit this is likely to generate
poor code because we can often end up with large local/stack
allocations, but by the time we get to this point there isn't much else
we can do. Better to ask for an allocation than 10k lines of unrolled
IR.
giacs-epic pushed a commit to giacs-epic/iree that referenced this pull request Dec 4, 2024
In certain cases/configurations we can end up with excessively large
static `linalg.generic` ops that will then vectorize and emit code with
huge vector sizes. This at a minimum will yield terrible code, and can
even crash/hang LLVM when trying to compile such an enormous function.

The implementation here only looks at parallel dims and linalg ops.
Large reductions and other vectorizable ops will likely need handling as
well, but "vectorizability" isn't really queryable today.

The pass has an options for a maximum vector size to tile to, but the
logic here could potentially use some future improvement (e.g. maybe
expose a target vector size too to use when we choose to tile).

Also note that in many cases where we hit this is likely to generate
poor code because we can often end up with large local/stack
allocations, but by the time we get to this point there isn't much else
we can do. Better to ask for an allocation than 10k lines of unrolled
IR.

Signed-off-by: Giacomo Serafini <[email protected]>
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.

3 participants