-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Weight compression via Lora Correction Algorithm (#2816)
### Changes Lora Correction algorithm for int4/nf4 weight compression. ### Reason for changes Method for improving accuracy by migrating quantization noise to “learnable” lora adapters. ### Related tickets 135863 ### Tests - [x] docstrings, proper names - [x] results for phi3 and stablelm2-1.6b on lambada, wikitext - [x] job/NNCF/job/manual/job/post_training_weight_compression/144/ ![image](https://github.com/user-attachments/assets/93721a8f-a0c5-4852-9d79-7b281cf2fe67) ![image](https://github.com/user-attachments/assets/2e3bd797-8535-4fbb-88da-2dbd92964d50) ![image](https://github.com/user-attachments/assets/8436009f-2827-4ea1-b481-e3f89bd35aef) ![image](https://github.com/user-attachments/assets/3aa290c9-5f41-4933-b0de-160fac3cce2a)
- Loading branch information
1 parent
ec25a29
commit 417c2a1
Showing
22 changed files
with
949 additions
and
193 deletions.
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
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
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
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
44 changes: 44 additions & 0 deletions
44
nncf/quantization/algorithms/weight_compression/activation_stats.py
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright (c) 2024 Intel Corporation | ||
# 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. | ||
|
||
from typing import List, Tuple, TypeVar | ||
|
||
from nncf.tensor import functions as fns | ||
|
||
TTensor = TypeVar("TTensor") | ||
|
||
|
||
def process_stats(stats: List[TTensor], subset_size: int) -> Tuple[TTensor, TTensor]: | ||
""" | ||
It's a processing of activations shared between AWQ, Scale Estimation and LoRA Correction algorithms. | ||
:param stats: list of activation statistics for a layer that contains N tensors with shape [SeqLen, HiddenDim] | ||
:type stats: List[TTensor] | ||
:param subset_size: The number of samples for AWQ. | ||
:type subset_size: int | ||
:return: tuple of the following tensors: | ||
s - maximum channel magnitude across samples [HiddenDim] | ||
X - average channel magnitude across tokens in the sequence [HiddenDim, SampleSize] | ||
:rtype: Tuple[TTensor, TTensor] | ||
""" | ||
X = fns.stack([fns.mean(stat, axis=0) for stat in stats]) # [Batch, HiddenDim] | ||
X_full = fns.transpose(X) # [HiddenDim, Batch] | ||
|
||
# prevent high memory and time consumption | ||
if X_full.shape[1] > subset_size: | ||
lens = [stat.shape[0] for stat in stats] | ||
step = X_full.shape[1] // subset_size | ||
idxs = [i[0] for i in sorted(enumerate(lens), key=lambda x: -x[1])][::step] | ||
X = X_full[:, idxs] # [HiddenDim, SampleSize] | ||
else: | ||
X = X_full | ||
s = fns.max(fns.abs(X_full), axis=1) # [HiddenDim] | ||
return s, X |
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
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
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
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
Oops, something went wrong.