Test that quantized parameters have proper gradient behavior.
(device, dtype)
| 413 | @pytest.mark.parametrize("device", get_available_devices()) |
| 414 | @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) |
| 415 | def test_gradient_behavior(device, dtype): |
| 416 | """Test that quantized parameters have proper gradient behavior.""" |
| 417 | if device == "hpu" and not is_supported_on_hpu("nf4", dtype): |
| 418 | pytest.skip("Configuration not supported on HPU.") |
| 419 | |
| 420 | module = ParametrizeTestModule(device=device, dtype=dtype) |
| 421 | |
| 422 | # Ensure original parameter requires gradients |
| 423 | module.weight_2d.requires_grad_(True) |
| 424 | assert module.weight_2d.requires_grad, "Original parameter should require gradients" |
| 425 | |
| 426 | # Apply quantization parametrization |
| 427 | replace_parameter_4bit(module, "weight_2d", quant_type="nf4") |
| 428 | |
| 429 | # Verify that quantized parameters don't require gradients (expected behavior) |
| 430 | # The underlying quantized parameter should have requires_grad=False |
| 431 | # The dequantized output should also not require gradients |
| 432 | reconstructed = module.weight_2d |
| 433 | assert not reconstructed.requires_grad, "Dequantized parameter should not require gradients" |
| 434 | |
| 435 | |
| 436 | class TestParametrizationCacheCounterUnderCheckpointing: |
nothing calls this directly
no test coverage detected