Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Assert all ptq-common bit widths are positive integers #931

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions src/brevitas_examples/imagenet_classification/ptq/ptq_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from copy import deepcopy

import math
import torch
import torch.backends.cudnn as cudnn
from tqdm import tqdm
Expand Down Expand Up @@ -153,6 +154,20 @@ def quantize_model(
weight_scale_type = scale_factor_type
act_scale_type = scale_factor_type

# We check all of the provided values are positive integers
check_bit_widths(
weight_bit_width,
act_bit_width,
bias_bit_width,
layerwise_first_last_bit_width,
layerwise_first_last_mantissa_bit_width,
layerwise_first_last_exponent_bit_width,
weight_mantissa_bit_width,
weight_exponent_bit_width,
act_mantissa_bit_width,
act_exponent_bit_width,
)

weight_quant_format = quant_format
act_quant_format = quant_format

Expand Down Expand Up @@ -535,3 +550,11 @@ def apply_learned_round_learning(
pbar.set_description(
"loss = {:.4f}, rec_loss = {:.4f}, round_loss = {:.4f}, b = {:.4f}".format(
loss, rec_loss, round_loss, b))

def check_bit_widths(*args):
"""
We check that every inputted value is positive, and an integer.
"""
for arg in args:
assert arg > 0.0
assert math.isclose(arg % 1, 0.0)
Loading