Skip to content

Feat (brevitas_examples/sdxl): better GPTQ #1250

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

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions src/brevitas/graph/gptq.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ def __init__(
self.blocksize = math.ceil(self.columns / num_blocks)

# Initialize Hessian matrix and counter. We need it in float32 to compute the inverse
self.H = torch.zeros((self.groups, self.columns, self.columns),
device='cpu',
dtype=torch.float32,
pin_memory=torch.cuda.is_available())
self.B = torch.zeros((self.groups, self.columns, self.columns),
device='cpu',
dtype=torch.float32,
pin_memory=torch.cuda.is_available())
self.H = torch.zeros(
(self.groups, self.columns, self.columns),
device='cpu',
dtype=torch.float32,
)
self.B = torch.zeros(
(self.groups, self.columns, self.columns),
device='cpu',
dtype=torch.float32,
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe I should re-introduce this but I was getting weird CUDA errors. Also, since we are storing in CPU, not sure why we need to use pin_memory.
@i-colbert maybe you can comment before we merge

Copy link
Collaborator

Choose a reason for hiding this comment

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

From what I can remember, it was used to improve data transfer speeds from GPU to CPU, which is why it is only enabled if a GPU is available.

self.nsamples = 0

assert torch_version >= version.parse('1.10'), "GPTQ requires torch 1.10 or higher"
Expand Down
25 changes: 19 additions & 6 deletions src/brevitas/graph/gpxq.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,26 @@ def single_layer_update(self):
pass

def get_quant_weights(self, i, i1, permutation_list, with_quant_history=False):
from brevitas.quant_tensor import _unpack_quant_tensor
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not required - already imported.


# We need to recompute quant weights at runtime since our float weights are being updated
# Add offset in case of blockwise computation
i = i1 + i

# For QuantLinear and for some QuantConvolutional layers, we exploit the possibility
# of quantizing only a subset of the entire matrix speeding up the computation of GPxQ
no_slice = False
# Groupwise Quantization does not support slicing
no_slice = no_slice or self.layer.weight_quant.is_groupwise
# If we need quantization of past channels, we do not use slicing
no_slice = no_slice or with_quant_history
# If we are in export mode (i.e., inference mode), we do not slice for torch.compile
# compatibility
no_slice = no_slice or self.layer.weight_quant.export_mode

if isinstance(self.layer, qnn.QuantLinear):
if self.layer.weight_quant.is_groupwise or with_quant_history:
if no_slice:

# No slicing, not optimized
q = self.layer.quant_weight(quant_input=self.quant_metadata)
q = _unpack_quant_tensor(q).unsqueeze(0) # [1, OC, IC]
Expand All @@ -264,11 +277,11 @@ def get_quant_weights(self, i, i1, permutation_list, with_quant_history=False):
subtensor_slice_list=subtensor_slice_list,
quant_input=self.quant_metadata)).unsqueeze(0) # [1, OC, 1]
elif isinstance(self.layer, SUPPORTED_CONV_OP):
# For depthwise and ConvTranspose we fall back to quantizing the entire martix.
# For all other cases, we create a mask that represent the slicing we will perform on the weight matrix
# and we quantize only the selected dimensions.
if self.layer.weight_quant.is_groupwise or with_quant_history or self.groups > 1 or (
self.groups == 1 and is_conv_transposed(self.layer)):
# Depthwise and ConvTranspose does not support slicing
no_slice_conv = no_slice or (self.groups > 1 or is_conv_transposed(self.layer))

if no_slice_conv:

quant_weight = self.layer.quant_weight(quant_input=self.quant_metadata)
quant_weight = _unpack_quant_tensor(quant_weight)

Expand Down
11 changes: 9 additions & 2 deletions src/brevitas_examples/stable_diffusion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ usage: main.py [-h] [-m MODEL] [-d DEVICE] [-b BATCH_SIZE] [--prompt PROMPT]
[--weight-quant-format WEIGHT_QUANT_FORMAT]
[--input-quant-format INPUT_QUANT_FORMAT]
[--weight-quant-granularity {per_channel,per_tensor,per_group}]
[--input-quant-granularity {per_tensor,per_group}]
[--input-quant-granularity {per_tensor,per_group,per_row}]
[--input-scale-type {static,dynamic}]
[--weight-group-size WEIGHT_GROUP_SIZE]
[--input-group-size INPUT_GROUP_SIZE]
Expand All @@ -116,6 +116,8 @@ usage: main.py [-h] [-m MODEL] [-d DEVICE] [-b BATCH_SIZE] [--prompt PROMPT]
[--inference-pipeline {samples,reference_images,mlperf}]
[--caption-path CAPTION_PATH]
[--reference-images-path REFERENCE_IMAGES_PATH]
[--few-shot-calibration [FEW_SHOT_CALIBRATION ...]]
[--calibration-batch-size CALIBRATION_BATCH_SIZE]
[--quantize-weight-zero-point | --no-quantize-weight-zero-point]
[--exclude-blacklist-act-eq | --no-exclude-blacklist-act-eq]
[--quantize-input-zero-point | --no-quantize-input-zero-point]
Expand Down Expand Up @@ -251,7 +253,7 @@ options:
--weight-quant-granularity {per_channel,per_tensor,per_group}
Granularity for scales/zero-point of weights. Default:
per_channel.
--input-quant-granularity {per_tensor,per_group}
--input-quant-granularity {per_tensor,per_group,per_row}
Granularity for scales/zero-point of inputs. Default:
per_tensor.
--input-scale-type {static,dynamic}
Expand Down Expand Up @@ -307,6 +309,11 @@ options:
Inference pipeline for evaluation. Default: None
--reference-images-path REFERENCE_IMAGES_PATH
Inference pipeline for evaluation. Default: None
--few-shot-calibration [FEW_SHOT_CALIBRATION ...]
What timesteps to use for few-shot-calibration.
Default: []
--calibration-batch-size CALIBRATION_BATCH_SIZE
Batch size for few-shot-calibration. Default: 1
--quantize-weight-zero-point
Enable Quantize weight zero-point. Default: Enabled
--no-quantize-weight-zero-point
Expand Down
Loading
Loading