Files
OBLITERATUS/obliteratus/recurrent_memory_backend.py
Joseph Magly 63c8300887 fix(admission): inspect all recurrent callsites
Qualify all four Qwen scan and convolution fallbacks and reject Hub replacements on every model submodule. Keep unbounded compiled kernels unknown in strict mode and document the remaining source and hardware qualification gaps.

Refs #384
2026-10-28 22:46:13 -04:00

73 lines
3.0 KiB
Python

"""Qualify Qwen scan and convolution callsites without loading or running kernels."""
import inspect
import sys
_QWEN_CALLS = (
"torch_chunk_gated_delta_rule", "torch_recurrent_gated_delta_rule",
"causal_conv1d_fn", "causal_conv1d_update",
)
def _local_function(function, owner, name):
"""Names preserved by wraps() alone do not identify the executing code."""
return (
inspect.isfunction(function)
and function.__module__ == owner.__name__
and function.__name__ == name
and function.__code__.co_name == name
and function.__code__.co_filename == getattr(owner, "__file__", None)
)
def _torch_fallback(selected, owner, name):
if _local_function(selected, owner, name):
return True
if not inspect.isfunction(selected):
return False
# Transformers freezes optional-package resolution in this decorator. Do
# not blindly unwrap: __wrapped__ describes the fallback even for FLA.
hub = sys.modules.get("transformers.integrations.hub_kernels")
if (selected.__code__.co_filename != getattr(hub, "__file__", None)
or selected.__code__.co_name != "wrapped"):
return False
implementation = inspect.getclosurevars(selected).nonlocals.get("implementation")
return (implementation is getattr(selected, "__wrapped__", None)
and _local_function(implementation, owner, name))
def qwen_recurrent_backend(model, family):
"""Return torch only for the inspected fallback; opaque paths remain unknown.
FLA 0.5.2 dispatches inside nested functions and may autotune at first use.
Neither its version nor the name of its public entry point proves a bound.
See docs/recurrent-memory-admission.md for the outstanding allocation work.
"""
source_family = {"qwen3_5_text": "qwen3_5", "qwen3_5_moe_text": "qwen3_5_moe"}.get(
family, family,
)
expected = f"transformers.models.{source_family}.modeling_{source_family}"
owner = sys.modules.get(expected)
modules = tuple(model.modules())
reason = "cannot identify the installed Transformers callsites"
# Check every module before accepting an owner: a later layer can have a
# Hub replacement even when the first layer still uses the torch fallback.
if getattr(model, "_use_kernels", False) or any(
getattr(module, "_use_kernels", False) for module in modules
):
reason = "model or submodule selects Hub kernels"
elif owner is not None and any(type(module).__module__ == expected for module in modules):
for name in _QWEN_CALLS:
selected = getattr(owner, name, None)
if not _torch_fallback(selected, owner, name):
reason = f"{name} is not the verified Transformers torch fallback"
break
else:
return "torch"
raise ValueError(
f"unmodelled {family} recurrent scan backend: {reason}; "
"only the installed Transformers torch scan/convolution fallback workspace is bounded; "
"set OBLITERATUS_PHASE_ADMISSION=off to explicitly bypass admission"
)