From c2c08b6df97495fdaa5353c1ed92d170fb895a5b Mon Sep 17 00:00:00 2001 From: shubhambhokare1 Date: Mon, 24 Jun 2024 00:07:01 +0000 Subject: [PATCH] Update readme for optimizer and rewriter tools --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index ee607d01e9..7a30a75838 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,56 @@ result = Hardmax(v) More examples can be found in the [docs/examples](docs/examples) directory. +## ONNX Script Tools + +### ONNX Optimizer + +The ONNX Script Optimizer tool provides the user with the functionality to optimize an ONNX model by performing optimizations and clean-ups such as constant folding, dead code elimination, etc. In order to utilize the optimizer tool: + +```python +import onnxscript + +onnxscript.optimizer.optimize(onnx_model) +``` + +For a detailed summary of all the optimizations applied by the optimizer call, refer to the tutorial [Optimizing a Model using the Optimizer](https://onnxscript.ai/tutorial/optimizer/optimize.html) + +### ONNX Rewriter + +The ONNX Rewriter tool provides the user with the functionality to replace certain patterns in an ONNX graph with another pattern based on user-defined rewrite rules. The rewriter tools allows two different methods in which patterns in the graph can be rewritten. + +### Pattern-based rewriting + +For this style of rewriting, the user provides a `target_pattern` that is to be replaced, a `replacement_pattern` and a `match_condition` (pattern rewrite will occur only if the match condition is satisfied). A simple example on how to use the pattern-based rewriting tool is as follows: + +```python +from onnxscript.rewriter import pattern + +# Create multiple rules +rule1 = pattern.RewriteRule( + erf_gelu_pattern, # Target Pattern + gelu, # Replacement +) +rule2 = pattern.RewriteRule( + erf_gelu_pattern_2, # Target Pattern + gelu, # Replacement +) +# Create a Rewrite Rule Set with multiple rules. +rewrite_rule_set = pattern.RewriteRuleSet([rule1, rule2]) +# Apply rewrites +model_with_rewrite_applied = onnxscript.rewriter.rewrite( + model, # Original ONNX Model + pattern_rewrite_rules=rewrite_rule_set, +) +return model_with_rewrite_applied +``` + +For a detailed tutorial on how to create target_pattern, replacement_pattern and match_condition blocks in order to utilize the pattern-based rewriter, refer to the tutorial [Pattern-based Rewrite Using Rules](https://onnxscript.ai/tutorial/rewriter/rewrite_patterns.html) + +### Function-based rewriting + +This style of rewriting matches a `FUNCTION_KEYWORD` and `PACKAGE_NAME` provided by the user to an existing function within the graph and replaces it with a new function provided by the user. + ## Development Guidelines Every change impacting the converter or the eager evaluation must be