From 627fa880c792dbcd046727fee2dbd3f3fa3b1885 Mon Sep 17 00:00:00 2001 From: bingxche Date: Fri, 24 Jul 2026 01:55:49 -0500 Subject: [PATCH 1/6] Optimize mmap-backed MoE weight loading Co-authored-by: Cursor --- .../workflows/nightly-test-amd-rocm720.yml | 39 ++++++++++++++++ python/sglang/srt/environ.py | 3 ++ .../srt/layers/moe/fused_moe_triton/layer.py | 24 ++++++++++ .../layers/moe/test_compact_weight_load.py | 44 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 test/registered/unit/layers/moe/test_compact_weight_load.py diff --git a/.github/workflows/nightly-test-amd-rocm720.yml b/.github/workflows/nightly-test-amd-rocm720.yml index 5c702bc6b3bff..0a432e7a1fe14 100644 --- a/.github/workflows/nightly-test-amd-rocm720.yml +++ b/.github/workflows/nightly-test-amd-rocm720.yml @@ -68,6 +68,7 @@ on: - nightly-8-gpu-mi35x-deepseek-r1-hicache-rocm720 # 8-GPU DeepSeek-V4 (MI35x only) - nightly-8-gpu-mi35x-deepseek-v4-flash-rocm720 + - nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-mtp-rocm720 # 8-GPU Kimi-K2.6 (MI30x + MI35x) @@ -1331,6 +1332,43 @@ jobs: python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-flash --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true + nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720: + if: github.event_name == 'workflow_dispatch' && contains(format(',{0},', inputs.job_filter || inputs.job_select), ',nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720,') + runs-on: linux-mi35x-gpu-8 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref || github.sha }} + + - name: Ensure VRAM is clear + run: bash scripts/ci/amd/ensure_vram_clear.sh rocm + + - name: Setup docker (ROCm 7.2) + run: | + touch github_summary.md + bash scripts/ci/amd/amd_ci_start_container.sh --rocm-version rocm720 + env: + GITHUB_WORKSPACE: ${{ github.workspace }} + ENABLE_CACHE_HOST: "1" + + - name: Install dependencies + run: | + bash scripts/ci/amd/amd_ci_install_dependency.sh --skip-test-time-deps + bash scripts/ci/amd/amd_ci_exec.sh pip install tabulate + + - name: DeepSeek-V4-Pro FP4 compact-load accuracy + performance + timeout-minutes: 90 + run: | + > github_summary.md + echo "## DeepSeek-V4-Pro FP4 compact MoE weight load" >> github_summary.md + bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ + -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ + -e SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton \ + -e SGLANG_MOE_COMPACT_WEIGHT_LOAD=1 \ + python3 registered/amd/test_deepseek_v4_pro_fp4.py + echo "$(> "$GITHUB_STEP_SUMMARY" || true + nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720: if: (github.repository == 'sgl-project/sglang' || github.event_name == 'pull_request') && (!(inputs.job_filter || inputs.job_select) || (inputs.job_filter || inputs.job_select) == 'all' || contains(format(',{0},', inputs.job_filter || inputs.job_select), ',nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720,')) runs-on: linux-mi35x-gpu-8 @@ -2026,6 +2064,7 @@ jobs: - nightly-8-gpu-mi35x-deepseek-r1-hicache-rocm720 # 8-GPU DeepSeek-V4 (MI35x only) - nightly-8-gpu-mi35x-deepseek-v4-flash-rocm720 + - nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-mtp-rocm720 # 8-GPU Kimi-K2.6 (MI30x + MI35x) diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index 20d47e37eefb5..f8704294d5383 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -994,6 +994,9 @@ class Envs: # Set False when using FP4-to-FP8 converted DeepSeek V4 checkpoint. SGLANG_DSV4_FP4_EXPERTS = EnvBool(True) SGLANG_DSV4_FP4_DEQUANT = EnvBool(False) + # Materialize rank-local MoE slices before H2D instead of copying directly + # from a larger mmap-backed checkpoint storage. + SGLANG_MOE_COMPACT_WEIGHT_LOAD = EnvBool(False) # Default reasoning_effort for dsv4 chat encoder when request doesn't set it. # Accepts "", "max", "high" (empty string means unset); other values filtered to None. SGLANG_DSV4_REASONING_EFFORT = EnvStr("") diff --git a/python/sglang/srt/layers/moe/fused_moe_triton/layer.py b/python/sglang/srt/layers/moe/fused_moe_triton/layer.py index 1c2aa6f8197af..696e6409b4cef 100644 --- a/python/sglang/srt/layers/moe/fused_moe_triton/layer.py +++ b/python/sglang/srt/layers/moe/fused_moe_triton/layer.py @@ -93,6 +93,27 @@ _use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip +def _make_loaded_weight_compact(loaded_weight: torch.Tensor) -> torch.Tensor: + """Materialize a CPU tensor view into independent contiguous storage.""" + if loaded_weight.device.type != "cpu": + return loaded_weight + tensor_bytes = loaded_weight.numel() * loaded_weight.element_size() + is_compact = ( + loaded_weight.is_contiguous() + and loaded_weight.storage_offset() == 0 + and loaded_weight.untyped_storage().nbytes() == tensor_bytes + ) + if is_compact: + return loaded_weight + return loaded_weight.clone(memory_format=torch.contiguous_format) + + +def _maybe_compact_loaded_weight(loaded_weight: torch.Tensor) -> torch.Tensor: + if not envs.SGLANG_MOE_COMPACT_WEIGHT_LOAD.get(): + return loaded_weight + return _make_loaded_weight_compact(loaded_weight) + + def _get_deepep_comm_group(a2a_backend): group = get_tp_group().device_group @@ -539,6 +560,7 @@ def _load_per_channel_weight_scale( ): # for per channel weight quantization if shard_id == "w2": + loaded_weight = _maybe_compact_loaded_weight(loaded_weight) expert_data.copy_(loaded_weight) elif shard_id in ("w1", "w3"): self._load_w13( @@ -611,6 +633,7 @@ def _load_w13( ) expert_data = expert_data.narrow(shard_dim, start, shard_size) + loaded_weight = _maybe_compact_loaded_weight(loaded_weight) expert_data.copy_(loaded_weight) def _load_w2( @@ -681,6 +704,7 @@ def _load_w2( ) # w2, down_proj: Load into only logical weight of w2. + loaded_weight = _maybe_compact_loaded_weight(loaded_weight) expert_data.copy_(loaded_weight) def _maybe_load_fp8_shared_expert_as_fp4( diff --git a/test/registered/unit/layers/moe/test_compact_weight_load.py b/test/registered/unit/layers/moe/test_compact_weight_load.py new file mode 100644 index 0000000000000..275c9b8b00fd2 --- /dev/null +++ b/test/registered/unit/layers/moe/test_compact_weight_load.py @@ -0,0 +1,44 @@ +import torch + +from sglang.srt.layers.moe.fused_moe_triton.layer import ( + _make_loaded_weight_compact, +) +from sglang.test.ci.ci_register import register_cpu_ci + +register_cpu_ci(est_time=2, suite="base-a-test-cpu") + + +def _assert_independent_compact_copy( + source: torch.Tensor, result: torch.Tensor +) -> None: + assert torch.equal(result, source) + assert result is not source + assert result.is_contiguous() + assert result.storage_offset() == 0 + assert result.untyped_storage().nbytes() == result.numel() * result.element_size() + + +def test_keeps_already_compact_tensor(): + source = torch.arange(16).reshape(4, 4) + assert _make_loaded_weight_compact(source) is source + + +def test_materializes_zero_offset_storage_view(): + backing = torch.arange(32).reshape(8, 4) + source = backing.narrow(0, 0, 2) + assert source.storage_offset() == 0 + assert source.untyped_storage().nbytes() > source.numel() * source.element_size() + _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) + + +def test_materializes_nonzero_offset_storage_view(): + backing = torch.arange(32).reshape(8, 4) + source = backing.narrow(0, 6, 2) + assert source.storage_offset() != 0 + _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) + + +def test_materializes_noncontiguous_tensor(): + source = torch.arange(16).reshape(4, 4).transpose(0, 1) + assert not source.is_contiguous() + _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) From b2fd33ccab2f52554adae3f23f816840b483a255 Mon Sep 17 00:00:00 2001 From: bingxche Date: Fri, 24 Jul 2026 05:31:12 -0500 Subject: [PATCH 2/6] Enable compact loading in DSV4 Pro nightly Co-authored-by: Cursor --- .../workflows/nightly-test-amd-rocm720.yml | 41 +------------------ 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/.github/workflows/nightly-test-amd-rocm720.yml b/.github/workflows/nightly-test-amd-rocm720.yml index 0a432e7a1fe14..6484ecba53639 100644 --- a/.github/workflows/nightly-test-amd-rocm720.yml +++ b/.github/workflows/nightly-test-amd-rocm720.yml @@ -68,7 +68,6 @@ on: - nightly-8-gpu-mi35x-deepseek-r1-hicache-rocm720 # 8-GPU DeepSeek-V4 (MI35x only) - nightly-8-gpu-mi35x-deepseek-v4-flash-rocm720 - - nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-mtp-rocm720 # 8-GPU Kimi-K2.6 (MI30x + MI35x) @@ -1332,43 +1331,6 @@ jobs: python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-flash --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true - nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720: - if: github.event_name == 'workflow_dispatch' && contains(format(',{0},', inputs.job_filter || inputs.job_select), ',nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720,') - runs-on: linux-mi35x-gpu-8 - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - ref: ${{ inputs.ref || github.sha }} - - - name: Ensure VRAM is clear - run: bash scripts/ci/amd/ensure_vram_clear.sh rocm - - - name: Setup docker (ROCm 7.2) - run: | - touch github_summary.md - bash scripts/ci/amd/amd_ci_start_container.sh --rocm-version rocm720 - env: - GITHUB_WORKSPACE: ${{ github.workspace }} - ENABLE_CACHE_HOST: "1" - - - name: Install dependencies - run: | - bash scripts/ci/amd/amd_ci_install_dependency.sh --skip-test-time-deps - bash scripts/ci/amd/amd_ci_exec.sh pip install tabulate - - - name: DeepSeek-V4-Pro FP4 compact-load accuracy + performance - timeout-minutes: 90 - run: | - > github_summary.md - echo "## DeepSeek-V4-Pro FP4 compact MoE weight load" >> github_summary.md - bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ - -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ - -e SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton \ - -e SGLANG_MOE_COMPACT_WEIGHT_LOAD=1 \ - python3 registered/amd/test_deepseek_v4_pro_fp4.py - echo "$(> "$GITHUB_STEP_SUMMARY" || true - nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720: if: (github.repository == 'sgl-project/sglang' || github.event_name == 'pull_request') && (!(inputs.job_filter || inputs.job_select) || (inputs.job_filter || inputs.job_select) == 'all' || contains(format(',{0},', inputs.job_filter || inputs.job_select), ',nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720,')) runs-on: linux-mi35x-gpu-8 @@ -1402,6 +1364,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton \ + -e SGLANG_MOE_COMPACT_WEIGHT_LOAD=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-pro --nightly --timeout-per-file 14400 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1414,6 +1377,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=triton \ + -e SGLANG_MOE_COMPACT_WEIGHT_LOAD=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-pro --nightly --timeout-per-file 14400 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true @@ -2064,7 +2028,6 @@ jobs: - nightly-8-gpu-mi35x-deepseek-r1-hicache-rocm720 # 8-GPU DeepSeek-V4 (MI35x only) - nightly-8-gpu-mi35x-deepseek-v4-flash-rocm720 - - nightly-8-gpu-mi35x-deepseek-v4-pro-fp4-compact-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-rocm720 - nightly-8-gpu-mi35x-deepseek-v4-pro-mtp-rocm720 # 8-GPU Kimi-K2.6 (MI30x + MI35x) From 322de80d8ba3554904c846121e612c91001f20ac Mon Sep 17 00:00:00 2001 From: bingxche Date: Fri, 24 Jul 2026 10:16:38 -0500 Subject: [PATCH 3/6] Fix compact weight test entry point Co-authored-by: Cursor --- .../unit/layers/moe/test_compact_weight_load.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/registered/unit/layers/moe/test_compact_weight_load.py b/test/registered/unit/layers/moe/test_compact_weight_load.py index 275c9b8b00fd2..508ec9a4bf25a 100644 --- a/test/registered/unit/layers/moe/test_compact_weight_load.py +++ b/test/registered/unit/layers/moe/test_compact_weight_load.py @@ -42,3 +42,11 @@ def test_materializes_noncontiguous_tensor(): source = torch.arange(16).reshape(4, 4).transpose(0, 1) assert not source.is_contiguous() _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) + + +if __name__ == "__main__": + import sys + + import pytest + + sys.exit(pytest.main([__file__, "-v"])) From d34c91e90733a95274a85017669c8a16e9f9fc7b Mon Sep 17 00:00:00 2001 From: bingxche Date: Tue, 28 Jul 2026 04:04:16 -0500 Subject: [PATCH 4/6] Rename MoE weight view copy symbols [skip ci] --- .../workflows/nightly-test-amd-rocm720.yml | 4 ++-- python/sglang/srt/environ.py | 6 ++--- .../srt/layers/moe/fused_moe_triton/layer.py | 22 ++++++++++--------- ...y => test_copy_weight_views_before_h2d.py} | 22 +++++++++---------- 4 files changed, 27 insertions(+), 27 deletions(-) rename test/registered/unit/layers/moe/{test_compact_weight_load.py => test_copy_weight_views_before_h2d.py} (61%) diff --git a/.github/workflows/nightly-test-amd-rocm720.yml b/.github/workflows/nightly-test-amd-rocm720.yml index e45ee3847a34b..cf4101e4eb4c2 100644 --- a/.github/workflows/nightly-test-amd-rocm720.yml +++ b/.github/workflows/nightly-test-amd-rocm720.yml @@ -1366,7 +1366,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton \ - -e SGLANG_MOE_COMPACT_WEIGHT_LOAD=1 \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-pro --nightly --timeout-per-file 14400 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1379,7 +1379,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=triton \ - -e SGLANG_MOE_COMPACT_WEIGHT_LOAD=1 \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-pro --nightly --timeout-per-file 14400 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index ad9ba2e3979bf..66b50b851e639 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -1021,9 +1021,9 @@ class Envs: # Set False when using FP4-to-FP8 converted DeepSeek V4 checkpoint. SGLANG_DSV4_FP4_EXPERTS = EnvBool(True) SGLANG_DSV4_FP4_DEQUANT = EnvBool(False) - # Materialize rank-local MoE slices before H2D instead of copying directly - # from a larger mmap-backed checkpoint storage. - SGLANG_MOE_COMPACT_WEIGHT_LOAD = EnvBool(False) + # Copy rank-local MoE slices into independent CPU storage before H2D when + # they reference a larger mmap-backed checkpoint storage. + SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D = EnvBool(False) # Default reasoning_effort for dsv4 chat encoder when request doesn't set it. # Accepts "", "max", "high" (empty string means unset); other values filtered to None. SGLANG_DSV4_REASONING_EFFORT = EnvStr("") diff --git a/python/sglang/srt/layers/moe/fused_moe_triton/layer.py b/python/sglang/srt/layers/moe/fused_moe_triton/layer.py index e8984c5472776..487bae5df1126 100644 --- a/python/sglang/srt/layers/moe/fused_moe_triton/layer.py +++ b/python/sglang/srt/layers/moe/fused_moe_triton/layer.py @@ -93,25 +93,27 @@ _use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip -def _make_loaded_weight_compact(loaded_weight: torch.Tensor) -> torch.Tensor: - """Materialize a CPU tensor view into independent contiguous storage.""" +def _copy_weight_view_before_h2d(loaded_weight: torch.Tensor) -> torch.Tensor: + """Copy a CPU tensor view into independent contiguous storage.""" if loaded_weight.device.type != "cpu": return loaded_weight tensor_bytes = loaded_weight.numel() * loaded_weight.element_size() - is_compact = ( + needs_copy = not ( loaded_weight.is_contiguous() and loaded_weight.storage_offset() == 0 and loaded_weight.untyped_storage().nbytes() == tensor_bytes ) - if is_compact: + if not needs_copy: return loaded_weight return loaded_weight.clone(memory_format=torch.contiguous_format) -def _maybe_compact_loaded_weight(loaded_weight: torch.Tensor) -> torch.Tensor: - if not envs.SGLANG_MOE_COMPACT_WEIGHT_LOAD.get(): +def _maybe_copy_weight_view_before_h2d( + loaded_weight: torch.Tensor, +) -> torch.Tensor: + if not envs.SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D.get(): return loaded_weight - return _make_loaded_weight_compact(loaded_weight) + return _copy_weight_view_before_h2d(loaded_weight) def _get_deepep_comm_group(a2a_backend): @@ -579,7 +581,7 @@ def _load_per_channel_weight_scale( ): # for per channel weight quantization if shard_id == "w2": - loaded_weight = _maybe_compact_loaded_weight(loaded_weight) + loaded_weight = _maybe_copy_weight_view_before_h2d(loaded_weight) expert_data.copy_(loaded_weight) elif shard_id in ("w1", "w3"): self._load_w13( @@ -652,7 +654,7 @@ def _load_w13( ) expert_data = expert_data.narrow(shard_dim, start, shard_size) - loaded_weight = _maybe_compact_loaded_weight(loaded_weight) + loaded_weight = _maybe_copy_weight_view_before_h2d(loaded_weight) expert_data.copy_(loaded_weight) def _load_w2( @@ -723,7 +725,7 @@ def _load_w2( ) # w2, down_proj: Load into only logical weight of w2. - loaded_weight = _maybe_compact_loaded_weight(loaded_weight) + loaded_weight = _maybe_copy_weight_view_before_h2d(loaded_weight) expert_data.copy_(loaded_weight) def _maybe_load_fp8_shared_expert_as_fp4( diff --git a/test/registered/unit/layers/moe/test_compact_weight_load.py b/test/registered/unit/layers/moe/test_copy_weight_views_before_h2d.py similarity index 61% rename from test/registered/unit/layers/moe/test_compact_weight_load.py rename to test/registered/unit/layers/moe/test_copy_weight_views_before_h2d.py index 508ec9a4bf25a..7d59e0bb2400e 100644 --- a/test/registered/unit/layers/moe/test_compact_weight_load.py +++ b/test/registered/unit/layers/moe/test_copy_weight_views_before_h2d.py @@ -1,16 +1,14 @@ import torch from sglang.srt.layers.moe.fused_moe_triton.layer import ( - _make_loaded_weight_compact, + _copy_weight_view_before_h2d, ) from sglang.test.ci.ci_register import register_cpu_ci register_cpu_ci(est_time=2, suite="base-a-test-cpu") -def _assert_independent_compact_copy( - source: torch.Tensor, result: torch.Tensor -) -> None: +def _assert_independent_copy(source: torch.Tensor, result: torch.Tensor) -> None: assert torch.equal(result, source) assert result is not source assert result.is_contiguous() @@ -18,30 +16,30 @@ def _assert_independent_compact_copy( assert result.untyped_storage().nbytes() == result.numel() * result.element_size() -def test_keeps_already_compact_tensor(): +def test_skips_copy_for_exact_storage(): source = torch.arange(16).reshape(4, 4) - assert _make_loaded_weight_compact(source) is source + assert _copy_weight_view_before_h2d(source) is source -def test_materializes_zero_offset_storage_view(): +def test_copies_zero_offset_storage_view(): backing = torch.arange(32).reshape(8, 4) source = backing.narrow(0, 0, 2) assert source.storage_offset() == 0 assert source.untyped_storage().nbytes() > source.numel() * source.element_size() - _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) + _assert_independent_copy(source, _copy_weight_view_before_h2d(source)) -def test_materializes_nonzero_offset_storage_view(): +def test_copies_nonzero_offset_storage_view(): backing = torch.arange(32).reshape(8, 4) source = backing.narrow(0, 6, 2) assert source.storage_offset() != 0 - _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) + _assert_independent_copy(source, _copy_weight_view_before_h2d(source)) -def test_materializes_noncontiguous_tensor(): +def test_copies_noncontiguous_tensor(): source = torch.arange(16).reshape(4, 4).transpose(0, 1) assert not source.is_contiguous() - _assert_independent_compact_copy(source, _make_loaded_weight_compact(source)) + _assert_independent_copy(source, _copy_weight_view_before_h2d(source)) if __name__ == "__main__": From 61738acb02249359ad422e744a1993b4b21c6c10 Mon Sep 17 00:00:00 2001 From: bingxche Date: Wed, 29 Jul 2026 03:36:56 -0500 Subject: [PATCH 5/6] Enable weight view copies in AMD DSV4 jobs [skip ci] --- .github/workflows/nightly-test-amd-rocm720.yml | 4 ++++ .github/workflows/pr-test-amd-rocm720.yml | 2 ++ 2 files changed, 6 insertions(+) diff --git a/.github/workflows/nightly-test-amd-rocm720.yml b/.github/workflows/nightly-test-amd-rocm720.yml index cf4101e4eb4c2..0f700a0d52d6c 100644 --- a/.github/workflows/nightly-test-amd-rocm720.yml +++ b/.github/workflows/nightly-test-amd-rocm720.yml @@ -1317,6 +1317,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-flash --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1329,6 +1330,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=triton \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-flash --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1417,6 +1419,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=unified_kv_triton \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-pro-mtp --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1429,6 +1432,7 @@ jobs: echo "## SGLANG_HACK_FLASHMLA_BACKEND=triton" >> github_summary.md bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e SGLANG_HACK_FLASHMLA_BACKEND=triton \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-pro-mtp --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} echo "$(> $GITHUB_STEP_SUMMARY || true diff --git a/.github/workflows/pr-test-amd-rocm720.yml b/.github/workflows/pr-test-amd-rocm720.yml index afdf9662f1d91..d820b58379dd7 100644 --- a/.github/workflows/pr-test-amd-rocm720.yml +++ b/.github/workflows/pr-test-amd-rocm720.yml @@ -1289,6 +1289,7 @@ jobs: bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ -e SGLANG_DSV4_ACCURACY_ONLY=1 \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ python3 run_suite.py --hw amd --suite nightly-amd-8-gpu-mi35x-deepseek-v4-flash --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} || TEST_EXIT_CODE=$? echo "$(> $GITHUB_STEP_SUMMARY || true exit ${TEST_EXIT_CODE:-0} @@ -1340,6 +1341,7 @@ jobs: bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ -e SGLANG_DSV4_ACCURACY_ONLY=1 \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ python3 registered/amd/test_deepseek_v4_pro_fp4.py || TEST_EXIT_CODE=$? echo "$(> $GITHUB_STEP_SUMMARY || true if [[ "${{ github.event_name == 'schedule' || inputs.continue_on_error }}" == "true" ]]; then From 79c5ab30f9d79e00a8d3d6103565b38b76c4be86 Mon Sep 17 00:00:00 2001 From: bingxche Date: Thu, 30 Jul 2026 22:36:23 -0500 Subject: [PATCH 6/6] [AMD][CI] Validate compact MoE loads on MI300 Enable the opt-in weight-view copy path only for the large-model jobs that exceeded their load budgets after the runner migration. --- .github/workflows/nightly-test-amd-rocm720.yml | 3 +++ .github/workflows/nightly-test-amd.yml | 1 + 2 files changed, 4 insertions(+) diff --git a/.github/workflows/nightly-test-amd-rocm720.yml b/.github/workflows/nightly-test-amd-rocm720.yml index 90e4b07f98428..d6dfe0ebe9a2d 100644 --- a/.github/workflows/nightly-test-amd-rocm720.yml +++ b/.github/workflows/nightly-test-amd-rocm720.yml @@ -493,6 +493,7 @@ jobs: timeout-minutes: 180 run: | bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-accuracy-8-gpu-gpt-oss --nightly --timeout-per-file 7200 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} || TEST_EXIT_CODE=$? echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1542,6 +1543,7 @@ jobs: run: | > github_summary.md # Clear summary file bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-8-gpu-qwen3-235b --nightly --timeout-per-file 3600 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} || TEST_EXIT_CODE=$? echo "$(> $GITHUB_STEP_SUMMARY || true @@ -1616,6 +1618,7 @@ jobs: run: | > github_summary.md # Clear summary file bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-accuracy-8-gpu-qwen35 --nightly --timeout-per-file 3600 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} || TEST_EXIT_CODE=$? echo "$(> $GITHUB_STEP_SUMMARY || true diff --git a/.github/workflows/nightly-test-amd.yml b/.github/workflows/nightly-test-amd.yml index da995a2c3c59c..492f7b1c5ece7 100644 --- a/.github/workflows/nightly-test-amd.yml +++ b/.github/workflows/nightly-test-amd.yml @@ -1464,6 +1464,7 @@ jobs: run: | > github_summary.md # Clear summary file bash scripts/ci/amd/amd_ci_exec.sh -w /sglang-checkout/test \ + -e SGLANG_MOE_COPY_WEIGHT_VIEWS_BEFORE_H2D=1 \ -e GITHUB_STEP_SUMMARY="/sglang-checkout/github_summary.md" \ python3 run_suite.py --hw amd --suite nightly-amd-accuracy-8-gpu-qwen35 --nightly --timeout-per-file 3600 ${{ (github.event_name == 'schedule' || inputs.continue_on_error) && '--continue-on-error' || '' }} || TEST_EXIT_CODE=$? echo "$(> $GITHUB_STEP_SUMMARY || true