This document is relevant for: Trn2, Trn3

determine_available_memory design#

1) KV cache budget#

By default, vLLM Neuron uses the memory-utilization and fractional-cap policy:

total_budget = int(logical_core_hbm * gpu_memory_utilization)
user_budget = max(total_budget - model_weight_bytes - encoder_cache_bytes, 0)
heuristic_cap = int(total_budget * cap_fraction)
available = max(min(user_budget, heuristic_cap), 0)

The default cap_fraction is 0.30. The worker respects gpu_memory_utilization and VLLM_NEURON_KV_GMU_BUDGET_CAP_FRACTION. This conservative policy limits the KV cache to a fraction of the memory-utilization budget.

An explicit headroom value enables the reserve policy:

memory_before_headroom = max(
    logical_core_hbm - model_weight_bytes - encoder_cache_bytes, 0)
available = max(memory_before_headroom - headroom_bytes, 0)
  • logical_core_hbm is the HBM capacity of one logical NeuronCore. The device path reads it already scaled from the runtime. The CPU-compile path divides the whole-stack figure of the static table to reach it.

  • model_weight_bytes includes model parameters and buffers.

  • encoder_cache_bytes is the on-device encoder cache of a multimodal model. It is zero for a text-only model.

  • headroom_bytes reserves HBM per logical core for all other allocations.

The headroom includes internal tensors, scratchpad, DMA rings, runtime data, compiled code, and a safety margin.

Only headroom mode sets gpu_memory_utilization to 1.0 and bypasses the fractional cap. An explicit zero also enables headroom mode.

2) Why headroom is necessary#

The compiler includes the KV cache shape in each compiled program. A larger KV cache changes the graph and its memory requirements.

The compiler gives the best estimate of non-KV memory after it receives a KV shape. However, the worker must select the KV shape before compilation. The worker therefore uses a configurable estimate for non-weight and non-KV memory.

In CPU inference, the default policy uses:

total_budget = int(host_available / local_workers * gpu_memory_utilization)
available = int(total_budget * cap_fraction)

Explicit headroom uses:

available = max(host_available / local_workers - headroom_bytes, 0)

Host available memory already excludes loaded weights and current allocations, so CPU mode does not subtract model weights again.

The reserve has no default. For example, 4 GiB is a tested reserve for GPT-OSS 120B on Trn3 with the Native backend and MXFP4 weights. Validate the reserve for your model and configuration.

3) Configuration#

Variable

Default

Description

VLLM_NEURON_KV_GMU_BUDGET_CAP_FRACTION

0.30

Fractional cap for default sizing. Headroom mode bypasses this value.

VLLM_NEURON_RESERVED_HEADROOM_GIB

unset

Explicit reserve per worker. Enables headroom sizing on both backends.

VLLM_NEURON_MIN_KV_BUDGET_GIB

1.0

Minimum KV budget. The server stops if the budget is smaller.

In headroom mode, increase the active reserve if model loading or compilation reports NCC_EOOM002, Maximum peak HBM usage, NRT_RESOURCE in nrt_load_util, or Allocation Failure. First confirm that the same configuration passes with a smaller KV cache or with the default policy. Start with 1 GiB increments. Each additional 1 GiB of headroom reduces the KV cache budget by 1 GiB. Rerun the same model, sharding, backend, LNC degree, and hardware configuration, and keep the smallest value that passes. Each backend deployment can use a different value for the same headroom variable.

Do not increase headroom when the worker reports that the KV cache budget is below its minimum threshold. Decrease the active headroom variable for that error. A validated configuration can also decrease headroom to use a larger KV cache. Calculate the limit from the HBM of one logical core. At LNC 1 on trn2 and trn3, that share is half of an HBM stack:

min_kv_bytes = int(VLLM_NEURON_MIN_KV_BUDGET_GIB * 1024 ** 3)
memory_before_headroom_bytes = max(
    logical_core_hbm - model_weight_bytes - encoder_cache_bytes, 0)
max_headroom_gib = (memory_before_headroom_bytes - min_kv_bytes) / 1024 ** 3

_validate_min_kv_budget converts the minimum to bytes first, so it subtracts bytes from bytes. It converts only the result to GiB. It prints max_headroom_gib only when the value is positive.

The startup log reports the logical-core HBM, model weights, active headroom, and resulting KV budget. An override at or above the logical-core HBM share leaves no usable KV cache.

Both backends and CPU use VLLM_NEURON_RESERVED_HEADROOM_GIB. When this variable is unset, they use default fractional-cap sizing. CPU compilation and device execution select the same budget policy.

In default mode, reduce the fractional cap to reduce KV capacity after an HBM allocation failure. An explicit headroom value changes the budget policy; validate the resulting KV capacity before deployment.

4) Implementation flow#

The implementation is in vllm_neuron/vllm/worker/neuron_worker.py.

determine_available_memory()
  |
  +-- VLLM_NEURON_CPU_COMPILE == 1
  |      # The static table reports a whole stack, so divide it here.
  |      family = normalize_target_family(get_platform_target())
  |      stack_hbm = get_total_available_memory() * GiB
  |      logical_core_hbm = (
  |          stack_hbm // get_logical_cores_per_hbm_stack(family)
  |      )
  |      model_weight_bytes = _get_byte_used_from_model()
  |      return _compute_kv_budget(
  |          logical_core_hbm, model_weight_bytes, _get_encoder_cache_bytes()
  |      )
  |
  +-- VLLM_NEURON_CPU_MODE == 1
  |      use the selected policy with the host memory fair share
  |
  +-- Neuron device
         # This call raises when both figures are zero.
         bytes_used, bytes_free = _query_runtime_memory_stats()
         if bytes_free == 0:
             # The used bytes are above zero here, so this core is at or
             # over its share and no HBM is left.
             _validate_min_kv_budget(0, memory_before_headroom_bytes=0, ...)
             return 0
         # The runtime scales its own per-vNC limit, so do not divide.
         logical_core_hbm = bytes_used + bytes_free
         model_weight_bytes = _get_byte_used_from_model()
         return _compute_kv_budget(
             logical_core_hbm, model_weight_bytes, _get_encoder_cache_bytes()
         )

CPU compilation and device execution use the same model weight calculation. This keeps the KV block count equal and permits compilation cache hits.

_compute_kv_budget(logical_core_hbm, model_weight_bytes, encoder_cache_bytes):
    if headroom is explicit:
        memory_before_headroom = max(
            logical_core_hbm - model_weight_bytes - encoder_cache_bytes, 0
        )
        available = max(memory_before_headroom - headroom_bytes, 0)
    else:
        total_budget = int(logical_core_hbm * gpu_memory_utilization)
        available = max(min(
            max(total_budget - model_weight_bytes - encoder_cache_bytes, 0),
            int(total_budget * cap_fraction),
        ), 0)
    # _validate_min_kv_budget raises RuntimeError when the budget is too small.
    # It compares bytes against bytes, and a minimum of zero or less
    # disables the check.
    _validate_min_kv_budget(
        available, memory_before_headroom_bytes=..., budget_formula=...
    )
    return available

The device path reads the runtime statistics only to get the logical-core HBM. It does not use the runtime memory usage as the model weight size.

The device HBM line and the CPU-compile HBM line name the platform family and the active LNC degree. The family comes from the compile target, so both lines use one family namespace and an operator can compare them. A compile override makes both lines name the override family, and not the host.

The CPU inference path uses current available host memory, which already reflects model weights and other allocations. Neither CPU inference policy subtracts model weights again.

  • HBM stack sharing (LNC 1) is split between the two sources. On trn2 and trn3 one HBM stack serves 2 physical NeuronCores. At LNC 2 a logical core owns the stack. At LNC 1 two logical cores share it. On a trn1 family and on an inf family, each core owns its own HBM region.

    A budget larger than that share makes two siblings size a KV cache against the same memory. The allocation then fails at load (nrt_tensor_allocate status=4). Neither the hardware nor the runtime enforces the share. The share is a planning rule, not a quota.

    The two sources report different things, so each path does different work:

    path

    source

    reports

    work

    device

    get_vnc_memory_stats

    the share of one logical core

    none

    CPU compile

    get_total_available_memory

    one whole HBM stack

    divide

    libtorch-neuronx-lite scales the per-vNC limit behind get_vnc_memory_stats to the active LNC degree. The device path therefore passes its figure through. The static table stays a plain hardware fact, the size of one stack. The CPU-compile path divides that figure by get_logical_cores_per_hbm_stack. Both paths then reach the same figure for a given platform and degree, which the compile cache needs.

    Do not move the divisor into _compute_kv_budget. It would apply to both paths and halve the device budget a second time. The asymmetry is the point: only the unscaled source needs dividing.

    The device path needs a libtorch-neuronx-lite that scales its per-vNC limit. An older build reports a whole stack there, and this plugin no longer divides. The device budget is then 2 times the share. Nothing detects that condition. A version floor in requirements/core.txt cannot cover it, because Dockerfile.plugin installs override wheels with --no-deps.

    Capacity on a trn1 family and on an inf family. get_logical_cores_per_hbm_stack keys the divisor on the platform family, so those families keep their full region at LNC 1. The rule tests the family name for the prefix trn1 or inf, which is the rule that libtorch-neuronx-lite applies. The library accepts the families trn1, trn1n, trn2, trn3 and inf2. The prefix can therefore match trn1, trn1n and inf2 only. A family-blind divisor would halve them. The manifests run trn2 and trn3 only, so no CI target covers this.

  • Zero free bytes with used bytes above zero give a zero budget. libtorch-neuronx-lite clamps the free bytes to zero when the used bytes reach the limit. A zero free value therefore means that this core is at or over its share. That reading holds only when the used bytes are above zero. The sum of the used bytes and the free bytes is then the used bytes, and not a capacity. It grows with the overflow. _determine_available_memory_neuron tests for the zero free value first. It logs an error line, reports the cause through _validate_min_kv_budget, and returns 0. The worker does not call _compute_kv_budget on this branch, so the budget log lines of that method do not appear. The default VLLM_NEURON_MIN_KV_BUDGET_GIB of 1.0 then stops the server. A value of zero or less disables that guard, and the worker returns a budget of 0. _query_runtime_memory_stats accepts this shape, because it is a real state and not a fault.

  • Zero free bytes with zero used bytes are unusable statistics. The library accumulates the used bytes and the limit over the visible logical cores. A visible count of zero leaves both sums at zero, and the clamp then reports zero free bytes with zero used bytes. This shape is not an at-or-over-share core, so _query_runtime_memory_stats raises RuntimeError for it:

    The Neuron memory stats are unusable: the runtime reports zero used HBM and zero free HBM. These figures describe no logical core, because a visible core always holds an HBM limit above zero. Check that this process sees one Neuron logical core. Then check the state of the Neuron driver.
    

    A negative figure keeps its own guard, which reports Neuron runtime reported unusable memory stats: used=..., free=.... The negative check runs first. The zero-free branch of _determine_available_memory_neuron is therefore reached only with used bytes above zero.

  • CPU Compile only mode uses the static HBM size for the target platform (looked up via NEURON_PLATFORM_TARGET_OVERRIDE) and computes bytes_used from model parameter and buffer sizes. No device is present — the goal is to produce the same KV block count that the execution phase will use, ensuring compilation cache hits.

  • Neuron mode (compile/execute) also uses _get_byte_used_from_model() (model parameter + buffer sizes after model loading) rather than raw runtime.get_vnc_memory_stats() for bytes_used. This ensures the KV block count is identical between CPU compile and on-device execution. The runtime stats are only used to derive logical_core_hbm.

  • CPU mode (no compilation) is a separate path that uses host memory fair-share — it does not go through _compute_kv_budget and is unrelated to the compile/execute flow.

  • An explicit headroom value reserves non-KV memory after the LNC share is known.

  • Minimum-KV guard fails fast for unusable configs.

5) Minimum budget check#

The worker compares both CPU and Neuron results with VLLM_NEURON_MIN_KV_BUDGET_GIB. It stops early if the result is too small.

Without a cause line, the default-mode error names memory utilization and the fractional cap. The headroom-mode error names the active reserve. It also reports max_headroom_gib when that value is positive. Increasing headroom makes a minimum-KV-budget error worse.

The zero-free-HBM branch adds a cause line to the same error. The cause line states that this core is at or over its HBM share. It also states that a larger memory utilization value cannot add HBM.

A cause line owns the corrective actions, so the error then drops the generic advice of the active policy. In default mode it does not ask you to raise memory utilization or the fractional cap. In headroom mode it reports no headroom ceiling, because the ceiling is not positive. It states instead that no headroom value can raise the budget to the minimum.

Known HBM allocation exceptions raised while loading, initializing the KV cache, compiling, or warming up retain their original exception and add a note with the active reserve and the 1 GiB tuning procedure in headroom mode.

This document is relevant for: Trn2, Trn3