[misc] add forward context for attention (#9029)

This commit is contained in:
youkaichao
2024-10-03 12:09:42 -07:00
committed by GitHub
parent 63e39937f9
commit 9aaf14c62e
8 changed files with 242 additions and 326 deletions

22
vllm/forward_context.py Normal file
View File

@@ -0,0 +1,22 @@
from contextlib import contextmanager
from typing import Any
_forward_context: Any = None
def get_forward_context() -> Any:
"""Get the current forward context."""
return _forward_context
@contextmanager
def set_forward_context(context: Any):
"""A context manager that stores the current forward context,
can be attention metadata, etc."""
global _forward_context
prev_context = _forward_context
_forward_context = context
try:
yield
finally:
_forward_context = prev_context