Spaces:
Sleeping
Sleeping
Kyryll Kochkin commited on
Commit ·
4e35cfb
1
Parent(s): 45e732f
AI tries to fix it's own tests v2
Browse filesFiles changed (2)
5 unmodified lines6789910111211 unmodified lines2425262728293031323334353637383940414243444546474849505152535455169 unmodified lines2252262272022032042052062072082282292302312322122332342352365 unmodified linesfrom dataclasses import dataclassimport inspectfrom threading import Lock, Threadfrom typing import Generator, Iterable, List, Optional, Sequence, Tuplefrom typing import Callable, Generator, Iterable, List, Optional, Sequence, Tuple
# Heavy libraries (torch, transformers) are imported lazily in functions# to keep application startup snappy.11 unmodified lines return getattr(callable_obj, "__func__", callable_obj)
def _install_tie_weights_compat_patch(model) -> Callable[[], None]: """Patch model class tie_weights to ignore newer unexpected kwargs.""" if not hasattr(model, "tie_weights"): return lambda: None
model_cls = model.__class__ had_local_attr = "tie_weights" in model_cls.__dict__ original_attr = model_cls.__dict__.get("tie_weights") original_callable = _unwrap_bound_callable(getattr(model_cls, "tie_weights"))
def _compat_tie_weights(self, *args, **kwargs): kwargs.pop("missing_keys", None) kwargs.pop("recompute_mapping", None) return original_callable(self, *args, **kwargs)
setattr(model_cls, "tie_weights", _compat_tie_weights)
def _restore() -> None: if had_local_attr: setattr(model_cls, "tie_weights", original_attr) else: delattr(model_cls, "tie_weights")
return _restore
def _lazy_import_torch(): # pragma: no cover - indirection import torch
169 unmodified lines _restore_loader_attr = _orig_load_pretrained_func def _patched_load_pretrained_func(cls, model, *args, **kwargs): # Patch model.tie_weights to accept and ignore unexpected kwargs orig_tie_weights = model.tie_weights def _compat_tie_weights(*tw_args, **tw_kwargs): tw_kwargs.pop("missing_keys", None) tw_kwargs.pop("recompute_mapping", None) return orig_tie_weights(*tw_args, **tw_kwargs) model.tie_weights = _compat_tie_weights # Patch tie_weights to absorb kwargs passed by newer transformers. restore_tie_weights = _install_tie_weights_compat_patch(model) try: return _orig_load_pretrained_func(cls, model, *args, **kwargs) finally: model.tie_weights = orig_tie_weights restore_tie_weights() modeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_patched_load_pretrained_func) try:2 unmodified lines345678910101 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331342 unmodified lines
import types
import pytest
from app.core import engine, tokensfrom app.core.prompting import DEFAULT_SYSTEM_PROMPT, render_chat_promptfrom app.schemas.chat import ChatMessage101 unmodified lines unwrapped = engine._unwrap_bound_callable(Demo.loader) assert callable(unwrapped) assert unwrapped.__name__ == "loader"
def test_install_tie_weights_compat_patch_strips_unexpected_kwargs() -> None: class Demo: def tie_weights(self): # pragma: no cover - shape-only test return "ok"
demo = Demo()
with pytest.raises(TypeError): demo.tie_weights(missing_keys=set(), recompute_mapping=False)
restore = engine._install_tie_weights_compat_patch(demo) try: assert demo.tie_weights(missing_keys=set(), recompute_mapping=False) == "ok" finally: restore()
with pytest.raises(TypeError): demo.tie_weights(missing_keys=set(), recompute_mapping=False)app/core/engine.py+30-9
tests/test_core_helpers.py+22