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 lines678910111211 unmodified lines242526272829169 unmodified lines1992002012022032042052062072082092102112122132142155 unmodified linesfrom dataclasses import dataclassimport inspectfrom threading import Lock, Threadfrom typing import Generator, Iterable, List, Optional, Sequence, Tuple# Heavy libraries (torch, transformers) are imported lazily in functions# to keep application startup snappy.11 unmodified linesreturn getattr(callable_obj, "__func__", callable_obj)def _lazy_import_torch(): # pragma: no cover - indirectionimport torch169 unmodified lines_restore_loader_attr = _orig_load_pretrained_funcdef _patched_load_pretrained_func(cls, model, *args, **kwargs):# Patch model.tie_weights to accept and ignore unexpected kwargsorig_tie_weights = model.tie_weightsdef _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_weightstry:return _orig_load_pretrained_func(cls, model, *args, **kwargs)finally:model.tie_weights = orig_tie_weightsmodeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_patched_load_pretrained_func)try:5 unmodified lines678910111211 unmodified lines2425262728293031323334353637383940414243444546474849505152535455169 unmodified lines2252262272282292302312322332342352365 unmodified linesfrom dataclasses import dataclassimport inspectfrom threading import Lock, Threadfrom 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 linesreturn 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: Nonemodel_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 _restoredef _lazy_import_torch(): # pragma: no cover - indirectionimport torch169 unmodified lines_restore_loader_attr = _orig_load_pretrained_funcdef _patched_load_pretrained_func(cls, model, *args, **kwargs):# 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:restore_tie_weights()modeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_patched_load_pretrained_func)try:
2 unmodified lines345678101 unmodified lines1101111122 unmodified linesimport typesfrom app.core import engine, tokensfrom app.core.prompting import DEFAULT_SYSTEM_PROMPT, render_chat_promptfrom app.schemas.chat import ChatMessage101 unmodified linesunwrapped = engine._unwrap_bound_callable(Demo.loader)assert callable(unwrapped)assert unwrapped.__name__ == "loader"2 unmodified lines345678910101 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331342 unmodified linesimport typesimport pytestfrom app.core import engine, tokensfrom app.core.prompting import DEFAULT_SYSTEM_PROMPT, render_chat_promptfrom app.schemas.chat import ChatMessage101 unmodified linesunwrapped = 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 testreturn "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