From dfc531e568895c09f3aaec6843e273e9780be44e Mon Sep 17 00:00:00 2001 From: Kyle Sayers Date: Tue, 19 Nov 2024 02:06:10 +0000 Subject: [PATCH 1/2] add model validator to quantization scheme Signed-off-by: Kyle Sayers --- .../quantization/quant_scheme.py | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/compressed_tensors/quantization/quant_scheme.py b/src/compressed_tensors/quantization/quant_scheme.py index 180d0f26..0244e232 100644 --- a/src/compressed_tensors/quantization/quant_scheme.py +++ b/src/compressed_tensors/quantization/quant_scheme.py @@ -13,14 +13,14 @@ # limitations under the License. from copy import deepcopy -from typing import List, Optional +from typing import Any, Dict, List, Optional from compressed_tensors.quantization.quant_args import ( QuantizationArgs, QuantizationStrategy, QuantizationType, ) -from pydantic import BaseModel +from pydantic import BaseModel, model_validator __all__ = [ @@ -47,12 +47,31 @@ class QuantizationScheme(BaseModel): input_activations: Optional[QuantizationArgs] = None output_activations: Optional[QuantizationArgs] = None + @model_validator(mode="after") + def validate_model_after(model: "QuantizationArgs") -> Dict[str, Any]: + inputs = model.input_activations + weights = model.weights + outputs = model.output_activations + + if inputs is not None: + if inputs.actorder is not None: + raise ValueError("Cannot apply actorder to input activations") + + if weights is not None: + if weights.dynamic: + raise ValueError("Cannot apply dynamic quantization to weights") + + if outputs is not None: + if outputs.actorder is not None: + raise ValueError("Cannot apply actorder to output activations") + + return model + @classmethod def default_scheme( cls, targets: Optional[List[str]] = None, ): - if targets is None: # default to quantizing all Linear layers targets = ["Linear"] From a2218d0c2abccf38155d430e6554a31769cf8a0a Mon Sep 17 00:00:00 2001 From: Kyle Sayers Date: Thu, 21 Nov 2024 16:24:51 +0000 Subject: [PATCH 2/2] allow dynamic weights Signed-off-by: Kyle Sayers --- src/compressed_tensors/quantization/quant_scheme.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/compressed_tensors/quantization/quant_scheme.py b/src/compressed_tensors/quantization/quant_scheme.py index 0244e232..7fe90ca3 100644 --- a/src/compressed_tensors/quantization/quant_scheme.py +++ b/src/compressed_tensors/quantization/quant_scheme.py @@ -50,17 +50,12 @@ class QuantizationScheme(BaseModel): @model_validator(mode="after") def validate_model_after(model: "QuantizationArgs") -> Dict[str, Any]: inputs = model.input_activations - weights = model.weights outputs = model.output_activations if inputs is not None: if inputs.actorder is not None: raise ValueError("Cannot apply actorder to input activations") - if weights is not None: - if weights.dynamic: - raise ValueError("Cannot apply dynamic quantization to weights") - if outputs is not None: if outputs.actorder is not None: raise ValueError("Cannot apply actorder to output activations")