TorchSpec

lightseekorgMIT活跃维护

基于 PyTorch 的投机解码模型训练库,支持 EAGLE3、FSDP 等方案,帮你在 vLLM 等推理服务里加速大模型生成。

A PyTorch native library for training speculative decoding models

暂无版本267star70fork同步于 2026-11-10 12:03 UTC

怎么上手

由 README 提炼,执行前请确认
rustup toolchain install 1.98.0 --profile minimal
./tools/build_conda.sh 1 vllm && micromamba activate torchspec
硬件要求按 README 说明

README 未给出显存要求;示例按单节点 4 卡(2 卡训练、2 卡推理)设计,另有 H200、H100 多节点示例;离线回放模式推荐单 GPU 使用。

支持 Apple Silicon需要 CUDA多卡 / 张量并行
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

PyTorch 原生投机解码草稿模型训练框架,把推理与训练解耦,让两侧各自独立扩容

基于 PyTorch 的投机解码模型训练库,支持 EAGLE3、FSDP 等方案,帮你在 vLLM 等推理服务里加速大模型生成。

核心能力

  • 通过 Mooncake 存储把推理引擎的隐藏状态直接流式传给训练 worker
  • 支持 vLLM、TensorRT-LLM、SGLang、HuggingFace 等多种推理后端
  • 提供 EAGLE3、DFlash、DSpark 等草稿模型训练示例与配置
  • 支持离线回放训练,单 GPU 即可做测试与开发
  • 提供 FSDP 到 HuggingFace 的 checkpoint 转换与词表裁剪工具

容易踩的坑

源码安装需要先装 Rust 1.98.0 工具链来编译内置 Rust tokenizer,预编译 wheel 和 Docker 镜像不需要;TokenSpeed 后端目前要求 Python 3.12 环境;TensorRT-LLM 后端需在其官方镜像内运行,且暂只支持单节点张量并行。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    TorchSpec

    TorchSpec is a torch-native speculative decoding training framework. We introduce a disaggregated way of training speculative decoding draft models where inference and training are fully decoupled and stream hidden states directly from inference engine groups to distributed training workers via Mooncake store, allowing each side to scale independently.

    Adoption

    TorchSpec has been adopted by production inference platforms and the vLLM ecosystem:

    • DigitalOcean used TorchSpec to train an EAGLE3 draft model for MiniMax-M2.5 on DigitalOcean Serverless Inference.
    • vLLM used TorchSpec and vLLM to train the custom EAGLE3 draft model featured in its Artificial Analysis leaderboard work.
    • CoreWeave used TorchSpec to train a DFlash speculative decoding model for Kimi K2.7 Code and contributed D-PACE support upstream.
    • fal used TorchSpec to train a DSpark speculative decoding model for its Ideogram V4 prompt expander, reporting 16x throughput gains with DSpark.
    • Tencent Hunyuan open-sourced AngelSpec, adopting TorchSpec for training and featuring it in the paper.

    🤗 Released Models

    Draft models trained with TorchSpec, available on the LightSeek Foundation Hugging Face organization:

    Draft models trained with TorchSpec, available from other organizations:

    🚀 Blogs and Announcements

    Table of Contents

    Architecture Overview

    TorchSpec is built around a disaggregated training pipeline:

    • Inference engines generate target-model hidden states with inference engines.
    • Mooncake store transfers tensors between inference and training without materializing them on disk.
    • Training workers consume streamed hidden states to train speculative decoding draft models.

    This separation keeps the training side focused on optimization while letting the inference side scale for hidden-state generation throughput.

    Inference Backend Support

    TorchSpec streams hidden states from inference engines into training workers.

    BackendSupport TierStatus
    vLLMFirst-classAvailable
    TokenSpeedFirst-classIn progress
    TensorRT-LLMFirst-classAvailable
    SGLangBest community effortAvailable
    HuggingFace TransformersBest community effortAvailable

    Quick Start

    Train an Eagle3 draft model for Qwen3-8B on a single node with 4 GPUs (2 for training and 2 for inference):

    ./examples/qwen3-8b-single-node/run.sh
    

    Override config values directly from the CLI:

    ./examples/qwen3-8b-single-node/run.sh training.learning_rate=5e-5 training.num_train_steps=500
    

    Setup

    Quick Setup

    Source installs build the bundled Rust tokenizer. Install Rust/rustup and select the pinned toolchain first (prebuilt wheels and Docker runtime images do not need a compiler):

    rustup toolchain install 1.98.0 --profile minimal
    
    # Install with vLLM
    ./tools/build_conda.sh 1 vllm
    micromamba activate torchspec
    
    # Or install with SGLang
    ./tools/build_conda.sh
    micromamba activate torchspec
    
    # Or install TokenSpeed from an editable source checkout
    ./tools/build_conda.sh 1 tokenspeed
    micromamba activate torchspec
    

    To install into your current environment instead:

    ./tools/build_conda.sh current tokenspeed  # or 'sglang', 'vllm', or 'both'
    

    The TokenSpeed backend currently requires a Python 3.12 environment because its native kernel dependency wheels do not support Python 3.14.

    Optional: install Flash Attention support:

    pip install -e ".[fa]"
    

    Training tokenizer

    Supported text profiles (llama3, qwen, qwen3-thinking) and Kimi-K3 use SMG's Rust tokenizer in-process by default. Its native extension ships in the TorchSpec wheel, and pip install -e . builds it automatically. Source builds need Rust 1.98.0; prebuilt wheels and our Docker runtime images do not. See installation details.

    To explicitly use the old tokenizer, set dataset.rendering_backend: huggingface or pass dataset.rendering_backend=huggingface on the CLI. Non-K3 deferred/decode training, other templates, and custom renderers retain their existing auto routing. Both built-in K3 backends require complete, unformatted conversations; the HF fallback does not enable deferred/decode or preformatted/generation-prefix modes. Missing SMG bindings and rendering errors never silently change the selected backend. See SMG setup, supported paths, and correctness gates.

    Backend-Specific Usage

    vLLM

    ./examples/qwen3-8b-single-node/run.sh configs/vllm_qwen3_8b.yaml
    

    SGLang

    ./examples/qwen3-8b-single-node/run.sh
    

    TensorRT-LLM

    Run inside the TensorRT-LLM image (docker/trtllm/v1.3.0rc18/Dockerfile), which ships tensorrt_llm pre-patched for Mooncake hidden-state capture:

    ./examples/qwen3-8b-single-node/run.sh configs/trtllm_qwen3_8b.yaml
    

    Single-node tensor parallelism only for now (multi-node TP is not yet wired up).

    TorchSpec uses vLLM's Worker Extension mechanism to hook into the model forward pass and capture hidden states directly inside worker processes, which avoids RPC serialization overhead during extraction. For SGLang, TorchSpec applies a patch to the existing codebase to enable hidden-state extraction. For TensorRT-LLM, TorchSpec builds on its native SaveHiddenStates speculative mode and applies a small patch that redirects the captured aux + final hidden states to Mooncake instead of writing them to disk.

    Updating the SGLang patch

    Commit your changes in the _sglang checkout, then regenerate patches/sglang/<version>/sglang.patch from the pinned base commit with ./tools/update_sglang_patch.sh.

    Examples

    ExampleBackendModel
    hf-quickstartHuggingFaceQwen3-8B
    qwen3-8b-single-nodeInference engineQwen3-8B
    kimi-k25-2node-h200Inference engineKimi-K2.5
    kimi-k25-3node-h100Inference engineKimi-K2.5

    See examples/README.md for more details about each example.

    Training Modes

    Offline Replay Training

    Offline replay (Docs) is a training mode that separates target-output generation from draft-model training by reading hidden states from disk. Recommended for testing and development on 1 GPU.

    Resume vs. Continual Training

    Both modes use training.load_path, but they restore different states:

    Goaltraining.load_pathtraining.continual_trainingWhat gets restored
    Resume an interrupted runRequiredfalse (default)Model, optimizer, LR scheduler, RNG, and step metadata
    Start a new run from existing weightsRequiredtrueModel weights only

    Resume the same run:

    training:
      load_path: /path/to/old_run/checkpoints
    
    output_dir: /path/to/old_run
    

    Start a new run from existing weights:

    training:
      load_path: /path/to/old_run/checkpoints
      continual_training: true
      learning_rate: 1e-5
      warmup_ratio: 0.01
      num_epochs: 1
    
    output_dir: /path/to/new_run
    

    Start a new run from an existing Hugging Face checkpoint. --input accepts a Hugging Face Hub repo id (downloaded automatically), a local HF/safetensors directory or .safetensors file, or a TorchSpec DCP checkpoint dir:

    python tools/convert_to_torchspec.py \
        --input org/dflash-checkpoint \
        --config torchspec/config/dspark_draft_config_qwen36_35b.json \
        --output ./outputs/dspark_init
    

    Then warm-start from the generated init:

    training:
      load_path: ./outputs/dspark_init
      continual_training: true
    

    Using this technique, you can also warm-start DSpark training runs from a pre-trained DFlash model for faster convergence.

    Checkpoint Conversion

    Convert an FSDP checkpoint to HuggingFace format:

    python tools/convert_to_hf.py --input-dir ./outputs/my_experiment/iter_0010000/
    

    Vocabulary pruning, which reduces the draft model lm_head to a smaller token set and emits d2t and t2d mappings, can be applied either during training or at conversion time.

    DFlash2 requires the full target vocabulary. Do not set draft_vocab_size or pass --prune-vocab for a DFlash2 model.

    • Pre-pruning: set draft_vocab_size in your training config. The checkpoint already contains the pruned lm_head and d2t/t2d buffers, so the basic conversion command is enough.
    • Post-pruning: train with the full vocabulary, then pass --prune-vocab at conversion time together with a representative dataset to compute token frequencies.
    python tools/convert_to_hf.py \
        --input-dir ./outputs/my_experiment/iter_0010000/ \
        --prune-vocab \
        --dataset-path Aeala/ShareGPT_Vicuna_unfiltered \
        --draft-vocab-size 32000 \
        --tokenizer Qwen/Qwen3-8B \
        --chat-template qwen \
        --prompt-key conversations
    

    Pass --cache-dir ./cache to reuse the tokenized dataset cache from training.

    Metrics Reporting

    W&B logging is disabled by default with report_to: none. To enable it, set report_to: wandb in your config and provide your API key.

    Troubleshooting

    Set TORCHSPEC_LOG_LEVEL=DEBUG for more verbose logging when diagnosing issues:

    TORCHSPEC_LOG_LEVEL=DEBUG ./examples/qwen3-8b-single-node/run.sh
    

    Mooncake SEGFAULT

    Current Mooncake version has a bug with TCP-only hosts causing a SEGFAULT error. Set MC_STORE_MEMCPY=0 until the upstream issue is fixed.

    RDMA failure

    If you get the error ... libcudart symbols not found globally. Make sure PyTorch with CUDA is installed before using TileLang, reinstall mooncake to match your system CUDA version, i.e. if you run on CUDA 13, mooncake is probably installed for cu12 wheels, re-install it using uv pip uninstall mooncake-transfer-engine && uv pip install mooncake-transfer-engine-cuda13.

    Per-Rank File Logging

    Set TORCHSPEC_LOG_DIR to an absolute path on a shared filesystem (NFS) to enable per-rank log files for every Ray actor on both training and inference:

    export TORCHSPEC_LOG_DIR=/my_project/running_logs
    

    This creates a structured directory with one file per actor, organized by role and node:

    running_logs/
      training/
        10.0.0.1/
          training_g0_rank0_20260301_080012.log
          training_g0_rank1_20260301_080012.log
        10.0.0.2/
          training_g0_rank2_20260301_080013.log
      inference/
        10.0.0.1/
          inference_g0_rank0_20260301_080014.log
        10.0.0.2/
          inference_g0_rank1_20260301_080015.log
    

    The path must be absolute and writable from all nodes. If TORCHSPEC_LOG_DIR is unset or not writable, per-rank file logging stays disabled and Ray falls back to stdout/stderr capture.

    IssueReference
    Stuck or failing distributed runs, Ray actor errorsdocs/debugging_ray_jobs.md
    Ray cluster setup, actor hierarchy, placement groupsdocs/ray.md
    Pipeline bottlenecks, slow steps, throughput analysisdocs/performance_metrics.md