7676 - reference
7777"""
7878
79+ import os
80+
7981from .types import OpImpl , BackendImplKind , BackendPriority , match_token
8082from .registry import OpRegistry , OpRegistrySnapshot
8183from .policy import (
8284 SelectionPolicy ,
8385 PolicyManager ,
8486 get_policy ,
87+ get_policy_epoch ,
8588 set_global_policy ,
8689 reset_global_policy ,
8790 policy_context ,
108111 enable_io_dump ,
109112 disable_io_dump ,
110113 io_dump_step ,
114+ is_dump_enabled ,
111115)
112116from .io_common import list_model_layers , register_tensor_stat , tensor_stats
113117
@@ -139,6 +143,94 @@ def resolve_op(op_name: str):
139143 return get_default_manager ().resolve (op_name )
140144
141145
146+ # Fast-path opt-out: set VLLM_FL_OP_FAST_PATH=0 to disable per-op fn caching
147+ # in hot OOT layers and route every call back through OpManager.call.
148+ _OP_FAST_PATH_ENABLED = os .environ .get ("VLLM_FL_OP_FAST_PATH" , "1" ) == "1"
149+
150+
151+ class CachedOp :
152+ """Resolve an op once at the call site and refresh on policy changes.
153+
154+ OpManager.call preserves fallback and IO-dump hooks, but it also pays the
155+ manager/fallback path on every invocation. Hot layer paths can use CachedOp
156+ to call the resolved implementation directly after the first lookup.
157+
158+ The cache is invalidated by both OpManager.policy_epoch and
159+ PolicyManager.policy_epoch. The latter matters for policy_context() and
160+ set_global_policy(), which can change the effective backend without
161+ touching the OpManager instance.
162+
163+ Cache refresh is best-effort under concurrent calls. If another thread
164+ changes policy at the same time, a call may observe the previous impl once
165+ before the next epoch check refreshes it.
166+ """
167+
168+ __slots__ = (
169+ "_op_name" ,
170+ "_impl" ,
171+ "_use_manager_call" ,
172+ "_manager_id" ,
173+ "_manager_epoch" ,
174+ "_policy_epoch" ,
175+ )
176+
177+ def __init__ (self , op_name : str ) -> None :
178+ self ._op_name = op_name
179+ self ._impl = None
180+ self ._use_manager_call = False
181+ self ._manager_id = - 1
182+ self ._manager_epoch = - 1
183+ self ._policy_epoch = - 1
184+
185+ def __call__ (self , * args , ** kwargs ):
186+ mgr = get_default_manager ()
187+
188+ if not _OP_FAST_PATH_ENABLED :
189+ return mgr .call (self ._op_name , * args , ** kwargs )
190+
191+ if is_dump_enabled ():
192+ return mgr .call (self ._op_name , * args , ** kwargs )
193+
194+ manager_epoch = mgr .policy_epoch
195+ manager_id = id (mgr )
196+ policy_epoch = get_policy_epoch ()
197+ if (
198+ self ._manager_id != manager_id
199+ or self ._manager_epoch != manager_epoch
200+ or self ._policy_epoch != policy_epoch
201+ ):
202+ self ._impl = None
203+ self ._use_manager_call = False
204+
205+ if self ._use_manager_call :
206+ return mgr .call (self ._op_name , * args , ** kwargs )
207+
208+ impl = self ._impl
209+ if (
210+ impl is None
211+ or self ._manager_id != manager_id
212+ or self ._manager_epoch != manager_epoch
213+ or self ._policy_epoch != policy_epoch
214+ ):
215+ impl = mgr ._resolve_impl (self ._op_name )
216+ mgr ._record_first_use (self ._op_name , impl )
217+ self ._impl = impl
218+ # resolve() can initialize the manager and bump its epoch.
219+ self ._manager_id = manager_id
220+ self ._manager_epoch = mgr .policy_epoch
221+ self ._policy_epoch = get_policy_epoch ()
222+
223+ try :
224+ return impl .fn (* args , ** kwargs )
225+ except Exception :
226+ self ._impl = None
227+ if get_policy ().strict :
228+ raise
229+ mgr ._mark_failed_impl (self ._op_name , impl .impl_id )
230+ self ._use_manager_call = True
231+ return mgr .call (self ._op_name , * args , ** kwargs )
232+
233+
142234__all__ = [
143235 # Types
144236 "OpImpl" ,
@@ -152,6 +244,7 @@ def resolve_op(op_name: str):
152244 "SelectionPolicy" ,
153245 "PolicyManager" ,
154246 "get_policy" ,
247+ "get_policy_epoch" ,
155248 "set_global_policy" ,
156249 "reset_global_policy" ,
157250 "policy_context" ,
@@ -188,4 +281,5 @@ def resolve_op(op_name: str):
188281 # Convenience functions
189282 "call_op" ,
190283 "resolve_op" ,
284+ "CachedOp" ,
191285]
0 commit comments