-
Notifications
You must be signed in to change notification settings - Fork 19
[WIP] Enable GPTQModel to handle GraniteMoeParallelExperts #122
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
cba3127
a103f5d
3fe665e
775e9fb
56dfbef
c23816b
11aae96
cff9a59
3cd53eb
12b206a
b0f9272
58d2722
f49fdc5
74d18b7
f98e85d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| "granite", | ||
| "gemma", | ||
| "dbrx_converted", | ||
| "granitemoe" | ||
| ] | ||
|
|
||
| EXLLAMA_DEFAULT_MAX_INPUT_LENGTH = 2048 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| ############################################################################### | ||
| # Adapted from https://github.com/ModelCloud/GPTQModel | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| ############################################################################### | ||
| # Local | ||
| from .base import BaseGPTQModel | ||
|
|
||
|
|
||
| class GraniteMoeGPTQ(BaseGPTQModel): | ||
| base_modules = ["model.embed_tokens", "model.norm"] | ||
| convert3dToModuleList = ["block_sparse_moe.input_linear", "block_sparse_moe.output_linear"] | ||
fabianlim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| layers_node = "model.layers" | ||
| layer_type = "GraniteMoeDecoderLayer" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest you add some simple key to inform the format of Also in the granitemoe case, another compilation is that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so basically the simple key needs to know what do look for to convert it to 3D tensor, and then when you write class GraniteMoeGPTQ(BaseGPTQModel):
convert3dToModuleList = ["block_sparse_moe.input_linear", "block_sparse_moe.output_linear"]
layer_modules = [
[
"block_sparse_moe.input_linear.0.weight",
"block_sparse_moe.input_linear.1.weight",
...
], [
"block_sparse_moe.output_linear.0.weight",
"block_sparse_moe.output_linear.1.weight",
...
]
] |
||
| layer_modules = [ | ||
| ["self_attn.k_proj", "self_attn.v_proj", "self_attn.q_proj"], | ||
| ["self_attn.o_proj"], | ||
| [f"block_sparse_moe.input_linear.{i}" for i in range(40)], | ||
| [f"block_sparse_moe.output_linear.{i}" for i in range(40)], | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,9 @@ | |
| import threadpoolctl as tctl | ||
| import torch | ||
| import torch.nn as nn | ||
| import torch.nn.functional as F | ||
| import transformers | ||
| import transformers.models.granitemoe.modeling_granitemoe as MOE | ||
|
|
||
| # Local | ||
| from ..models._const import ( | ||
|
|
@@ -52,6 +54,24 @@ | |
| logger.addHandler(handler) | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
| class ThreeDTensorModuleList(nn.ModuleList): | ||
| def forward(self, inputs: torch.Tensor) -> torch.Tensor: | ||
| # Shape of input: (num_selected_experts * batch_size (expert_size), input_features_size) | ||
|
||
| expert_size = len(self) | ||
| input_list = inputs.split(expert_size, dim=0) | ||
| output_list = [] | ||
|
|
||
| # Iterate over the number of selected experts and apply each expert to the corresponding input | ||
| for i in range(len(self)): | ||
| # Shape of input_list[i]: (batch_size, input_features_size); Shape of self[i]: (output_features_size, input_features_size) | ||
| # Shape of output: (batch_size, output_features_size); | ||
| expert_output = F.linear(input_list[i], self[i]) | ||
| output_list.append(expert_output) | ||
|
|
||
| # Concatenate the outputs along the first dimension | ||
| results = torch.cat(output_list, dim=0) # Shape: (num_selected_experts * batch_size, output_features_size) | ||
| return results | ||
|
|
||
|
|
||
| def recurse_getattr(obj, attr: str): | ||
| """ | ||
|
|
@@ -100,14 +120,40 @@ def nested_move_to(v, device): | |
| return v | ||
|
|
||
|
|
||
| def check3DTensor(module, name, convert3dToModuleList=["block_sparse_moe.input_linear", "block_sparse_moe.output_linear"]): | ||
| if convert3dToModuleList and name in convert3dToModuleList: | ||
| # print("INSIDE check3DTensor module, name, convert3dToModuleList", module, name, convert3dToModuleList) | ||
| num_experts = module.num_experts | ||
fabianlim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| input_size = module.input_size | ||
| output_size = module.output_size | ||
| module = ThreeDTensorModuleList([ | ||
| nn.Linear(input_size, output_size, bias=False) for _ in range(num_experts) | ||
| ]) | ||
|
|
||
| return module | ||
|
|
||
|
|
||
| def find_layers(module, layers=None, name=""): | ||
| # print("1- INSIDE find_layers module", module) | ||
| module = check3DTensor(module, name) | ||
|
||
| # print("2- AFTER check3DTensor module", module) | ||
| if not layers: | ||
| layers = [transformers.pytorch_utils.Conv1D, nn.Conv2d, nn.Linear] | ||
| layers = [transformers.pytorch_utils.Conv1D, nn.Conv2d, nn.Linear] | ||
|
|
||
| # print("2- INFO: type(module), name", type(module), name) | ||
| # if hasattr(module, "weight"): | ||
| # print("3- type(module.weight), module.weight.shape, module.weight.ndim", type(module.weight), module.weight.shape, module.weight.ndim) | ||
| for layer in layers: | ||
| if isinstance(module, layer): | ||
| return {name: module} | ||
|
|
||
| res = {} | ||
| # if isinstance(module, MOE.GraniteMoeParallelExperts): | ||
| # print("Print GraniteMoeParallelExperts Layer children") | ||
| # for name1, child in module.named_children(): | ||
| # print("4- name1, child", name1, child) | ||
| for name1, child in module.named_children(): | ||
| # print("PROCESS- name, name1, child", name, name1, child) | ||
| res.update( | ||
| find_layers( | ||
| child, layers=layers, name=name + "." + name1 if name != "" else name1 | ||
|
|
||
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.
why does this need to be changed?