@@ -4825,6 +4825,13 @@ def unexpected_validation(*_args, **_kwargs):
48254825 model (tensor , m_splits )
48264826 assert model ._custom_quantizer_cache == {}
48274827
4828+ def unexpected_grad_mode_query ():
4829+ pytest .fail ("built-in recipe validation must not query grad mode" )
4830+
4831+ with monkeypatch .context () as context :
4832+ context .setattr (torch , "is_grad_enabled" , unexpected_grad_mode_query )
4833+ model ._validate_custom_recipe_quantizers (False , recipe .DelayedScaling ())
4834+
48284835 def unexpected_custom_validation (* _args , ** _kwargs ):
48294836 pytest .fail ("built-in recipes must not validate custom quantizers per forward" )
48304837
@@ -4869,12 +4876,29 @@ def tracked_validate(quantizers, *, operand_name="operand"):
48694876 first_call_count = len (validation_calls )
48704877 first_generation = model ._custom_quantizer_cache ["scaling_fwd" ]
48714878 assert first_call_count > 0
4879+ assert "scaling_bwd" not in model ._custom_quantizer_cache
48724880
48734881 with torch .no_grad (), autocast (enabled = True , recipe = original_recipe ):
48744882 model (tensor , m_splits )
48754883 assert len (validation_calls ) == first_call_count
48764884 assert model ._custom_quantizer_cache ["scaling_fwd" ] is first_generation
48774885
4886+ # Backward quantizers are validated the first time quantizers are selected
4887+ # with gradients enabled. Do not run a full forward here: this validation
4888+ # test intentionally uses a columnwise-only configuration that is not
4889+ # supported by the split-quantization kernel.
4890+ with torch .enable_grad (), autocast (enabled = True , recipe = original_recipe ):
4891+ model ._get_quantizers ()
4892+ assert len (validation_calls ) == first_call_count + 1
4893+ assert model ._custom_quantizer_cache ["scaling_bwd" ] is model .quantizers ["scaling_bwd" ]
4894+
4895+ def unexpected_grad_mode_query ():
4896+ pytest .fail ("cached validation must not query grad mode" )
4897+
4898+ with monkeypatch .context () as context :
4899+ context .setattr (torch , "is_grad_enabled" , unexpected_grad_mode_query )
4900+ model ._validate_custom_recipe_quantizers (False , original_recipe )
4901+
48784902 rebuilt_recipe = recipe .CustomRecipe (qfactory = make_qfactory ("rowwise_dequantized" ))
48794903 with torch .no_grad (), autocast (enabled = True , recipe = rebuilt_recipe ):
48804904 model (tensor , m_splits )
0 commit comments