Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
irenaby committed Sep 16, 2024
1 parent 0ea601a commit ef39522
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion model_compression_toolkit/gptq/common/gptq_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class QFractionLinearAnnealingConfig:

def __post_init__(self):
if not (0 <= self.initial_q_fraction < self.target_q_fraction <= 1):
raise ValueError(f'0 <= self.initial_q_fraction < self.target_q_fraction <= 1, received initial_q_fraction '
raise ValueError(f'Expected 0 <= initial_q_fraction < target_q_fraction <= 1, received initial_q_fraction '
f'{self.initial_q_fraction} and target_q_fraction {self.target_q_fraction}.')
if self.start_step < 0:
raise ValueError(f'Expected start_step >= 0. received {self.start_step}.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from model_compression_toolkit.trainable_infrastructure.pytorch.annealing_schedulers import LinearAnnealingScheduler


WARMUP_STEP_FRACTION = 0.2

def get_regularization(gptq_config: GradientPTQConfig, get_total_grad_steps_fn: Callable[[], int]) -> Callable:
"""
Returns a function that computes the regularization term for GPTQ training based on the given
Expand All @@ -35,7 +37,7 @@ def get_regularization(gptq_config: GradientPTQConfig, get_total_grad_steps_fn:
"""
if gptq_config.rounding_type == RoundingType.SoftQuantizer:
total_gradient_steps = get_total_grad_steps_fn()
t_start = int(0.2 * total_gradient_steps)
t_start = int(WARMUP_STEP_FRACTION * total_gradient_steps)
scheduler = LinearAnnealingScheduler(t_start=t_start, t_end=total_gradient_steps, initial_val=20, target_val=2)
return SoftQuantizerRegularization(scheduler)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ def test_gptq(self):
weights_quant_method=QuantizationMethod.UNIFORM,
params_learning=False).run_test() # TODO: When params learning is True, the uniform quantizer gets a min value > max value

# gradual activation quantization
def test_gptq_with_gradual_activation(self):
"""
This test checks the GPTQ feature with gradual activation quantization.
"""
GPTQAccuracyTest(self, gradual_activation_quantization=True).run_test()
GPTQAccuracyTest(self, rounding_type=RoundingType.SoftQuantizer,
gradual_activation_quantization=True).run_test()
Expand Down
22 changes: 11 additions & 11 deletions tests_pytest/pytorch/gptq/test_annealing_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@


def test_linear_annealing_cfg_validation():
with pytest.raises(ValueError, match='Expected.* initial_factor <= 1'):
QFractionLinearAnnealingConfig(initial_factor=1.1, target_factor=0.1, start_step=0, end_step=None)
with pytest.raises(ValueError, match='Expected.* target_q_fraction <= 1'):
QFractionLinearAnnealingConfig(initial_q_fraction=0.1, target_q_fraction=1.1, start_step=0, end_step=None)

with pytest.raises(ValueError, match='Expected.* 0 <= target_factor'):
QFractionLinearAnnealingConfig(initial_factor=0.9, target_factor=-0.1, start_step=0, end_step=100)
with pytest.raises(ValueError, match='Expected.* 0 <= initial_q_fraction'):
QFractionLinearAnnealingConfig(initial_q_fraction=-0.1, target_q_fraction=-0.9, start_step=0, end_step=100)

with pytest.raises(ValueError, match='Expected.* target_factor < initial_factor'):
QFractionLinearAnnealingConfig(initial_factor=0.1, target_factor=0.1, start_step=0, end_step=100)
with pytest.raises(ValueError, match='Expected.* initial_q_fraction < target_q_fraction'):
QFractionLinearAnnealingConfig(initial_q_fraction=0.1, target_q_fraction=0.1, start_step=0, end_step=100)

with pytest.raises(ValueError, match='Expected.* target_factor < initial_factor'):
QFractionLinearAnnealingConfig(initial_factor=0.1, target_factor=0.2, start_step=0, end_step=100)
with pytest.raises(ValueError, match='Expected.* initial_q_fraction < target_q_fraction'):
QFractionLinearAnnealingConfig(initial_q_fraction=0.2, target_q_fraction=0.1, start_step=0, end_step=100)

with pytest.raises(ValueError, match='Expected.* start_step >= 0'):
QFractionLinearAnnealingConfig(initial_factor=1, target_factor=0, start_step=-1, end_step=100)
QFractionLinearAnnealingConfig(initial_q_fraction=0, target_q_fraction=1, start_step=-1, end_step=100)

with pytest.raises(ValueError, match='Expected.* start_step < end_step'):
QFractionLinearAnnealingConfig(initial_factor=1, target_factor=0, start_step=100, end_step=100)
QFractionLinearAnnealingConfig(initial_q_fraction=0, target_q_fraction=1, start_step=100, end_step=100)

with pytest.raises(ValueError, match='Expected.* start_step < end_step'):
QFractionLinearAnnealingConfig(initial_factor=1, target_factor=0, start_step=100, end_step=99)
QFractionLinearAnnealingConfig(initial_q_fraction=0, target_q_fraction=1, start_step=100, end_step=99)
1 change: 1 addition & 0 deletions tests_pytest/pytorch/gptq/test_gradual_act_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_factory_linear_common_case(self, x):
assert torch.allclose(y_last, quantizer(x, True))

def _run_factory_test(self, qdrop_cfg, get_grad_steps_fn):
# Mocks are used to just pass anything
gptq_cfg = GradientPTQConfig(n_epochs=5, optimizer=Mock(), loss=Mock(),
gradual_activation_quantization_config=qdrop_cfg)
factory = get_gradual_activation_quantizer_wrapper_factory(gptq_cfg, get_grad_steps_fn)
Expand Down

0 comments on commit ef39522

Please sign in to comment.