Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix binary search #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions model_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,23 @@ def quantize_array_lbda(A, lbda):
def quantize_array_target(A, target_err):
low = 1
high = 128
mid = low
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this

A_norms = np.sqrt(np.sum(A**2, axis=-1))

while high - low > 1:
mid = (high + low) / 2
while low < high:
mid = low + (high - low) / 2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python has arbitrary precision integers, no need to do this

quant_A, dequant_A = quantize_array(A, mid)
mean_err = np.mean(np.sqrt(np.sum((dequant_A - A)**2, axis=-1)) / A_norms)
logging.info("Binary search: q=%d, err=%.3f", mid, mean_err)

if mean_err > target_err:
low = mid
low = mid + 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the loop invariant that error(high) <= target_err < error(low)

else:
high = mid

mid = low
quant_A, dequant_A = quantize_array(A, mid)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copypasta

mean_err = np.mean(np.sqrt(np.sum((dequant_A - A)**2, axis=-1)) / A_norms)
logging.info("Result: q=%d, err=%.3f", mid, mean_err)
return mid, mean_err, quant_A, dequant_A


Expand Down