-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add fusions for OpenAI CLIP #20721
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
Merged
hanbitmyths
merged 5 commits into
microsoft:main
from
kunal-vaishnavi:kvaishnavi/clip-fusion
May 18, 2024
Merged
Add fusions for OpenAI CLIP #20721
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8755e1f
Add OpenAI CLIP fusions
kunal-vaishnavi 2135cc8
Add QuickGelu fusion test
kunal-vaishnavi c50a4aa
Remove commented out code
kunal-vaishnavi 433d3fc
Add changes suggested by linter
kunal-vaishnavi 9bc5af3
Fix path mismatch check
kunal-vaishnavi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
|
|
||
| import logging | ||
|
|
||
| from fusion_base import Fusion | ||
| from onnx import helper | ||
| from onnx_model import OnnxModel | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class FusionQuickGelu(Fusion): | ||
| def __init__(self, model: OnnxModel): | ||
| super().__init__(model, "QuickGelu", ["Mul"]) | ||
|
|
||
| def fuse(self, node, input_name_to_nodes, output_name_to_node): | ||
| # Fuse the following subgraph to `QuickGelu` | ||
| # | ||
| # root_input | ||
| # / \ | ||
| # | Mul ----+ | ||
| # | (B = ~1.702) | | ||
| # \ | | | ||
| # \ Sigmoid |---- `QuickGelu` | ||
| # \ / | | ||
| # \ / | | ||
| # Mul ----+ | ||
| # | | ||
| # root_output | ||
|
|
||
| if node.op_type != "Mul": | ||
| logger.debug("fuse_quickgelu: failed to match second Mul node") | ||
| return | ||
|
|
||
| second_mul_node = node | ||
| root_input = second_mul_node.input[0] | ||
|
|
||
| sigmoid_node = self.model.match_parent_path(second_mul_node, ["Sigmoid"], [1]) | ||
| if sigmoid_node is None: | ||
| logger.debug("fuse_quickgelu: failed to match Sigmoid node") | ||
| return | ||
| sigmoid_node = sigmoid_node[0] | ||
|
|
||
| first_mul_node = self.model.match_parent_path(sigmoid_node, ["Mul"], [0]) | ||
| if first_mul_node is None: | ||
| logger.debug("fuse_quickgelu: failed to match first Mul node") | ||
| return | ||
| first_mul_node = first_mul_node[0] | ||
|
|
||
| approximation_value = self.model.get_constant_value(first_mul_node.input[1]).item() | ||
| if abs(approximation_value - 1.7021484375) >= 1e-3: | ||
| logger.debug("fuse_quickgelu: failed to match approximation value") | ||
| return | ||
|
|
||
| if first_mul_node.input[0] != root_input: | ||
| logger.debug("fuse_quickgelu: failed to match root input with first Mul node's input") | ||
| return | ||
|
|
||
| new_node = helper.make_node( | ||
| "QuickGelu", | ||
| inputs=[root_input], | ||
| outputs=[second_mul_node.output[0]], | ||
| name=self.model.create_node_name("QuickGelu"), | ||
| ) | ||
| new_node.domain = "com.microsoft" | ||
| new_node.attribute.extend([helper.make_attribute("alpha", approximation_value)]) | ||
|
|
||
| self.nodes_to_remove.extend([first_mul_node, sigmoid_node, second_mul_node]) | ||
| self.nodes_to_add.append(new_node) | ||
| self.node_name_to_graph_name[new_node.name] = self.this_graph_name | ||
| self.increase_counter("QuickGelu") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Check failure
Code scanning / CodeQL
Potentially uninitialized local variable