|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +from typing import Dict, List, Tuple |
| 10 | + |
| 11 | +import executorch.backends.vulkan.custom_ops_lib # noqa: needed to access vk op |
| 12 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 13 | +from executorch.exir.pass_base import ExportPass, NodeMetadata, ProxyValue |
| 14 | + |
| 15 | +from torch.fx.node import Argument |
| 16 | + |
| 17 | + |
| 18 | +class SqueezeInt4LinearInputs(ExportPass): |
| 19 | + def call_operator( |
| 20 | + self, |
| 21 | + op, # pyre-ignore |
| 22 | + args: Tuple[Argument, ...], |
| 23 | + kwargs: Dict[str, Argument], |
| 24 | + meta: NodeMetadata, |
| 25 | + ) -> ProxyValue: |
| 26 | + def _squeezable(shape: List[int]) -> bool: |
| 27 | + return len(shape) > 2 and 1 in shape |
| 28 | + |
| 29 | + if op != exir_ops.edge.et_vk.linear_weight_int4.default: |
| 30 | + return super().call_operator(op, args, kwargs, meta) |
| 31 | + |
| 32 | + # pyre-ignore[16]: `None` has no attribute `node` |
| 33 | + input_shape = args[0].node.meta["val"].shape |
| 34 | + output_shape = meta["val"].shape |
| 35 | + if not _squeezable(input_shape): |
| 36 | + return super().call_operator(op, args, kwargs, meta) |
| 37 | + |
| 38 | + # squeeze input tensor |
| 39 | + squeeze_shape = list(input_shape) |
| 40 | + while _squeezable(squeeze_shape): |
| 41 | + squeeze_shape.remove(1) |
| 42 | + |
| 43 | + squeeze_out = super().call_operator( |
| 44 | + exir_ops.edge.aten.view_copy.default, |
| 45 | + (args[0], squeeze_shape), |
| 46 | + kwargs, |
| 47 | + meta, |
| 48 | + ) |
| 49 | + # call linear on squeezed output |
| 50 | + new_args = (squeeze_out, *args[1:]) |
| 51 | + linear_out = super().call_operator( |
| 52 | + op, |
| 53 | + new_args, |
| 54 | + kwargs, |
| 55 | + meta, |
| 56 | + ) |
| 57 | + # unsqueeze output |
| 58 | + unsqueeze_shape = list(output_shape) |
| 59 | + return super().call_operator( |
| 60 | + exir_ops.edge.aten.view_copy.default, |
| 61 | + (linear_out, unsqueeze_shape), |
| 62 | + kwargs, |
| 63 | + meta, |
| 64 | + ) |
0 commit comments