diff --git a/examples/01_quant_linear_inference.py b/examples/01_quant_linear_inference.py index 348d473..4efe886 100644 --- a/examples/01_quant_linear_inference.py +++ b/examples/01_quant_linear_inference.py @@ -56,8 +56,7 @@ def main() -> None: print("\n[3] Converted to QuantLinear (BitNet 1.58b {-1, 0, +1}):") print(f" Packed Buffer : {list(q_ternary.packed_weight.shape)} (4 weights/byte)") print( - f" Memory Footprint : {mb_t:.2f} MB " - f"({r_t_fp32:.2f}x vs FP32, {r_t_fp16:.2f}x vs FP16)" + f" Memory Footprint : {mb_t:.2f} MB ({r_t_fp32:.2f}x vs FP32, {r_t_fp16:.2f}x vs FP16)" ) # 3. Ampere 2:4 Structured Sparsity QuantLinear @@ -71,8 +70,7 @@ def main() -> None: print(f" Condensed Buffer : {list(q_sparse.condensed_weight.shape)} (50% non-zeros)") print(f" Index Metadata : {list(q_sparse.packed_metadata.shape)} (2-bit column nibbles)") print( - f" Memory Footprint : {mb_s:.2f} MB " - f"({r_s_fp32:.2f}x vs FP32, {r_s_fp16:.2f}x vs FP16)" + f" Memory Footprint : {mb_s:.2f} MB ({r_s_fp32:.2f}x vs FP32, {r_s_fp16:.2f}x vs FP16)" ) # 4. Verify Forward Pass & Reconstruction SQNR diff --git a/pyproject.toml b/pyproject.toml index 9b1bc01..b156a48 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "quantbench-engine" -version = "0.1.0" +version = "0.1.1" description = "First-principles LLM quantization engine, bit-packing runtimes, 2:4 sparsity, and Quality-vs-Speed Pareto evaluation benchmark." readme = "README.md" license = "Apache-2.0" diff --git a/src/quantbench/__init__.py b/src/quantbench/__init__.py index e492514..dbe7c13 100644 --- a/src/quantbench/__init__.py +++ b/src/quantbench/__init__.py @@ -1,6 +1,6 @@ """quantbench: Unified Quantization Engine, Bit-Packing Runtime & Quality-vs-Speed Benchmark.""" -__version__ = "0.1.0" +__version__ = "0.1.1" from quantbench.bench import ParetoBenchmark, ParetoPoint from quantbench.eval import ( diff --git a/src/quantbench/bench/pareto.py b/src/quantbench/bench/pareto.py index f1ed2bf..a89fc3a 100644 --- a/src/quantbench/bench/pareto.py +++ b/src/quantbench/bench/pareto.py @@ -82,9 +82,7 @@ def run_sweep( stride=self.stride, ) - base_bytes = sum( - p.numel() * 2 for p in self.base_model.parameters() - ) + base_bytes = sum(p.numel() * 2 for p in self.base_model.parameters()) base_mb = base_bytes / (1024 * 1024) points: List[ParetoPoint] = [] diff --git a/src/quantbench/cli.py b/src/quantbench/cli.py index f0d9ea4..745edff 100644 --- a/src/quantbench/cli.py +++ b/src/quantbench/cli.py @@ -257,9 +257,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: model = DemoTransformer(vocab=vocab_size, dim=hidden_dim) tokens = generate_synthetic_corpus(vocab_size=vocab_size, seq_length=seq_len, seed=1) - bench = ParetoBenchmark( - base_model=model, test_tokens=tokens, window_size=64, stride=32 - ) + bench = ParetoBenchmark(base_model=model, test_tokens=tokens, window_size=64, stride=32) console.print( "[bold yellow]Running Pareto sweep across all precision runtimes...[/bold yellow]" ) @@ -279,9 +277,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: for p in points: grp = str(p.group_size) if p.group_size > 0 else "None" delta_str = ( - f"+{p.perplexity_delta:.3f}" - if p.perplexity_delta > 0 - else f"{p.perplexity_delta:.3f}" + f"+{p.perplexity_delta:.3f}" if p.perplexity_delta > 0 else f"{p.perplexity_delta:.3f}" ) table.add_row( p.name, diff --git a/src/quantbench/quant/algorithms/awq.py b/src/quantbench/quant/algorithms/awq.py index 03d51ab..c85bdc6 100644 --- a/src/quantbench/quant/algorithms/awq.py +++ b/src/quantbench/quant/algorithms/awq.py @@ -103,11 +103,13 @@ def search_best_awq_scale( - best_alpha: Optimal alpha hyperparameter. """ if quant_fn is None: + def default_quant(w: torch.Tensor) -> torch.Tensor: q_b, s_b, zp_b, meta = quantize_blockwise( w, group_size=min(128, w.shape[1]), num_bits=4 ) return dequantize_blockwise(q_b, s_b, zp_b, meta) + quant_fn = default_quant # Reference unquantized linear output @@ -169,11 +171,13 @@ def awq_quantize_weights( Tuple of (dequantized_weight, scale). """ if quant_fn is None: + def default_quant(w: torch.Tensor) -> torch.Tensor: q_b, s_b, zp_b, meta = quantize_blockwise( w, group_size=min(128, w.shape[1]), num_bits=4 ) return dequantize_blockwise(q_b, s_b, zp_b, meta) + quant_fn = default_quant w_scaled = weight * scale.unsqueeze(0) diff --git a/src/quantbench/quant/algorithms/gptq.py b/src/quantbench/quant/algorithms/gptq.py index caa6fc9..8e8a284 100644 --- a/src/quantbench/quant/algorithms/gptq.py +++ b/src/quantbench/quant/algorithms/gptq.py @@ -132,9 +132,7 @@ def gptq_quantize_weights( def quantize_dequantize_cols(col_w: torch.Tensor) -> torch.Tensor: # col_w: (out_features, num_cols) if group_size > 0: - q_b, s_b, zp_b, meta = quantize_blockwise( - col_w, group_size=group_size, num_bits=bits - ) + q_b, s_b, zp_b, meta = quantize_blockwise(col_w, group_size=group_size, num_bits=bits) return dequantize_blockwise(q_b, s_b, zp_b, meta) else: q, scale = quantize_absmax(col_w, num_bits=bits, dim=0) diff --git a/src/quantbench/quant/packing/sparse_pack.py b/src/quantbench/quant/packing/sparse_pack.py index 511d772..2dde166 100644 --- a/src/quantbench/quant/packing/sparse_pack.py +++ b/src/quantbench/quant/packing/sparse_pack.py @@ -48,12 +48,7 @@ def pack_2_4_indices(indices: torch.Tensor) -> torch.Tensor: idx2 = i32[..., 2::4] idx3 = i32[..., 3::4] - packed = ( - (idx0 & 0x03) - | ((idx1 & 0x03) << 2) - | ((idx2 & 0x03) << 4) - | ((idx3 & 0x03) << 6) - ) + packed = (idx0 & 0x03) | ((idx1 & 0x03) << 2) | ((idx2 & 0x03) << 4) | ((idx3 & 0x03) << 6) return packed.to(torch.uint8) diff --git a/tests/test_packing.py b/tests/test_packing.py index 7cca3a1..a476f00 100644 --- a/tests/test_packing.py +++ b/tests/test_packing.py @@ -163,10 +163,13 @@ class TestSparse24Packing: def test_index_packing_roundtrip(self) -> None: """Test packing and unpacking of 2-bit column position indices.""" - indices = torch.tensor([ - [0, 2, 1, 3], - [1, 2, 0, 3], - ], dtype=torch.int32) + indices = torch.tensor( + [ + [0, 2, 1, 3], + [1, 2, 0, 3], + ], + dtype=torch.int32, + ) packed = pack_2_4_indices(indices) assert packed.dtype == torch.uint8 diff --git a/tests/test_primitives.py b/tests/test_primitives.py index fc88525..47e158b 100644 --- a/tests/test_primitives.py +++ b/tests/test_primitives.py @@ -167,8 +167,8 @@ def test_fp8_analytical_emulator(): # E4M3 (max 448.0) q_e4m3 = _emulate_fp8(x, exp_bits=4, mantissa_bits=3, bias=7, max_val=448.0) assert q_e4m3[0].item() == -448.0 # saturated at -max - assert q_e4m3[6].item() == 448.0 # saturated at +max - assert q_e4m3[3].item() == 0.0 # exact zero + assert q_e4m3[6].item() == 448.0 # saturated at +max + assert q_e4m3[3].item() == 0.0 # exact zero # E5M2 (max 57344.0) q_e5m2 = _emulate_fp8(x, exp_bits=5, mantissa_bits=2, bias=15, max_val=57344.0) @@ -203,4 +203,3 @@ def test_cli_runner(): # Test invalid format res_err = runner.invoke(app, ["pack-inspect", "--format", "invalid_format"]) assert res_err.exit_code != 0 - diff --git a/tests/test_quant_linear.py b/tests/test_quant_linear.py index d053ece..eb3b88e 100644 --- a/tests/test_quant_linear.py +++ b/tests/test_quant_linear.py @@ -20,9 +20,7 @@ def test_int4_blockwise_forward_pass(self) -> None: in_f, out_f = 64, 128 linear = nn.Linear(in_f, out_f, bias=True) - quant_linear = QuantLinear.from_linear( - linear, scheme="int4", group_size=32, symmetric=True - ) + quant_linear = QuantLinear.from_linear(linear, scheme="int4", group_size=32, symmetric=True) # 128 * 64 = 8192 elements -> 8192 / 32 = 256 blocks of 32 elements # 32 elements pack into 16 uint8 bytes @@ -43,9 +41,7 @@ def test_int4_per_channel_forward_pass(self) -> None: in_f, out_f = 64, 128 linear = nn.Linear(in_f, out_f, bias=True) - quant_linear = QuantLinear.from_linear( - linear, scheme="int4", group_size=-1, symmetric=True - ) + quant_linear = QuantLinear.from_linear(linear, scheme="int4", group_size=-1, symmetric=True) # Per-channel: (out_f, in_f // 2) = (128, 32) assert quant_linear.packed_weight.shape == (out_f, in_f // 2) @@ -145,6 +141,7 @@ class TestModelConvert: def test_convert_transformer_mlp_block(self) -> None: """Verify recursive conversion swaps targeted linear layers while respecting exclusions.""" + class MockTransformerMLP(nn.Module): def __init__(self, dim: int, hidden_dim: int): super().__init__()