.. meta::
   :description: Use the Neuron Parallelism Planner skill to choose tensor, context, and data parallelism and batch size for transformer LLM inference on AWS Trainium before running on hardware.
   :keywords: parallelism planner, tensor parallelism, context parallelism, data parallelism, expert parallelism, LLM inference, Trainium, entitlement, roofline, ITL, TTFT, KV cache
   :date-modified: 2026-11-13

.. _neuron-parallelism-planner:

Deep dive: Plan inference parallelism with the Parallelism Planner skill
========================================================================

**Why read this guide?** This guide is for ML engineers who need to decide how to shard a
transformer LLM for serving on AWS Trainium. It covers how many NeuronCores to use, which tensor,
context, and data parallelism degrees to pick, and what batch size to run at. It explains the
Parallelism Planner skill. The skill is an agent-driven workflow that turns a model's
HuggingFace ``config.json`` and a set of hardware constants into a ranked parallelism
recommendation, using an analytic hardware-entitlement model. You get the recommendation
before you compile anything or reserve a Trainium instance.

**How to use this guide:** Read this guide before your first on-hardware sweep for a new model,
or when a deployed config misses its latency or throughput target and you want to know whether
a different sharding could meet it. Skip to the :ref:`workflow <planner-workflow>` if you
already understand run-specs and serving objectives. For per-operator analysis of one chosen
config, see the :ref:`Hardware Entitlement skill <neuron-hardware-entitlement>`.

This topic explores the Parallelism Planner skill in depth. It covers the run-spec input, the
three-layer analytic engine, oracle validation, the serving objectives and SLA gates, the
parallelism axes the planner sweeps, and how to read its output. You need experience with
transformer inference, tensor parallelism, and basic roofline analysis to fully understand this
content.

Prerequisites
-------------

Before you start, you must be familiar with the following:

- **Transformer inference phases:** The difference between prefill (processing the prompt) and
  decode (generating one token per step), and what time-to-first-token (TTFT) and inter-token
  latency (ITL) measure.
- **Parallelism concepts:** How tensor, context, and data parallelism split work across
  NeuronCores. See the parallelism section of :doc:`/vllm-neuron/docs/guides/features-guide`.
- **Roofline analysis:** The difference between compute-bound and memory-bandwidth-bound work.

Overview
--------

Picking a parallelism config by trial and error on hardware is slow. Each candidate needs a
compile, a warm-up, and a benchmark. The Parallelism Planner skill narrows the search before
you touch hardware. It computes a theoretical lower bound, called the *entitlement*, on decode
ITL, prefill TTFT, throughput, and per-device HBM for every candidate config. It then selects
the config that best meets your serving objective.

The planner is an **entitlement model, not a measurement**. Real latencies are higher because
of dispatch overhead, synchronization, idle gaps, and kernel inefficiency. Use the planner's
recommendation to decide where to point the first on-hardware sweep, then validate the
prediction with the profiling skills.

The skill handles:

- **Dense and Mixture of Experts (MoE) models:** Including always-on shared experts and
  dense-then-MoE layer splits.
- **Attention variants:** Standard multi-head attention (MHA), grouped-query attention (GQA),
  Multi-head Latent Attention (MLA), and mixed sliding-window and global attention.
- **Precision options:** BF16 or FP8 weights, FP8 KV cache, and FP8 compute.
- **Trainium generations:** trn2 and trn3, plus trn1 (older hardware). Each generation is
  described by a hardware-constants file.

Linear-attention, recurrent, and state-space layers are not supported. See
:ref:`planner-supported-architectures` for the full list.

The workflow has four stages:

1. **Run-spec authoring:** Describe the model (pristine HF ``config.json``), the hardware
   constants, and your SLAs in a run-spec JSON file.
2. **Oracle validation:** Confirm the analytic engine's FLOP and weight counts for this model
   match a shape-exact meta-device oracle.
3. **Sweep and select:** Evaluate every config in the parallelism × context-bucket grid at
   every compiled decode batch bucket, up to the largest admitted, gate on the SLAs, and
   rank the survivors.
4. **Cell inspection:** Break a single operating point down per component and layer type to
   see what binds, and hand off to profiling for validation.

.. important::
   The planner plans **servers within one node**, in two modes selected by the run-spec's
   ``serving`` field: ``colocated`` plans one replica that serves both prefill and decode
   (``TP × DP ≤ cores_per_node``), and ``disaggregated`` plans one prefill server and one decode
   server, recommended separately, each on half the node's logical cores by default. No server
   spans more than one node. If no candidate meets your SLAs, the planner reports the Pareto
   candidates and leaves ``selected_candidate_id`` null.

.. _planner-supported-architectures:

Supported architectures
-----------------------

The planner models **decoder-only, causal transformer LLMs with softmax attention**. The
architecture is read from the HF ``config.json`` by the ``arch_from_hf`` parser. The
following table lists what the parser and oracle model today.

.. list-table::
   :header-rows: 1
   :widths: 25 45 30

   * - Area
     - Supported
     - Config fields read
   * - Attention
     - Multi-head (MHA), grouped-query (GQA), and multi-query (MQA) softmax attention.
     - ``num_attention_heads``, ``num_key_value_heads``, ``head_dim`` (derived from
       ``hidden_size`` when absent)
   * - Latent attention
     - Multi-head Latent Attention (MLA) with a compressed KV cache, as in DeepSeek-V2/V3.
     - ``kv_lora_rank``, ``q_lora_rank``, ``qk_nope_head_dim``, ``qk_rope_head_dim``,
       ``v_head_dim``
   * - Sliding-window attention
     - Uniform sliding window, and mixed sliding-window and full-context layers with
       separate KV-head count and head size for the global layers, as in Gemma.
     - ``sliding_window``, ``layer_types`` (``sliding_attention`` / ``full_attention``),
       ``num_global_key_value_heads``, ``global_head_dim``
   * - K/V aliasing
     - Global layers where V aliases K.
     - ``attention_k_eq_v``
   * - Dense FFN
     - Gated MLP (gate, up, down projections).
     - ``intermediate_size``
   * - Mixture of Experts
     - Routed experts with top-k routing, always-on shared experts, dense-then-MoE layer
       splits, and a routed pool in parallel with a dense MLP.
     - ``n_routed_experts`` / ``num_experts`` / ``num_local_experts``,
       ``num_experts_per_tok`` / ``top_k_experts``, ``moe_intermediate_size``,
       ``n_shared_experts`` / ``shared_expert_intermediate_size``,
       ``first_k_dense_replace``, ``enable_moe_block``
   * - Embeddings
     - Tied or untied embedding and lm_head tables.
     - ``tie_word_embeddings``, ``vocab_size``
   * - Config layout
     - Flat configs, and multimodal configs with a nested ``text_config``. Only the text
       decoder is modeled.
     - ``text_config``

Models the planner has been exercised on include Granite-4.1-8B (dense GQA, tied
embeddings), Gemma-4-31B (mixed sliding and global attention, ``attention_k_eq_v``),
Gemma-4-26B-A4B (MoE in parallel with a dense MLP), and Devstral-2-123B (untied embeddings,
FP8 weights).

Not supported
^^^^^^^^^^^^^

.. list-table::
   :header-rows: 1
   :widths: 35 65

   * - Architecture
     - Status
   * - **Linear attention and recurrent layers**, including Gated DeltaNet (GDN) and other
       hybrid linear/full-attention models
     - Not modeled. The engine has no representation for recurrent state or the non-tensor-
       engine work these layers do.
   * - **State-space layers** (for example, Mamba)
     - Not modeled.
   * - **Encoder-only and encoder-decoder models**
     - Not modeled. The engine assumes causal self-attention with a KV cache, and has no
       cross-attention.
   * - **Vision and audio towers** in multimodal models
     - Ignored. Only the text decoder in ``text_config`` is costed.
   * - **Per-layer FFN widths** other than the dense-then-MoE split
     - Not modeled.
   * - **MoE with** ``n_routed_experts`` **combined with mixed** ``layer_types``
     - Raises ``NotImplementedError``.
   * - **MoE configs that use other field names** (for example, ``moe_num_experts`` and
       ``moe_top_k``)
     - Not recognized. The model is parsed as dense.
   * - **Operators that exist only in a kernel language**
     - Must be expressed in the oracle by hand before validation. See
       ``references/oracle-construction.md``.

.. warning::
   The parser does **not** reject unsupported architectures. It mis-models them without an
   error. In ``layer_types``, it recognizes only ``sliding_attention`` and
   ``full_attention``. Depending on the other layer types present, it then either treats
   unrecognized layers, such as ``linear_attention``, as full softmax attention, or drops
   them from the layer count. An unrecognized MoE config is planned as a dense model.
   Before you trust any plan for a new model, confirm that every value in ``layer_types``
   is ``sliding_attention`` or ``full_attention``, that the parsed layer count equals
   ``num_hidden_layers``, and that the corrected weight total matches the model card. Do
   not plan only the layers that happen to parse. To support a new architecture, extend
   the engine and oracle first.

.. _planner-prerequisites:

Hardware and software requirements
-----------------------------------

The planner does not need Neuron hardware. It runs on any machine, including a laptop or a
CPU-only instance.

* **Python:** 3.12 (the skill was developed and tested with ``python3.12``).
* **Planner CLI:** Python standard library only.
* **Oracle validation:** ``torch`` (CPU build is enough; the oracle runs on the ``meta``
  device) and ``pytest`` for the tests.
* **Figures (optional):** ``matplotlib`` for the ``--visualize`` output.
* **Model config:** The model's unmodified HuggingFace ``config.json``. Model weights are
  not needed.

.. note::
   Hardware is needed only when you validate the planner's prediction. Use the
   ``neuron-framework-profiling-vllm-neuron`` and
   ``neuron-framework-profile-analysis-vllm-neuron`` skills on a Trainium instance for that
   step.

.. _planner-inputs:

Inputs: the run-spec
--------------------

Everything the planner computes is driven by a **run-spec**, a JSON file that points at the
model config and the hardware constants and states your serving targets. The skill ships no
default model. If you omit the run-spec, the planner stops with an error rather than silently
analyzing some other model.

A run-spec resolves ``hf_config`` and ``hardware`` relative to its own location, so keep the
three files in one directory.

.. code-block:: json

   {
     "model": "granite-4.1-8B (dense)",
     "hardware_name": "trn2",
     "hf_config": "granite4-8b-hf.json",
     "hardware": "trn2_constants.json",
     "serving": "colocated",
     "decode_itl_ceiling_ms": 50,
     "prefill_itps_floor": 30000
   }

Model, hardware, and SLA fields
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. list-table::
   :header-rows: 1
   :widths: 30 15 55

   * - Field
     - Default
     - Description
   * - ``model``
     - (required)
     - Display name recorded in every report.
   * - ``hardware_name``
     - (required)
     - Short hardware label, for example ``trn2``.
   * - ``hf_config``
     - (required)
     - Path to the model's **pristine** HuggingFace ``config.json``. Do not edit it. Put all
       serving choices in the run-spec instead.
   * - ``hardware``
     - (required)
     - Path to a hardware-constants JSON file. The skill ships trn2 constants at
       ``references/trn2_constants.json``, which price collectives with the analytic ring
       model. ``references/trn2_constants_empirical_collectives.json`` prices them from the
       measured table ``trn2_collectives_lnc2.json`` instead, with the analytic model as the
       fallback for a group or message size the table does not cover.
   * - ``serving``
     - ``colocated``
     - Serving mode. ``colocated`` selects the global sharding planner: one replica serves
       prefill and decode. ``disaggregated`` plans the two DI servers: the prefill server and
       the component-aware decode server. The field chooses the planner; the CLI has no flag
       for it, and any other value raises ``ValueError``.
   * - ``contexts``
     - (required for ``disaggregated``)
     - Strictly increasing context lengths within the model's maximum, for example
       ``[8192, 32768]``: the decode server is planned at each, the prefill server on a prompt
       of that length. Colocated planning ignores it and derives six buckets from
       ``max_position_embeddings``.
   * - ``di_placement``
     - ``single_node``
     - Disaggregated only. ``single_node`` puts the prefill server and the decode server on one
       node, half its logical cores each (``TP × DP = 32`` on trn2.48xlarge); ``separate_nodes``
       gives each server a whole node (``TP × DP = 64``). The planner fixes that core budget per
       server; it does not size the prefill-to-decode ratio.
   * - ``decode_itl_ceiling_ms``
     - 50
     - Maximum acceptable decode inter-token latency, in milliseconds.
   * - ``prefill_itps_floor``
     - 30000
     - Prefill SLA expressed as an input-tokens-per-second floor. The TTFT ceiling scales
       with context: ``ttft_ceiling(S) = S / floor``.
   * - ``prefill_ttft_ceiling_ms``
     - (unset)
     - Alternative prefill SLA: a flat TTFT ceiling in milliseconds at every context.
       Mutually exclusive with ``prefill_itps_floor``.

Physical serving fields
^^^^^^^^^^^^^^^^^^^^^^^

These fields change the system being modeled, not what the planner optimizes for. All are
optional.

.. list-table::
   :header-rows: 1
   :widths: 30 15 55

   * - Field
     - Default
     - Description
   * - ``weight_dtype_bytes``
     - model dtype
     - Served weight element size (``1`` = FP8, ``2`` = BF16). Decode is weight-HBM-bound, so
       this is the largest single decode lever for a quantized checkpoint.
   * - ``lm_head_dtype_bytes``
     - ``weight_dtype_bytes``
     - Element size for a separate, **untied** lm_head. A tied model rejects a conflicting
       value.
   * - ``kv_dtype_bytes``
     - weight dtype
     - KV-cache element size, for example ``1`` for an FP8 KV cache.
   * - ``compute_precision``
     - ``bf16``
     - ``bf16`` or ``fp8``. ``fp8`` scales the tensor-engine peak by
       ``fp8_te_peak_multiplier`` (default 2.0), which speeds up compute-bound prefill.
   * - ``activation_io_policy``
     - ``component_boundary``
     - How activation HBM traffic is charged. See :ref:`planner-cost-model`.
   * - ``ep_degree_choices``
     - every valid degree
     - MoE only. List of expert-parallel degrees to sweep; ``1`` adds the EP-off mesh. A
       mesh that admits none of the listed degrees leaves the grid
       (``feasibility.excluded_meshes``). See :ref:`planner-axes`.
   * - ``expert_weight_dtype_bytes``
     - ``weight_dtype_bytes``
     - Served bytes per routed-expert weight element, for example ``0.5`` for MXFP4 experts.
       The router and any dense MLP stay at ``weight_dtype_bytes``.
   * - ``expert_traffic``
     - ``balanced``
     - Expert routing assumption: ``balanced``, ``worst``, or a path to a measured histogram.
   * - ``usable_hbm_fraction``
     - (unset)
     - Legacy sensitivity mode: capacity × fraction instead of the serving stack's KV budget.

.. note::
   When you model an FP8 deployment, set ``weight_dtype_bytes: 1`` **and**
   ``compute_precision: "fp8"`` together. Halving weight bytes without modeling the FLOP
   speedup understates FP8 prefill by about 2×. Both factors are applied inside the engine.
   Do not scale the planner's output by hand.

Hardware constants
^^^^^^^^^^^^^^^^^^

The hardware-constants file supplies the numbers every planner result divides by. The shipped
trn2 file describes one LNC2 logical NeuronCore:

.. list-table::
   :header-rows: 1
   :widths: 40 25 35

   * - Constant
     - trn2 value
     - Meaning
   * - ``te_peak_flops``
     - 158 TFLOP/s
     - BF16 tensor-engine peak per logical core.
   * - ``hbm_bw_bytes_per_s``
     - 716 GB/s
     - HBM bandwidth per logical core.
   * - ``hbm_capacity_bytes_per_core``
     - 24 GiB
     - HBM capacity per logical core (96 GiB device ÷ 4 logical cores).
   * - ``cores_per_node``
     - 64
     - Logical cores per trn2.48xlarge at LNC2. This is the single-node cap.
   * - ``link_bw_within_node_bytes_per_s``
     - 1.28 TB/s
     - Link bandwidth used by the analytic collective model.

A stale or wrong constant produces a silently wrong plan. If a result looks implausible,
calibrate the constants by microbenchmarking. See the Hardware Entitlement skill's
``references/microbench-calibration.md``.

.. _planner-concepts:

Key concepts
------------

Entitlement and the roofline
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For each piece of work, the planner computes two lower bounds and takes the larger:

.. code-block:: text

   compute time = FLOPs / tensor-engine peak
   load time    = (weight bytes + KV bytes + activation bytes) / HBM bandwidth
   floor        = max(compute time, load time)

The ratio ``tensor-engine peak / HBM bandwidth`` is the *ridge point*, about 221 FLOP/byte on
trn2. Work whose arithmetic intensity (FLOPs per byte loaded) is below the ridge point is
memory-bound. Work above it is compute-bound. Small-batch decode sits far below the ridge
point. Whole-prompt prefill sits far above it.

.. _planner-cost-model:

The component cost model
^^^^^^^^^^^^^^^^^^^^^^^^

The planner does not apply a single roofline to the whole model, and it does not sum
per-operator latencies. It applies the roofline to each **component**: an attention body, an
FFN body, or the terminal lm_head. It then adds the component floors in sequence and adds the
collective time:

.. code-block:: text

   component_floor(c) = max(TE_work(c) / TE_peak, HBM_bytes(c) / HBM_bandwidth)
   step_time          = sum(component_floor(c)) + collective_time

Components are serialized because in a tensor-parallel transformer graph each attention and
FFN body ends at an all-reduce or reduce-scatter whose result the next body consumes. Inside
a component, the model assumes ideal fusion: internal operator interfaces stay on chip, and
only one gathered input read and one pre-collective output write are charged as activation
HBM traffic. This is the default ``component_boundary`` policy. Setting
``activation_io_policy`` to ``per_operator`` instead charges every operator's input and output,
which models an unfused or eager execution.

Collective time is modeled separately from the TE/HBM floor using a topology-aware ring model
(TP all-reduce, CP K/V ring). It is added after the component floors, not overlapped with them.

KV cache replication
^^^^^^^^^^^^^^^^^^^^

This is the most error-prone part of planning tensor parallelism for a GQA or MQA model:

- **The TP ceiling is the query-head count**, not the KV-head count. TP shards query heads,
  one per rank at most.
- **The KV cache does not shard past the KV-head count. It replicates.** Per-device KV bytes
  divide by ``min(TP, n_kv_heads)`` for each attention group. Past that point, extra ranks
  hold redundant copies of the cache.

For example, a model with 32 query heads and 8 KV heads can run at TP=32, but at TP=16 every
KV head is stored twice, and at TP=32 four times. That replication makes high-TP decode go
KV-read-bound early and wastes HBM that could hold more sequences or a prefix cache.

Context parallelism is the planner's remedy. A CP group is carved out of the TP group, and
during decode it splits the sequence dimension of the replicated KV cache across those
otherwise-redundant ranks. CP therefore recovers exactly the replication factor and no more:
the effective CP for a group is ``min(CP, TP / n_kv_heads)``. See :ref:`planner-axes`.

.. _planner-kv-budget:

KV memory budget and batch admission
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The batch a config can hold is the largest the serving stack, vLLM Neuron, admits under its KV
budget (run-spec ``gpu_memory_utilization``, default 0.9, and ``kv_gmu_budget_cap_fraction``,
default 0.30). Each
operating point reports ``memory.kv_budget_gb_per_rank`` and ``memory.kv_budget_binding``.

Oracle validation
^^^^^^^^^^^^^^^^^

The analytic engine is cross-checked against a **meta-device oracle**: a hand-written,
shape-exact ``torch`` module built from the same parsed architecture and run on the ``meta``
device under a FLOP counter. Because it runs on ``meta``, it needs no accelerator and no
weights.

.. list-table::
   :header-rows: 1
   :widths: 30 70

   * - Quantity
     - Authority
   * - Projection, FFN, router, and lm_head FLOPs
     - Oracle (exact).
   * - Weight bytes
     - Oracle. The analytic ``corrected_weight_bytes`` must match to well under 1%.
   * - KV-cache bytes
     - Analytic. The oracle reports zero because SDPA decomposes on ``meta``.
   * - Prefill SDPA FLOPs
     - Analytic causal active-pair count. The oracle's raw dense count is reported separately
       and is expected to be higher.

.. warning::
   The oracle validates that the engine and the oracle agree, not that the architecture
   was parsed correctly. Both are built from the same parse, so a mis-parsed model can still
   read +0.0%. Sanity-check the corrected weight total against the model card's parameter
   count, and confirm the layer count matches ``num_hidden_layers``.

Serving objectives
^^^^^^^^^^^^^^^^^^

Selection is **gate-then-rank**. SLA ceilings decide which configs are valid, and a
throughput metric ranks the survivors.

.. list-table::
   :header-rows: 1
   :widths: 30 70

   * - Objective
     - Behavior
   * - **Maximize OTPS under SLAs** (default)
     - Keep configs that meet both the prefill TTFT ceiling and the decode ITL ceiling, then
       pick the highest decode output-tokens-per-second (OTPS) per node.
   * - **Minimize prefill TTFT**
     - Pick the lowest TTFT, ignoring decode throughput. Favors configs with more cores,
       because prefill parallelizes almost perfectly.
   * - **Maximize decode OTPS**
     - Pick the highest per-node OTPS without the ITL ceiling. Favors large-batch configs.

Per-node throughput is ``otps × cores_per_node / cores``. This assumes the node is filled
with independent data-parallel replicas of the chosen config, so a small replica that holds
the weights scores well even though each replica uses few cores. In disaggregated mode the
figure covers the server's cores, which is half the node under ``single_node`` placement.

Throughput-capacity versus interactivity
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The sweep evaluates every config up to its **largest admitted batch**, the largest batch whose
KV cache fits the KV budget (see :ref:`planner-kv-budget`). Those cells answer the
throughput-capacity question: how much throughput can this footprint hold? They favor low TP
with DP scale-out.

Low-ITL operating points live **below** maximum batch and favor higher TP. The
``--visualize`` per-node Pareto figure sweeps batch size below the maximum for each config
and draws the ITL versus per-node-throughput frontier. Use that figure, not the top-batch
candidates alone, when interactivity matters.

.. _planner-axes:

Supported parallelism
---------------------

The planner sweeps **TP, CP, DP, and EP** on a single node. **PP is not supported.** A
config's core count is ``world = TP × DP``. CP is carved out of TP and adds no cores.

.. list-table::
   :header-rows: 1
   :widths: 12 18 70

   * - Axis
     - Status
     - Behavior and restrictions
   * - **TP**
     - Supported, swept
     - * Candidate degrees: 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, and 128, capped by the
         query-head count and the server's cores. TP=1 and TP=2 are not in the grid.
       * A degree is kept only if it does not exceed the query-head count, and divides
         exactly: every attention group's query-head count, ``vocab_size``, the dense FFN
         intermediate size, and the MoE expert intermediate size. A TP that needs padding is
         never recommended.
       * Divides compute and weights by TP. Divides the KV cache by ``min(TP, n_kv_heads)``.
       * Adds a per-layer all-reduce whose cost grows with TP.
       * Sequence parallelism is folded into TP for prefill. It is not a separate axis.
   * - **CP**
     - Supported, swept
     - * Candidate degrees: 1, 2, 4, and 8. CP must divide TP (``TP % CP == 0``).
       * **Decode:** shards the KV cache of full-context layers by
         ``min(CP, TP / n_kv_heads)``, which only recovers the cache TP replicated past
         ``n_kv_heads``; CP is offered only where it reclaims replicated cache. For MLA, the
         limit is TP.
       * **Sliding-window layers:** a model with sliding-window attention gets no CP config.
       * **Prefill:** shards the prompt's KV cache by token, not the compute
         (``decode_context_parallel_size`` applied to the prefill pass).
       * Adds a K/V ring collective.
   * - **DP**
     - Supported
     - * Candidate degrees: 1, 2, 4, 8, 16, and 32, subject to ``TP × DP`` not exceeding the
         server's cores.
       * **Dense models:** not swept. DP is pinned to 1 in the grid, and the node is assumed
         to be filled with independent replicas. That is the per-node throughput metric.
         Replicas share no communication. Under ``single_node`` disaggregated planning a dense
         mesh is widened with DP to span the server (TP8 becomes one TP8 × DP4 server).
       * **MoE models with EP enabled:** swept, because DP widens the expert-sharding grid.
   * - **EP**
     - Supported, swept (MoE only)
     - * On by default for models with routed experts; ``--disable-ep`` plans them with EP off.
       * Swept over the world ``TP × DP``: every valid degree ``EP`` of a mesh, each rank holding
         ``num_experts / EP`` experts over ``ep_tp = world / EP`` ranks. ``ep_degree_choices``
         restricts the list; ``1`` names the EP-off mesh.
       * Only routed experts are EP-sharded, and the router is replicated. The launch block
         carries ``expert_parallel_size``.
       * Expert parallelism across DP engines is planned for the disaggregated decode server;
         the prefill server plans EP within an engine.
   * - **PP**
     - **Not supported**
     - * Not implemented by the Neuron inference stack, so the engine has no PP axis and the
         grid has no PP degree.
       * The Hardware Entitlement scripts accept a ``--pp`` option only for compatibility.
         Any value other than 1 raises an error.

Other topology restrictions:

- **One node per server.** No server spans more than ``cores_per_node``, which is 64 logical
  cores on trn2.48xlarge at LNC2.
- **Two serving modes.** ``colocated``: one replica serves prefill and decode.
  ``disaggregated``: one prefill server and one decode server, planned separately on half the
  node each by default (see :ref:`planner-disaggregated-output`); the prefill-to-decode replica
  ratio and the KV transfer are set at launch.
- **Weights must fit.** A config whose per-rank weights do not fit the core's
  HBM × ``gpu_memory_utilization``, or leave less than the 1 GiB minimum KV budget, is dropped
  from the grid.
- **Speculative decoding is not a parallelism axis.** It is modeled separately; see
  :ref:`planner-tools-reference`.

The candidate degrees (``TP_CHOICES``, ``CP_CHOICES``, ``DP_CHOICES``) and the number of context
buckets (``N_BUCKETS``) are constants in ``scripts/planner/plan.py``. Edit them only to retune
the search. To exclude an axis for one run, use the ``--disable-tp``, ``--disable-cp``,
``--disable-dp``, or ``--disable-ep`` flags, which pin that axis to 1 (EP: off).

.. _planner-workflow:

Workflow
--------

The examples below run from your project directory with the skill's ``scripts/`` directory on
``PYTHONPATH``. Do not ``cd`` into the skill. The scripts refuse to write artifacts inside the
skill package.

.. code-block:: bash

   # ~/.claude for Claude Code, ~/.kiro for Kiro
   export NEURON_AGENTIC_DEVELOPMENT_ROOT=~/.claude
   SKILL=$NEURON_AGENTIC_DEVELOPMENT_ROOT/skills/neuron-parallelism-planner/scripts

.. _planner-stage1:

Stage 1: Write the run-spec
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Create a directory containing the model's pristine ``config.json``, a copy of the hardware
constants, and a run-spec that points at both. The skill's
``samples/granite4-8b-walkthrough.md`` contains a complete, known-good example you can copy.

Before trusting results for a new model, check the parse:

1. Confirm the config's field names are ones ``arch_from_hf`` recognizes. For example, MoE
   configs that use ``moe_num_experts`` instead of ``n_routed_experts`` have previously been
   modeled as dense.
2. Confirm every ``layer_types`` value is ``sliding_attention`` or ``full_attention``, and
   that the parsed layer count equals ``num_hidden_layers``. Other values, such as
   ``linear_attention``, are silently mis-modeled or dropped.
3. If the architecture is not in :ref:`planner-supported-architectures` (for example, a new
   attention variant or recurrent linear attention), extend the engine and oracle first. See
   ``references/oracle-construction.md``.

.. _planner-stage2:

Stage 2: Validate the engine against the oracle
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: bash

   python3.12 $SKILL/validate_oracle.py ./my-model/my-model-run.json

The script prints, per segment, the oracle and analytic FLOPs and weight bytes for a decode
cell and a prefill cell.

**Pass criteria:**

- Weight bytes match to well under 1% on every segment.
- Decode FLOPs match within a few percent.
- Prefill attention FLOPs may differ. The oracle counts the dense score rectangle, while the
  engine charges causal active pairs. Projection, FFN, and lm_head prefill FLOPs should still
  match.

A large gap localizes a missing term to the segment where it appears. Fix it before sweeping.

.. _planner-stage3:

Stage 3: Sweep and select
^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: bash

   PYTHONPATH=$SKILL python3.12 -m planner ./my-model/my-model-run.json

The planner:

1. Builds the config grid, rejecting configs that do not shard evenly, whose per-rank weights
   do not fit the core's HBM × ``gpu_memory_utilization`` or leave less than the 1 GiB minimum
   KV budget, or whose ``TP × DP`` exceeds the server's cores.
2. Derives the context buckets from the model's ``max_position_embeddings``. It keeps six
   buckets, pinning the full context as the last one.
3. Solves each config's largest admitted batch under the KV budget (see
   :ref:`planner-kv-budget`), then costs the config at that batch and at every power of two
   below it.
4. Costs every candidate for decode and prefill, gates on the SLAs, and selects one candidate
   per bucket: the highest server throughput among those that meet every constraint.

The sweep prints a summary table per bucket and writes ``result.json`` and
``execution_breakdown.json`` to ``<project root>/agent_artifacts/tmp/``. Per bucket, read four
fields of ``contexts[<ctx>]``:
``selection_status`` (``meets_sla``, ``no_candidate_meets_sla``, or
``no_runnable_configuration``), ``selected_candidate_id`` (null when nothing meets every SLA: a
documented miss, not an error), ``pareto_candidates`` (each ``configuration_id`` points into
``configurations``, whose ``launch`` block is the vLLM Neuron recipe), and ``branch_ladder``
(the pick at every lower decode batch bucket). The full contract for both modes is in the
skill's ``SKILL.md``.

Useful options:

.. list-table::
   :header-rows: 1
   :widths: 40 60

   * - Option
     - Effect
   * - ``--decode-itl-ceiling-ms N``
     - Override the run-spec's ITL ceiling.
   * - ``--prefill-itps-floor N`` / ``--prefill-ttft-ceiling-ms N``
     - Override the prefill SLA (mutually exclusive).
   * - ``--kv-cap-fraction F``
     - Override ``kv_gmu_budget_cap_fraction``. ``--usable-fraction F`` selects the legacy
       capacity × fraction mode. Precedence: CLI flag, then run-spec, then hardware JSON, then
       defaults.
   * - ``--disable-<axis>`` / ``--enable-<axis>``
     - Pin an axis (``tp``, ``cp``, ``dp``, ``ep``) to degree 1, or allow it.
   * - ``--visualize``
     - Colocated only: write the roofline, Pareto, per-node Pareto, and prefill-footprint
       figures (PNG) and print the path of each.
   * - ``--emit``
     - Deprecated and ignored: both JSON files are always written.
   * - ``--project-root DIR``
     - Write artifacts under ``DIR/agent_artifacts/tmp/`` instead of the current directory.
       ``DIR`` must exist.

.. _planner-disaggregated-output:

Disaggregated output
^^^^^^^^^^^^^^^^^^^^

With ``"serving": "disaggregated"`` and a ``contexts`` list, the same command plans both DI
servers; there is no second command and no CLI flag for the mode. The decode server is the
top-level ``contexts`` block. The run adds a ``prefill`` block with the same shape per context
(``selection_status``, ``selected_candidate_id``, and the Pareto ``candidates``, each with the
``kv_producer`` server's ``launch`` block), ``placement`` (the cores of each server), and
``prefill.limitations``, which states the plan's scope.

.. _planner-component-aware:

Component-aware decode planning
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Inside the DI decode server, the FFN and each attention variant may take their own
data-parallel route. A route set shows up as ``attention_dp_size`` and ``mlp_dp_size`` in
``neuron_config`` and ``decode_context_parallel_size`` on the launch block, with the routes
listed in the configuration's ``component_routes``.

.. _planner-stage4:

Stage 4: Inspect a single cell
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To compare against one on-hardware run, evaluate the exact operating point you plan to
deploy:

.. code-block:: bash

   PYTHONPATH=$SKILL python3.12 -m planner ./my-model/my-model-run.json \
     --cell --tp 8 --cp 1 --bs 16 --ctx 8192

``--cell`` skips the sweep and prints a per-(component, layer type) breakdown: layer count,
weight, KV, and activation megabytes, FLOPs, load time, compute time, the component floor, and
its binding term. Add ``--prefill`` to evaluate the prefill regime instead of decode.

The cell is the hypothesis a profile confirms or refutes. Hand it to
``neuron-framework-profiling-vllm-neuron`` to capture a trace at the same operating point,
then to ``neuron-framework-profile-analysis-vllm-neuron`` to compare measured per-segment
time against the planner's expected time. For a per-operator breakdown of the same cell, use
the :ref:`Hardware Entitlement skill <neuron-hardware-entitlement>`.

.. _planner-file-organization:

File organization
-----------------

The planner writes every artifact under ``<project root>/agent_artifacts/tmp/``. The project
root is the current directory unless you set ``--project-root`` or ``$NEURON_PROJECT_ROOT``.
The Hardware Entitlement skill honors the same variable, so one analysis keeps one evidence
trail.

.. code-block:: text

   <project root>/agent_artifacts/tmp/
   ├── result.json                          # Launch registry, candidates, SLA verdicts, ladder
   ├── execution_breakdown.json             # Component totals and priced collectives per candidate
   ├── roofline_<model>.png                 # Decode cells on the TE/HBM roofline (--visualize)
   ├── pareto_<model>.png                   # OTPS vs ITL, area = cores (--visualize)
   ├── pareto_per_node_<model>.png          # Interactivity Pareto, DP-normalized (--visualize)
   └── prefill_footprint_<model>.png        # Prefill TTFT vs footprint with SLA (--visualize)

.. _planner-tools-reference:

Tools reference
---------------

Planner CLI
^^^^^^^^^^^

**Module:** ``scripts/planner/__main__.py`` (``python3.12 -m planner``)

Reads ``serving`` from the run-spec, runs the colocated or disaggregated sweep and selection,
and writes ``result.json`` and ``execution_breakdown.json``; or evaluates a single cell with
``--cell``.

Engine package
^^^^^^^^^^^^^^

**Package:** ``scripts/planner/``

.. list-table::
   :header-rows: 1
   :widths: 30 70

   * - Module
     - Role
   * - ``segments.py``
     - Aggregates the model into per-(class, layer type) weight, KV, and FLOP segments.
   * - ``shard.py``
     - Owns every TP, CP, DP, and EP divisor rule, including KV replication.
   * - ``activation.py``
     - Builds the ordered component graph and the activation boundary tensors.
   * - ``cost.py``
     - Applies the per-component roofline and serializes components into a step time.
   * - ``plan.py``
     - Builds the grid, solves maximum feasible batch, selects buckets, and applies
       gate-then-rank selection.
   * - ``build.py``
     - Loads the run-spec. ``cell(...)`` evaluates one (config, bucket, batch, regime) point.
   * - ``component_plan.py`` / ``prefill_plan.py``
     - The disaggregated decode-server sweep (per-component routes, with ``routes.py``) and the
       prefill-server sweep. ``result.py`` assembles the two JSON files for both modes.
   * - ``emit.py`` / ``visualize.py``
     - Write the JSON artifacts and PNG figures.

Model and hardware layer
^^^^^^^^^^^^^^^^^^^^^^^^

**Module:** ``scripts/entitlement_formulas.py``

Parses the HF config (``arch_from_hf``) and hardware constants, and provides the accounting
primitives and the topology-aware collective model the engine builds on.

Oracle and validation
^^^^^^^^^^^^^^^^^^^^^

.. list-table::
   :header-rows: 1
   :widths: 35 65

   * - Module
     - Role
   * - ``scripts/oracle_model.py``
     - The shape-exact ``torch`` oracle, built from the same parsed architecture.
   * - ``scripts/entitlement_counter.py``
     - FLOP, weight, and KV counter (subclasses ``torch.utils.flop_counter``) and the
       speculative-decoding cost model.
   * - ``scripts/validate_oracle.py``
     - Diffs the engine's analytic totals against the oracle per segment.

Speculative decoding model
^^^^^^^^^^^^^^^^^^^^^^^^^^

Speculative decoding is not a sharding axis. It changes the cost per accepted token. The model
in ``entitlement_counter.py`` takes the draft length γ and the acceptance rate α:

.. code-block:: text

   expected accepted tokens  N = (1 - α^(γ+1)) / (1 - α)
   weight-read speedup         = N / (1 + γ · draft_weight / target_weight)

The planner infers nothing about the draft model. You must supply its weight bytes, KV bytes,
and FLOPs explicitly, either directly, from a measured report, or from the draft's HF config.

.. _planner-debugging:

Common issues
-------------

.. list-table::
   :header-rows: 1
   :widths: 40 60

   * - Symptom
     - Solution
   * - ``error: a run-spec path is required``
     - Pass a path to a run-spec JSON. There is no default model.
   * - ``error: run-spec not found``
     - The argument must be a file path. Model names are not resolved.
   * - ``ValueError: run-spec 'serving' must be one of ('colocated', 'disaggregated')``
     - Those are the only two modes, both single-node. Set the field to one of them, or omit
       it for ``colocated``.
   * - ``run-spec serving='disaggregated' requires a non-empty 'contexts' list``
     - Add ``contexts``: a strictly increasing list of context lengths within the model's
       maximum. There is no default.
   * - Scripts refuse to write artifacts
     - You are running from inside the skill package. Run from your project directory, or pass
       ``--project-root``.
   * - ``validate_oracle.py`` weight gap above 1%
     - A weight term is missing or double counted. Check tied embeddings, MoE expert counts,
       and the ``attention_k_eq_v`` flag.
   * - Model planned as dense but it is MoE
     - The config uses MoE field names ``arch_from_hf`` does not recognize. Extend the parser.
   * - Parsed layer count is lower than ``num_hidden_layers``, or a hybrid model plans like a
       pure-attention model
     - ``layer_types`` contains a value other than ``sliding_attention`` or
       ``full_attention``, such as ``linear_attention``. The architecture is not supported.
       See :ref:`planner-supported-architectures`.
   * - ``selection_status`` is ``no_candidate_meets_sla`` and ``selected_candidate_id`` is null
     - No candidate meets every SLA at any swept batch. Relax an SLA, or plan the two phases
       separately with ``"serving": "disaggregated"``; the planner keeps the serving mode the
       run-spec names.
   * - No CP candidates for a TP degree
     - Expected when ``TP ≤ n_kv_heads``. CP only reclaims KV replicated past the KV-head
       count, so those CP configs are not in the grid. Models with sliding-window attention
       get no CP config at all.

.. _planner-guidelines:

Project guidelines
------------------

The Parallelism Planner skill enforces these rules:

- **Plan servers within one node, in the mode the run-spec names.** No server spans more than
  one node, and the serving mode stays the one the run-spec names, even when no candidate meets
  the SLA.
- **Always pass an explicit run-spec path.** No environment variable or default selects a
  model.
- **Keep the HF config pristine.** Put precision, KV dtype, and memory budget in the
  run-spec.
- **Validate against the oracle before trusting a new model.**
- **Report the full TP sweep, not just the winner.** For collective-bound models, the
  latency optimum is often an interior TP degree, not the maximum.
- **Treat every result as a prediction** until it is profiled on hardware.

.. _planner-knowledge-base:

Knowledge base
--------------

The skill's ``references/`` and ``samples/`` directories contain:

- ``methodology.md``: The full sweep methodology, the findings that drive config selection,
  the two serving objectives, prefill, and the prefix-cache tradeoff.
- ``parallelism-cost-model.md``: How each parallelism axis scales compute, weight, KV, and
  communication cost, and the speculative-decoding cost model.
- ``tp-feasibility-and-kv-replication.md``: Why the TP ceiling is the query-head count and
  how KV replication works. Read before trusting any high-TP number.
- ``oracle-construction.md``: How the oracle is built, and how to extend it for a new
  architecture.
- ``design.md``: The engine's design: aggregation, sharding, the cost model, and selection.
- ``trn2_constants.json``: The shipped trn2 hardware constants.
- ``samples/granite4-8b-walkthrough.md``: An end-to-end walkthrough for Granite-4.1-8B on trn2,
  including oracle validation, the sweep, a cell breakdown, and a comparison against measured
  hardware.

Related resources
-----------------

- :ref:`neuron-hardware-entitlement`: Per-operator floors for a chosen config.
- :ref:`parallelism-planner-tutorial`: Step-by-step tutorial for this skill.
- :doc:`/vllm-neuron/docs/guides/features-guide`: Tensor, data, and expert parallelism in vLLM Neuron.
- :doc:`/vllm-neuron/docs/getting-started/quickstart-online-serving`: Deploying models with vLLM Neuron.
- `Neuron Agentic Development <https://github.com/aws-neuron/neuron-agentic-development>`_: Source for the Parallelism Planner skill.
