Skip to content
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

If Branch Constant Folding #18105

Merged
merged 65 commits into from
Nov 14, 2023
Merged

If Branch Constant Folding #18105

merged 65 commits into from
Nov 14, 2023

Conversation

yuslepukhin
Copy link
Member

@yuslepukhin yuslepukhin commented Oct 26, 2023

Description

When and if If condition proves to be a constant value, inline the corresponding subgraph yielding to more constant folding and optimization.

Motivation and Context

Newly converted models feature lots of nested If nodes that can be inlined and collapsed.

In particular, for the sample models we are gaining on TorchScript exported models.
For HF Mobile Bert Dynamo runtime went down from 0.069 -> 0.046. In total, AOT inlining + If constant folding
yields improvement of about 50% 0.102 -> 0.046. Brining us very close to TorchScript exported models.

HF Bart Dynamo further improves 0.668 -> 0.45. AOT + If constant folding improves 0.98 -> 0.45

Earlier the size of
HF Mobile Bert 161Mb+, now 98Mb
HF Bart Dynamo pre-optimized model was about 1.2Gb. It is now 710MB

image

Complete Finalize
Currently, there is a problem QDQ pairing transformer that ensures pairing between
QDQ pairs including within the graph.
  Change the type of the templates map so memory is partially released
  when not all of the functions are removed.
  Change the inline naming schema with slashes.
  Change the type of the templates map so memory is partially released
  when not all of the functions are removed.
  Change the inline naming schema with slashes.
  Add tests
  Rename the function.
  Remove functions directly from the partitioner.
gramalingam
gramalingam previously approved these changes Nov 8, 2023
edgchen1
edgchen1 previously approved these changes Nov 8, 2023
@snnn
Copy link
Member

snnn commented Nov 8, 2023

/azp run Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, orttraining-ortmodule-distributed

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

  when GraphViewer is not able to topologically sort new nodes
  due to lack of edges. We generate edges and make sure that
  nodes with subgraphs get implicit inputs setup.
@yuslepukhin yuslepukhin dismissed stale reviews from edgchen1 and gramalingam via 60d99fb November 10, 2023 01:36
Copy link
Contributor

@pranavsharma pranavsharma left a comment

Choose a reason for hiding this comment

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

Some minor comments.

include/onnxruntime/core/graph/graph.h Show resolved Hide resolved
include/onnxruntime/core/graph/graph.h Show resolved Hide resolved
onnxruntime/core/graph/graph.cc Show resolved Hide resolved
onnxruntime/core/graph/graph.cc Show resolved Hide resolved
@yuslepukhin yuslepukhin merged commit f19c673 into main Nov 14, 2023
84 of 89 checks passed
@yuslepukhin yuslepukhin deleted the yuslepukhin/if_constant_folding branch November 14, 2023 01:33
petermcaughan pushed a commit that referenced this pull request Nov 20, 2023
…args (#18462)

### Description
Truncate traling non-existing arguments.
  Make sure we do not skip on the non-existing arguments in the middle,
  because shape inferece relies on their proper position.
This also affects the argument position in the Edges that must be
properly rebuilt
  each time If node branch is inlined.
Make sure that when we rename Defs in subgraphs, new renamed defs are
created in those subgraphs
  instead of pointing to outer scope defs.
  Add unit test.

### Motivation and Context
This is a follow up for
#18105
Currently, the non-trailing arguments are simply ignored and the edges
are created
with potentially incorrect positions.
kleiti pushed a commit to kleiti/onnxruntime that referenced this pull request Mar 22, 2024
### Description

When and if `If` condition proves to be a constant value, inline the
corresponding subgraph yielding to more constant folding and
optimization.

### Motivation and Context
Newly converted models feature lots of nested `If` nodes that can be
inlined and collapsed.

In particular, for the sample models we are gaining on TorchScript
exported models.
For `HF Mobile Bert Dynamo` runtime went down from 0.069 -> 0.046. In
total, AOT inlining + `If` constant folding
yields improvement of about 50% 0.102 -> 0.046. Brining us very close to
TorchScript exported models.

`HF Bart Dynamo` further improves 0.668 -> 0.45. AOT + `If` constant
folding improves 0.98 -> 0.45

Earlier the size of 
HF Mobile Bert **161Mb+**, now **98Mb**
HF Bart Dynamo pre-optimized model was about **1.2Gb**. It is now
**710MB**


![image](https://github.com/microsoft/onnxruntime/assets/11303988/1491a247-d371-4e66-85a3-2aeb702e8ca0)
kleiti pushed a commit to kleiti/onnxruntime that referenced this pull request Mar 22, 2024
…args (microsoft#18462)

### Description
Truncate traling non-existing arguments.
  Make sure we do not skip on the non-existing arguments in the middle,
  because shape inferece relies on their proper position.
This also affects the argument position in the Edges that must be
properly rebuilt
  each time If node branch is inlined.
Make sure that when we rename Defs in subgraphs, new renamed defs are
created in those subgraphs
  instead of pointing to outer scope defs.
  Add unit test.

### Motivation and Context
This is a follow up for
microsoft#18105
Currently, the non-trailing arguments are simply ignored and the edges
are created
with potentially incorrect positions.
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.

6 participants