This document is relevant for: Trn3

Tutorial: Serve a multi-turn chat with Kimi-K3#

Note

SGLang-Neuron is in public preview. Features, APIs, and supported models can change in later releases.

Applies to: trn3.48xlarge (three instances), Trn3 UltraServer

Overview#

In this tutorial, you serve Kimi-K3 with prefill-decode (PD) disaggregation on three Trn3 nodes, at a context of up to 1,048,576 tokens. Then you send a conversation of two turns to the server.

The layout has these parts:

  • A prefill server on one node.

  • A decode server that spans two nodes in one Trn3 UltraServer. The decode server uses DSPARK speculative decoding.

  • A balancer on the prefill node. It sends each request to the prefill server and the decode server.

  • A chat front on the prefill node. The prefill server accepts token IDs only. The chat front applies the chat template of the checkpoint, sends the token IDs to the balancer, and returns an OpenAI-compatible chat completion.

The prefill server keeps the end of each finished prompt in its kimi-prompt prefix cache. When the next turn of a conversation extends the prompt of the turn before, the prefill server finds the cached prompt and computes only the new tokens.

This tutorial takes about 40 minutes after the checkpoints are on the nodes. The first start of the servers compiles their programs, which takes most of that time. Downloading the checkpoints takes longer, because each node needs about 1.6 TB.

Before you start#

Do the SGLang-Neuron quickstart and the GPT-OSS-120B disaggregated serving tutorial first. This tutorial uses the same container options and the same PD concepts.

Model details#

  • Checkpoints: moonshotai/Kimi-K3 and its DSPARK drafter, Inferact/Kimi-K3-DSpark, both on Hugging Face.

  • Prefill server: eight pipeline stages of tensor parallelism 16 (PP8 x TP16) on one node.

  • Decode server: TP256 over two nodes, as eight data-parallel attention groups of attention TP32, with DSPARK speculative decoding. The draft length is K=3 (the default) or K=7.

  • Weights and KV cache: MXFP4 experts and an FP8 KV cache.

  • Sampling: the recommended sampling for Kimi-K3 is temperature 1.0 (0.6 with thinking off) and top-p 0.95.

Prerequisites#

  • Three trn3.48xlarge instances with Docker Engine and the Neuron driver of Neuron SDK 2.33.0 (aws-neuronx-dkms 2.31.7.0 or later). In this tutorial, the instances are the prefill node, decode node 0, and decode node 1.

  • Decode node 0 and decode node 1 in one Trn3 UltraServer, on two nodes whose UltraServer indices are 1 and 2, or 0 and 3. The cross-node collectives of the decode server are slower between other pairs. To see the indices, run neuron-ls in the container. It prints ultraserver-id and ultraserver-instance-index for each node.

  • EFA devices on all three instances.

  • One security group for all three instances that allows all traffic between them.

  • Approximately 1,080 GB of free host memory on the prefill node, for the host tier of the prefix cache at the validated values. On a node with less host memory, set SGLANG_NEURON_KIMI_PROMPT_HOST_SLOTS and SGLANG_NEURON_KIMI_PROMPT_HOST_STATE_SLOTS to lower values before you start run_prefill.sh.

  • The address of each node that the other nodes can reach.

  • Approximately 1.6 TB of free disk space on each node, for the Kimi-K3 checkpoint (1,561 GB) and the drafter (7.1 GB).

The prefill node does not need to be in the UltraServer of the decode nodes. To get a Trn3 UltraServer, you can reserve an UltraServer Capacity Block. For the steps, see Launch instances using Capacity Blocks.

Prepare your environment#

Do these steps on all three nodes.

  1. Set the image variable:

    IMAGE=public.ecr.aws/neuron/pytorch-inference-sglang-neuron:v0.1.0-neuron-py314-sdk-2.33.0-ubuntu26.04
    
  2. Download the two checkpoints to the host models directory:

    docker run --rm -v <models dir>:/models $IMAGE hf download moonshotai/Kimi-K3 --local-dir /models/Kimi-K3
    docker run --rm -v <models dir>:/models $IMAGE hf download Inferact/Kimi-K3-DSpark --local-dir /models/Kimi-K3-DSpark
    
  3. Start a container with the Neuron devices, the EFA devices, and the network of the node, and open a shell in it:

    DEVICES=$(for d in /dev/neuron* /dev/infiniband/uverbs*; do printf -- '--device %s ' "$d"; done)
    docker run -it --rm --name sglang --init --ipc=host --network host \
      --cap-add SYS_NICE --cap-add IPC_LOCK --ulimit memlock=-1 $DEVICES \
      -v <models dir>:/models:ro -v <cache dir>:/cache $IMAGE bash
    

To open more shells in the same container, run docker exec -it sglang bash on the host.

Step 1: Start the decode server on two nodes#

In this step, you start the two halves of the decode server. Both nodes run the same command, each with its own HOST and NODE_RANK, and the same DIST_INIT_ADDR. Decode node 0 serves HTTP.

  1. On decode node 0, run:

    # decode node 0
    HOST=<decode node 0 address> NODE_RANK=0 DIST_INIT_ADDR=<decode node 0 address>:29500 \
      DRAFT_PATH=/models/Kimi-K3-DSpark bash /opt/sglang-neuron/examples/kimi_k3/run_decode.sh
    
  2. On decode node 1, run:

    # decode node 1
    HOST=<decode node 1 address> NODE_RANK=1 DIST_INIT_ADDR=<decode node 0 address>:29500 \
      DRAFT_PATH=/models/Kimi-K3-DSpark bash /opt/sglang-neuron/examples/kimi_k3/run_decode.sh
    

Step 2: Start the prefill server#

In this step, you start the prefill server. The prefill server also uses the drafter, to write its context rows for the decode server. On the prefill node, run:

# prefill node
HOST=<prefill node address> DRAFT_PATH=/models/Kimi-K3-DSpark bash /opt/sglang-neuron/examples/kimi_k3/run_prefill.sh

Wait until the prefill server and the decode server are ready. Each server log shows The server is fired up and ready to roll! when the server is ready.

Step 3: Start the balancer and the chat front#

In this step, you start the balancer and the chat front on the prefill node.

  1. Open a second shell in the container on the prefill node, and start the balancer:

    # prefill node, second shell, once both servers are up
    PREFILL_URL=http://<prefill node address>:30000 DECODE_URL=http://<decode node 0 address>:30000 \
      bash /opt/sglang-neuron/examples/kimi_k3/run_balancer.sh
    
  2. Open a third shell in the container on the prefill node, and start the chat front after the balancer is ready:

    # prefill node, third shell, once the balancer is up
    bash /opt/sglang-neuron/examples/kimi_k3/run_chat_front.sh
    

    The chat front listens on 127.0.0.1:8021 and sends requests to the balancer at http://127.0.0.1:8000.

Important

Send chat requests to the chat front, not to the balancer. A text request to the balancer fails, because the prefill server accepts token IDs only.

Step 4: Check the server#

In this step, you send one request whose answer you know. Open a fourth shell in the container on the prefill node, and run:

bash /opt/sglang-neuron/examples/kimi_k3/run_client.sh

The answer is 391, and the reasoning of the model is in reasoning_content.

Step 5: Send a multi-turn conversation#

In this step, you send a conversation of two turns to the chat front. Set temperature and top_p in every request. If a request does not set them, the chat front samples at temperature 1.0 and top-p 1.0, which is not the recommended sampling for Kimi-K3.

  1. Send the first turn:

    curl -s http://127.0.0.1:8021/v1/chat/completions -H 'Content-Type: application/json' -d '{
      "model": "/models/Kimi-K3", "temperature": 1.0, "top_p": 0.95, "max_tokens": 4096,
      "messages": [
        {"role": "user", "content": "Name three uses of a binary search tree."}]}'
    
  2. Send the second turn. Put the first question, the whole assistant message of the first answer, and the new question in messages. Kimi-K3 needs the reasoning_content of the first answer as well as its content. Replace <reasoning from the first turn> and <answer from the first turn> with the reasoning_content and content values of the first answer:

    curl -s http://127.0.0.1:8021/v1/chat/completions -H 'Content-Type: application/json' -d '{
      "model": "/models/Kimi-K3", "temperature": 1.0, "top_p": 0.95, "max_tokens": 4096,
      "messages": [
        {"role": "user", "content": "Name three uses of a binary search tree."},
        {"role": "assistant", "reasoning_content": "<reasoning from the first turn>", "content": "<answer from the first turn>"},
        {"role": "user", "content": "Give a Python example for the first use."}]}'
    

The prompt of the second turn starts with the prompt and the answer of the first turn. The prefill server finds the end of the first turn in its kimi-prompt prefix cache, so it computes only the tokens of the new question.

Confirmation#

  • run_client.sh returns 391.

  • Each turn of the conversation returns a chat completion with content and reasoning_content.

To check the accuracy, run the GSM8K check in the fourth shell:

bash /opt/sglang-neuron/examples/kimi_k3/run_gsm8k.sh

The check runs the SGLang chat evaluation client through the chat front, at temperature 1.0 and top-p 0.95. It prints Score, which is the accuracy. For Kimi-K3, expect a score of approximately 0.98. The client also prints a deprecation notice and lines about spec_accept_length. You can ignore these lines.

Common issues#

  • A text request fails. The request went to the balancer on port 8000. Send chat requests to the chat front on port 8021.

  • The prefill server exits at start, or the host stops it, while it allocates the host tier of the prefix cache. The node does not have enough free host memory for the host tier. The log line the host tier holds ... on this rank (... GiB) shows the size of the tier of each rank. Set SGLANG_NEURON_KIMI_PROMPT_HOST_SLOTS and SGLANG_NEURON_KIMI_PROMPT_HOST_STATE_SLOTS to lower values, and start run_prefill.sh again.

  • Requests time out behind long prompts. The KV cache of a request can reach the decode server late when the prefill server computes long prompts. The scripts set SGLANG_DISAGGREGATION_BOOTSTRAP_TIMEOUT=3600 on the prefill server and SGLANG_DISAGGREGATION_WAITING_TIMEOUT=3600 on the decode server. To change these values, set both variables to the same value before you start the scripts.

  • The decode server is slow between the two decode nodes. The decode nodes are not a recommended pair in the UltraServer. Use two nodes whose UltraServer indices are 1 and 2, or 0 and 3.

For more problems and fixes, see Troubleshooting: SGLang-Neuron.

Clean up#

Stop the chat front and the balancer first, and then the servers:

  1. In the chat front shell and in the balancer shell, press Ctrl+C.

  2. In the shell of the prefill server and in the shells of the two decode nodes, press Ctrl+C. Wait until each shell prompt shows again.

  3. Exit the container shells. The --rm option removes the containers.

Next steps#

Further reading#

This document is relevant for: Trn3