patch_vllm_topk_softplus_sqrt_control_flow.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. """Fix the misplaced XPU-only return introduced by vLLM PR #49408."""
  3. import sys
  4. from pathlib import Path
  5. source_root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path.cwd()
  6. target = source_root / "vllm/_custom_ops.py"
  7. function_marker = "def topk_hash_softplus_sqrt("
  8. direct_call = "\n torch.ops._moe_C.topk_softplus_sqrt("
  9. misplaced_return = "\n\n return" + direct_call
  10. fixed_return = "\n return\n" + direct_call
  11. if not target.exists():
  12. raise SystemExit(f"{target} not found; cannot inspect vLLM PR #49408 regression")
  13. text = target.read_text()
  14. start = text.find(function_marker)
  15. if start == -1:
  16. print("topk_hash_softplus_sqrt is absent; vLLM PR #49408 is not applicable")
  17. else:
  18. end = text.find("\ndef ", start + len(function_marker))
  19. end = len(text) if end == -1 else end
  20. function = text[start:end]
  21. if "is_padding" not in function:
  22. print("topk_hash_softplus_sqrt predates vLLM PR #49408; skipping workaround")
  23. elif fixed_return in function:
  24. print("vLLM topk_softplus_sqrt non-XPU path already fixed; skipping")
  25. elif (
  26. direct_call in function
  27. and "current_platform.is_xpu()" not in function
  28. and "\n return" not in function
  29. ):
  30. print("topk_hash_softplus_sqrt has no XPU workaround; patch is not applicable")
  31. elif function.count(misplaced_return) == 1:
  32. function = function.replace(misplaced_return, fixed_return, 1)
  33. updated = text[:start] + function + text[end:]
  34. compile(updated, str(target), "exec")
  35. target.write_text(updated)
  36. print("Applied vLLM PR #49452 topk_softplus_sqrt control-flow fix")
  37. else:
  38. raise SystemExit(
  39. "Unknown is_padding-aware topk_hash_softplus_sqrt layout; "
  40. "refusing to guess whether vLLM PR #49408 is fixed"
  41. )