# High-Performance LLM Serving and Training with SGLang

## Abstract

This training lab shows you how to optimize and scale LLM workflows with SGLang. We walk through practical performance tuning using the SGL-Cookbook, dive into profiling and bottleneck analysis for developers, and demonstrate its deep integration into reinforcement learning (RL) training frameworks by showing how to use SGLang in a real RL run with the Miles RL framework. Prerequisite(s): Comfortable with Python and command-line workflows Curious about how LLM systems are optimized and scaled in practice No prior experience with SGLang, performance tuning, or RL training is required Explore training opportunities from the NVIDIA Deep Learning Institute &nbsp;(DLI). Access a wide range of self-paced courses &nbsp;or instructor-led virtual workshops &nbsp;designed to build key skills in AI, HPC, graphics &amp; simulation, and more. &nbsp; Validate your skills with NVIDIA certification &nbsp;and distinguish yourself in the industry.

## AI Summary

- Introduced the SGLAM system, highlighting its open-source nature and performance metrics, achieving 52K input tokens per second and 22K output tokens per second per node in May 2025.
- Covered recent features such as Zero Overhead Spec Decoding, which provided a 20% speedup, and optimizations for image and video generation, including support for multimodal models.
- Demonstrated how to set up and run the SGLang server for inference, including configuration parameters and profiling techniques to optimize performance.
- Explained the process of speculative decoding, where a smaller model generates tokens that are verified by the original model, leading to significant speed improvements for small batch sizes.
- Walked through the setup of a minimal reinforcement learning (RL) framework using SGLang and FSDP, including the training loop, reward calculation, and weight updates.
- Showed an improvement in model accuracy from 64% to 70% on the GSM8K dataset after 40 iterations of RL training, and discussed future plans to support more models and hardware.

## Transcript

**[00:00:04 – 00:00:09]** Hello everyone, my name is Charlene and today I want to first

**[00:00:09 – 00:00:14]** introduce the roadmap of SGLAM before we do the actual training.

**[00:00:14 – 00:00:19]** So I want to highlight the SGLAM overview and recent

**[00:00:19 – 00:00:22]** features and our future roadmap.

**[00:00:23 – 00:00:27]** So as an overview, SGL is the first open-source system

**[00:00:27 – 00:00:30]** that nearly matched the performance of DeepSeek's official blog

**[00:00:30 – 00:00:33]** with PD, disaggregation, and EP.

**[00:00:33 – 00:00:37]** You can see in this figure, in the evaluation, we used three

**[00:00:37 – 00:00:41]** workers for the preview workers and nine nodes for the decode workers.

**[00:00:41 – 00:00:46]** And we used DP-EP to do the expert parallelism, also DP-Attention

**[00:00:46 – 00:00:48]** to maximize the throughput.

**[00:00:48 – 00:00:57]** And here are the metrics in May 2025, we achieved like 52K input

**[00:00:57 – 00:01:04]** tokens per second per node and 22K output tokens per second per node.

**[00:01:04 – 00:01:13]** And we support multiple hardware like H20, VL72, also JAX on

**[00:01:13 – 00:01:17]** TPU, and SPARC, Intel, and AMD.

**[00:01:17 – 00:01:20]** We have several blocks on semi-analysis related

**[00:01:20 – 00:01:23]** to GB200 and GB300.

**[00:01:24 – 00:01:26]** And we have a very large community.

**[00:01:26 – 00:01:32]** We have over 1,000 computers, and over 60 institutions,

**[00:01:32 – 00:01:38]** and over 20 enterprise users like XIMD, and either LinkedIn

**[00:01:38 – 00:01:42]** and also other big tech companies.

**[00:01:42 – 00:01:46]** I want to highlight recent features, there are several

**[00:01:46 – 00:01:50]** important features like Zero Overhead Spec Decoding, SGR

**[00:01:50 – 00:01:56]** Diffusion, Spec Forge, Mini SGLump, and EPD disaggregation.

**[00:01:56 – 00:01:59]** For the zero overhead spec decoding, I think this is

**[00:01:59 – 00:02:03]** one of the most important features for the low latency

**[00:02:03 – 00:02:09]** cases because we can see previously there are CPU sync between the

**[00:02:09 – 00:02:15]** draft extent and draft, so which will cause the GPU bubble and the

**[00:02:15 – 00:02:17]** Which will slow down the performance.

**[00:02:17 – 00:02:21]** But now we have implemented overlap scheduling for the

**[00:02:21 – 00:02:22]** speculative decoding.

**[00:02:22 – 00:02:27]** So there are no CPU overhead between DraftX10 and the draft.

**[00:02:27 – 00:02:32]** So we can see in this providing results, we can see all the

**[00:02:32 – 00:02:36]** GPU kernels are...

**[00:02:37 – 00:02:40]** It will have no bubble anymore.

**[00:02:40 – 00:02:45]** And with that feature, we have about 20% speedup across the board.

**[00:02:50 – 00:02:54]** Also, we are actively accelerating image and video generation.

**[00:02:54 – 00:03:00]** For SGLon Diffusion, we have 1.2 to 5.9 speedup for image

**[00:03:00 – 00:03:02]** or video generation.

**[00:03:02 – 00:03:05]** And we have supported autoregressive plus diffusion

**[00:03:06 – 00:03:11]** multimodal models like WAN, Huanyuan, Queen Image, and Flux.

**[00:03:11 – 00:03:15]** We have optimized kernels and parallelism technicals.

**[00:03:15 – 00:03:18]** We are collaborating with Fast Video and End Group to

**[00:03:19 – 00:03:21]** do external diffusion.

**[00:03:21 – 00:03:26]** For the spec bundle and spec forge, it's native,

**[00:03:26 – 00:03:30]** it's done in integration for spec decoding optimization.

**[00:03:30 – 00:03:34]** And with Zestor, you can train your own ego model with your

**[00:03:34 – 00:03:36]** own data set and workload.

**[00:03:37 – 00:03:41]** It will have a very high acceptance if you train it on your own

**[00:03:41 – 00:03:44]** data set and it's production ready.

**[00:03:44 – 00:03:49]** It's used by the industry like Ant Group, Meituan,

**[00:03:49 – 00:03:51]** Nex-AGI, and Eigen AI.

**[00:03:51 – 00:03:55]** And we have supported models like Llama for DeepSeek, Kun3,

**[00:03:55 – 00:03:59]** MoE, and also GPT-OSS.

**[00:03:59 – 00:04:04]** For the mini-SGLAM, mini-SGLAM is a lightweight yet high-performance

**[00:04:04 – 00:04:08]** inference framework sharing the high-level system architecture

**[00:04:08 – 00:04:13]** as SGLAM, which means it will have some components very

**[00:04:13 – 00:04:18]** similar to SGLAM, but it only has thousands of codes.

**[00:04:18 – 00:04:23]** And it has two main goals, like providing learning

**[00:04:23 – 00:04:28]** resources and enabling fast prototyping for research.

**[00:04:28 – 00:04:33]** It has like attention backend like Flash Attention and Flash Infer,

**[00:04:33 – 00:04:39]** also support KV caching and also support some of features which make

**[00:04:39 – 00:04:44]** it as fast as SGLAM in some cases.

**[00:04:44 – 00:04:50]** Also for the roadmap in Q1 2026, we want to feature compatibility

**[00:04:50 – 00:04:55]** and reliability because we have so many features like

**[00:04:55 – 00:04:59]** PD desegregation, spec decoding, all kinds of parallelism,

**[00:04:59 – 00:05:03]** and all kinds of memory pool, also overlap scheduler.

**[00:05:03 – 00:05:07]** And we want to make all of them compatible and can be like

**[00:05:07 – 00:05:09]** open by default, open together.

**[00:05:09 – 00:05:14]** So for PD desegregation, we are improving the compatibility

**[00:05:14 – 00:05:16]** like these other features.

**[00:05:16 – 00:05:20]** Also for spec decoding, we are doing spec v2 and it's in progress.

**[00:05:20 – 00:05:24]** We are planning to make it by default, maybe next month.

**[00:05:24 – 00:05:28]** Which means it's compatible with Overlap scheduler and also for the

**[00:05:28 – 00:05:33]** parallelism, we are doing PP and EP refactor and also we are doing,

**[00:05:33 – 00:05:36]** actively doing CP development.

**[00:05:36 – 00:05:39]** Also for the memory port, we are doing memory port V2,

**[00:05:39 – 00:05:43]** which is a refactor of the memory port V1 and it will

**[00:05:43 – 00:05:50]** provide a better compatibility with SWA, also membar cache.

**[00:05:50 – 00:05:55]** And for overlap scheduler, we are doing the compatibility

**[00:05:55 – 00:05:58]** with spec decoding.

**[00:05:59 – 00:06:05]** So you can also check it out on our sjarngit hub and we are doing so

**[00:06:05 – 00:06:09]** many performance improvements like overlap scheduler for spec decoding

**[00:06:09 – 00:06:11]** and we are making it by default.

**[00:06:11 – 00:06:15]** For the peripheral CUDA graph we have made it by default

**[00:06:15 – 00:06:20]** and we use a piecewise CUDA graph for the peripheral stage

**[00:06:20 – 00:06:23]** Which means like you can

**[00:06:24 – 00:06:28]** We can do the CUDA Graph for peripheral without doing the

**[00:06:28 – 00:06:30]** padding for attention.

**[00:06:30 – 00:06:34]** And also, we are developing a unified memory pool for

**[00:06:34 – 00:06:37]** hybrid models like QN 3.5.

**[00:06:38 – 00:06:43]** It will have a linear layer for attention layer.

**[00:06:43 – 00:06:46]** Also, mixed trunk peripheral refactor

**[00:06:46 – 00:06:48]** and also torch compile stack.

**[00:06:48 – 00:06:51]** It's related to the P-slice CUDA graph.

**[00:06:51 – 00:06:54]** Also, we are doing SRT core plugin refactor.

**[00:06:54 – 00:06:58]** Also, DP, attention backend refactor.

**[00:06:58 – 00:07:01]** For the parallelism, we are also doing several refactors

**[00:07:01 – 00:07:04]** for pipeline parallelism, expert parallelism, and also we

**[00:07:04 – 00:07:08]** are developing context parallelism.

**[00:07:09 – 00:07:13]** We always actively doing enhancements offered by

**[00:07:13 – 00:07:15]** the EP on GB200 and GB300.

**[00:07:16 – 00:07:21]** We have several blogs on this, also similar analysis.

**[00:07:21 – 00:07:27]** And for the multi-modality, we are doing SGL-Diffusion, SGL-OMNI, and

**[00:07:27 – 00:07:35]** also we are making cookbook better to improve the user's experience.

**[00:07:36 – 00:07:42]** Yeah, so welcome to follow us and the stars.

**[00:07:42 – 00:07:46]** So, Pedro, maybe come.

**[00:07:48 – 00:07:51]** Yeah, then I will guide you through the training lab.

**[00:07:51 – 00:07:54]** Like first, if you want to launch the training lab, you

**[00:07:54 – 00:07:56]** can use this code.

**[00:07:56 – 00:08:00]** Please make sure you enter this event code under this

**[00:08:00 – 00:08:05]** URL so that the training lab can be launched faster.

**[00:08:06 – 00:08:12]** Then I will go through the content of our training lab.

**[00:08:18 – 00:08:21]** So for our training lab, it will be shown in the way of

**[00:08:21 – 00:08:25]** a Jupyter notebook.

**[00:08:31 – 00:08:34]** Our training lab includes two parts.

**[00:08:34 – 00:08:37]** Now I'm showing the first part, which is for SGLang inference.

**[00:08:37 – 00:08:42]** And about one hour later, Yueming, another instructor,

**[00:08:42 – 00:08:45]** she will show the second part.

**[00:08:48 – 00:08:52]** If you need any help for launching into this environment, you can

**[00:08:52 – 00:08:59]** just put a red cap on your table and then our TA will come to help.

**[00:09:02 – 00:09:06]** First I will introduce briefly about this training lab.

**[00:09:06 – 00:09:09]** It's mostly for the usage of SGLang, how to do the

**[00:09:09 – 00:09:12]** launching, how do you configure different parameters to

**[00:09:12 – 00:09:17]** make it run faster, and also I will introduce some common benchmarking

**[00:09:17 – 00:09:22]** and profiling techniques to harness SGLang better.

**[00:09:22 – 00:09:25]** During this training lab, we will use this model called

**[00:09:25 – 00:09:32]** QN3-30B-A3B-Instruct as example, which is the 30B MoE model

**[00:09:32 – 00:09:37]** with 3B parameters activated.

**[00:09:39 – 00:09:43]** So before running any codes, please make sure that the

**[00:09:43 – 00:09:47]** environment satisfies the following requirements.

**[00:09:47 – 00:09:51]** The notebook should be running inside the SGLang v.0.5.9

**[00:09:51 – 00:09:54]** docker container.

**[00:09:55 – 00:10:03]** And here is this instruction to check the environment sanity.

**[00:10:03 – 00:10:07]** Like as you can see, the SGLang version is 0.5.9.

**[00:10:08 – 00:10:13]** And for this environment, we have to NVIDIA H100.

**[00:10:13 – 00:10:19]** So you can check this through the NVIDIA SMI command.

**[00:10:24 – 00:10:28]** So first, we will do some simple configuration for the environment.

**[00:10:28 – 00:10:32]** Like we need to load, to set the default model path and

**[00:10:32 – 00:10:35]** the default host or port.

**[00:10:35 – 00:10:37]** The host will be set by the local host.

**[00:10:37 – 00:10:45]** And with the host and port, we can set a base link to this server.

**[00:10:45 – 00:10:49]** Each one, if you need to run a cell in the Jupyter notebook, you can

**[00:10:49 – 00:10:54]** just click the triangle up here.

**[00:10:55 – 00:11:00]** And here we set another variable called profile directory.

**[00:11:00 – 00:11:04]** Like, we need to set this environment variable before

**[00:11:04 – 00:11:09]** every time we need to gather profile results.

**[00:11:11 – 00:11:15]** Then we will launch the basic setting of SGLang server.

**[00:11:15 – 00:11:17]** Like here we are setting these two flags.

**[00:11:17 – 00:11:22]** The first flag is TP, like it just means the size for tensor parallel.

**[00:11:22 – 00:11:26]** If you know about distributed training or distributed

**[00:11:26 – 00:11:30]** inference, like TP is really useful for cutting a huge

**[00:11:30 – 00:11:32]** model into smaller models.

**[00:11:32 – 00:11:36]** And here's another flag, Trusted Remote Code, mostly for fetching

**[00:11:36 – 00:11:42]** weights and launching weights from HuggingFace transformers.

**[00:11:42 – 00:11:46]** Let's build the launch command.

**[00:11:48 – 00:11:54]** As you can see, the launch command is shown in the cell.

**[00:11:55 – 00:12:00]** And when launching the basic commands, even with the basic

**[00:12:00 – 00:12:04]** commands, we have set many optimization by default to

**[00:12:04 – 00:12:06]** make it run faster.

**[00:12:06 – 00:12:10]** For example, continuous batching, which is a common technique

**[00:12:10 – 00:12:14]** in ALM inference, and also overlap scheduler, which tries

**[00:12:14 – 00:12:20]** to overlap the GPU computation stream with CPU scheduler

**[00:12:20 – 00:12:24]** stream to make better performance.

**[00:12:24 – 00:12:29]** And by default, the FlashAttention3 kernel on Hopper is set as

**[00:12:29 – 00:12:32]** the attention backend for attention computation.

**[00:12:32 – 00:12:36]** And also, we set the TritonFusedMoeKernel

**[00:12:36 – 00:12:40]** as the default MoE kernel runner.

**[00:12:40 – 00:12:43]** On decode batches, we will enable CUDA Graph by default

**[00:12:43 – 00:12:47]** to avoid extra launching times.

**[00:12:48 – 00:12:51]** And also there's another technique called FlushingThroughOrReduce

**[00:12:51 – 00:12:56]** fusion, which will fuse the OrReduce and RMS norm together

**[00:12:56 – 00:12:59]** to make it run faster.

**[00:13:00 – 00:13:04]** To launch the server on Jupyter Notebook, will you use the

**[00:13:05 – 00:13:07]** interface called subprocess.popen.

**[00:13:07 – 00:13:10]** So let's try running it.

**[00:13:12 – 00:13:15]** Also, it prints nothing, but it will generate a server

**[00:13:15 – 00:13:17]** log under the current directory.

**[00:13:17 – 00:13:22]** Like, you can find it from the folder here.

**[00:13:27 – 00:13:31]** During launching, you can see the server printing some logs.

**[00:13:31 – 00:13:36]** First is the list of all of the server arguments.

**[00:13:40 – 00:13:45]** Yeah, you need to keep closing this file and open it again

**[00:13:45 – 00:13:49]** to make it updated.

**[00:13:49 – 00:13:54]** Like this is the limitation of this environment.

**[00:13:54 – 00:14:00]** And it also prints some other useful logs, like here.

**[00:14:00 – 00:14:04]** You will find that the attention backend is set by FH3, which

**[00:14:04 – 00:14:07]** means FlashAttention3 by default.

**[00:14:08 – 00:14:11]** And there are also some other logs for checking the status

**[00:14:11 – 00:14:13]** of communication.

**[00:14:13 – 00:14:19]** Like here, it's initializing the workspace for torch distributed.

**[00:14:21 – 00:14:25]** And now it's trying to load the weights from

**[00:14:25 – 00:14:28]** HuggingFace transformers.

**[00:14:28 – 00:14:30]** I will open it again.

**[00:14:30 – 00:14:36]** Yeah, now the model loading is completed, and it enters

**[00:14:36 – 00:14:39]** the stage of capturing CUDA graphs.

**[00:14:39 – 00:14:45]** Like, for this model, we will capture a CUDA graph with its

**[00:14:45 – 00:14:48]** batch sizes ranging from 1 to 256.

**[00:14:55 – 00:14:57]** Yeah, that's...

**[00:15:03 – 00:15:08]** Now we can check that all of the codagraphs have been captured.

**[00:15:12 – 00:15:16]** And then as the server prints, the server is

**[00:15:16 – 00:15:18]** fired up and ready to roll.

**[00:15:18 – 00:15:23]** It means that the server has ended all of its initialization work.

**[00:15:23 – 00:15:29]** And we can start sending requests to the server.

**[00:15:30 – 00:15:35]** Here the cell is for checking the server is ready.

**[00:15:40 – 00:15:45]** And then we are trying to send a simple prompt to the server.

**[00:15:45 – 00:15:50]** Like we are going to let the model to introduce itself.

**[00:15:55 – 00:15:58]** Yes, from the response, we can find that the model is

**[00:15:58 – 00:16:05]** running correctly, like it prints, hello, I'm Quinn.

**[00:16:05 – 00:16:12]** And it also shows some statistics like there are 92 completed tokens.

**[00:16:16 – 00:16:21]** Also, we provide some endpoint for, you know, for fetching

**[00:16:21 – 00:16:22]** information from the server.

**[00:16:22 – 00:16:27]** Like this check server info endpoint, it will print some

**[00:16:27 – 00:16:30]** useful parameters.

**[00:16:31 – 00:16:32]** Like, it's really long.

**[00:16:33 – 00:16:35]** Yeah.

**[00:16:37 – 00:16:41]** Then I will introduce how to do some single benchmarking

**[00:16:41 – 00:16:42]** on the SGLang server.

**[00:16:42 – 00:16:46]** Here we will use this API called Bench1BatchServer,

**[00:16:46 – 00:16:54]** which means send a batch with fixed batch size, like one or 16 or 256.

**[00:16:54 – 00:17:00]** So we can check this under this batch size, how the

**[00:17:00 – 00:17:03]** server is performing.

**[00:17:05 – 00:17:08]** But before we do any benchmark, we need to do some warm up,

**[00:17:09 – 00:17:12]** including flushing the KV cache to avoid cache hits

**[00:17:12 – 00:17:15]** that will affect performance.

**[00:17:15 – 00:17:19]** And also, we will send some warm up batches.

**[00:17:27 – 00:17:29]** Like when running one map, it might take a little time

**[00:17:29 – 00:17:34]** to finish, because it will trigger some kernel JIT-ing.

**[00:17:34 – 00:17:36]** JIT means just-in-time.

**[00:17:36 – 00:17:40]** Like it will compile some kernel before running anything,

**[00:17:40 – 00:17:46]** so the system can always pick the best kernel.

**[00:17:48 – 00:17:52]** Like after it shows warm-up end, it means that the server

**[00:17:52 – 00:17:55]** is ready for doing benchmarking.

**[00:17:55 – 00:17:59]** First, we will try to send the batch with batch size

**[00:17:59 – 00:18:03]** 1 and see how it performs.

**[00:18:08 – 00:18:13]** During the service running, you can always check the server output to

**[00:18:13 – 00:18:18]** make sure it's running correctly.

**[00:18:26 – 00:18:30]** Yeah, now the benchmark for batch size one has ended.

**[00:18:30 – 00:18:35]** It will print some useful metrics, like the input throughput.

**[00:18:36 – 00:18:40]** It shows that it's about 18,000 tokens per second, and output

**[00:18:40 – 00:18:45]** throughput, which is about 200 tokens per second.

**[00:18:48 – 00:18:54]** Then we will try another batch for batch size 64.

**[00:19:11 – 00:19:17]** Yeah, we can check from the server log that there are

**[00:19:17 – 00:19:24]** some metrics like the generation throughput for each decode batches.

**[00:19:34 – 00:19:39]** Yeah, now the result for batch size 64 has been printed.

**[00:19:39 – 00:19:42]** The total input-support is like 39K, and the total

**[00:19:42 – 00:19:45]** output-support is like 4,000.

**[00:19:47 – 00:19:50]** And then we can use this cell for comparing the results

**[00:19:50 – 00:19:54]** between different batch sizes.

**[00:19:56 – 00:20:00]** Yeah, it will just list them.

**[00:20:02 – 00:20:05]** Then we will do another experiment to check how

**[00:20:05 – 00:20:11]** the performance changes between different batch sizes.

**[00:20:11 – 00:20:17]** Here we define this function called switchSweepBatchSizes.

**[00:20:18 – 00:20:22]** And also a function for drawing the curve.

**[00:20:22 – 00:20:27]** It will take some time, like about two minutes.

**[00:20:27 – 00:20:33]** Like before the result is returned, I will explain the curve, which

**[00:20:33 – 00:20:35]** is also called Pareto Frontier.

**[00:20:35 – 00:20:40]** Like you might have watched the keynote on Monday.

**[00:20:40 – 00:20:46]** Like Jensen showed many plots on a Pareto curve.

**[00:20:46 – 00:20:50]** which is a metric for checking the power of hardware.

**[00:20:50 – 00:20:53]** It looks like this.

**[00:20:54 – 00:20:59]** On the left-up side is the place where the server is trying to reach

**[00:20:59 – 00:21:01]** throughput as high as possible.

**[00:21:01 – 00:21:07]** But for these cases, the throughput per sequence or per request

**[00:21:07 – 00:21:10]** is usually very slow.

**[00:21:10 – 00:21:14]** But on the right bottom side, it means that the system is

**[00:21:14 – 00:21:21]** trying to reach the low latency, like as low as possible.

**[00:21:21 – 00:21:24]** In these cases, you can only serve a small batch size,

**[00:21:24 – 00:21:29]** but the throughput per sequence will be really high.

**[00:21:38 – 00:21:42]** Yeah, now it is still running.

**[00:21:48 – 00:21:53]** You can always check the server output to make sure

**[00:21:53 – 00:21:55]** it's still running.

**[00:22:44 – 00:22:47]** Finally, it shows the plot.

**[00:22:47 – 00:22:51]** You can see on the plot, for small batch sizes, like batch

**[00:22:51 – 00:22:56]** size equals to 1, its overall throughput is really small, like

**[00:22:56 – 00:23:00]** a few hundred, but the throughput per request is really high.

**[00:23:00 – 00:23:05]** But for large batch sizes, like batch size equals to

**[00:23:05 – 00:23:09]** 256, the overall throughput is high, Because the support

**[00:23:09 – 00:23:11]** per request is really low.

**[00:23:11 – 00:23:15]** So this is just the Pareto Frontier curve.

**[00:23:16 – 00:23:21]** Like in practical use, we will flip the order.

**[00:23:22 – 00:23:26]** Like the large batch sizes is usually on the top.

**[00:23:29 – 00:23:32]** Then we will go through a short tutorial on how to profile

**[00:23:32 – 00:23:35]** with PyTorch Profiler.

**[00:23:35 – 00:23:40]** Here we have different choice of the method for doing the profiling.

**[00:23:41 – 00:23:42]** Here is one of them.

**[00:23:42 – 00:23:45]** It's the endpoint called startProfile.

**[00:23:45 – 00:23:50]** Each time you send a request to the startProfile endpoint,

**[00:23:50 – 00:23:53]** it will start collecting some statistics over the kernels and

**[00:23:53 – 00:24:00]** the servers So you can check the bottleneck of the running server.

**[00:24:14 – 00:24:18]** So the simplest approach for profiling is to append

**[00:24:18 – 00:24:24]** the profile arguments to the bench1-batch-server command.

**[00:24:25 – 00:24:30]** After finishing profiling, it will generate a file, a

**[00:24:30 – 00:24:35]** folder, under the current folder.

**[00:24:35 – 00:24:43]** So it will include some files with trace.json.jz postfix.

**[00:24:43 – 00:24:48]** So I will not show how to watch them in this lab because

**[00:24:48 – 00:24:52]** the time is limited, but basically you can just download them

**[00:24:52 – 00:25:02]** onto your laptop and open them with this URL called ui.profetto.

**[00:25:05 – 00:25:07]** Yeah, this one.

**[00:25:08 – 00:25:12]** So you just drag the trace files into this interface,

**[00:25:12 – 00:25:20]** and it will show a good trace of all of the CPU and GPU behaviors.

**[00:25:23 – 00:25:26]** Since the time is limited, we will leave the following

**[00:25:26 – 00:25:30]** parts for the learners.

**[00:25:34 – 00:25:37]** Then I will briefly introduce some common techniques for

**[00:25:37 – 00:25:39]** tuning the server.

**[00:25:39 – 00:25:44]** Like for SGLang, there are so many arguments, but sometimes

**[00:25:44 – 00:25:49]** we will get confused when to tune which parameter.

**[00:25:49 – 00:25:53]** So here I list some of the most common knobs

**[00:25:53 – 00:25:55]** for tuning the performance.

**[00:25:55 – 00:26:00]** For example, the tensor parallelism I had introduced before, the

**[00:26:00 – 00:26:06]** larger tensor parallelism, you are using more GPUs, and

**[00:26:06 – 00:26:09]** since you have more GPUs, you can just divide all the computation

**[00:26:09 – 00:26:16]** costs evenly onto each of them, so it will make the running faster.

**[00:26:16 – 00:26:20]** But tensor parallelism is often useful for small batch sizes

**[00:26:20 – 00:26:23]** rather than large batch sizes.

**[00:26:23 – 00:26:28]** And the data parallel attention, this technique is usually used

**[00:26:28 – 00:26:33]** in GQA or MLA models like DeepSeek.

**[00:26:33 – 00:26:36]** It's really useful under some really large batch sizes,

**[00:26:36 – 00:26:37]** maybe 1,000, 2,000.

**[00:26:37 – 00:26:39]** So,

**[00:26:40 – 00:26:44]** It will send different requests to different GPUs, so they

**[00:26:44 – 00:26:49]** can be computed together, but for low batch sizes like

**[00:26:49 – 00:26:53]** BS equals to 1 or 4, data parallel attention will cause some load

**[00:26:53 – 00:26:57]** imbalance between different GPUs.

**[00:26:57 – 00:26:59]** Quantization is also a common technique.

**[00:26:59 – 00:27:05]** Like with quantization, you can lower the model from higher

**[00:27:05 – 00:27:11]** data formats like BF16 to lower data formats like FP4 or FP8.

**[00:27:11 – 00:27:15]** Like with lower data format, the model can be run faster

**[00:27:15 – 00:27:20]** because it can utilize some of the latest features on

**[00:27:20 – 00:27:25]** Blackwell or Ruby in hardware.

**[00:27:26 – 00:27:31]** For the memory fraction, it controls the fraction of memory

**[00:27:31 – 00:27:33]** reserved for kvcache.

**[00:27:33 – 00:27:36]** Like, if there are many, many requests, we need a large

**[00:27:36 – 00:27:41]** space for kvcache, then we will raise the value of memory fraction.

**[00:27:41 – 00:27:45]** But it also has a downside, which is that a large memory

**[00:27:45 – 00:27:49]** fraction will easily lead to out-of-memory issues.

**[00:27:49 – 00:27:55]** So if you see out-of-memory issue any time, we can always

**[00:27:55 – 00:28:01]** think about lowering the fraction of kvcache memory.

**[00:28:01 – 00:28:05]** This argument called Schedule Conservativeness is for controlling

**[00:28:05 – 00:28:06]** the batching strategy.

**[00:28:07 – 00:28:11]** Like if you are setting it to a lower value, it means the batching

**[00:28:11 – 00:28:14]** strategy will be more aggressive.

**[00:28:14 – 00:28:19]** And so it will more easily cause the sequence retraction.

**[00:28:20 – 00:28:22]** Like for the sequence retraction, it means that the KV cache

**[00:28:22 – 00:28:26]** is not enough for the following running sequences.

**[00:28:26 – 00:28:30]** And the KV cache for some of the sequences will be released.

**[00:28:30 – 00:28:34]** So it will cause some waste of time.

**[00:28:34 – 00:28:38]** And also CUDA Graph Max BS, it controls the number of

**[00:28:39 – 00:28:40]** CUDA Graph you want to capture.

**[00:28:40 – 00:28:44]** The more CUDA Graph, the better performance you can get on

**[00:28:44 – 00:28:52]** some larger batch sizes, but also it will occupy more memory.

**[00:28:54 – 00:28:58]** And chunk profile size is useful on some really long sequences,

**[00:28:58 – 00:29:03]** when the sequence is so long that you need to cut it into chunks

**[00:29:03 – 00:29:05]** to avoid out-of-memory issue.

**[00:29:06 – 00:29:12]** And also Max Concurrency, you can control the number of running

**[00:29:12 – 00:29:16]** requests with this argument.

**[00:29:18 – 00:29:22]** Here we have some metrics for monitoring the server health.

**[00:29:22 – 00:29:28]** They can be found on the server log I have shown before.

**[00:29:29 – 00:29:37]** Like the token usage, it shows like the fraction of space

**[00:29:37 – 00:29:40]** for kvcache you are using now.

**[00:29:40 – 00:29:42]** If this value is really close to 1, it means that

**[00:29:42 – 00:29:45]** the kvcache is almost full.

**[00:29:45 – 00:29:50]** And also the throughput, like on the last time slice was

**[00:29:50 – 00:29:54]** the speed of your server.

**[00:29:55 – 00:30:01]** And also the running request, it checks how many requests

**[00:30:01 – 00:30:04]** it's running on the server.

**[00:30:08 – 00:30:12]** Yeah, we can try to fetch some of them.

**[00:30:17 – 00:30:20]** You can also fetch the server arguments from

**[00:30:21 – 00:30:24]** the GetServerInfo endpoint.

**[00:30:26 – 00:30:29]** And then I would terminate the current server and try to launch

**[00:30:29 – 00:30:33]** with some other configurations

**[00:30:33 – 00:30:38]** to see whether it's faster or not.

**[00:30:42 – 00:30:46]** Yeah, after it shows server terminated, like if we check

**[00:30:46 – 00:30:49]** the server output,

**[00:30:52 – 00:30:55]** It will show that like

**[00:30:56 – 00:30:58]** Yeah, it will print nothing.

**[00:30:58 – 00:31:03]** It just means it's exiting in a normal way.

**[00:31:05 – 00:31:11]** Then I will go through two really simple example on how to

**[00:31:11 – 00:31:14]** How do you accelerate the server with different techniques?

**[00:31:15 – 00:31:17]** First, it's called FP8 quantization.

**[00:31:17 – 00:31:21]** Like here, we are launching an alternative checkpoint

**[00:31:21 – 00:31:25]** of this model called QN3-30B blah, blah, blah, FP8.

**[00:31:25 – 00:31:30]** It means that this checkpoint is quantized from the original

**[00:31:30 – 00:31:37]** checkpoint, but with the lower data format, FP8.

**[00:31:40 – 00:31:45]** To launch a FPA model in SGLang, basically you just don't need to

**[00:31:45 – 00:31:50]** do any extra operations because the system will automatically detect

**[00:31:50 – 00:31:54]** the quantization format for you.

**[00:31:54 – 00:32:00]** So here we just changed the model path to the quantized model path.

**[00:32:02 – 00:32:07]** And then also, I will try to launch the server with

**[00:32:07 – 00:32:15]** the new configurations and then wait until the server is ready.

**[00:32:30 – 00:32:36]** We can keep checking the server output log until it's ready.

**[00:32:59 – 00:33:03]** Here it might take some extra time, like we are interviewing

**[00:33:03 – 00:33:08]** the stage of DeepGEMM warm-up where we need to warm up some

**[00:33:08 – 00:33:14]** of the AP8 GEMM kernels so that it can run faster during runtime.

**[00:34:53 – 00:34:59]** We need to wait for another two or three minutes.

**[00:35:27 – 00:35:33]** Before it finishes launching, I can start to explain our

**[00:35:33 – 00:35:37]** next optimization called Speculative decoding.

**[00:35:37 – 00:35:42]** Speculative decoding is also another common practice for

**[00:35:42 – 00:35:45]** accelerating the inference server.

**[00:35:45 – 00:35:47]** Like with speculative decoding, you're launching actually

**[00:35:47 – 00:35:49]** two models together.

**[00:35:49 – 00:35:52]** The first model is the original model you want to do inference

**[00:35:52 – 00:35:55]** on, but the second model is a much smaller model.

**[00:35:55 – 00:36:00]** Like, first you are trying to generate tokens with

**[00:36:00 – 00:36:02]** the smaller model.

**[00:36:02 – 00:36:07]** So after the smaller model generates like four or five tokens,

**[00:36:07 – 00:36:13]** you will set them together to the original model for verification.

**[00:36:13 – 00:36:17]** In most of the cases, like maybe two or three of them

**[00:36:18 – 00:36:20]** will be accepted at the same time.

**[00:36:20 – 00:36:25]** So in this case, we are reducing the number of doing batch

**[00:36:25 – 00:36:30]** forwards for the original server to reach speedup.

**[00:36:30 – 00:36:36]** And ego is one of the ways for speculative coding, which

**[00:36:36 – 00:36:41]** might be the most popular one in the industry.

**[00:36:42 – 00:36:48]** Yeah, we can check the server again and, oh yeah.

**[00:36:50 – 00:36:53]** Yeah, now it's fired up and ready to roll.

**[00:36:54 – 00:36:57]** Yeah, we will first sweep some batches on the prior

**[00:36:57 – 00:37:02]** setting with IP8 quantization.

**[00:37:22 – 00:37:26]** Like, we still need to wait for like one minute

**[00:37:26 – 00:37:30]** to collect the result.

**[00:37:34 – 00:37:38]** We can see that the server is printing something.

**[00:38:59 – 00:39:03]** Yeah, this is the Walmart sweep up.

**[00:39:07 – 00:39:13]** After the warm-up sweep, we can start the official sweep

**[00:39:13 – 00:39:16]** for our benchmarking.

**[00:39:19 – 00:39:23]** Yes, the environment is a little slow.

**[00:41:03 – 00:41:08]** Like now with better tools like Cloud or GPT, we can

**[00:41:08 – 00:41:13]** try to automate this whole process like with some skills.md

**[00:41:13 – 00:41:18]** or automated benchmark scripts.

**[00:41:18 – 00:41:20]** So it will make things much easier.

**[00:41:20 – 00:41:26]** Like during the machine is doing benchmarking, like we can do

**[00:41:26 – 00:41:29]** something else like drink a coffee.

**[00:41:29 – 00:41:33]** Yeah, now we have the new results for the Pareto frontier.

**[00:41:33 – 00:41:38]** Like, we can try to compare this graph into the graph before.

**[00:41:38 – 00:41:45]** Like, expectedly, the curve for FP8 model, it should be

**[00:41:45 – 00:41:49]** like a little higher than the curve for the BF16 model.

**[00:41:50 – 00:41:52]** But in many cases, if...

**[00:41:52 – 00:41:58]** If it doesn't have any huge improvement, there might be

**[00:41:58 – 00:42:03]** some bugs or you need to do some analyze on the workloads

**[00:42:03 – 00:42:06]** and the model computation.

**[00:42:08 – 00:42:11]** Yeah, then we will try to launch with Eagle 3.

**[00:42:12 – 00:42:20]** Like it will be much more obvious, like, I mean, for the acceleration.

**[00:42:32 – 00:42:35]** Also, since we are launching a new server, we also need to

**[00:42:35 – 00:42:39]** wait for the server to be booted.

**[00:42:53 – 00:42:58]** Here we are turning off the overlap scheduler for speculative

**[00:42:58 – 00:43:03]** decoding because this feature is still under experiment.

**[00:43:03 – 00:43:08]** If you want to enable overlap scheduler with speculative

**[00:43:08 – 00:43:14]** decoding together, you need to add a flag called enable spec v2.

**[00:43:14 – 00:43:17]** But here we choose to turn it off.

**[00:43:32 – 00:43:38]** Oops, it seems that here is a bug, but we can just do

**[00:43:38 – 00:43:42]** some debugging, like from the log it says the target

**[00:43:42 – 00:43:48]** model's context length is greater than the derived context length.

**[00:43:49 – 00:43:53]** Yeah, like from the hints of the log, we can try to

**[00:43:53 – 00:43:57]** set this environment variable.

**[00:44:42 – 00:44:45]** And try it again.

**[00:45:03 – 00:45:08]** Like here, because we are launching with Speculative decoding,

**[00:45:08 – 00:45:12]** here we added another model pass called the Ego3 pass, which

**[00:45:13 – 00:45:17]** points to the pass of draft model.

**[00:45:17 – 00:45:22]** Like the draft model is also released on Hugging Face.

**[00:45:38 – 00:45:43]** Now we can see that the system is capturing CUDA graphs for both the

**[00:45:43 – 00:45:48]** target model and the draft model.

**[00:45:55 – 00:45:57]** Yeah, now the server is fired up and ready to roll.

**[00:45:57 – 00:46:01]** Let's do the benchmark again.

**[00:46:21 – 00:46:28]** Then wait patiently for the result of benchmarking and

**[00:46:28 – 00:46:35]** keep checking that the server is running in a healthy way.

**[00:46:37 – 00:46:41]** As a result, we already see that the throughput for batch

**[00:46:41 – 00:46:47]** size 1 is like 400, like much, much larger than the prior one.

**[00:47:04 – 00:47:09]** Yeah, here it will print an extra metric called Accepted Length.

**[00:47:09 – 00:47:13]** When the Accepted Length is really high, it means that the

**[00:47:13 – 00:47:19]** acceleration for the speculative decoding is really good.

**[00:47:29 – 00:47:33]** Yes, now we are drawing the curve.

**[00:47:33 – 00:47:36]** Here, I didn't try the larger batch sizes because Speculative

**[00:47:36 – 00:47:41]** decoding is actually not really good on large batch sizes.

**[00:47:42 – 00:47:45]** Like we can compare in this batch sizes for the results

**[00:47:45 – 00:47:50]** before, for batch size one, the throughput per request is like 900.

**[00:47:50 – 00:47:54]** But for the prior one on FP8 model, it's only 450.

**[00:47:54 – 00:47:59]** But the speedup is almost doubled.

**[00:48:03 – 00:48:08]** And for the DS16, for example, the overall throughput is

**[00:48:08 – 00:48:13]** 4,000 and the throughput per request is about 200.

**[00:48:15 – 00:48:20]** Comparing with the prior one, which is also about 200, it

**[00:48:20 – 00:48:26]** means that on larger batch sizes, speculative decoding is behaving

**[00:48:26 – 00:48:29]** not as good as small batch sizes.

**[00:48:34 – 00:48:39]** At last, we will terminate this server.

**[00:48:41 – 00:48:44]** And for the last part, there are some references for the

**[00:48:45 – 00:48:48]** CLI commands we usually use.

**[00:48:51 – 00:48:55]** Yeah, now it's the end of the training for inference part.

**[00:48:55 – 00:48:59]** Then we will start the training for RL training.

**[00:48:59 – 00:49:04]** So I think Yuemi, she will come instead of me for this

**[00:49:04 – 00:49:07]** part of tutorial.

**[00:49:54 – 00:49:58]** Is this your presentation? Yes, yes.

**[00:50:05 – 00:50:07]** Okay, how can I show the training?

**[00:50:08 – 00:50:11]** Oh, it works, thank you.

**[00:50:14 – 00:50:18]** Okay, so now we can come to the second part of today's

**[00:50:18 – 00:50:21]** training lab, which is SGLang RL.

**[00:50:21 – 00:50:24]** So let me first very quickly introduce what

**[00:50:24 – 00:50:25]** reinforcement learning is.

**[00:50:25 – 00:50:29]** So basically, you will let the language model, you may give

**[00:50:29 – 00:50:35]** it one prompt, which is a question without any standard responses.

**[00:50:35 – 00:50:38]** And you let the model to generate multiple responses, maybe

**[00:50:38 – 00:50:42]** a lot of them, and you will give them some scores.

**[00:50:42 – 00:50:47]** So you can rate the quality of those responses.

**[00:50:47 – 00:50:50]** You give a positive score to the good ones and give

**[00:50:50 – 00:50:52]** a negative score to the bad ones.

**[00:50:52 – 00:50:56]** Then you can optimize your model using the responses

**[00:50:56 – 00:50:58]** that generated by the model itself.

**[00:50:58 – 00:51:02]** So this is what reinforcement learning does.

**[00:51:02 – 00:51:05]** And that's why we'll introduce reinforcement learning

**[00:51:05 – 00:51:06]** in this SGLang lab.

**[00:51:06 – 00:51:10]** You can see that the generate process is a very important part

**[00:51:10 – 00:51:14]** during the entire reinforcement learning because the faster

**[00:51:15 – 00:51:18]** your model can generate responses, the faster you can train your

**[00:51:18 – 00:51:20]** model and make it better.

**[00:51:20 – 00:51:26]** So into this lab, I will lead you to build a minimal but functional

**[00:51:26 – 00:51:32]** RL framework using SGLang and one FSDP server with our own

**[00:51:32 – 00:51:35]** scheduler, which is the notebook.

**[00:51:35 – 00:51:38]** And we'll be together to training a clean model with

**[00:51:38 – 00:51:41]** 0.6 billion parameters.

**[00:51:41 – 00:51:48]** And you can see how its performance grows on the GSM8K dataset.

**[00:51:50 – 00:51:53]** So first, let's look at the RL lifecycle.

**[00:51:53 – 00:51:57]** In this figure, we draw the system design principles of

**[00:51:57 – 00:52:00]** our reinforcement learning framework, MILES.

**[00:52:00 – 00:52:04]** So the first stage is draw out, where you use SGLang

**[00:52:04 – 00:52:08]** engine to generate the responses for all the prompts, and you

**[00:52:08 – 00:52:13]** can also let your engine to interact with the environment.

**[00:52:13 – 00:52:16]** This process can have actually multiple turns.

**[00:52:16 – 00:52:22]** For example, you generate one response and you do a two-call

**[00:52:22 – 00:52:26]** to interact with the environment and you gather two-call result back

**[00:52:26 – 00:52:30]** to generate the second response and do the second two-call.

**[00:52:30 – 00:52:33]** And after all the turns, you gather the full trajectory

**[00:52:33 – 00:52:35]** from the rollout stage.

**[00:52:35 – 00:52:39]** And then you send this trajectory to our scheduler.

**[00:52:39 – 00:52:44]** So in our actual I-O training with SGLang, we use mouse.

**[00:52:44 – 00:52:48]** And the mouse plays the role of the scheduler.

**[00:52:48 – 00:52:53]** So it will process the data, calculate the

**[00:52:53 – 00:52:59]** There are also necessary metrics and laws, and you will also process

**[00:52:59 – 00:53:06]** all of the training responses as the input of the training engine.

**[00:53:06 – 00:53:10]** And you send the loss and the input to the training engine.

**[00:53:10 – 00:53:15]** It will just do one forward and one backward to calculate the gradient.

**[00:53:15 – 00:53:18]** After that, it will just optimize the model and get

**[00:53:18 – 00:53:21]** the weight updated.

**[00:53:21 – 00:53:23]** And at this time, you will also have the training engine

**[00:53:23 – 00:53:26]** with the newly updated weight.

**[00:53:26 – 00:53:30]** So you will also need to send the updated weight to the inference

**[00:53:30 – 00:53:33]** engine, which is your SGLang.

**[00:53:35 – 00:53:40]** So you can see that we draw SGLang as two green blocks as the

**[00:53:40 – 00:53:43]** right lower part of this figure.

**[00:53:43 – 00:53:46]** We call it SGLang server, and we use SGLang router to

**[00:53:46 – 00:53:49]** choose between the SGLang servers.

**[00:53:49 – 00:53:52]** And we also draw Megatron in these red blocks.

**[00:53:52 – 00:53:58]** In this training lab, it will also take the form of a server.

**[00:54:00 – 00:54:04]** So let's see this process in a more detailed way.

**[00:54:04 – 00:54:07]** First, it's a rule out, we use SGLang.

**[00:54:07 – 00:54:11]** And you can also use any kind of external environment.

**[00:54:11 – 00:54:15]** So for example, you can give an agent framework to SGLang, and

**[00:54:15 – 00:54:21]** you make SGLang interact with agent framework to get a full trajectory.

**[00:54:21 – 00:54:24]** So after that, you will do the data processing.

**[00:54:24 – 00:54:29]** It includes the reward calculation, the value estimation, so maybe

**[00:54:29 – 00:54:33]** the KL divergence, and the advantage calculation.

**[00:54:33 – 00:54:37]** So after those, you will compute the gradient loss

**[00:54:37 – 00:54:40]** and send the gradient loss to the training stage.

**[00:54:40 – 00:54:43]** The training backend also have a lot of choices, maybe

**[00:54:43 – 00:54:45]** the Megatron and FSDP.

**[00:54:45 – 00:54:49]** The Megatron way will be more...

**[00:54:49 – 00:54:52]** At most of the time, more efficient, but it's kind

**[00:54:52 – 00:54:55]** of complex, so it's more suitable for large-scale

**[00:54:55 – 00:54:58]** trainings and also FSDP.

**[00:54:58 – 00:55:02]** FSDP is easier to use and the code is very clean, but it

**[00:55:02 – 00:55:05]** can be kind of slow at large scale.

**[00:55:05 – 00:55:11]** So actually we'll first do a forward only path.

**[00:55:11 – 00:55:16]** In this path, it will feed the responses you get from the reward

**[00:55:16 – 00:55:21]** engine to the training engine and calculate the log probabilities

**[00:55:21 – 00:55:23]** from the training engine.

**[00:55:23 – 00:55:26]** So we will use this log probabilities in the PPO

**[00:55:26 – 00:55:29]** loss and calculate.

**[00:55:29 – 00:55:33]** After that, it will send the responses again to the training

**[00:55:33 – 00:55:38]** engine and do a full training path, including forward and backward

**[00:55:38 – 00:55:42]** and optimizer states updates, so it can train the model there.

**[00:55:43 – 00:55:47]** And at the end, you will call a weight update process to update

**[00:55:47 – 00:55:50]** the trained weight to SGLang.

**[00:55:51 – 00:55:56]** So this is the most simple

**[00:55:56 – 00:55:59]** framework of our system.

**[00:56:00 – 00:56:03]** At the beginning, we want to briefly introduce why we want

**[00:56:03 – 00:56:06]** to use SGLang as a reward backend.

**[00:56:06 – 00:56:12]** First, because we are actively developing on the RL workload,

**[00:56:12 – 00:56:15]** so it is always co-evolving with the RL framework.

**[00:56:15 – 00:56:20]** For example, we enable partial reward and dedicate reward

**[00:56:20 – 00:56:22]** control in SGLang.

**[00:56:22 – 00:56:25]** So you don't need to make all of those as plugins in

**[00:56:25 – 00:56:27]** your RL framework.

**[00:56:27 – 00:56:30]** You can just use that to support them.

**[00:56:30 – 00:56:34]** Second is SGLang has a well-designed router,

**[00:56:34 – 00:56:41]** so it has cache-aware load balancing for the high throughput

**[00:56:41 – 00:56:44]** route, which is the RL workload.

**[00:56:44 – 00:56:48]** And also it has native async handling, so you can easily

**[00:56:48 – 00:56:52]** support your reinforcement learning with async training.

**[00:56:52 – 00:56:55]** Also, as we all know, SGLang is a fast-moving framework.

**[00:56:55 – 00:57:00]** It will have a lot of rich optimizations on the route itself.

**[00:57:00 – 00:57:05]** So we don't need to worry about the route speed as a training engineer.

**[00:57:05 – 00:57:12]** You can just use SGLang to unlock all of the inverse powers.

**[00:57:13 – 00:57:17]** Okay, so now we can introduce today's training lab.

**[00:57:17 – 00:57:20]** In this lab, we will build a minimal reinforcement

**[00:57:20 – 00:57:24]** framework using SGLang as the inference engine.

**[00:57:24 – 00:57:29]** Actually, we extract some essential components from Miles, which

**[00:57:29 – 00:57:34]** is the native I-O framework for SGLang, to create this tiny Miles.

**[00:57:34 – 00:57:38]** This is a simplified but functional GRPO training pipeline that

**[00:57:38 – 00:57:42]** will improve the language model's math reasoning capability

**[00:57:42 – 00:57:46]** on the GSM8K dataset.

**[00:57:46 – 00:57:50]** So now let's look at the training lab itself.

**[00:57:56 – 00:58:03]** Okay, so you can start with this training lab SGLang RIO file.

**[00:58:08 – 00:58:13]** Yeah, basically, you can first look at the architecture at

**[00:58:13 – 00:58:16]** the top of this notebook.

**[00:58:16 – 00:58:22]** So we use SGLang and FSDP as the inference engine and training

**[00:58:22 – 00:58:26]** engine for this tiny mouse.

**[00:58:27 – 00:58:32]** We will use torch memory server to build both of the engines because

**[00:58:32 – 00:58:37]** we will make the two engines co-locate on their two H100s.

**[00:58:38 – 00:58:42]** So during the rollout stage, SGLang will own the GPUs,

**[00:58:42 – 00:58:47]** and during the training, the FSDP training engine will own the GPUs.

**[00:58:47 – 00:58:50]** That means when you are doing rollout, you have to upload

**[00:58:50 – 00:58:54]** all your training engine onto the CPU to save the GPU memory.

**[00:58:55 – 00:58:58]** And when you do training, you have to upload your inference system

**[00:58:59 – 00:59:02]** to CPU to save the GPU memory.

**[00:59:02 – 00:59:05]** And that's why we enable the touch memory saver here.

**[00:59:05 – 00:59:11]** This is also the same design we use in mouse.

**[00:59:12 – 00:59:15]** So now let's look at this entire system step by step.

**[00:59:15 – 00:59:20]** The first is let's install the prerequests.

**[00:59:21 – 00:59:24]** Just run those two blocks.

**[00:59:37 – 00:59:41]** Okay, that's fine.

**[00:59:41 – 00:59:44]** And this block shows configuration we used.

**[00:59:44 – 00:59:47]** Let me first briefly introduce.

**[00:59:47 – 00:59:53]** The model path is Hugging Face.

**[00:59:53 – 00:59:58]** This is a Hugging Face model path for the Queen 0.6 billion model,

**[00:59:58 – 01:00:04]** and it will automatically download the model from Hugging Face.

**[01:00:05 – 01:00:10]** And we set the SGLAN host and SGLAN port and URL for our

**[01:00:10 – 01:00:17]** inference server, and also the host port URL for our training server.

**[01:00:17 – 01:00:21]** And then those parts are our RL hyperparameters.

**[01:00:21 – 01:00:25]** First is the batch size and its own samples per prompt.

**[01:00:25 – 01:00:29]** So let me briefly explain those two parameters.

**[01:00:29 – 01:00:34]** Fetch size means you will select 16 samples in this

**[01:00:34 – 01:00:38]** training integration, and you will generate 8 responses

**[01:00:38 – 01:00:40]** for each of the 16 samples.

**[01:00:40 – 01:00:44]** So this is in your GRPO workload, which means you will generate

**[01:00:44 – 01:00:52]** multiple responses to one input prompt and use the relative

**[01:00:52 – 01:00:54]** scores to optimize the model.

**[01:00:54 – 01:00:59]** So we will totally generate 16 times 8.

**[01:00:59 – 01:01:04]** Which is 128 outputs in one iteration.

**[01:01:04 – 01:01:07]** And raw temperature means what temperature you want

**[01:01:07 – 01:01:10]** to use in your learning task.

**[01:01:10 – 01:01:15]** Basically, you can choose 1.0 or 0.8, something like that.

**[01:01:16 – 01:01:18]** And the max new tokens means the max response length you

**[01:01:18 – 01:01:23]** want to generate for each response.

**[01:01:23 – 01:01:27]** Because we are using a very simple task, which is GSM8K,

**[01:01:27 – 01:01:29]** let's just do 1K length here.

**[01:01:29 – 01:01:34]** In actual training, it can be a very large number.

**[01:01:35 – 01:01:37]** And LR, this is the learning rate.

**[01:01:37 – 01:01:41]** So actually, in mouse, we usually use a smaller learning

**[01:01:41 – 01:01:45]** rate, like one in minus six.

**[01:01:45 – 01:01:49]** But here, because we want to see the performance improvement

**[01:01:49 – 01:01:53]** in a short time, let's use a higher learning rate here.

**[01:01:53 – 01:01:58]** And EPSG is the PPO clipping epsilon.

**[01:01:58 – 01:02:02]** This means we want to control our policy to not be too far

**[01:02:02 – 01:02:05]** from our original policy in PPO.

**[01:02:05 – 01:02:10]** And this is like a control parameter.

**[01:02:10 – 01:02:14]** The number of iterations means they totally go around 48

**[01:02:14 – 01:02:16]** training iterations.

**[01:02:16 – 01:02:18]** And the macro batch size, this parameter

**[01:02:18 – 01:02:25]** only affects the efficiency.

**[01:02:25 – 01:02:30]** This means how much samples you want to put in one run

**[01:02:31 – 01:02:32]** of the training engine.

**[01:02:32 – 01:02:36]** So if you use a large number, it can process more prompt

**[01:02:36 – 01:02:40]** at one time, so it will be faster, but it will also increase

**[01:02:40 – 01:02:45]** the activation memory and you may easily get it out of memory.

**[01:02:45 – 01:02:49]** So we have already picked a proper value here for you.

**[01:02:49 – 01:02:52]** Use the four to get some performance speed up, but

**[01:02:52 – 01:02:56]** just do not let it out of memory.

**[01:02:56 – 01:02:57]** And we use tbsize equals two.

**[01:02:57 – 01:03:01]** This is just a random pick for the style.

**[01:03:02 – 01:03:07]** Okay, it should already download the models, and we can go ahead.

**[01:03:08 – 01:03:14]** Now let's launch the SGLang server with this simple command.

**[01:03:14 – 01:03:21]** We can run those blocks one by one and wait it to be finished.

**[01:03:22 – 01:03:25]** It might take like 30 seconds.

**[01:03:49 – 01:03:53]** So after it's done, it will print a line called SGLang server is ready.

**[01:03:53 – 01:03:57]** Let's just wait for a little bit more seconds.

**[01:04:12 – 01:04:15]** Okay, now we can see the SGLang server is ready.

**[01:04:15 – 01:04:20]** And let's do a quick sanity check for our SGLang server.

**[01:04:20 – 01:04:25]** So we send a simple question, what is 2 plus 2, to our SGLang server

**[01:04:25 – 01:04:29]** and see if it gives a response.

**[01:04:31 – 01:04:34]** Okay, now it works.

**[01:04:41 – 01:04:46]** So now we can go into the second part, which is the training server.

**[01:04:47 – 01:04:49]** The training server in our training lab is running through

**[01:04:50 – 01:04:53]** a separate process via torch run.

**[01:04:53 – 01:04:57]** This is because it will require the torch distributed to call the

**[01:04:57 – 01:05:02]** init process group, and it cannot run inside the Jupyter notebook, so

**[01:05:02 – 01:05:04]** we have to use the external server.

**[01:05:04 – 01:05:09]** By the way, you can find the SDP server here.

**[01:05:09 – 01:05:12]** in this FSDP training server file.

**[01:05:12 – 01:05:19]** So I've copied the core logic from miles in this file.

**[01:05:19 – 01:05:22]** If you are interested, you are recommended to have

**[01:05:22 – 01:05:23]** a quick look here.

**[01:05:23 – 01:05:28]** Here's also some algorithmic calculation, like the loss

**[01:05:28 – 01:05:32]** in this training server file.

**[01:05:32 – 01:05:36]** Okay, let's just go back to our notebook.

**[01:05:39 – 01:05:43]** Here we use several HTTP endpoint.

**[01:05:43 – 01:05:46]** First it's the health, it's the same as SGLang, just do

**[01:05:46 – 01:05:48]** some health check.

**[01:05:48 – 01:05:51]** And the train step, when we call train step, we will give

**[01:05:51 – 01:05:57]** the full training data to our FSDP server and it will

**[01:05:57 – 01:05:59]** do all the calculations.

**[01:05:59 – 01:06:02]** And the update rate, oh sorry.

**[01:06:04 – 01:06:08]** UpdateWidth, this will sync the trainer's width to our

**[01:06:08 – 01:06:13]** SGLang inference engine to update SGLang's engine's width.

**[01:06:13 – 01:06:17]** And wakeup, wakeup will load the model and the optimizer

**[01:06:17 – 01:06:20]** state from the CPU.

**[01:06:20 – 01:06:22]** To upload to the GPU.

**[01:06:22 – 01:06:27]** As we said, we have to offload the training engine

**[01:06:27 – 01:06:28]** we were doing rollout.

**[01:06:28 – 01:06:31]** So after the rollout, we have to call this wakeup

**[01:06:31 – 01:06:32]** to unload it again.

**[01:06:32 – 01:06:34]** And similarly, the sleep.

**[01:06:34 – 01:06:38]** When we are done with the training, we need to call the

**[01:06:38 – 01:06:43]** sleep to offload the model to CPU.

**[01:06:46 – 01:06:50]** So now let's run this simple train command and also wait for

**[01:06:50 – 01:06:53]** the training server to be ready.

**[01:06:54 – 01:06:59]** This might take another 30 seconds or faster.

**[01:06:59 – 01:07:03]** Oh, this is very fast, okay.

**[01:07:11 – 01:07:15]** Okay, so now we can come to the data preparation stage.

**[01:07:15 – 01:07:20]** We use this GSM8K dataset, which is grade school math 8K.

**[01:07:20 – 01:07:25]** So from the name you can know, it's a simple math dataset

**[01:07:25 – 01:07:29]** just consists of the grade school level problems.

**[01:07:29 – 01:07:37]** So each problem will have a question and a numeric number.

**[01:07:39 – 01:07:43]** Let's use the data loader to load data set here.

**[01:07:54 – 01:08:00]** Okay, so we can go into our training loop.

**[01:08:00 – 01:08:03]** Before the full training, we will first show the training

**[01:08:03 – 01:08:06]** loop step by step to help you understand the entire process.

**[01:08:06 – 01:08:10]** And after those steps, we will launch 40 iterations

**[01:08:10 – 01:08:13]** of a full training to see how the model improves.

**[01:08:13 – 01:08:18]** So let's first look at the real-world step.

**[01:08:23 – 01:08:28]** This mirrors from the generated endpoint in

**[01:08:28 – 01:08:30]** Miles' SGLang reward file.

**[01:08:30 – 01:08:35]** So it will first send the tokenized prompt to SGLang's generated

**[01:08:35 – 01:08:40]** endpoint, and it will collect the response text, token IDs, and

**[01:08:40 – 01:08:43]** log probabilities for RL training.

**[01:08:43 – 01:08:48]** And it will then pack all the data for later reward

**[01:08:48 – 01:08:51]** computation and training.

**[01:08:53 – 01:08:55]** Let's run this.

**[01:08:56 – 01:09:01]** So you may wonder why we need to record the log probability here.

**[01:09:01 – 01:09:06]** Because in PPO, which is the most common RL algorithm,

**[01:09:06 – 01:09:13]** we need to use an old policy to optimize the model.

**[01:09:13 – 01:09:17]** The old policy is just from the reality engine because

**[01:09:17 – 01:09:21]** it is using the old weight.

**[01:09:21 – 01:09:25]** The log probabilities from the robot engine just serve

**[01:09:25 – 01:09:28]** as this old policy.

**[01:09:34 – 01:09:37]** As wrong as I was...

**[01:09:41 – 01:09:47]** Okay, you can see 128 total prompts per step, and it will

**[01:09:47 – 01:09:52]** take just like 10 seconds.

**[01:09:54 – 01:09:58]** Okay, it now generated 128 responses.

**[01:09:58 – 01:10:02]** This is the prompt list, the response list, and one example

**[01:10:02 – 01:10:08]** prompt, and also some example log probabilities.

**[01:10:09 – 01:10:12]** The log probabilities is calculated per token

**[01:10:12 – 01:10:17]** because we are calculating our loss for each token.

**[01:10:19 – 01:10:21]** Now let's calculate the rewards.

**[01:10:21 – 01:10:25]** So for the GSM8K, we use a simple reward method that

**[01:10:25 – 01:10:31]** we just check if the model's answer matches the ground truth.

**[01:10:32 – 01:10:36]** So in our system prompt we give to the model, we will

**[01:10:36 – 01:10:42]** allow the model to wrap his answer with this boxed notation.

**[01:10:42 – 01:10:47]** So we just extract the value from this notation, and if it matches

**[01:10:47 – 01:10:49]** the ground truth, we give one.

**[01:10:49 – 01:10:53]** If it does not match, we give zero.

**[01:10:55 – 01:11:02]** Okay, you can see a mean reward of 0.6 in 9.5.

**[01:11:02 – 01:11:06]** This value can actually be kind of random, so it might

**[01:11:06 – 01:11:12]** be like from 0.6 to 0.7.

**[01:11:12 – 01:11:17]** Any value can be possible because it's a random sampling.

**[01:11:19 – 01:11:23]** Now we can come to this filter zero-standard groups.

**[01:11:23 – 01:11:30]** This is actually an optional but recommended group, sorry,

**[01:11:30 – 01:11:33]** optional but recommended step in your IELTS training.

**[01:11:33 – 01:11:37]** You can just filter out all of the reward groups with

**[01:11:37 – 01:11:39]** zero-standard across the groups.

**[01:11:39 – 01:11:40]** So what does this mean?

**[01:11:40 – 01:11:43]** Assume that you have a question.

**[01:11:43 – 01:11:47]** If this is too easy, then all of the responses can have

**[01:11:47 – 01:11:49]** the correct answer.

**[01:11:49 – 01:11:52]** If it is too hard, all of the responses cannot have

**[01:11:52 – 01:11:54]** the correct answer.

**[01:11:54 – 01:11:57]** In both cases, you don't know which response is good, which response

**[01:11:57 – 01:12:02]** is bad, so it cannot contribute to the model training in any way.

**[01:12:02 – 01:12:06]** So in both cases, you just want to throw all of the responses

**[01:12:06 – 01:12:09]** to save your compute.

**[01:12:09 – 01:12:13]** Let's also run this filter.

**[01:12:17 – 01:12:21]** Okay, now we filter the seven groups out and we only kept

**[01:12:21 – 01:12:25]** nine of them in our training.

**[01:12:41 – 01:12:45]** Okay, so now it comes to our training phase.

**[01:12:45 – 01:12:48]** Let me first quickly introduce our algorithm to use,

**[01:12:48 – 01:12:49]** which is the GRPO.

**[01:12:49 – 01:12:54]** GRPO is Group-Related Policy Optimization, which was introduced

**[01:12:54 – 01:12:57]** by DeepSeek in their R1 model.

**[01:12:57 – 01:13:05]** The basic idea is how to estimate one response is good or not.

**[01:13:05 – 01:13:08]** You just need to compare this response with all of the other

**[01:13:08 – 01:13:12]** responses for the same prompt.

**[01:13:12 – 01:13:17]** So use this way to calculate the advantage.

**[01:13:18 – 01:13:26]** RI here, this RI is the reward for the current response.

**[01:13:26 – 01:13:30]** And new here, this is the average of this group.

**[01:13:31 – 01:13:35]** And here is the standard of this group, which means you

**[01:13:35 – 01:13:40]** use the reward of the current response to minus the average

**[01:13:40 – 01:13:43]** reward within this group and divide by the standard of

**[01:13:43 – 01:13:46]** this group to do a normalization.

**[01:13:46 – 01:13:52]** So you can calculate a relative score of the current response.

**[01:13:53 – 01:13:58]** For all responses with an above-average reward, it will

**[01:13:58 – 01:14:00]** give it a positive advantage.

**[01:14:00 – 01:14:04]** And for all responses with a below-average reward, it

**[01:14:04 – 01:14:06]** will give it a negative advantage.

**[01:14:06 – 01:14:10]** So for a positive advantage, the model will try to increase

**[01:14:10 – 01:14:15]** this response's probability.

**[01:14:15 – 01:14:17]** And for all the negative advantages, the model will

**[01:14:18 – 01:14:22]** try to describe their probability.

**[01:14:28 – 01:14:36]** So this loss calculation is actually implemented in our

**[01:14:36 – 01:14:39]** FSDP training server.

**[01:14:41 – 01:14:46]** So you can see the calculated GRPO advantages here.

**[01:14:46 – 01:14:49]** This is just the algorithm we just mentioned on the slides.

**[01:14:49 – 01:14:53]** Oh, sorry, on the logbook.

**[01:15:04 – 01:15:07]** And after we calculate the advantages, we will use them

**[01:15:07 – 01:15:09]** to calculate the policy loss.

**[01:15:09 – 01:15:13]** The policy loss comes from the PPO style.

**[01:15:13 – 01:15:20]** So we will use the new policy, which is the newest model's weight

**[01:15:20 – 01:15:24]** calculated log props to divide by the old policy, which is the

**[01:15:24 – 01:15:28]** raw-out engine's output log props.

**[01:15:29 – 01:15:33]** To get a ratio, and we will use this ratio to count the

**[01:15:33 – 01:15:39]** advantages we just calculated from the GRPO estimator.

**[01:15:39 – 01:15:43]** And we will do a PPO clip, which means we set an epsilon

**[01:15:43 – 01:15:49]** here, and if the ratio is larger than the upper bound

**[01:15:49 – 01:15:53]** or lower than the lower bound, we just clip it to the bounds.

**[01:15:53 – 01:16:00]** This is preventing the policy to go too far from our original policy.

**[01:16:00 – 01:16:04]** For this formula, you can see the implementation in

**[01:16:04 – 01:16:10]** the compute policy loss function in this FF-CDP server file.

**[01:16:12 – 01:16:15]** And let's run those blocks.

**[01:16:15 – 01:16:20]** First, we'll pack the raw data, just simply return it

**[01:16:20 – 01:16:27]** as a dictionary, and we send our packed data to our training server.

**[01:16:27 – 01:16:34]** It will calculate the rewards, the policy loss, and do the gradient

**[01:16:34 – 01:16:38]** compute and optimizer update here.

**[01:16:39 – 01:16:42]** We need to wait a little bit.

**[01:16:52 – 01:16:56]** Okay, so you can see your training staff is done.

**[01:16:57 – 01:17:00]** And now we come to the last step, which is the weight

**[01:17:00 – 01:17:03]** update and memory management.

**[01:17:05 – 01:17:09]** So SGLang has a native HTTP endpoint called

**[01:17:09 – 01:17:13]** updateWait from tensor.

**[01:17:13 – 01:17:17]** So what we need to do in our training server is just to

**[01:17:17 – 01:17:22]** gather the full state dictionary to get a full-width copy, and we group

**[01:17:22 – 01:17:27]** the parameters into size-bounded buckets, and we serialize it

**[01:17:27 – 01:17:30]** using the flattened tensor bucket,

**[01:17:30 – 01:17:36]** and we send the serialized tensors to SGLang via this HTTP endpoint.

**[01:17:36 – 01:17:39]** Oh, sorry.

**[01:17:39 – 01:17:41]** Update width from tensor.

**[01:17:41 – 01:17:44]** This transfer will be very fast because it will be

**[01:17:44 – 01:17:47]** done on the same GPU.

**[01:17:49 – 01:17:54]** We'll also do the unload and offload process here.

**[01:17:54 – 01:17:59]** When we transit from row out to train, we let SGLang to release

**[01:17:59 – 01:18:07]** the GPU memory and we let the training engine to load from CPU.

**[01:18:07 – 01:18:10]** And when we transit from train to row out, we let FSDP to

**[01:18:10 – 01:18:16]** offload to CPU and let SGLang to load to the GPU.

**[01:18:18 – 01:18:21]** So we run this HTTP.

**[01:18:23 – 01:18:25]** Wait update process.

**[01:18:25 – 01:18:28]** And you can see the SGLang version goes to one.

**[01:18:28 – 01:18:33]** This means before update, the version is default, and

**[01:18:33 – 01:18:34]** after update, it comes to one.

**[01:18:34 – 01:18:38]** This means your update is success.

**[01:18:40 – 01:18:45]** Okay, so till now, we've already covered all the necessary

**[01:18:45 – 01:18:47]** parts in our training system.

**[01:18:47 – 01:18:51]** And here, we can see a full training loop.

**[01:18:51 – 01:18:54]** You can train your own model, start from here.

**[01:18:54 – 01:18:58]** Let's first do a baseline evaluation here.

**[01:18:58 – 01:19:04]** So we are using the original model to evaluate this GSM8K dataset.

**[01:19:04 – 01:19:09]** We will get an evaluation score on the test set.

**[01:19:09 – 01:19:12]** After the training, we can compare our new score with this baseline

**[01:19:12 – 01:19:15]** score to see how it improves.

**[01:19:15 – 01:19:18]** This process might take like one to two minutes, so we

**[01:19:18 – 01:19:21]** need to wait a bit.

**[01:19:34 – 01:19:39]** So after this baseline evaluation, we'll just run this block.

**[01:19:39 – 01:19:44]** This block, you can see this iteration, which is a train loop.

**[01:19:44 – 01:19:50]** After running the full train loop, we'll plot the training curves

**[01:19:50 – 01:19:52]** and see the final evaluation.

**[01:19:52 – 01:19:57]** And during the training run, we'll do some more introduction to

**[01:19:57 – 01:20:03]** our framework mouse and some recent optimizations that you can learn.

**[01:20:04 – 01:20:08]** So let's still wait for this baseline evaluation.

**[01:20:39 – 01:20:42]** And by the way, if you have any questions or anything

**[01:20:42 – 01:20:46]** you need to help, you can find our teaching assistant

**[01:20:46 – 01:20:51]** at the back of this classroom.

**[01:20:51 – 01:20:56]** Okay, so we are done with our baseline evaluation.

**[01:20:58 – 01:21:02]** And let's run this training loop block.

**[01:21:06 – 01:21:09]** So it will automatically do 40 iterations.

**[01:21:09 – 01:21:14]** Each iteration has one rollout stage.

**[01:21:15 – 01:21:18]** And then do a training stage and wait update.

**[01:21:18 – 01:21:24]** Okay, you can see the printed result and it enters the

**[01:21:24 – 01:21:27]** second iteration.

**[01:21:38 – 01:21:41]** And during this time, I can do a quick introduction

**[01:21:41 – 01:21:45]** to our enterprise-facing reinforcement learning

**[01:21:45 – 01:21:48]** framework for the large-scale post-training and production

**[01:21:48 – 01:21:51]** workload, which is MILES.

**[01:21:51 – 01:21:55]** Mouth is natively built with SGLang as the route engine

**[01:21:56 – 01:21:59]** and Megatron as the default training engine, with many

**[01:21:59 – 01:22:01]** advanced features supported.

**[01:22:01 – 01:22:05]** And Mouth is also co-evolving with LIME, which is used in

**[01:22:05 – 01:22:11]** real production-level LLM training, like the GLM file.

**[01:22:15 – 01:22:19]** So we can introduce some new features we developed recently.

**[01:22:19 – 01:22:23]** For example, the spec RL.

**[01:22:23 – 01:22:28]** In the inference training lab we just done with, you

**[01:22:28 – 01:22:31]** do some benchmarking with speculative decoding, and you

**[01:22:31 – 01:22:37]** can actually also use speculative decoding in your training task.

**[01:22:37 – 01:22:43]** And you can trim the MTP layer during the RL to let it follow

**[01:22:43 – 01:22:45]** the target output.

**[01:22:45 – 01:22:53]** If you don't do this, imagine that when you train a model, the policy

**[01:22:53 – 01:22:57]** will always change, but if your MTP layer always remains at the

**[01:22:57 – 01:23:02]** beginning stage, it will just get a lower and lower acceptance risk.

**[01:23:02 – 01:23:06]** But if you train it together with the target model, you can

**[01:23:06 – 01:23:11]** try to keep a high acceptance risk.

**[01:23:11 – 01:23:16]** Just like this figure, you can see with the training

**[01:23:16 – 01:23:19]** acceptance rate is always increasing, but without the

**[01:23:19 – 01:23:23]** training, it's like a low level.

**[01:23:27 – 01:23:29]** And the Training Inference Mismatch,

**[01:23:29 – 01:23:32]** this is actually a large topic.

**[01:23:32 – 01:23:36]** We did not cover this in our training lab because we just

**[01:23:36 – 01:23:40]** used a simple S3LAR engine and a FSDP engine.

**[01:23:41 – 01:23:44]** Also, the model is too small to have a large mismatch.

**[01:23:44 – 01:23:49]** But if you are trying to do reinforcement learning at a large

**[01:23:49 – 01:23:54]** scale, this can be a large problem.

**[01:23:54 – 01:23:58]** So the issue is, even for the exactly same weights,

**[01:23:58 – 01:24:03]** the inference log props is not equal to the training log props.

**[01:24:03 – 01:24:07]** The reason is even though you use exactly the same input

**[01:24:07 – 01:24:10]** and exactly the same model weights, the inference engine and training

**[01:24:10 – 01:24:13]** engine have different kernels.

**[01:24:13 – 01:24:18]** So the floating point difference can be enlarged during the

**[01:24:18 – 01:24:23]** inference and training process and cause different final

**[01:24:23 – 01:24:26]** log props on each token.

**[01:24:26 – 01:24:28]** So what will it cause?

**[01:24:28 – 01:24:32]** It means your behavior policy is actually different from

**[01:24:32 – 01:24:34]** the target policy.

**[01:24:34 – 01:24:37]** This introduces some off-policy bias.

**[01:24:37 – 01:24:41]** During the reinforcement learning, people are more preferred

**[01:24:41 – 01:24:44]** to use on-policy training because off-policy can cause

**[01:24:44 – 01:24:47]** the train collapse.

**[01:24:47 – 01:24:50]** This often happens in large-scale MoE models.

**[01:24:50 – 01:24:54]** For example, maybe hundreds of billion MoE models.

**[01:24:54 – 01:24:58]** We usually observe this kind of train collapse with

**[01:24:58 – 01:25:01]** a large train mismatch.

**[01:25:03 – 01:25:05]** You can see the reasons.

**[01:25:05 – 01:25:08]** We have non-deterministic kernels.

**[01:25:08 – 01:25:13]** It can output different results even in different runs.

**[01:25:13 – 01:25:17]** And in MoE models, we have the router.

**[01:25:17 – 01:25:21]** For the router, a little bit of different input can cause a large

**[01:25:21 – 01:25:28]** difference in the output because it can select different experts.

**[01:25:31 – 01:25:36]** So how do people solve this problem in industry?

**[01:25:36 – 01:25:38]** First is an algorithm solution.

**[01:25:38 – 01:25:44]** We can mitigate the mismatch using PIS or MIS, which means

**[01:25:44 – 01:25:49]** we just be aware of this training inference mismatch, and we

**[01:25:49 – 01:25:54]** use some algorithmic important sampling to try to eliminate

**[01:25:54 – 01:25:57]** the influence of this mismatch.

**[01:25:57 – 01:26:02]** Just take it as a standard of policy.

**[01:26:03 – 01:26:06]** For TIS, it means Truncated Importance Sampling.

**[01:26:06 – 01:26:09]** MRS means Masked Importance Sampling.

**[01:26:09 – 01:26:12]** There are a lot of different

**[01:26:12 – 01:26:16]** algorithms trying to solve this problem.

**[01:26:16 – 01:26:20]** And also, we can try to make the inference and

**[01:26:20 – 01:26:22]** training just aligned.

**[01:26:22 – 01:26:26]** This means we replace all kernels in FSDP or Megatron

**[01:26:26 – 01:26:32]** to ensure it is fully aligned with SGLang's implementation.

**[01:26:32 – 01:26:35]** Both of them should use deterministic kernels.

**[01:26:35 – 01:26:38]** With this, you can ensure that your training engine

**[01:26:38 – 01:26:42]** and inference engine will output exactly the same log props

**[01:26:42 – 01:26:46]** and solve this issue from the root.

**[01:26:49 – 01:26:51]** We also introduced this feature in mouse.

**[01:26:51 – 01:26:55]** You can see with this optimization, the log property

**[01:26:55 – 01:26:59]** can reduce to exactly zero.

**[01:27:00 – 01:27:03]** We will also even develop this feature with large-scale

**[01:27:03 – 01:27:08]** MoE training with EP and Megatron.

**[01:27:09 – 01:27:12]** Another idea is the rollout routing replay.

**[01:27:12 – 01:27:18]** So, as I mentioned, the MoE router will largely increase

**[01:27:18 – 01:27:23]** the mismatch because a slightly different input can cause

**[01:27:23 – 01:27:25]** a large difference in output.

**[01:27:25 – 01:27:30]** So based on this, we can just record the MoE routing result

**[01:27:30 – 01:27:35]** from our rollout and replay the exactly same routing result

**[01:27:35 – 01:27:37]** in the training stage.

**[01:27:37 – 01:27:42]** In this case, even though it will still have some mismatch, but will

**[01:27:42 – 01:27:50]** not be that large because it will not be increased by the MoE router.

**[01:27:56 – 01:28:00]** Another topic is the low-precision training.

**[01:28:01 – 01:28:08]** So actually in industry, there are a lot of different low-precision

**[01:28:08 – 01:28:09]** training recipes.

**[01:28:09 – 01:28:14]** For example, FP8 training and INT4 QAT training.

**[01:28:14 – 01:28:20]** Because the recent models are very large, like maybe hundreds

**[01:28:20 – 01:28:22]** of billions or even one trillion.

**[01:28:22 – 01:28:30]** So we have to use a lower precision to reduce our resource requirement.

**[01:28:30 – 01:28:35]** And we develop both FPA training and InterFOR QAT training in mouse.

**[01:28:35 – 01:28:38]** It can be easily used by just adding some arguments

**[01:28:38 – 01:28:40]** to the training script.

**[01:28:40 – 01:28:44]** And we also do some of the

**[01:28:44 – 01:28:49]** experiments, like the algorithm dereg with those localization

**[01:28:50 – 01:28:53]** training, and we have several blogs on the LMSYS org.

**[01:28:53 – 01:28:58]** If you are interested, you can just read those blogs.

**[01:29:01 – 01:29:06]** Also, we will provide some more comprehensive examples in mouse.

**[01:29:06 – 01:29:10]** For example, in our training lab, we just show a reasoning

**[01:29:10 – 01:29:14]** example using GSM8K.

**[01:29:14 – 01:29:20]** But in mouse, we can actually do it with a visual language model.

**[01:29:23 – 01:29:28]** And you can definitely use mouse to do agent training.

**[01:29:28 – 01:29:33]** We provide a lot of the agent examples in our repo.

**[01:29:33 – 01:29:37]** And this is one example called multi-agent IRO.

**[01:29:37 – 01:29:42]** You can even use it to train with a multi-agent system.

**[01:29:42 – 01:29:48]** Because of mouse design, it's easy to insert any kind of

**[01:29:48 – 01:29:53]** environment to this IRO system, including another agent.

**[01:29:58 – 01:30:02]** Okay, here's also some future roadmap to share, but just

**[01:30:02 – 01:30:07]** briefly speaking, we will support more new models, more

**[01:30:07 – 01:30:13]** new hardwares, and maybe even the diffusion and omni models.

**[01:30:16 – 01:30:20]** Okay, after this brief introduction, let's try to go back

**[01:30:20 – 01:30:23]** to our lab and see if it's done.

**[01:30:26 – 01:30:33]** Okay, still has some iterations remaining, but we can just wait.

**[01:30:47 – 01:30:51]** Let me first quickly introduce this training dynamic.

**[01:30:51 – 01:30:57]** So after our training loop down, we will get the three figures.

**[01:30:57 – 01:31:00]** First is the training reward, the second is the policy loss,

**[01:31:00 – 01:31:03]** and the third is the gradient norm.

**[01:31:03 – 01:31:06]** So for training reward, it is actually, you can

**[01:31:06 – 01:31:13]** say it's unstable, but generally it is increasing.

**[01:31:14 – 01:31:16]** The reason is in reinforcement learning, you are always

**[01:31:16 – 01:31:18]** doing random sampling.

**[01:31:18 – 01:31:23]** So for different samples, your reward scores sometimes

**[01:31:23 – 01:31:25]** can be high, sometimes can be low.

**[01:31:25 – 01:31:30]** But it will get some capability improvement, cause the general

**[01:31:30 – 01:31:33]** increasing change.

**[01:31:35 – 01:31:39]** And you can also say this grand norm, a healthy grand

**[01:31:39 – 01:31:42]** norm should not be too large.

**[01:31:42 – 01:31:47]** So it may have some spike in this figure, but it's roughly

**[01:31:47 – 01:31:50]** like equals to one.

**[01:31:50 – 01:31:53]** Actually, this is because we're using a large learning

**[01:31:53 – 01:31:59]** rate, so the grand norm is larger than a normal IELTS trainee.

**[01:31:59 – 01:32:02]** If you observe a very large grand norm, it could be kind

**[01:32:02 – 01:32:09]** of dangerous in a real IELTS trainee, because it sometimes means

**[01:32:09 – 01:32:13]** a signal of the trainee collapse.

**[01:32:20 – 01:32:23]** Also, sometimes in the real training, you may observe

**[01:32:23 – 01:32:26]** your training reward decrease.

**[01:32:26 – 01:32:30]** That is mostly training collapse.

**[01:32:31 – 01:32:34]** Okay, we can see the training is complete.

**[01:32:34 – 01:32:37]** So we can run this final UI.

**[01:32:38 – 01:32:42]** Okay, we see our actual training score, policy loss,

**[01:32:42 – 01:32:45]** and the gradient norm.

**[01:32:48 – 01:32:51]** And run the final evaluation.

**[01:32:57 – 01:33:01]** Now we can wait for just maybe one or two minutes to see how our

**[01:33:01 – 01:33:04]** model performs after the training.

**[01:34:26 – 01:34:32]** Okay, you can see before training, our accuracy result is 64%,

**[01:34:32 – 01:34:37]** and after the training, it comes to the 70%.

**[01:34:37 – 01:34:43]** So in this training, we got a lack of 6% improvement on

**[01:34:43 – 01:34:47]** this GSM8K evaluation.

**[01:34:53 – 01:34:58]** Okay, now we can run this clean-up block to terminate

**[01:34:58 – 01:35:01]** all of our servers.

**[01:35:02 – 01:35:07]** Okay, that's all for today's Ceylon RL training lab.

**[01:35:07 – 01:35:10]** And if you are interested in reinforcement learning, you're

**[01:35:10 – 01:35:15]** welcome to try with Miles, which is our enterprise-grade reinforcement

**[01:35:15 – 01:35:19]** learning framework, and also maybe you are very welcome to

**[01:35:19 – 01:35:21]** contribute or collaborate with us.

**[01:35:21 – 01:35:21]** Thank you.

