-
Notifications
You must be signed in to change notification settings - Fork 216
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]: Fallback to KleidiAI channelwise kernel groupsize isnt suitable #1647
Open
ng-05
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
ng-05:kleidiai_fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -587,8 +587,6 @@ def apply(weight, bias: Optional[torch.Tensor] = None): | |
f"granularity must be PerGroup or PerRow, got {granularity}" | ||
) | ||
|
||
assert weight.shape[-1] % group_size == 0 | ||
|
||
layout = layout_arg | ||
scale_dtype = None | ||
tensor_quantizer = to_affine_quantized_intx | ||
|
@@ -605,13 +603,8 @@ def apply(weight, bias: Optional[torch.Tensor] = None): | |
assert ( | ||
act_mapping_type == MappingType.ASYMMETRIC | ||
), "PackedLinearInt8DynamicActivationIntxWeightLayout requires act_mapping_type=MappingType.ASYMMETRIC" | ||
assert not layout.has_params_set(), "PackedLinearInt8DynamicActivationIntxWeightLayout params should not already be set" | ||
layout = PackedLinearInt8DynamicActivationIntxWeightLayout( | ||
bit_width=bit_width, | ||
group_size=group_size, | ||
has_weight_zeros=has_weight_zeros, | ||
target="aten" if layout.target == Target.ATEN else "native", | ||
) | ||
assert not layout.has_params_set( | ||
), "PackedLinearInt8DynamicActivationIntxWeightLayout params should not already be set" | ||
if layout.target == Target.ATEN: | ||
if ( | ||
weight_dtype != torch.int4 | ||
|
@@ -628,15 +621,28 @@ def apply(weight, bias: Optional[torch.Tensor] = None): | |
assert ( | ||
TORCH_VERSION_AT_LEAST_2_6 | ||
), "aten target is requires torch version > 2.6.0" | ||
# Fallback to Channelwise scheme if group_size is too big | ||
if weight.shape[-1] < group_size: | ||
logger.warning(f"Changing group_size to { | ||
weight.shape[-1]}. Weight shape {weight.shape} can not support group_size {group_size}.") | ||
group_size = weight.shape[-1] | ||
if torch.backends.kleidiai.is_available(): | ||
if isinstance(granularity, PerGroup): | ||
if weight.shape[-1] != group_size and group_size % 32 == 0: | ||
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.
IIRC the constraint is |
||
scale_dtype = ( | ||
torch.bfloat16 | ||
) # KleidiAI kernel requires bfloat16 scale_dtype | ||
tensor_quantizer = ( | ||
to_packedlinearint8dynamicactivationintxweight_quantized_intx | ||
) | ||
|
||
layout = PackedLinearInt8DynamicActivationIntxWeightLayout( | ||
bit_width=bit_width, | ||
group_size=group_size, | ||
has_weight_zeros=has_weight_zeros, | ||
target="aten" if layout.target == Target.ATEN else "native", | ||
) | ||
|
||
assert weight.shape[-1] % group_size == 0 | ||
quantizer_args = [ | ||
weight, | ||
weight_mapping_type, | ||
|
@@ -658,7 +664,7 @@ def apply(weight, bias: Optional[torch.Tensor] = None): | |
# Note that PackedLinearInt8DynamicActivationIntxWeightLayout has dynamic activation quantization fused | ||
# with the kernel and it should not be applied separately | ||
if not isinstance(layout, PackedLinearInt8DynamicActivationIntxWeightLayout): | ||
activation_quant_func = lambda x: to_affine_quantized_intx( | ||
def activation_quant_func(x): return to_affine_quantized_intx( | ||
x, | ||
mapping_type=act_mapping_type, | ||
block_size=_get_per_token_block_size(x), | ||
|
@@ -668,7 +674,8 @@ def apply(weight, bias: Optional[torch.Tensor] = None): | |
scale_dtype=torch.float32, | ||
zero_point_dtype=torch.int32, | ||
) | ||
weight = to_linear_activation_quantized(weight, activation_quant_func) | ||
weight = to_linear_activation_quantized( | ||
weight, activation_quant_func) | ||
return weight | ||
|
||
return _get_linear_subclass_inserter(apply, propagate_bias=propagate_bias) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'd recommend adding a top-level configuration option which clearly tells the user "group size can be changed for certain weight shapes" to handle this case, and throwing an exception if that config setting isn't on.
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.
I don't understand why we are doing a fallback here. If group_size doesn't divide weight.shape[-1], why not raise exception and let user explicitly move to channelwise?