-
Notifications
You must be signed in to change notification settings - Fork 607
Qualcomm AI Engine Direct - Mimi Enablement Stage 2 #10098
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,81 @@ | ||
# Copyright (c) Qualcomm Innovation Center, Inc. | ||
# All rights reserved | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import torch | ||
from executorch.exir.pass_base import ExportPass, PassResult | ||
|
||
|
||
class CDist(torch.nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, x, y): | ||
# Step 1: Compute differences | ||
diff = x.unsqueeze(-2) - y.unsqueeze(-3) | ||
|
||
# Step 2: Square differences | ||
sq_diff = diff**2 | ||
|
||
# Step 3: Sum of squares | ||
sum_sq_diff = sq_diff.sum(dim=-1) | ||
|
||
# Step 4: Square root | ||
distances = torch.sqrt(sum_sq_diff) | ||
|
||
return distances | ||
|
||
|
||
class DecomposeCDist(ExportPass): | ||
""" | ||
Decompose for math equivalent op. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def call(self, graph_module: torch.fx.GraphModule) -> PassResult: | ||
graph = graph_module.graph | ||
for node in graph.nodes: | ||
model = CDist() | ||
if torch.ops.aten.cdist.default == node.target: | ||
if len(node.args) > 2: | ||
assert ( | ||
node.args[2] == 2 | ||
), "Currently only p=2 is supported for CDist Decomposition" | ||
decomposed_module = torch.export.export( | ||
model, | ||
(node.args[0].meta["val"], node.args[1].meta["val"]), | ||
strict=True, | ||
).module() | ||
with graph.inserting_before(node): | ||
# remap is used to map original node values to new node values, | ||
# which ensures that reference to nodes are correctly updated in the new graph | ||
remap = {"x": node.args[0], "y": node.args[1]} | ||
|
||
for decomposed_node in decomposed_module.graph.nodes: | ||
# no need to copy existent 'output' | ||
if decomposed_node.op == "output": | ||
for user in node.users.copy(): | ||
# remap | ||
user.replace_input_with( | ||
node, | ||
remap[decomposed_node.args[0][0]], | ||
) | ||
# no need to copy existent placeholders | ||
elif decomposed_node.op == "placeholder": | ||
# replace node map from string to graph node | ||
remap[decomposed_node] = remap.pop(decomposed_node.name) | ||
else: | ||
remap[decomposed_node] = graph.node_copy( | ||
decomposed_node, | ||
arg_transform=lambda x, remap=remap: remap[x], | ||
) | ||
|
||
graph.erase_node(node) | ||
|
||
graph.eliminate_dead_code() | ||
graph_module.recompile() | ||
return PassResult(graph_module, True) |
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,36 @@ | ||
# Copyright (c) Qualcomm Innovation Center, Inc. | ||
# All rights reserved | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import torch | ||
from executorch.exir.dialects._ops import ops as exir_ops | ||
from executorch.exir.pass_base import ExportPass, PassResult | ||
|
||
|
||
class Remove0DTensor(ExportPass): | ||
""" | ||
QNN does not allow 0D tensor, we remove the node that will output an 0D tensor. | ||
Before adding operations to the list of nodes to be removed, please ensure that it will not change the logic. | ||
""" | ||
|
||
remove_ops = { | ||
exir_ops.edge.aten.select.int, | ||
exir_ops.edge.aten.select_copy.int, | ||
} | ||
|
||
def __init__(self, quantization_capture=False) -> None: | ||
super().__init__() | ||
|
||
def call(self, graph_module: torch.fx.GraphModule) -> PassResult: | ||
graph = graph_module.graph | ||
for node in graph.nodes: | ||
if node.target in self.remove_ops and len(node.meta["val"].shape) == 0: | ||
for user_n in list(node.users.keys()): | ||
user_n.replace_input_with(node, node.args[0]) | ||
graph.erase_node(node) | ||
|
||
graph.eliminate_dead_code() | ||
graph_module.recompile() | ||
return PassResult(graph_module, True) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the logic in https://github.com/pytorch/executorch/blob/main/backends/transforms/rank_0_to_rank_1.py can be applied here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @cccclai,

Thanks for reviewing the PR.
I believe the pass you are suggesting is to change input from 0d to 1d tensor.
However, for our case, this 0D tensor happened during
select
op in the middle of graph. We just removed thisselect
op since it does not affect the logic of the graph.The exact point where 0D tensor occurs in mimi is under
moshi/quantization/core_vq.py
, where is tries to create a 1D tensor and retrieve index 0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I guess for this specific logic, it's more efficient to remove the select op.
A more generic way to handle 0-d tensor is to convert it to 1-d tensor. Like following