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

Add additional matching logic to MMGroupQuantRewriterPass #473

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Max191
Copy link

@Max191 Max191 commented Feb 22, 2024

This adds additional matching logic on tensor.view ops required for using MMGroupQuantRewriterPass in the llama.turbine model (used here stellaraccident/llama.turbine#2).

Copy link
Contributor

@stellaraccident stellaraccident left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the test failures, I think I need to see the IR we are trying to match. But this seems like it might be tricky to keep working.

@@ -313,8 +315,12 @@ def evict_kvcache_space(self):
if quantization == "int4" and not compile_to == "linalg":
from shark_turbine.transforms.quantization import mm_group_quant

print(param_set)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

@Max191
Copy link
Author

Max191 commented Feb 23, 2024

Aside from the test failures, I think I need to see the IR we are trying to match. But this seems like it might be tricky to keep working.

The IR this is matching looks like this:

    %_params.blk.0.ffn_gate.weight = util.global.load @_params.blk.0.ffn_gate.weight : tensor<3200x8640xf32>
    %152 = torch_c.from_builtin_tensor %_params.blk.0.ffn_gate.weight : tensor<3200x8640xf32> -> !torch.vtensor<[3200,8640],f32>
    %int8640 = torch.constant.int 8640
    %int3200_171 = torch.constant.int 3200
    %153 = torch.prim.ListConstruct %int8640, %int3200_171 : (!torch.int, !torch.int) -> !torch.list<int>
    %154 = torch.aten.view %152, %153 : !torch.vtensor<[3200,8640],f32>, !torch.list<int> -> !torch.vtensor<[8640,3200],f32>
    %int1_172 = torch.constant.int 1
    %int0_173 = torch.constant.int 0
    %155 = torch.prim.ListConstruct %int1_172, %int0_173 : (!torch.int, !torch.int) -> !torch.list<int>
    %156 = torch.aten.permute %154, %155 : !torch.vtensor<[8640,3200],f32>, !torch.list<int> -> !torch.vtensor<[3200,8640],f32>
    %int3200_174 = torch.constant.int 3200
    %157 = torch.prim.ListConstruct %4, %int3200_174 : (!torch.int, !torch.int) -> !torch.list<int>
    %158 = torch.aten.view %151, %157 : !torch.vtensor<[1,?,3200],f32>, !torch.list<int> -> !torch.vtensor<[?,3200],f32>
    %159 = torch.aten.mm %158, %156 : !torch.vtensor<[?,3200],f32>, !torch.vtensor<[3200,8640],f32> -> !torch.vtensor<[?,8640],f32>

This change basically just rewrites the parameter to the viewed type and elides the view (%154) as long as the view is just swapping the shape sizes, and the group size evenly divides the new inner shape.

This pass was already pretty specific to llama, especially since it was matching for a specific parameter name, so I figured that this should be okay for now. Maybe the view rewrite could be moved to its own pass, but this just seemed like the easiest path forward for me, and I like the idea of having fewer passes to call after exporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants