Skip to content

Commit 8c9e13f

Browse files
committed
fix(npu): align colocate with pr285 container validation
Split TMS env for NPU torch vs GPU preload, set PYTORCH_NPU_ALLOC_CONF=expandable_segments:False on colocate, guard empty_cache under TMS, and add vllm/vllm-ascend patches for flash_attn and NPU IPC engine API. Signed-off-by: kaiyuan <kyxiezju@163.com>
1 parent 95c6c8d commit 8c9e13f

7 files changed

Lines changed: 63 additions & 23 deletions

File tree

docker/npu_patch/vllm-ascend.patch

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ index 63ee94c..4e81943 100644
3737
self.available_kv_cache_memory_bytes = (
3838
self.requested_memory - profile_result.non_kv_cache_memory - npugraph_memory_estimate_applied
3939
)
40+
diff --git a/vllm_ascend/distributed/weight_transfer/npu_ipc_engine.py b/vllm_ascend/distributed/weight_transfer/npu_ipc_engine.py
41+
index 0000000..0000000 100644
42+
--- a/vllm_ascend/distributed/weight_transfer/npu_ipc_engine.py
43+
+++ b/vllm_ascend/distributed/weight_transfer/npu_ipc_engine.py
44+
@@ -116,8 +116,8 @@ class NPUIPCWeightTransferEngine(WeightTransferEngine[NPUIPCWeightTransferInitIn
45+
init_info_cls = NPUIPCWeightTransferInitInfo
46+
update_info_cls = NPUIPCWeightTransferUpdateInfo
47+
48+
- def __init__(self, config: WeightTransferConfig, parallel_config: ParallelConfig) -> None:
49+
- super().__init__(config, parallel_config)
50+
+ def __init__(self, config: WeightTransferConfig, parallel_config: ParallelConfig, model: torch.nn.Module = None) -> None:
51+
+ super().__init__(config, parallel_config, model)
52+
53+
def parse_update_info(self, update_dict: dict[str, Any]) -> NPUIPCWeightTransferUpdateInfo:
54+
"""Parse update dict, deserializing pickled IPC handles if present.

docker/npu_patch/vllm.patch

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,16 @@ index cb61bcabd..5c076d521 100644
1111

1212
# Cache the new tokens. Preempted requests should be skipped.
1313
if status_before_update == RequestStatus.RUNNING:
14+
diff --git a/vllm/model_executor/layers/rotary_embedding/common.py b/vllm/model_executor/layers/rotary_embedding/common.py
15+
index 0000000..0000000 100644
16+
--- a/vllm/model_executor/layers/rotary_embedding/common.py
17+
+++ b/vllm/model_executor/layers/rotary_embedding/common.py
18+
@@ -135,7 +135,9 @@ class ApplyRotaryEmb(CustomOp):
19+
self.enable_fp32_compute = enable_fp32_compute
20+
21+
self.apply_rotary_emb_flash_attn = None
22+
- if not current_platform.is_cpu() and find_spec("flash_attn") is not None:
23+
+ if not current_platform.is_cpu() and find_spec("flash_attn") is not None and not hasattr(current_platform, "is_npu"):
24+
from flash_attn.ops.triton.rotary import apply_rotary
25+
26+
self.apply_rotary_emb_flash_attn = apply_rotary

vime/backends/megatron_utils/actor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212

1313
if is_npu():
1414
from mindspeed.megatron_adaptor import repatch
15+
16+
_orig_npu_empty_cache = torch.npu.empty_cache
17+
18+
def _safe_empty_cache():
19+
try:
20+
_orig_npu_empty_cache()
21+
except RuntimeError:
22+
pass
23+
24+
torch.npu.empty_cache = _safe_empty_cache
25+
torch.cuda.empty_cache = _safe_empty_cache
1526
from megatron.core import mpu
1627
from torch_memory_saver import torch_memory_saver
1728
from transformers import AutoConfig, AutoTokenizer

vime/backends/vllm_utils/vllm_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def build_vllm_subprocess_env(server_args: dict[str, Any]) -> dict[str, str]:
366366
if cann_python_path is not None:
367367
prepend_pythonpath(env, cann_python_path)
368368
if getattr(args, "colocate", False):
369-
env.pop("PYTORCH_NPU_ALLOC_CONF", None)
369+
env["PYTORCH_NPU_ALLOC_CONF"] = "expandable_segments:False"
370370
else:
371371
env["CUDA_VISIBLE_DEVICES"] = server_args["visible_devices"]
372372
env.setdefault("VLLM_SERVER_DEV_MODE", "1")

vime/ray/actor_group.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,34 @@ def _allocate_gpus_for_actor(self, pg, num_gpus_per_actor):
6363
if self.args.offload_train and self.args.train_backend == "megatron":
6464
import torch_memory_saver
6565

66-
for path in [
67-
"torch_memory_saver_hook_mode_preload_cu12.abi3.so",
68-
"torch_memory_saver_hook_mode_preload.abi3.so",
69-
]:
70-
dynlib_path = os.path.join(
71-
os.path.dirname(os.path.dirname(torch_memory_saver.__file__)),
72-
path,
73-
)
74-
if os.path.exists(dynlib_path):
75-
break
76-
else:
77-
raise FileNotFoundError(
78-
"Cannot find torch_memory_saver dynamic library. Please make sure torch_memory_saver is properly installed."
79-
)
80-
81-
env_vars["LD_PRELOAD"] = dynlib_path
82-
env_vars["TMS_INIT_ENABLE"] = "1"
83-
env_vars["TMS_INIT_ENABLE_CPU_BACKUP"] = "1"
84-
8566
if is_npu():
8667
env_vars["TMS_HOOK_MODE"] = "torch"
8768
env_vars["TMS_REGION_TAG"] = "training"
8869
env_vars["TMS_ENABLE_CPU_BACKUP"] = "1"
8970
if self.args.colocate:
90-
env_vars.pop("PYTORCH_NPU_ALLOC_CONF", None)
71+
env_vars["PYTORCH_NPU_ALLOC_CONF"] = "expandable_segments:False"
9172
cann_python_path = get_cann_python_site_packages()
9273
if cann_python_path is not None:
9374
prepend_pythonpath(env_vars, cann_python_path)
75+
else:
76+
for path in [
77+
"torch_memory_saver_hook_mode_preload_cu12.abi3.so",
78+
"torch_memory_saver_hook_mode_preload.abi3.so",
79+
]:
80+
dynlib_path = os.path.join(
81+
os.path.dirname(os.path.dirname(torch_memory_saver.__file__)),
82+
path,
83+
)
84+
if os.path.exists(dynlib_path):
85+
break
86+
else:
87+
raise FileNotFoundError(
88+
"Cannot find torch_memory_saver dynamic library. Please make sure torch_memory_saver is properly installed."
89+
)
90+
91+
env_vars["LD_PRELOAD"] = dynlib_path
92+
env_vars["TMS_INIT_ENABLE"] = "1"
93+
env_vars["TMS_INIT_ENABLE_CPU_BACKUP"] = "1"
9494

9595
# We cannot do routing replay for critic.
9696
if self.args.use_routing_replay and self.role == "actor":

vime/ray/rollout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def start_engines(self, port_cursors: dict[int, int] | None = None) -> tuple[lis
131131
if cann_python_path is not None:
132132
prepend_pythonpath(env_vars, cann_python_path)
133133
if self.args.colocate:
134-
env_vars["PYTORCH_NPU_ALLOC_CONF"] = ""
134+
env_vars["PYTORCH_NPU_ALLOC_CONF"] = "expandable_segments:False"
135135
env_vars["VLLM_USE_AOT_COMPILE"] = "0"
136136
rollout_engine = RolloutRayActor.options(
137137
num_cpus=num_cpus,

vime/utils/memory_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def clear_memory(clear_host_memory: bool = False):
1515
else:
1616
torch.cuda.synchronize()
1717
gc.collect()
18-
torch.cuda.empty_cache()
18+
if not is_npu():
19+
torch.cuda.empty_cache()
1920
if is_npu():
2021
try:
2122
torch.npu.empty_cache()

0 commit comments

Comments
 (0)