sgl-project/sglang · error · ValueError

encounter invalid h_bar: {h_bar}, w_bar: {w_bar}

Error message

encounter invalid h_bar: {h_bar}, w_bar: {w_bar}

What it means

Raised by ernie45_vl.smart_resize after scaling the image to satisfy a min_pixels constraint: the ceil-by-factor adjustment pushed h_bar*w_bar below min_pixels or above max_pixels, so no valid factored resolution exists. This happens when min_pixels/max_pixels are inconsistent (min > max) or the pixel bounds are incompatible with the factor (patch size) quantization.

Source

Thrown at python/sglang/srt/multimodal/processors/ernie45_vl.py:79

            new_height = max(factor, round_by_factor(height, factor))
            new_width = floor_by_factor(new_height * MAX_RATIO, factor)

        height = new_height
        width = new_width

    h_bar = max(factor, round_by_factor(height, factor))
    w_bar = max(factor, round_by_factor(width, factor))
    if h_bar * w_bar > max_pixels:
        beta = math.sqrt((height * width) / max_pixels)
        h_bar = floor_by_factor(height / beta, factor)
        w_bar = floor_by_factor(width / beta, factor)
    elif h_bar * w_bar < min_pixels:
        beta = math.sqrt(min_pixels / (height * width))
        h_bar = ceil_by_factor(height * beta, factor)
        w_bar = ceil_by_factor(width * beta, factor)

    if min_pixels > h_bar * w_bar or h_bar * w_bar > max_pixels:
        raise ValueError(f"encounter invalid h_bar: {h_bar}, w_bar: {w_bar}")

    return h_bar, w_bar


def resize_image(
    image,
    min_pixels: int = MIN_PIXELS,
    max_pixels: int = MAX_PIXELS,
    size_factor: int = IMAGE_FACTOR,
) -> Image.Image:
    width, height = image.size
    min_pixels = min_pixels
    max_pixels = max_pixels
    resized_height, resized_width = smart_resize(
        height,
        width,
        factor=size_factor,
        min_pixels=min_pixels,

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure min_pixels <= max_pixels and leave a factor-sized gap between them (multiple of 28*28=784 for factor 16 style patches)
  2. Sanitize user overrides before calling preprocess: clamp min_pixels to at most max_pixels
  3. Reject or upscale degenerate images (width/height < factor) before preprocessing

Example fix

# before
res = processor.resize_image(img, min_pixels=1003520, max_pixels=786432)
# after
MIN = 4*28*28*4; MAX = 16384*28*28
res = processor.resize_image(img, min_pixels=min(MIN, MAX), max_pixels=MAX)
Defensive patterns

Strategy: validation

Validate before calling

F = 28; FAC = F*F
if not (min_pixels <= max_pixels) or (max_pixels - min_pixels) < 4*FAC:
    min_pixels = min(min_pixels, max_pixels - 4*FAC)
if img.width < F or img.height < F:
    raise SkipSample('image smaller than patch factor')

Type guard

def sane_pixel_bounds(min_p: int, max_p: int, factor: int = 28) -> bool:
    return min_p <= max_p and (max_p - min_p) >= factor*factor

Try / catch

try:
    h, w = smart_resize(ih, iw, factor, min_p, max_p)
except ValueError:
    h, w = smart_resize(ih, iw, factor, FACTOR*FACTOR*4, 16384*FACTOR*FACTOR)  # safe defaults

Prevention

When it happens

Trigger: Calling resize_image or preprocess_video with min_pixels > max_pixels; min_pixels so close to max_pixels that ceil_by_factor jumps over the window; height or width of 0 / degenerate input.

Common situations: Overriding processor kwargs with user-supplied min_pixels/max_pixels without ordering them (e.g. min=1000000, max=786432); copying limits tuned for a different patch factor; tiny/corrupt images where scaling can't land on a factor multiple inside bounds.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/03b57b617ebee919. Report an issue: GitHub.