sgl-project--sglang
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
247 行
7.3 KiB
Python
247 行
7.3 KiB
Python
import unittest
|
|
|
|
import torch
|
|
|
|
from sglang.srt.lora.backend.torch_backend import TorchNativeLoRABackend
|
|
from sglang.srt.model_executor.forward_batch_info import ForwardBatch, ForwardMode
|
|
from sglang.test.lora_utils import reference_sgmv_expand, reference_sgmv_shrink
|
|
from sglang.test.test_utils import CustomTestCase
|
|
|
|
|
|
class TestTorchNativeLoRABackend(CustomTestCase):
|
|
|
|
device = "cpu"
|
|
|
|
# set duplicate weights to test merging during prepare_lora_batch
|
|
weight_indices = [0, 0, 1]
|
|
lora_ranks = [1, 1]
|
|
scalings = [1.0, 0.5]
|
|
seq_lens = [1, 1, 1]
|
|
use_cuda_graph = False
|
|
|
|
forward_batch = ForwardBatch(
|
|
forward_mode=ForwardMode.EXTEND,
|
|
batch_size=3,
|
|
input_ids=torch.tensor([[1], [2], [3]], dtype=torch.int32),
|
|
req_pool_indices=None,
|
|
seq_lens=None,
|
|
out_cache_loc=None,
|
|
seq_lens_sum=3,
|
|
extend_seq_lens=torch.tensor(seq_lens, dtype=torch.int32),
|
|
extend_seq_lens_cpu=seq_lens,
|
|
)
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.backend = TorchNativeLoRABackend(max_loras_per_batch=2, device=cls.device)
|
|
cls.backend.prepare_lora_batch(
|
|
forward_batch=cls.forward_batch,
|
|
weight_indices=cls.weight_indices,
|
|
lora_ranks=cls.lora_ranks,
|
|
scalings=cls.scalings,
|
|
use_cuda_graph=cls.use_cuda_graph,
|
|
)
|
|
|
|
def test_run_lora_a_sgemm(self):
|
|
batch_size = 3
|
|
input_dim = 4
|
|
output_dim = 6
|
|
num_loras = 3
|
|
dtype = torch.float32
|
|
|
|
x = torch.randn(batch_size, input_dim, dtype=dtype)
|
|
weights = torch.randn(num_loras, output_dim, input_dim, dtype=dtype)
|
|
|
|
weight_indices_tensor = torch.tensor(
|
|
self.weight_indices, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
seg_len_tensor = torch.tensor(
|
|
self.seq_lens, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
lora_ranks_tensor = torch.tensor(
|
|
self.lora_ranks, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
scalings_tensor = torch.tensor(
|
|
self.scalings, dtype=torch.float, device=self.device
|
|
)
|
|
|
|
expect_output = reference_sgmv_shrink(
|
|
x,
|
|
weights,
|
|
weight_indices_tensor,
|
|
seg_len_tensor,
|
|
lora_ranks_tensor,
|
|
scalings_tensor,
|
|
)
|
|
|
|
actual_output = self.backend.run_lora_a_sgemm(x, weights)
|
|
|
|
self.assertTrue(torch.allclose(actual_output, expect_output))
|
|
|
|
def test_run_lora_b_sgemm(self):
|
|
batch_size = 3
|
|
input_dim = 6
|
|
output_dim = 4
|
|
num_loras = 3
|
|
dtype = torch.float32
|
|
|
|
x = torch.randn(batch_size, input_dim, dtype=dtype)
|
|
weights = torch.randn(num_loras, output_dim, input_dim, dtype=dtype)
|
|
_, weight_out_dim, _ = weights.shape
|
|
|
|
weight_indices_tensor = torch.tensor(
|
|
self.weight_indices, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
seg_len_tensor = torch.tensor(
|
|
self.seq_lens, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
lora_ranks_tensor = torch.tensor(
|
|
self.lora_ranks, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
expect_output = reference_sgmv_expand(
|
|
x,
|
|
weights,
|
|
weight_indices_tensor,
|
|
seg_len_tensor,
|
|
lora_ranks_tensor,
|
|
slice_offsets=torch.tensor(
|
|
[0, weight_out_dim], dtype=torch.int32, device="cpu"
|
|
),
|
|
)
|
|
|
|
actual_output = self.backend.run_lora_b_sgemm(x, weights)
|
|
|
|
self.assertTrue(torch.allclose(actual_output, expect_output))
|
|
|
|
def test_run_qkv_lora(self):
|
|
batch_size = 3
|
|
num_loras = 3
|
|
input_dim = 6
|
|
output_offset = [0, 3, 6, 9]
|
|
output_dim = output_offset[-1]
|
|
num_slices = len(output_offset) - 1 # 3 slices for Q, K, V
|
|
max_lora_rank = max(self.lora_ranks)
|
|
dtype = torch.float32
|
|
|
|
x = torch.randn(batch_size, input_dim, dtype=dtype)
|
|
output_offset_cpu = torch.tensor(output_offset, dtype=torch.int32)
|
|
qkv_lora_a = torch.randn(
|
|
num_loras, max_lora_rank * num_slices, input_dim, dtype=dtype
|
|
)
|
|
qkv_lora_b = torch.randn(
|
|
num_loras, output_dim, max_lora_rank * num_slices, dtype=dtype
|
|
)
|
|
|
|
weight_indices_tensor = torch.tensor(
|
|
self.weight_indices, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
seg_len_tensor = torch.tensor(
|
|
self.seq_lens, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
lora_ranks_tensor = torch.tensor(
|
|
self.lora_ranks, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
scalings_tensor = torch.tensor(
|
|
self.scalings, dtype=torch.float, device=self.device
|
|
)
|
|
|
|
expect_lora_a_output = reference_sgmv_shrink(
|
|
x,
|
|
qkv_lora_a,
|
|
weight_indices_tensor,
|
|
seg_len_tensor,
|
|
lora_ranks_tensor,
|
|
scalings_tensor,
|
|
num_slices,
|
|
)
|
|
|
|
expect_output = reference_sgmv_expand(
|
|
expect_lora_a_output,
|
|
qkv_lora_b,
|
|
weight_indices_tensor,
|
|
seg_len_tensor,
|
|
lora_ranks_tensor,
|
|
output_offset_cpu,
|
|
)
|
|
|
|
actual_output = self.backend.run_qkv_lora(
|
|
x, qkv_lora_a, qkv_lora_b, None, output_offset_cpu, 0
|
|
)
|
|
self.assertTrue(torch.allclose(actual_output, expect_output))
|
|
|
|
def test_run_gate_up_lora(self):
|
|
batch_size = 3
|
|
input_dim = 6
|
|
output_dim = 4
|
|
num_loras = 3
|
|
dtype = torch.float32
|
|
|
|
max_lora_rank = max(self.lora_ranks)
|
|
|
|
num_slices = 2
|
|
|
|
x = torch.randn(batch_size, input_dim, dtype=dtype)
|
|
gate_up_lora_a = torch.randn(
|
|
num_loras, max_lora_rank * num_slices, input_dim, dtype=dtype
|
|
)
|
|
gate_up_lora_b = torch.randn(
|
|
num_loras, output_dim, max_lora_rank * num_slices, dtype=dtype
|
|
)
|
|
|
|
_, weight_out_dim, _ = gate_up_lora_b.shape
|
|
slice_size = weight_out_dim // num_slices
|
|
output_offset = torch.tensor(
|
|
[0, slice_size, weight_out_dim], dtype=torch.int32, device="cpu"
|
|
)
|
|
|
|
weight_indices_tensor = torch.tensor(
|
|
self.weight_indices, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
seg_len_tensor = torch.tensor(
|
|
self.seq_lens, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
lora_ranks_tensor = torch.tensor(
|
|
self.lora_ranks, dtype=torch.int32, device=self.device
|
|
)
|
|
|
|
scalings_tensor = torch.tensor(
|
|
self.scalings, dtype=torch.float, device=self.device
|
|
)
|
|
|
|
expect_lora_a_output = reference_sgmv_shrink(
|
|
x,
|
|
gate_up_lora_a,
|
|
weight_indices_tensor,
|
|
seg_len_tensor,
|
|
lora_ranks_tensor,
|
|
scalings_tensor,
|
|
num_slices,
|
|
)
|
|
|
|
expect_output = reference_sgmv_expand(
|
|
expect_lora_a_output,
|
|
gate_up_lora_b,
|
|
weight_indices_tensor,
|
|
seg_len_tensor,
|
|
lora_ranks_tensor,
|
|
slice_offsets=output_offset,
|
|
)
|
|
|
|
actual_output = self.backend.run_gate_up_lora(x, gate_up_lora_a, gate_up_lora_b)
|
|
self.assertTrue(torch.allclose(actual_output, expect_output))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|