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

update SqueezeInt4LinearInputs to process relu/gelu inputs too #8601

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backends/transforms/fuse_view_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,24 @@ def merge_view_copy_chains(graph: torch.fx.Graph) -> torch.fx.Graph:
return graph


def remove_noop_view_copy(graph: torch.fx.Graph) -> torch.fx.Graph:
"""
Remove view_copy nodes that are no-ops.
"""
ops = exir_ops.edge
view_op = ops.aten.view_copy.default
for node in graph.nodes:
if node.op == "call_function" and node.target == view_op:
input_shape = list(node.args[0].meta["val"].shape)
target_shape = node.args[1]
if input_shape == target_shape:
node.replace_all_uses_with(node.args[0])
graph.eliminate_dead_code()
return graph


class FuseViewCopyTransform(ExportPass):
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
graph_module.graph = merge_view_copy_chains(graph_module.graph)
graph_module.graph = remove_noop_view_copy(graph_module.graph)
return PassResult(graph_module, True)
7 changes: 4 additions & 3 deletions backends/vulkan/_passes/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ runtime.python_library(
)

runtime.python_library(
name = "squeeze_int4_linear_inputs",
name = "squeeze_unsqueeze_inputs",
srcs = [
"squeeze_int4_linear_inputs.py",
"squeeze_unsqueeze_inputs.py",
],
visibility = [
"//executorch/backends/...",
],
deps = [
"//caffe2:torch",
"//executorch/backends/vulkan:custom_ops_lib",
"//executorch/exir:pass_base",
"//executorch/exir/dialects:lib",
Expand Down Expand Up @@ -114,7 +115,7 @@ runtime.python_library(
":remove_asserts",
":remove_local_scalar_dense",
":remove_redundant_ops",
":squeeze_int4_linear_inputs",
":squeeze_unsqueeze_inputs",
":tag_memory_meta_pass",
]
)
6 changes: 3 additions & 3 deletions backends/vulkan/_passes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from executorch.backends.vulkan._passes.remove_redundant_ops import (
RemoveRedundantOpsTransform,
)
from executorch.backends.vulkan._passes.squeeze_int4_linear_inputs import (
SqueezeInt4LinearInputs,
from executorch.backends.vulkan._passes.squeeze_unsqueeze_inputs import (
SqueezeUnsqueezeInputs,
)
from executorch.backends.vulkan._passes.tag_memory_meta_pass import TagMemoryMetaPass

Expand All @@ -32,6 +32,6 @@
"RemoveAssertsTransform",
"RemoveLocalScalarDenseOpsTransform",
"RemoveRedundantOpsTransform",
"SqueezeInt4LinearInputs",
"SqueezeUnsqueezeInputs",
"TagMemoryMetaPass",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@

# pyre-strict

from typing import Dict, List, Tuple
from typing import Dict, List, Set, Tuple, Union

import executorch.backends.vulkan.custom_ops_lib # noqa: needed to access vk op
from executorch.exir.dialects._ops import ops as exir_ops
from executorch.exir.dialects.edge._ops import EdgeOpOverload
from executorch.exir.pass_base import ExportPass, NodeMetadata, ProxyValue

from torch._ops import OpOverload

from torch.fx.node import Argument

OpType = Union[str, OpOverload, EdgeOpOverload]


class SqueezeUnsqueezeInputs(ExportPass):
_squeezable_ops: Set[OpType] = {
exir_ops.edge.et_vk.linear_weight_int4.default,
exir_ops.edge.aten.relu.default,
exir_ops.edge.aten.gelu.default,
}

class SqueezeInt4LinearInputs(ExportPass):
def call_operator(
self,
op, # pyre-ignore
Expand All @@ -26,7 +37,7 @@ def call_operator(
def _squeezable(shape: List[int]) -> bool:
return len(shape) > 2 and 1 in shape

if op != exir_ops.edge.et_vk.linear_weight_int4.default:
if op not in self._squeezable_ops:
return super().call_operator(op, args, kwargs, meta)

# pyre-ignore[16]: `None` has no attribute `node`
Expand Down
4 changes: 2 additions & 2 deletions backends/vulkan/vulkan_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
insert_prepack_nodes,
RemoveLocalScalarDenseOpsTransform,
RemoveRedundantOpsTransform,
SqueezeInt4LinearInputs,
SqueezeUnsqueezeInputs,
TagMemoryMetaPass,
)

Expand Down Expand Up @@ -153,7 +153,7 @@ def preprocess( # noqa: C901
RemoveRedundantOpsTransform(),
AddmmToLinearTransform(),
FuseDequantLinearPass(),
SqueezeInt4LinearInputs(),
SqueezeUnsqueezeInputs(),
FuseViewCopyTransform(),
ViewCopyToSqueezeUnsqueezePass(),
FuseBatchNormWithConvPass(program),
Expand Down
Loading