Skip to content

Harden KV Cache qparams #262

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/compressed_tensors/linear/compressed_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
QuantizationStatus,
initialize_module_for_quantization,
)
from compressed_tensors.utils import register_offload_parameter
from torch import Tensor
from torch.nn import Parameter
from torch.nn.functional import linear
Expand Down Expand Up @@ -68,7 +69,7 @@ def from_linear(
param = Parameter(
torch.empty(shape, device=device, dtype=dtype), requires_grad=False
)
module.register_parameter(name, param)
register_offload_parameter(module, name, param)

# mark module as compressed
module.quantization_status = QuantizationStatus.COMPRESSED
Expand Down
19 changes: 7 additions & 12 deletions src/compressed_tensors/quantization/lifecycle/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,19 @@ def _initialize_scale_zero_point(
register_offload_parameter(module, f"{base_name}_g_idx", init_g_idx)


def _initialize_attn_scales(module: Module) -> None:
"""Initlaize k_scale, v_scale for self_attn"""
def _initialize_attn_scales(module: Module):
"""Initlaize k_scale, v_scale for self_attn"""

expected_shape = 1 # per tensor

param = next(module.parameters())
Copy link
Member

Choose a reason for hiding this comment

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

WHAAAAAAAAAAAAAAT 😨 we just assumed next(model.parameters()) give us the weight param

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry this needs to be reworked. This is meant to be called on an "attention" module, so we need to get the k_proj and v_proj attributes, not the weight attribute.

But yes, making any assumption of ordering on module.parameters() is incorrect

scale_dtype = param.dtype
device = param.device
weight_param = getattr(module, "weight", next(module.parameters()))
Copy link
Member

Choose a reason for hiding this comment

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

Should this be q_proj weights?

scale_dtype = weight_param.dtype
device = weight_param.device

init_scale = Parameter(
torch.empty(expected_shape, dtype=scale_dtype, device=device),
requires_grad=False,
)

module.register_parameter(KVCacheScaleType.KEY.value, init_scale)

init_scale = Parameter(
torch.empty(expected_shape, dtype=scale_dtype, device=device),
requires_grad=False,
)
module.register_parameter(KVCacheScaleType.VALUE.value, init_scale)
register_offload_parameter(module, KVCacheScaleType.KEY.value, init_scale)
register_offload_parameter(module, KVCacheScaleType.VALUE.value, init_scale.clone())