Registry / ai-ml / diffq
library0.2.4pypypi✓ verified 88d ago

DiffQ is a differentiable quantization framework for PyTorch that provides tools to quantize PyTorch models, primarily focusing on large language models (LLMs) and computer vision models. It enables quantization-aware training and leverages various quantization methods like GPTQ, HQQ, and AWQ. Currently at version 0.2.4, it has seen active development, especially in late 2023, with periodic releases addressing new features and bug fixes.

pip install diffq
INSTALL
IMPORT
SIG · DIFFQ
D
diffq
ai-mlpythonv0.2.4
Install
—
Import
—
Disk
—
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.2.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
3/6 runs
5/6 runs
py 3.11
3/6 runs
3/6 runs
py 3.12
3/6 runs
3/6 runs
py 3.13
3/6 runs
3/6 runs
py 3.9
3/6 runs
1/6 runs
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DiffQuantizer
✓ from diffq import DiffQuantizer
✗ from diffq import DiffQModel

This quickstart demonstrates how to convert a standard PyTorch model into a `DiffQModel` using a `BaseQuantizationConfig`. While this example uses `quant_method="none"` for structural conversion, for actual quantization (e.g., 4-bit, 8-bit), you would specify a method like `"gptq"` and then run a specific quantizer (e.g., `GPTQQuantizer`) with a dataloader.

import torch import torch.nn as nn from diffq import DiffQModel, BaseQuantizationConfig # 1. Define a simple PyTorch model class SimpleModel(nn.Module): def __init__(self): super().__init__() self.linear1 = nn.Linear(10, 20) self.relu = nn.ReLU() self.linear2 = nn.Linear(20, 1) def forward(self, x): return self.linear2(self.relu(self.linear1(x))) model = SimpleModel() # 2. Define a basic quantization configuration # For actual quantization (e.g., 'gptq', 'hqq', 'awq'), # additional steps with a specific quantizer (e.g., GPTQQuantizer) # and a dataloader would be required. quant_config = BaseQuantizationConfig( quant_method="none", # Use "gptq", "hqq", "awq" for actual methods w_bits=8, w_group_size=128, # Not strictly applicable for "none", but often part of config w_sym=False, w_mse_scheme="per_tensor" ) # 3. Convert the PyTorch model into a DiffQModel # This automatically replaces modules with their quantized counterparts based on config. diffq_model = DiffQModel(model, quantization_config=quant_config) # Print the model structure to see the converted modules print("Original model:") print(model) print("\nDiffQModel (converted structure):") print(diffq_model) # Example forward pass (will not perform actual quantization during inference # without a preceding quantizer.quantize() call for methods like GPTQ) dummy_input = torch.randn(1, 10) output = diffq_model(dummy_input) print(f"\nOutput shape: {output.shape}")
Debug
Known issues
gotchaQuantization libraries like `diffq` and its dependency `pytorch_quantization` are highly sensitive to PyTorch and CUDA version compatibility. Mismatches can lead to cryptic `RuntimeError`s, `cuDNN` errors, or unexpected behavior.
fix
Verify your PyTorch, CUDA toolkit, and GPU driver versions against the `pytorch_quantization` and `diffq` requirements (usually found in their GitHub repositories' `setup.py` or documentation). Reinstalling PyTorch with the correct CUDA version is often necessary.
affects: All versions
gotchaMany advanced quantization methods (e.g., HQQ, AWQ, 8-bit quantization via `bitsandbytes`) require additional, often hardware-specific, optional dependencies. Forgetting to install these will result in `ModuleNotFoundError` or `ImportError` when attempting to use the corresponding methods.
fix
Install the specific optional dependencies required for your desired quantization method. For example, `pip install diffq[hqq_ext]` for HQQ, or `pip install bitsandbytes` for 8-bit quantization.
affects: All versions
gotchaWhile `diffq` supports general PyTorch models, its primary examples and optimizations are often for large language models (LLMs) from the `transformers` library. Applying aggressive quantization to arbitrary custom models or models with complex, non-standard layers may require manual adaptations or might not yield optimal results.
fix
Review `diffq` documentation on supported module types and recommended practices for custom models. Start with simpler quantization schemes and gradually increase complexity. Be prepared to implement custom `QuantizedModule`s if necessary.
affects: All versions
Upgrade
Version history
0.2.4latest on PyPI · released May 5, 2023
Audit
Dependencies
pytorch_quantizationrequiredCore dependency for low-level quantization operations.
torchrequiredUnderlying deep learning framework. Version compatibility is crucial.
transformersoptionalCommonly used for integrating with pre-trained models, especially LLMs, in many examples.
bitsandbytesoptionalRequired for 8-bit quantization methods, especially with Hugging Face Transformers.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
diffq — pip install diffq · libregistry