sgl-project/sglang · error · ValueError

n must be a positive integer

Error message

n must be a positive integer

What it means

_prime_factors validates its input and raises ValueError for n < 1, since prime factorization is undefined for zero/negative numbers. plan_out_scales calls it to factor MLP layer counts, so a zero or negative layer/size value propagates here. It signals an invalid model config (e.g. n_layers = 0 or a negative dimension) rather than a runtime math bug.

Source

Thrown at python/sglang/srt/models/inkling_common/hmlp.py:17

from __future__ import annotations

from typing import cast

import numpy as np
import torch
from torch import nn
from torch.nn import functional as F

from sglang.srt.configs.inkling import InklingVisionConfig
from sglang.srt.models.inkling_common.norm import RMSNorm


def _prime_factors(n: int) -> list[int]:
    """Return the prime factors of ``n`` in ascending order."""
    if n < 1:
        raise ValueError("n must be a positive integer")

    factors: list[int] = []

    while n % 2 == 0:
        factors.append(2)
        n //= 2

    p = 3
    while p * p <= n:
        while n % p == 0:
            factors.append(p)
            n //= p
        p += 2

    if n > 1:
        factors.append(n)
    return factors

View on GitHub (pinned to 0132848349)

Solutions

  1. Fix the model config so the layer/size integers passed to the HMLP planner are >= 1 (check config.json num_hidden_layers and vision patch sizes)
  2. Validate and clamp/abort early on invalid config before model construction
  3. If building the config programmatically, assert the values before calling plan_out_scales

Example fix

# before
plan_out_scales(temporal_patch_size=3, patch_size=16, n_layers=0)
# after
assert n_layers >= 1
plan_out_scales(temporal_patch_size=3, patch_size=16, n_layers=n_layers)
Defensive patterns

Strategy: validation

Validate before calling

if temporal_patch_size < 1 or patch_size <= 1 or n_layers < 1:
    raise ConfigError("HMLP planner requires positive ints")
plan_out_scales(temporal_patch_size, patch_size, n_layers)

Type guard

def is_valid_hmlp_config(t: int, p: int, l: int) -> bool:
    return isinstance(t, int) and isinstance(p, int) and isinstance(l, int) and t >= 1 and p > 1 and l >= 1

Prevention

When it happens

Trigger: Calling plan_out_scales(temporal_patch_size, patch_size, n_layers, ...) (from HMLP __init__) with a non-positive value that reaches _prime_factors — typically n_layers <= 0 or a temporal_patch_size <= 0 being factored.

Common situations: Loading a checkpoint/config with num_hidden_layers: 0; typo'd config (negative or zero patch/layer counts); test code passing 0 defaults; sliced/partial configs from a quantized export.

Related errors


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