replace batch_norm to numerically identical without cudnn (#51734)

Signed-off-by: khushali9 <khushali.desai9@gmail.com>
Signed-off-by: wang.yuqi <yuqi.wang@daocloud.io>
Co-authored-by: wang.yuqi <yuqi.wang@daocloud.io>
This commit is contained in:
Khushali Desai
2026-08-11 12:43:20 +08:00
committed by GitHub
co-authored by wang.yuqi
parent 90fd4a333f
commit dc5101fb1b
3 changed files with 80 additions and 19 deletions
+60
View File
@@ -13,6 +13,7 @@ from vllm.distributed.parallel_state import (
initialize_model_parallel,
)
from vllm.model_executor.models.vision import (
FusedInputNorm,
get_load_balance_assignment,
resolve_visual_encoder_outputs,
run_dp_sharded_mrope_vision_model,
@@ -492,3 +493,62 @@ def test_simple_mrope_vision_model_spatial_merge(spatial_merge_size: int):
assert output.shape[0] == expected_output_patches
assert output.shape[1] == vision_model.out_hidden_size
def _reference_input_norm(
pixel_values: torch.Tensor,
image_mean: list[float],
image_std: list[float],
rescale_factor: float,
channel: int,
out_dtype: torch.dtype,
) -> torch.Tensor:
"""Straightforward per-channel affine: (x * rescale - mean) / std."""
patches, size = pixel_values.shape
patch_size = size // channel
mean = torch.tensor(image_mean, dtype=torch.float32).view(1, channel, 1)
std = torch.tensor(image_std, dtype=torch.float32).view(1, channel, 1)
x = pixel_values.to(torch.float32).view(patches, channel, patch_size)
x = (x * rescale_factor - mean) / std
return x.view(patches, size).to(out_dtype)
@pytest.mark.parametrize("num_patches", [1, 37, 70000])
def test_fused_input_norm_matches_reference(num_patches: int):
"""FusedInputNorm must equal the plain affine, including for num_patches
above the cuDNN batch-norm grid limit (~65535) that previously raised
CUDNN_STATUS_INTERNAL_ERROR (issue #51717)."""
channel = 3
patch_size = 14 * 14
image_mean = [0.48145466, 0.4578275, 0.40821073]
image_std = [0.26862954, 0.26130258, 0.27577711]
rescale_factor = 1.0 / 255.0
set_random_seed(0)
pixel_values = torch.randint(
0, 256, (num_patches, channel * patch_size), dtype=torch.float32
)
norm = FusedInputNorm(
image_mean=image_mean,
image_std=image_std,
rescale_factor=rescale_factor,
channel=channel,
)
assert not norm.is_identity
out = norm(pixel_values, visual_dtype=torch.float32)
expected = _reference_input_norm(
pixel_values, image_mean, image_std, rescale_factor, channel, torch.float32
)
torch.testing.assert_close(out, expected)
def test_fused_input_norm_identity_passthrough():
"""The identity configuration returns the input unchanged (cast only)."""
norm = FusedInputNorm.identity()
assert norm.is_identity
pixel_values = torch.randn(8, 3 * 196, dtype=torch.float32)
out = norm(pixel_values, visual_dtype=torch.bfloat16)
torch.testing.assert_close(out, pixel_values.to(torch.bfloat16))