Examples · Compiling

quantize_model

What quantization does to scores, measured against the float reference, with the worst-case error bound.

Code#

quantize_model.py
from _setup import X_test, whitebox
 
from compileml.compile import (
extract_trees,
max_depth,
quantization_error_bound,
quantize_model,
score_float,
)
from compileml.runtime import score_micro
 
extracted = extract_trees(whitebox)
model = quantize_model(extracted)
 
print("max depth ", max_depth(model))
print(f"quantization error bound {quantization_error_bound(model):.2e}")
 
# The float reference and the integer model on the same row. The gap is the
# quantization error, and it is bounded above rather than hoped about.
row = X_test[0]
reference = score_float(extracted, row)
integer = score_micro(model, row.tolist()) / 1_000_000
 
print()
print(f"float reference {reference:.6f}")
print(f"integer model {integer:.6f}")
print(f"difference {abs(reference - integer):.2e}")

Output#

Captured from an actual run against compileml 0.9.0 and the UCI credit panel. If this script stops working, the build fails.

captured in CIquantize_model.py
max depth 2
quantization error bound 1.55e-05
 
float reference 0.341215
integer model 0.341214
difference 1.09e-06

Notes#

  • score_float exists for extraction validation only. Production never calls it.
  • The bound is per-tree rounding error summed over trees — it is a proof obligation, not an estimate.

API reference: quantize_model →