Spaces:
Running
Running
Kyryll Kochkin commited on
Commit ·
4e35cfb
1
Parent(s): 45e732f
AI tries to fix it's own tests v2
Browse files- app/core/engine.py +30 -9
- tests/test_core_helpers.py +22 -0
app/core/engine.py
CHANGED
|
@@ -6,7 +6,7 @@ import time
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
import inspect
|
| 8 |
from threading import Lock, Thread
|
| 9 |
-
from typing import Generator, Iterable, List, Optional, Sequence, Tuple
|
| 10 |
|
| 11 |
# Heavy libraries (torch, transformers) are imported lazily in functions
|
| 12 |
# to keep application startup snappy.
|
|
@@ -24,6 +24,32 @@ def _unwrap_bound_callable(callable_obj):
|
|
| 24 |
return getattr(callable_obj, "__func__", callable_obj)
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def _lazy_import_torch(): # pragma: no cover - indirection
|
| 28 |
import torch
|
| 29 |
|
|
@@ -199,17 +225,12 @@ class _ModelHandle:
|
|
| 199 |
_restore_loader_attr = _orig_load_pretrained_func
|
| 200 |
|
| 201 |
def _patched_load_pretrained_func(cls, model, *args, **kwargs):
|
| 202 |
-
# Patch
|
| 203 |
-
|
| 204 |
-
def _compat_tie_weights(*tw_args, **tw_kwargs):
|
| 205 |
-
tw_kwargs.pop("missing_keys", None)
|
| 206 |
-
tw_kwargs.pop("recompute_mapping", None)
|
| 207 |
-
return orig_tie_weights(*tw_args, **tw_kwargs)
|
| 208 |
-
model.tie_weights = _compat_tie_weights
|
| 209 |
try:
|
| 210 |
return _orig_load_pretrained_func(cls, model, *args, **kwargs)
|
| 211 |
finally:
|
| 212 |
-
|
| 213 |
|
| 214 |
modeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_patched_load_pretrained_func)
|
| 215 |
try:
|
|
|
|
| 6 |
from dataclasses import dataclass
|
| 7 |
import inspect
|
| 8 |
from threading import Lock, Thread
|
| 9 |
+
from typing import Callable, Generator, Iterable, List, Optional, Sequence, Tuple
|
| 10 |
|
| 11 |
# Heavy libraries (torch, transformers) are imported lazily in functions
|
| 12 |
# to keep application startup snappy.
|
|
|
|
| 24 |
return getattr(callable_obj, "__func__", callable_obj)
|
| 25 |
|
| 26 |
|
| 27 |
+
def _install_tie_weights_compat_patch(model) -> Callable[[], None]:
|
| 28 |
+
"""Patch model class tie_weights to ignore newer unexpected kwargs."""
|
| 29 |
+
if not hasattr(model, "tie_weights"):
|
| 30 |
+
return lambda: None
|
| 31 |
+
|
| 32 |
+
model_cls = model.__class__
|
| 33 |
+
had_local_attr = "tie_weights" in model_cls.__dict__
|
| 34 |
+
original_attr = model_cls.__dict__.get("tie_weights")
|
| 35 |
+
original_callable = _unwrap_bound_callable(getattr(model_cls, "tie_weights"))
|
| 36 |
+
|
| 37 |
+
def _compat_tie_weights(self, *args, **kwargs):
|
| 38 |
+
kwargs.pop("missing_keys", None)
|
| 39 |
+
kwargs.pop("recompute_mapping", None)
|
| 40 |
+
return original_callable(self, *args, **kwargs)
|
| 41 |
+
|
| 42 |
+
setattr(model_cls, "tie_weights", _compat_tie_weights)
|
| 43 |
+
|
| 44 |
+
def _restore() -> None:
|
| 45 |
+
if had_local_attr:
|
| 46 |
+
setattr(model_cls, "tie_weights", original_attr)
|
| 47 |
+
else:
|
| 48 |
+
delattr(model_cls, "tie_weights")
|
| 49 |
+
|
| 50 |
+
return _restore
|
| 51 |
+
|
| 52 |
+
|
| 53 |
def _lazy_import_torch(): # pragma: no cover - indirection
|
| 54 |
import torch
|
| 55 |
|
|
|
|
| 225 |
_restore_loader_attr = _orig_load_pretrained_func
|
| 226 |
|
| 227 |
def _patched_load_pretrained_func(cls, model, *args, **kwargs):
|
| 228 |
+
# Patch tie_weights to absorb kwargs passed by newer transformers.
|
| 229 |
+
restore_tie_weights = _install_tie_weights_compat_patch(model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
try:
|
| 231 |
return _orig_load_pretrained_func(cls, model, *args, **kwargs)
|
| 232 |
finally:
|
| 233 |
+
restore_tie_weights()
|
| 234 |
|
| 235 |
modeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_patched_load_pretrained_func)
|
| 236 |
try:
|
tests/test_core_helpers.py
CHANGED
|
@@ -3,6 +3,8 @@ from __future__ import annotations
|
|
| 3 |
|
| 4 |
import types
|
| 5 |
|
|
|
|
|
|
|
| 6 |
from app.core import engine, tokens
|
| 7 |
from app.core.prompting import DEFAULT_SYSTEM_PROMPT, render_chat_prompt
|
| 8 |
from app.schemas.chat import ChatMessage
|
|
@@ -110,3 +112,23 @@ def test_unwrap_bound_callable_extracts_underlying_method_function() -> None:
|
|
| 110 |
unwrapped = engine._unwrap_bound_callable(Demo.loader)
|
| 111 |
assert callable(unwrapped)
|
| 112 |
assert unwrapped.__name__ == "loader"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import types
|
| 5 |
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
from app.core import engine, tokens
|
| 9 |
from app.core.prompting import DEFAULT_SYSTEM_PROMPT, render_chat_prompt
|
| 10 |
from app.schemas.chat import ChatMessage
|
|
|
|
| 112 |
unwrapped = engine._unwrap_bound_callable(Demo.loader)
|
| 113 |
assert callable(unwrapped)
|
| 114 |
assert unwrapped.__name__ == "loader"
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def test_install_tie_weights_compat_patch_strips_unexpected_kwargs() -> None:
|
| 118 |
+
class Demo:
|
| 119 |
+
def tie_weights(self): # pragma: no cover - shape-only test
|
| 120 |
+
return "ok"
|
| 121 |
+
|
| 122 |
+
demo = Demo()
|
| 123 |
+
|
| 124 |
+
with pytest.raises(TypeError):
|
| 125 |
+
demo.tie_weights(missing_keys=set(), recompute_mapping=False)
|
| 126 |
+
|
| 127 |
+
restore = engine._install_tie_weights_compat_patch(demo)
|
| 128 |
+
try:
|
| 129 |
+
assert demo.tie_weights(missing_keys=set(), recompute_mapping=False) == "ok"
|
| 130 |
+
finally:
|
| 131 |
+
restore()
|
| 132 |
+
|
| 133 |
+
with pytest.raises(TypeError):
|
| 134 |
+
demo.tie_weights(missing_keys=set(), recompute_mapping=False)
|