-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathunit_dumpers.py
More file actions
360 lines (274 loc) · 13.1 KB
/
unit_dumpers.py
File metadata and controls
360 lines (274 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from __future__ import annotations
import importlib
import json
import logging
from typing import TYPE_CHECKING, Any, TypeVar
import aiofiles
import joblib
import numpy as np
import numpy.typing as npt
import peft
from pydantic import BaseModel
from sklearn.base import BaseEstimator
from autointent import Embedder, Ranker, VectorIndex
from autointent._utils import require
from autointent._wrappers import BaseTorchModule
from autointent.schemas import TagsList
from .base import BaseObjectDumper, ModuleSimpleAttributes
if TYPE_CHECKING:
from pathlib import Path
from catboost import CatBoostClassifier
from peft import PeftModel
from transformers import PreTrainedModel, PreTrainedTokenizer, PreTrainedTokenizerFast
T = TypeVar("T")
logger = logging.getLogger(__name__)
class TagsListDumper(BaseObjectDumper[TagsList]):
dir_or_file_name = "tags"
@staticmethod
def dump(obj: TagsList, path: Path, exists_ok: bool) -> None: # noqa: ARG004
obj.dump(path)
@staticmethod
def load(path: Path, **kwargs: Any) -> TagsList: # noqa: ANN401
return TagsList.load(path)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, TagsList)
class SimpleAttributesDumper(BaseObjectDumper[dict[str, ModuleSimpleAttributes]]):
dir_or_file_name = "simple_attrs.json"
@staticmethod
def dump(obj: dict[str, ModuleSimpleAttributes], path: Path, exists_ok: bool) -> None: # noqa: ARG004
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as file:
json.dump(obj, file, ensure_ascii=False, indent=4)
@staticmethod
def load(path: Path, **kwargs: Any) -> dict[str, ModuleSimpleAttributes]: # noqa: ANN401
with path.open(encoding="utf-8") as file:
return json.load(file) # type: ignore[no-any-return]
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401, ARG003
# This dumper is used for collections of simple attributes, not individual objects
# It should not match individual objects in the main loop
return False
class ArraysDumper(BaseObjectDumper[dict[str, npt.NDArray[Any]]]):
dir_or_file_name = "arrays.npz"
@staticmethod
def dump(obj: dict[str, npt.NDArray[Any]], path: Path, exists_ok: bool) -> None: # noqa: ARG004
np.savez(path, allow_pickle=False, **obj)
@staticmethod
def load(path: Path, **kwargs: Any) -> dict[str, npt.NDArray[Any]]: # noqa: ANN401
return dict(np.load(path))
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401, ARG003
# This dumper is used for collections of arrays, not individual objects
# It should not match individual objects in the main loop
return False
class EmbedderDumper(BaseObjectDumper[Embedder]):
dir_or_file_name = "embedders"
@staticmethod
def dump(obj: Embedder, path: Path, exists_ok: bool) -> None: # noqa: ARG004
obj.dump(path)
@staticmethod
def load(path: Path, **kwargs: Any) -> Embedder: # noqa: ANN401
embedder_config = kwargs.get("embedder_config")
return Embedder.load(path, override_config=embedder_config)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, Embedder)
class VectorIndexDumper(BaseObjectDumper[VectorIndex]):
dir_or_file_name = "vector_indexes"
@staticmethod
def dump(obj: VectorIndex, path: Path, exists_ok: bool) -> None: # noqa: ARG004
obj.dump(path)
@staticmethod
def load(path: Path, **kwargs: Any) -> VectorIndex: # noqa: ANN401
return VectorIndex.load(path)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, VectorIndex)
class EstimatorDumper(BaseObjectDumper[BaseEstimator]):
dir_or_file_name = "estimators"
@staticmethod
def dump(obj: BaseEstimator, path: Path, exists_ok: bool) -> None: # noqa: ARG004
path.parent.mkdir(parents=True, exist_ok=True)
joblib.dump(obj, path)
@staticmethod
def load(path: Path, **kwargs: Any) -> BaseEstimator: # noqa: ANN401
return joblib.load(path)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, BaseEstimator)
class RankerDumper(BaseObjectDumper[Ranker]):
dir_or_file_name = "cross_encoders"
@staticmethod
def dump(obj: Ranker, path: Path, exists_ok: bool) -> None: # noqa: ARG004
obj.save(str(path))
@staticmethod
def load(path: Path, **kwargs: Any) -> Ranker: # noqa: ANN401
cross_encoder_config = kwargs.get("cross_encoder_config")
return Ranker.load(path, override_config=cross_encoder_config)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, Ranker)
class PydanticModelDumper(BaseObjectDumper[BaseModel]):
dir_or_file_name = "pydantic"
@staticmethod
def dump(obj: BaseModel, path: Path, exists_ok: bool) -> None:
class_info = {"name": obj.__class__.__name__, "module": obj.__class__.__module__}
path.mkdir(parents=True, exist_ok=exists_ok)
with (path / "class_info.json").open("w", encoding="utf-8") as file:
json.dump(class_info, file, ensure_ascii=False, indent=4)
with (path / "model_dump.json").open("w", encoding="utf-8") as file:
json.dump(obj.model_dump(), file, ensure_ascii=False, indent=4)
@staticmethod
async def dump_async(obj: BaseModel, path: Path, exists_ok: bool) -> None:
class_info = {"name": obj.__class__.__name__, "module": obj.__class__.__module__}
path.mkdir(parents=True, exist_ok=exists_ok) # noqa: ASYNC240
async with aiofiles.open(path / "class_info.json", mode="w", encoding="utf-8") as file:
await file.write(json.dumps(class_info, ensure_ascii=False, indent=4))
async with aiofiles.open(path / "model_dump.json", mode="w", encoding="utf-8") as file:
await file.write(json.dumps(obj.model_dump(), ensure_ascii=False, indent=4))
@staticmethod
def load(path: Path, **kwargs: Any) -> BaseModel: # noqa: ANN401
with (path / "model_dump.json").open("r", encoding="utf-8") as file:
content = json.load(file)
with (path / "class_info.json").open("r", encoding="utf-8") as file:
class_info = json.load(file)
model_type = importlib.import_module(class_info["module"])
model_type = getattr(model_type, class_info["name"])
return model_type.model_validate(content) # type: ignore[no-any-return]
@staticmethod
async def load_async(path: Path, **kwargs: Any) -> BaseModel: # noqa: ANN401
async with aiofiles.open(path / "model_dump.json", encoding="utf-8") as file:
content_str = await file.read()
content = json.loads(content_str)
async with aiofiles.open(path / "class_info.json", encoding="utf-8") as file:
class_info_str = await file.read()
class_info = json.loads(class_info_str)
model_type = importlib.import_module(class_info["module"])
model_type = getattr(model_type, class_info["name"])
return model_type.model_validate(content) # type: ignore[no-any-return]
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, BaseModel)
class PeftModelDumper(BaseObjectDumper["PeftModel"]):
dir_or_file_name = "peft_models"
@staticmethod
def dump(obj: PeftModel, path: Path, exists_ok: bool) -> None:
path.mkdir(parents=True, exist_ok=exists_ok)
if obj._is_prompt_learning: # noqa: SLF001
# strategy to save prompt learning models: save prompt encoder and bert classifier separately
ptuning_path = path / "ptuning"
ptuning_path.mkdir(parents=True, exist_ok=exists_ok)
obj.save_pretrained(str(ptuning_path / "peft"))
obj.base_model.save_pretrained(ptuning_path / "base_model")
else:
# strategy to save lora models: merge adapters and save as usual hugging face model
lora_path = path / "lora"
lora_path.mkdir(parents=True, exist_ok=exists_ok)
merged_model: PreTrainedModel = obj.merge_and_unload()
merged_model.save_pretrained(lora_path)
@staticmethod
def load(path: Path, **kwargs: Any) -> PeftModel: # noqa: ANN401
require("peft", extra="peft")
require("transformers", extra="transformers")
import transformers
if (path / "ptuning").exists():
# prompt learning model
ptuning_path = path / "ptuning"
model = transformers.AutoModelForSequenceClassification.from_pretrained(ptuning_path / "base_model")
return peft.PeftModel.from_pretrained(model, ptuning_path / "peft")
if (path / "lora").exists():
# merged lora model
lora_path = path / "lora"
return transformers.AutoModelForSequenceClassification.from_pretrained(lora_path) # type: ignore[no-any-return]
msg = f"Invalid PeftModel directory structure at {path}. Expected 'ptuning' or 'lora' subdirectory."
raise ValueError(msg)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
try:
require("peft", extra="peft")
import peft
return isinstance(obj, peft.PeftModel)
except ImportError:
return False
class HFModelDumper(BaseObjectDumper["PreTrainedModel"]):
dir_or_file_name = "hf_models"
@staticmethod
def dump(obj: PreTrainedModel, path: Path, exists_ok: bool) -> None:
path.mkdir(parents=True, exist_ok=exists_ok)
obj.save_pretrained(path)
@staticmethod
def load(path: Path, **kwargs: Any) -> PreTrainedModel: # noqa: ANN401
require("transformers", extra="transformers")
import transformers
return transformers.AutoModelForSequenceClassification.from_pretrained(path) # type: ignore[no-any-return]
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
try:
require("transformers", extra="transformers")
import transformers
return isinstance(obj, transformers.PreTrainedModel)
except ImportError:
return False
class HFTokenizerDumper(BaseObjectDumper["PreTrainedTokenizer | PreTrainedTokenizerFast"]):
dir_or_file_name = "hf_tokenizers"
@staticmethod
def dump(obj: PreTrainedTokenizer | PreTrainedTokenizerFast, path: Path, exists_ok: bool) -> None:
path.mkdir(parents=True, exist_ok=exists_ok)
obj.save_pretrained(path)
@staticmethod
def load(path: Path, **kwargs: Any) -> PreTrainedTokenizer | PreTrainedTokenizerFast: # noqa: ANN401
require("transformers", extra="transformers")
import transformers
return transformers.AutoTokenizer.from_pretrained(path) # type: ignore[no-any-return,no-untyped-call]
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
try:
require("transformers", extra="transformers")
import transformers
return isinstance(obj, transformers.PreTrainedTokenizer | transformers.PreTrainedTokenizerFast)
except ImportError:
return False
class TorchModelDumper(BaseObjectDumper[BaseTorchModule]):
dir_or_file_name = "torch_models"
@staticmethod
def dump(obj: BaseTorchModule, path: Path, exists_ok: bool) -> None:
path.mkdir(parents=True, exist_ok=exists_ok)
class_info = {
"module": obj.__class__.__module__,
"name": obj.__class__.__name__,
}
with (path / "class_info.json").open("w") as f:
json.dump(class_info, f)
obj.dump(path)
@staticmethod
def load(path: Path, **kwargs: Any) -> BaseTorchModule: # noqa: ANN401
with (path / "class_info.json").open("r") as f:
class_info = json.load(f)
module = importlib.import_module(class_info["module"])
model_class: BaseTorchModule = getattr(module, class_info["name"])
return model_class.load(path)
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
return isinstance(obj, BaseTorchModule)
class CatBoostDumper(BaseObjectDumper["CatBoostClassifier"]):
dir_or_file_name = "catboost_models"
@staticmethod
def dump(obj: CatBoostClassifier, path: Path, exists_ok: bool) -> None: # noqa: ARG004
path.parent.mkdir(parents=True, exist_ok=True)
obj.save_model(str(path), format="cbm")
@staticmethod
def load(path: Path, **kwargs: Any) -> CatBoostClassifier: # noqa: ANN401
require("catboost", extra="catboost")
from catboost import CatBoostClassifier
model = CatBoostClassifier()
model.load_model(str(path))
return model
@classmethod
def check_isinstance(cls, obj: Any) -> bool: # noqa: ANN401
try:
require("catboost", extra="catboost")
from catboost import CatBoostClassifier
return isinstance(obj, CatBoostClassifier)
except ImportError:
return False