MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / test_gradient_behavior

Function test_gradient_behavior

tests/test_parametrize.py:415–433  ·  view source on GitHub ↗

Test that quantized parameters have proper gradient behavior.

(device, dtype)

Source from the content-addressed store, hash-verified

413@pytest.mark.parametrize("device", get_available_devices())
414@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype)
415def 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
436class TestParametrizationCacheCounterUnderCheckpointing:

Callers

nothing calls this directly

Calls 3

is_supported_on_hpuFunction · 0.90
replace_parameter_4bitFunction · 0.90

Tested by

no test coverage detected