worker: fix torchao stub — return stub classes not modules for isinstance()
peft/tuners/lora/torchao.py does: from torchao.dtypes import AffineQuantizedTensor, LinearActivationQuantizedTensor isinstance(weight, (AffineQuantizedTensor, LinearActivationQuantizedTensor)) The stub __getattr__ was returning stub modules, which isinstance() rejects with "arg 2 must be a type, a tuple of types, or a union". Add _StubTypeMeta metaclass whose __instancecheck__ always returns False, and _make_stub_type() to create stub classes via it. Change _make_mod_stub __getattr__ to return stub classes instead of stub modules for leaf attribute access, so isinstance() gets a valid type and returns False. _StubSubpackageFinder still handles import-style subpackage creation (those still need module objects in sys.modules); __getattr__ only fires for from-import or direct attribute access, which are the isinstance paths.
This commit is contained in:
parent
a3c94e7f39
commit
a87077ef69
1 changed files with 27 additions and 3 deletions
|
|
@ -1099,6 +1099,30 @@ def run_training_process(
|
|||
|
||||
_STUB_SENTINEL = object()
|
||||
|
||||
# Metaclass for stub types so that isinstance(x, StubClass) returns False
|
||||
# instead of raising TypeError ("arg 2 must be a type").
|
||||
# peft/tuners/lora/torchao.py does:
|
||||
# from torchao.dtypes import AffineQuantizedTensor, LinearActivationQuantizedTensor
|
||||
# isinstance(weight, (AffineQuantizedTensor, LinearActivationQuantizedTensor))
|
||||
# If those names resolve to stub modules rather than types, isinstance() raises.
|
||||
class _StubTypeMeta(type):
|
||||
def __instancecheck__(cls, instance):
|
||||
return False
|
||||
def __subclasscheck__(cls, subclass):
|
||||
return False
|
||||
def __getattr__(cls, attr):
|
||||
if attr.startswith("__"):
|
||||
raise AttributeError(attr)
|
||||
child = _StubTypeMeta(attr, (), {})
|
||||
setattr(cls, attr, child)
|
||||
return child
|
||||
def __call__(cls, *args, **kwargs):
|
||||
return None
|
||||
|
||||
def _make_stub_type(name):
|
||||
"""Stub class: accepted by isinstance() (always False), supports attr access."""
|
||||
return _StubTypeMeta(name, (), {})
|
||||
|
||||
def _make_mod_stub(mod_name):
|
||||
m = _types.ModuleType(mod_name)
|
||||
m.__path__ = []
|
||||
|
|
@ -1108,9 +1132,9 @@ def run_training_process(
|
|||
def _ga(attr, _m=m, _n=mod_name):
|
||||
if attr.startswith("__"):
|
||||
raise AttributeError(attr)
|
||||
child_name = f"{_n}.{attr}"
|
||||
child = _make_mod_stub(child_name)
|
||||
sys.modules.setdefault(child_name, child)
|
||||
# Return a stub CLASS (not a module) so that isinstance(x, attr)
|
||||
# works and returns False instead of raising TypeError.
|
||||
child = _make_stub_type(f"{_n}.{attr}")
|
||||
setattr(_m, attr, child)
|
||||
return child
|
||||
m.__getattr__ = _ga
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue