Skip to content

Commit 4d4645b

Browse files
adapt(metax): MetaX C550 backend adaptation for vLLM 0.24.0 (#294)
## Background This PR upgrades the MetaX C550 backend from vLLM 0.20.2 to 0.24.0, fixing two regressions that blocked offline inference on MetaX C550 hardware. --- ## Fix 1: Missing `USE_EXP2` parameter in MetaX delta-rule kernel **File:** `vllm_fl/dispatch/backends/vendor/metax/patches/chunk_delta_h.py` vLLM 0.24.0 updated the upstream fla-org `chunk_gated_delta_rule_fwd_kernel_h_blockdim64` kernel signature to add `USE_EXP2: tl.constexpr`. The MetaX-patched copy was not updated accordingly, causing a `TypeError` at kernel dispatch time. **Fix:** Add `USE_EXP2: tl.constexpr = False` to the MetaX kernel signature. The parameter is accepted but unused — MetaX uses its own exp implementation. --- ## Fix 2: Incomplete `torch.accelerator` memory API patch for torch 2.8+metax **File:** `vllm_fl/dispatch/backends/vendor/metax/patches/accelerator_compat.py` vLLM 0.24.0 uses several `torch.accelerator` memory APIs added in PyTorch 2.9+. The MetaX torch distribution ships as `2.8+metax` and is missing these APIs, causing `AttributeError` during worker startup: ``` AttributeError: module 'torch.accelerator' has no attribute 'max_memory_allocated' ``` The existing `accelerator_compat.py` only patched `empty_cache` and missed 5 other APIs. **Fix:** Patch all 6 missing APIs to their `torch.cuda.*` equivalents: `empty_cache`, `memory_stats`, `memory_reserved`, `memory_allocated`, `reset_peak_memory_stats`, `max_memory_allocated` Reference: [MetaX vLLM-metax v0.21.0](https://github.com/MetaX-MACA/vLLM-metax/blob/releases/v0.21.0/vllm_metax/patch/torch_fix/fix_standalone_compile.py) --- ## Testing | Model | Type | TP | dtype | max_model_len | Result | |-------|------|----|-------|---------------|--------| | Qwen3.6-27B | Mamba hybrid | 4 | bfloat16 | 512 | ✅ PASS | | Qwen3.6-35B-A3B | Mamba + MoE (256 experts) | 4 | bfloat16 | 512 | ✅ PASS | Hardware: MetaX C550 × 8, MACA 3.7.0, torch 2.8+metax
1 parent e8f15e6 commit 4d4645b

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,73 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# 2026 - Modified by MetaX Integrated Circuits (Shanghai) Co., Ltd. All Rights Reserved.
33

4-
# -----------------------------------------------------
5-
# Note: torch 2.8+metax does not have torch.accelerator.empty_cache
6-
# (added in PyTorch 2.10). Patch it to use torch.cuda.empty_cache.
7-
# _____________________________________________________
4+
# --------------------------------------------------------------------------------
5+
# Hotfix: torch 2.8+metax is missing several torch.accelerator memory APIs that
6+
# were added in PyTorch 2.9+. Patch them to the equivalent torch.cuda calls.
7+
#
8+
# Guard: only applied when torch version is < 2.9 AND the API is actually absent.
9+
# - Version check avoids silently overriding APIs that exist (and may have changed
10+
# signatures) in torch >= 2.9.
11+
# - hasattr check is the authoritative guard: if the API exists we never touch it,
12+
# regardless of version.
13+
#
14+
# Reference: https://github.com/MetaX-MACA/vLLM-metax/blob/releases/v0.21.0/vllm_metax/patch/torch_fix/fix_standalone_compile.py
15+
# TODO: remove when MetaX ships torch >= 2.9 with full accelerator API support.
16+
# --------------------------------------------------------------------------------
17+
18+
import logging
19+
from typing import Tuple
820

921
import torch
1022

23+
logger = logging.getLogger(__name__)
24+
25+
26+
def _torch_version_tuple() -> Tuple[int, ...]:
27+
"""Return (major, minor) of torch version, ignoring vendor suffixes like '+metax'."""
28+
ver = torch.__version__.split("+")[0] # strip "+metax", "+cu124", etc.
29+
parts = ver.split(".")[:2]
30+
try:
31+
return tuple(int(p) for p in parts)
32+
except ValueError:
33+
return (0, 0)
34+
35+
36+
_torch_ver = _torch_version_tuple()
37+
38+
# Only apply this patch on torch < 2.9. On torch >= 2.9, these APIs should exist
39+
# natively; patching them would risk silently overriding a potentially changed
40+
# signature or implementation.
41+
if _torch_ver < (2, 9):
42+
_MISSING_APIS = {
43+
"empty_cache": torch.cuda.empty_cache,
44+
"memory_stats": torch.cuda.memory_stats,
45+
"memory_reserved": torch.cuda.memory_reserved,
46+
"memory_allocated": torch.cuda.memory_allocated,
47+
"reset_peak_memory_stats": torch.cuda.reset_peak_memory_stats,
48+
"max_memory_allocated": torch.cuda.max_memory_allocated,
49+
}
1150

12-
if not hasattr(torch.accelerator, "empty_cache"):
13-
torch.accelerator.empty_cache = torch.cuda.empty_cache
51+
for _name, _impl in _MISSING_APIS.items():
52+
if not hasattr(torch.accelerator, _name):
53+
setattr(torch.accelerator, _name, _impl)
54+
logger.debug(
55+
"accelerator_compat: patched torch.accelerator.%s -> torch.cuda.%s "
56+
"(torch %s lacks this API)",
57+
_name, _name, torch.__version__,
58+
)
59+
else:
60+
# Sanity check: warn if any expected API is still missing on torch >= 2.9,
61+
# which would indicate an unexpected regression.
62+
_EXPECTED_APIS = [
63+
"empty_cache", "memory_stats", "memory_reserved",
64+
"memory_allocated", "reset_peak_memory_stats", "max_memory_allocated",
65+
]
66+
for _name in _EXPECTED_APIS:
67+
if not hasattr(torch.accelerator, _name):
68+
logger.warning(
69+
"accelerator_compat: torch.accelerator.%s is missing on torch %s "
70+
"(expected it to exist on >= 2.9). "
71+
"Please update this patch file.",
72+
_name, torch.__version__,
73+
)

vllm_fl/dispatch/backends/vendor/metax/patches/chunk_delta_h.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def chunk_gated_delta_rule_fwd_kernel_h_blockdim64(
6868
STORE_FINAL_STATE: tl.constexpr,
6969
SAVE_NEW_VALUE: tl.constexpr,
7070
IS_VARLEN: tl.constexpr,
71+
USE_EXP2: tl.constexpr = False, # accepted but unused on MetaX
7172
):
7273
i_v, i_nh = tl.program_id(0), tl.program_id(1)
7374
i_n, i_h = i_nh // H, i_nh % H

0 commit comments

Comments
 (0)