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

Fix target matching for fused layers with compressed-tensors #12617

Merged
merged 4 commits into from
Feb 1, 2025
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def find_matched_target(layer_name: Optional[str], module: Module,

matched_target = (_find_first_match(layer_name, targets)
or _find_first_match(module.__class__.__name__, targets,
True))
True)
or _match_fused_layer(layer_name, targets))

if matched_target is None:
raise ValueError(f"Unable to find matching target for {module} in the "
Expand Down Expand Up @@ -152,3 +153,39 @@ def _is_equal_or_regex_match(value: str,
elif target == value:
return True
return False


def _match_fused_layer(layer_name: str,
target_layers: Iterable[str]) -> Optional[str]:
"""
Match a fused layer name to its corresponding individual layer in
target_layers.

Examples:
layer_name = "model.layers.0.self_attn.qkv_proj"
target_layers = ["model.layers.0.self_attn.q_proj",
"model.layers.0.self_attn.k_proj",
"model.layers.0.self_attn.v_proj"]
"""
# Split into parent path and layer type
# e.g., "model.layers.0.self_attn" and "qkv_proj"
parent_path = ".".join(layer_name.split(".")[:-1])
layer_type = layer_name.split(".")[-1]

if layer_type not in FUSED_LAYER_NAME_MAPPING:
return None

possible_layer_types = FUSED_LAYER_NAME_MAPPING[layer_type]

# Look for a target layer that:
# 1. Has the same parent path
# 2. Ends with one of the possible individual layer types
for target in target_layers:
is_same_parent = parent_path in target
is_matching_type = any(type_suffix in target
for type_suffix in possible_layer_types)

if is_same_parent and is_matching_type:
return target
eldarkurtic marked this conversation as resolved.
Show resolved Hide resolved

return None