Skip to content

Commit fba601b

Browse files
committed
Update benchmark
1 parent 1680485 commit fba601b

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

python/benchmark/benchmark.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,38 @@
1414
torch.set_num_threads(multiprocessing.cpu_count())
1515

1616
NUM_RUNS: int = 1_000
17-
NUMEL: int = 10000
17+
NUMEL: int = 1000000
1818

1919
QUANT_DTYPES_TO_BENCH: list[torch.dtype] = [
2020
torch.quint8,
2121
torch.quint4x2,
2222
]
2323

24+
def quantize_torch(t: torch.Tensor, scale: float, zp: int, dtype: torch.dtype) -> torch.tensor:
25+
return torch.quantize_per_tensor(t, scale=scale, zero_point=zp, dtype=dtype)
2426

25-
def quantize_torch(t: torch.Tensor, scale: float, zp: int, dtype: torch.dtype) -> None:
26-
torch.quantize_per_tensor(t, scale=scale, zero_point=zp, dtype=dtype).int_repr()
2727

28-
29-
def quantize_piquant(t: torch.Tensor, scale: float, zp: int, dtype: torch.dtype) -> None:
30-
piquant.torch.quantize(t, scale=scale, zero_point=zp, dtype=dtype)
28+
def quantize_piquant(t: torch.Tensor, scale: float, zp: int, dtype: torch.dtype) -> torch.tensor:
29+
return piquant.torch.quantize(t, scale=scale, zero_point=zp, dtype=dtype)
3130

3231

3332
dtype_labels: list[str] = []
3433
torch_times: list[float] = []
3534
piquant_times: list[float] = []
3635

37-
tensor = torch.rand(NUMEL, dtype=torch.float32, device='cpu')
38-
3936
for torch_d in QUANT_DTYPES_TO_BENCH:
37+
tensor = torch.rand(NUMEL, dtype=torch.float32, device='cpu')
38+
torch_results = []
39+
results_piquant = []
40+
4041
scale, zp = piquant.torch.compute_quant_params(tensor, dtype=torch_d)
4142
zp = int(zp)
4243

4344
def _bench_torch() -> None:
44-
quantize_torch(tensor, scale, zp, torch_d)
45+
torch_results.append(quantize_torch(tensor, scale, zp, torch_d))
4546

4647
def _bench_piquant() -> None:
47-
quantize_piquant(tensor, scale, zp, torch_d)
48+
results_piquant.append(quantize_piquant(tensor, scale, zp, torch_d))
4849

4950
# Warmup runs
5051
_bench_torch()
@@ -55,8 +56,17 @@ def _bench_piquant() -> None:
5556
dtype_labels.append(str(torch_d).replace('torch.', ''))
5657
torch_times.append(torch_time)
5758
piquant_times.append(piquant_time)
59+
60+
# Verify that the results are the same
61+
for i in range(NUM_RUNS): # We compare dequantized results, because .int_repr() is implemented for packed types in torch
62+
dq_torch = torch_results[i].dequantize()
63+
dq_piquant = piquant.torch.dequantize(results_piquant[i], scale=scale, zero_point=zp, dtype=torch.float32)
64+
assert dq_torch.numel() == dq_piquant.numel()
65+
assert dq_torch.dtype == dq_piquant.dtype
66+
assert torch.allclose(dq_torch, dq_piquant, atol=1e-1)
5867
print(f'{dtype_labels[-1]:<10} | torch: {torch_time:.6f}s | piquant: {piquant_time:.6f}s')
5968

69+
6070
x = np.arange(len(dtype_labels))
6171
width = 0.35
6272
plt.figure(figsize=(8, 5))

0 commit comments

Comments
 (0)