@@ -94,6 +94,10 @@ class _InstallPlan:
9494 layers : tuple [_AttentionPlan , ...]
9595 quantize : bool
9696 sparse_algorithm : str | None
97+ q_format : str = "nvfp4"
98+ k_format : str = "nvfp4"
99+ p_format : str = "nvfp4"
100+ v_format : str = "nvfp4"
97101
98102
99103def _unwrapped_model (model_runner ):
@@ -315,7 +319,16 @@ def _raise_unsupported(errors: list[str], policy: str) -> None:
315319 )
316320
317321
318- def _plan_vllm_attention (model_runner , * , quantize : bool , sparse_cfg ) -> _InstallPlan :
322+ def _plan_vllm_attention (
323+ model_runner ,
324+ * ,
325+ quantize : bool ,
326+ sparse_cfg ,
327+ q_format : str = "nvfp4" ,
328+ k_format : str = "nvfp4" ,
329+ p_format : str = "nvfp4" ,
330+ v_format : str = "nvfp4" ,
331+ ) -> _InstallPlan :
319332 model = _unwrapped_model (model_runner )
320333 resolved_sparse_cfg , sparse_algorithm = _resolve_sparse_config (model_runner , sparse_cfg )
321334 candidates = []
@@ -329,7 +342,9 @@ def _plan_vllm_attention(model_runner, *, quantize: bool, sparse_cfg) -> _Instal
329342 candidates .append ((name , module , sparse_kw ))
330343
331344 if not candidates and not quantize :
332- return _InstallPlan (model_runner , (), False , sparse_algorithm )
345+ return _InstallPlan (
346+ model_runner , (), False , sparse_algorithm , q_format , k_format , p_format , v_format
347+ )
333348
334349 _require_supported_vllm ()
335350 errors = _global_errors (model_runner ) if quantize else []
@@ -373,7 +388,16 @@ def _plan_vllm_attention(model_runner, *, quantize: bool, sparse_cfg) -> _Instal
373388 if quantize and attention_count == 0 :
374389 errors .append ("no regular attention layers were found" )
375390 _raise_unsupported (errors , "NVFP4 attention" if quantize else "sparse attention" )
376- return _InstallPlan (model_runner , tuple (plans ), quantize , sparse_algorithm )
391+ return _InstallPlan (
392+ model_runner ,
393+ tuple (plans ),
394+ quantize ,
395+ sparse_algorithm ,
396+ q_format ,
397+ k_format ,
398+ p_format ,
399+ v_format ,
400+ )
377401
378402
379403def _build_report (plan : _InstallPlan ) -> VllmAttentionInstallReport :
@@ -405,15 +429,36 @@ def _apply_vllm_attention_plans(plan: _InstallPlan) -> VllmAttentionInstallRepor
405429 for layer in plan .layers :
406430 layer .new_impl .sparse_kw = layer .sparse_kw
407431 if plan .quantize :
432+ # Pass cfg only for non-default formats: keeps the default call
433+ # signature stable for callers/fakes that predate the cfg parameter.
434+ _cfg_kwargs = (
435+ {
436+ "cfg" : quant_plugin .build_vllm_attention_quant_cfg (
437+ q_format = plan .q_format ,
438+ k_format = plan .k_format ,
439+ p_format = plan .p_format ,
440+ v_format = plan .v_format ,
441+ )
442+ }
443+ if (plan .q_format , plan .k_format , plan .p_format , plan .v_format )
444+ != ("nvfp4" , "nvfp4" , "nvfp4" , "nvfp4" )
445+ else {}
446+ )
408447 converted = quant_plugin .configure_vllm_nvfp4_attention_quantizers (
409448 layer .module ,
410449 device = layer .device ,
411450 dtype = layer .dtype ,
451+ ** _cfg_kwargs ,
412452 )
413453 if converted is not None and converted is not layer .module :
414454 raise RuntimeError ("vLLM attention quantization must convert modules in place" )
415455 p_qdq , p_qdq_amax = attention_plugin ._p_qdq_from_layer (layer .module )
416456 v_qdq , v_qdq_amax = attention_plugin ._v_qdq_from_layer (layer .module )
457+ if plan .v_format == "fp8" :
458+ # Per-tensor FP8 V is quantized module-level BEFORE the cache
459+ # write (each token is self-contained; no block geometry), so
460+ # the kernel sees pre-QDQ'd V and needs no V transform at all.
461+ v_qdq , v_qdq_amax = None , None
417462 layer .new_impl .quant_kw = {
418463 "p_qdq" : p_qdq ,
419464 "p_qdq_amax" : p_qdq_amax ,
@@ -425,8 +470,10 @@ def _apply_vllm_attention_plans(plan: _InstallPlan) -> VllmAttentionInstallRepor
425470 old_query_flag = getattr (layer .module , "_query_quant_in_kernel" , missing )
426471 old_value_flag = getattr (layer .module , "_value_quant_in_kernel" , missing )
427472 if plan .quantize :
428- layer .module ._query_quant_in_kernel = True
429- layer .module ._value_quant_in_kernel = True
473+ # fp8 Q is module-level (bf16 losslessly carries E4M3 QDQ values);
474+ # the kernel then runs a plain bf16 BMM1 with no Q transform.
475+ layer .module ._query_quant_in_kernel = plan .q_format != "fp8"
476+ layer .module ._value_quant_in_kernel = plan .v_format != "fp8"
430477 try :
431478 # Publish the adapter last so a native impl never runs with in-kernel
432479 # quantization flags that only the ModelOpt adapter understands.
@@ -463,6 +510,10 @@ def install_vllm_nvfp4_attention(
463510 model_runner ,
464511 * ,
465512 sparse_cfg = "checkpoint" ,
513+ q_format : str = "nvfp4" ,
514+ k_format : str = "nvfp4" ,
515+ p_format : str = "nvfp4" ,
516+ v_format : str = "nvfp4" ,
466517) -> VllmAttentionInstallReport :
467518 """Install fixed NVFP4 attention with optional checkpoint sparsity.
468519
@@ -471,6 +522,22 @@ def install_vllm_nvfp4_attention(
471522 sparse_cfg: ``"checkpoint"`` to consume optional exported metadata, a
472523 resolved sparse config dict, or ``None`` for NVFP4-only attention.
473524 """
525+ for name , fmt in (
526+ ("q_format" , q_format ),
527+ ("k_format" , k_format ),
528+ ("p_format" , p_format ),
529+ ("v_format" , v_format ),
530+ ):
531+ if fmt not in ("nvfp4" , "fp8" ):
532+ raise ValueError (f"{ name } must be 'nvfp4' or 'fp8', got { fmt !r} " )
474533 return _apply_vllm_attention_plans (
475- _plan_vllm_attention (model_runner , quantize = True , sparse_cfg = sparse_cfg )
534+ _plan_vllm_attention (
535+ model_runner ,
536+ quantize = True ,
537+ sparse_cfg = sparse_cfg ,
538+ q_format = q_format ,
539+ k_format = k_format ,
540+ p_format = p_format ,
541+ v_format = v_format ,
542+ )
476543 )
0 commit comments