Files
sgl-project--sglang/python/sglang/srt/debug_utils/model_truncator.py
T
wehub-resource-sync 94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

113 lines
3.6 KiB
Python

# This file also references Slime :: fp8_cast_bf16.py
import json
import os
import re
from argparse import ArgumentParser
from pathlib import Path
from typing import Dict
import torch
from huggingface_hub import snapshot_download
from safetensors.torch import load_file, save_file
def main(args):
dir_input = Path(_maybe_snapshot_download(args.input))
dir_output = Path(args.output)
print(f"{dir_input=} {dir_output=}")
dir_output.mkdir(parents=True, exist_ok=True)
for pattern in ["generation_config.json", "*.py", "tokenizer*"]:
os.system(f"cp -rf {dir_input}/{pattern} {dir_output}")
_transform_json(
dir_input,
dir_output,
"config.json",
lambda data: _transform_config(args, data),
)
safetensors_index = _transform_json(
dir_input,
dir_output,
"model.safetensors.index.json",
lambda data: _transform_safetensors_index(args, data),
)
for path_input_safetensors in sorted(list(dir_input.glob("*.safetensors"))):
path_output_safetensors = dir_output / path_input_safetensors.relative_to(
dir_input
)
state_dict = load_file(path_input_safetensors)
_transform_safetensors_file(
state_dict, safetensors_index, debug_name=str(path_output_safetensors)
)
if len(state_dict) > 0:
print(f"Save {len(state_dict)} tensors to {path_output_safetensors}")
save_file(state_dict, path_output_safetensors)
else:
print(f"Skip saving {path_output_safetensors} since it is empty")
def _maybe_snapshot_download(path):
if Path(path).exists():
return path
return snapshot_download(path)
def _transform_json(dir_input, dir_output, filename, fn):
data = json.loads((dir_input / filename).read_text())
fn(data)
(dir_output / filename).write_text(json.dumps(data, indent=4))
return data
def _transform_config(args, config_json):
config_json["num_hidden_layers"] = args.keep_num_layers
def _transform_safetensors_index(args, safetensors_index):
weight_map = safetensors_index["weight_map"]
weight_map = {
name: loc for name, loc in weight_map.items() if _filter_tensor_name(args, name)
}
safetensors_index["weight_map"] = weight_map
def _transform_safetensors_file(
state_dict: Dict[str, torch.Tensor], safetensors_index, debug_name: str
):
names_to_remove = set(state_dict) - set(safetensors_index["weight_map"])
print(f"Remove {list(names_to_remove)} in {debug_name}")
for name in names_to_remove:
del state_dict[name]
def _filter_tensor_name(args, tensor_name: str):
# We focus on DeepSeek-like names currently, but can be easily extended to more kinds of models
m = re.match(r"^model.layers.(\d+).*", tensor_name)
if m is None:
return True
layer_id = int(m.group(1))
return layer_id < args.keep_num_layers
if __name__ == "__main__":
"""
Example:
python -m sglang.srt.debug_utils.model_truncator --input deepseek-ai/DeepSeek-V3-0324 --output /tmp/DeepSeek-V3-0324-5layer
hf upload my_name/DeepSeek-V3-0324-5layer /tmp/DeepSeek-V3-0324-5layer
Alternatively, the following may be used on-the-fly.
But this may not be useful to test RL frameworks, and sometimes it may have issues.
--json-model-override-args '{"num_hidden_layers": 5}'
"""
parser = ArgumentParser(description="Create truncated model for fast debugging.")
parser.add_argument("--input", type=str, required=True)
parser.add_argument("--output", type=str, required=True)
parser.add_argument("--keep-num-layers", type=int, default=5)
main(parser.parse_args())