patch_vllm_sm120_cooperative_topk.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. """Restrict vLLM's cooperative sparse-attention top-k path to SM90."""
  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/model_executor/layers/sparse_attn_indexer.py"
  7. old = ''' use_cooperative_topk = (
  8. current_platform.is_cuda()
  9. and topk_tokens in (512, 1024, 2048)
  10. and num_rows <= 32
  11. and logits.stride(0) % 4 == 0 # TMA 16-byte alignment
  12. and current_platform.has_device_capability(90)
  13. )'''
  14. new = ''' device_capability = current_platform.get_device_capability()
  15. use_cooperative_topk = (
  16. current_platform.is_cuda()
  17. and topk_tokens in (512, 1024, 2048)
  18. and num_rows <= 32
  19. and logits.stride(0) % 4 == 0 # TMA 16-byte alignment
  20. and device_capability is not None
  21. and device_capability.to_int() == 90
  22. )'''
  23. if not target.exists():
  24. print(f"{target} not found; skipping SM120 cooperative_topk workaround")
  25. else:
  26. text = target.read_text()
  27. if "device_capability.to_int() == 90" in text:
  28. print("SM120 cooperative_topk workaround already present; skipping")
  29. elif old in text:
  30. target.write_text(text.replace(old, new, 1))
  31. print("Applied SM120 cooperative_topk workaround")
  32. else:
  33. print("Known cooperative_topk selector pattern not found; skipping")