isa-model 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- isa_model/eval/__init__.py +56 -0
- isa_model/eval/benchmarks.py +469 -0
- isa_model/eval/factory.py +582 -0
- isa_model/eval/metrics.py +628 -0
- isa_model/training/__init__.py +44 -0
- isa_model/training/factory.py +393 -0
- {isa_model-0.0.1.dist-info → isa_model-0.0.2.dist-info}/METADATA +1 -1
- {isa_model-0.0.1.dist-info → isa_model-0.0.2.dist-info}/RECORD +11 -5
- {isa_model-0.0.1.dist-info → isa_model-0.0.2.dist-info}/WHEEL +0 -0
- {isa_model-0.0.1.dist-info → isa_model-0.0.2.dist-info}/licenses/LICENSE +0 -0
- {isa_model-0.0.1.dist-info → isa_model-0.0.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,393 @@
|
|
1
|
+
"""
|
2
|
+
Unified Training Factory for ISA Model Framework
|
3
|
+
|
4
|
+
This factory provides a single interface for all training operations:
|
5
|
+
- LLM fine-tuning (SFT, DPO, RLHF)
|
6
|
+
- Image model training (Flux, LoRA)
|
7
|
+
- Model evaluation and benchmarking
|
8
|
+
"""
|
9
|
+
|
10
|
+
import os
|
11
|
+
import logging
|
12
|
+
from typing import Optional, Dict, Any, Union, List
|
13
|
+
from pathlib import Path
|
14
|
+
import datetime
|
15
|
+
|
16
|
+
from .engine.llama_factory import LlamaFactory, TrainingStrategy, DatasetFormat
|
17
|
+
from .engine.llama_factory.config import SFTConfig, RLConfig, DPOConfig
|
18
|
+
|
19
|
+
logger = logging.getLogger(__name__)
|
20
|
+
|
21
|
+
|
22
|
+
class TrainingFactory:
|
23
|
+
"""
|
24
|
+
Unified factory for all AI model training operations.
|
25
|
+
|
26
|
+
This class provides simplified interfaces for:
|
27
|
+
- LLM training using LlamaFactory
|
28
|
+
- Image model training using Flux/LoRA
|
29
|
+
- Model evaluation and benchmarking
|
30
|
+
|
31
|
+
Example usage for fine-tuning Gemma 3:4B:
|
32
|
+
```python
|
33
|
+
from isa_model.training import TrainingFactory
|
34
|
+
|
35
|
+
factory = TrainingFactory()
|
36
|
+
|
37
|
+
# Fine-tune with your dataset
|
38
|
+
model_path = factory.finetune_llm(
|
39
|
+
model_name="google/gemma-2-4b-it",
|
40
|
+
dataset_path="path/to/your/data.json",
|
41
|
+
training_type="sft",
|
42
|
+
use_lora=True,
|
43
|
+
num_epochs=3,
|
44
|
+
batch_size=4,
|
45
|
+
learning_rate=2e-5
|
46
|
+
)
|
47
|
+
|
48
|
+
# Train with DPO for preference optimization
|
49
|
+
dpo_model = factory.train_with_preferences(
|
50
|
+
model_path=model_path,
|
51
|
+
preference_data="path/to/preferences.json",
|
52
|
+
beta=0.1
|
53
|
+
)
|
54
|
+
```
|
55
|
+
"""
|
56
|
+
|
57
|
+
def __init__(self, base_output_dir: Optional[str] = None):
|
58
|
+
"""
|
59
|
+
Initialize the training factory.
|
60
|
+
|
61
|
+
Args:
|
62
|
+
base_output_dir: Base directory for all training outputs
|
63
|
+
"""
|
64
|
+
self.base_output_dir = base_output_dir or os.path.join(os.getcwd(), "training_outputs")
|
65
|
+
os.makedirs(self.base_output_dir, exist_ok=True)
|
66
|
+
|
67
|
+
# Initialize sub-factories
|
68
|
+
self.llm_factory = LlamaFactory(base_output_dir=os.path.join(self.base_output_dir, "llm"))
|
69
|
+
|
70
|
+
logger.info(f"TrainingFactory initialized with output dir: {self.base_output_dir}")
|
71
|
+
|
72
|
+
def _get_output_dir(self, model_name: str, training_type: str) -> str:
|
73
|
+
"""Generate timestamped output directory."""
|
74
|
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
75
|
+
safe_model_name = model_name.replace("/", "_").replace(":", "_")
|
76
|
+
return os.path.join(self.base_output_dir, f"{safe_model_name}_{training_type}_{timestamp}")
|
77
|
+
|
78
|
+
# =================
|
79
|
+
# LLM Training Methods
|
80
|
+
# =================
|
81
|
+
|
82
|
+
def finetune_llm(
|
83
|
+
self,
|
84
|
+
model_name: str,
|
85
|
+
dataset_path: str,
|
86
|
+
training_type: str = "sft",
|
87
|
+
output_dir: Optional[str] = None,
|
88
|
+
dataset_format: str = "alpaca",
|
89
|
+
use_lora: bool = True,
|
90
|
+
batch_size: int = 4,
|
91
|
+
num_epochs: int = 3,
|
92
|
+
learning_rate: float = 2e-5,
|
93
|
+
max_length: int = 1024,
|
94
|
+
lora_rank: int = 8,
|
95
|
+
lora_alpha: int = 16,
|
96
|
+
val_dataset_path: Optional[str] = None,
|
97
|
+
**kwargs
|
98
|
+
) -> str:
|
99
|
+
"""
|
100
|
+
Fine-tune an LLM model.
|
101
|
+
|
102
|
+
Args:
|
103
|
+
model_name: Model identifier (e.g., "google/gemma-2-4b-it", "meta-llama/Llama-2-7b-hf")
|
104
|
+
dataset_path: Path to training dataset
|
105
|
+
training_type: Type of training ("sft", "dpo", "rlhf")
|
106
|
+
output_dir: Custom output directory
|
107
|
+
dataset_format: Dataset format ("alpaca", "sharegpt", "custom")
|
108
|
+
use_lora: Whether to use LoRA for efficient training
|
109
|
+
batch_size: Training batch size
|
110
|
+
num_epochs: Number of training epochs
|
111
|
+
learning_rate: Learning rate
|
112
|
+
max_length: Maximum sequence length
|
113
|
+
lora_rank: LoRA rank parameter
|
114
|
+
lora_alpha: LoRA alpha parameter
|
115
|
+
val_dataset_path: Path to validation dataset (optional)
|
116
|
+
**kwargs: Additional training parameters
|
117
|
+
|
118
|
+
Returns:
|
119
|
+
Path to the trained model
|
120
|
+
|
121
|
+
Example:
|
122
|
+
```python
|
123
|
+
# Fine-tune Gemma 3:4B with your dataset
|
124
|
+
model_path = factory.finetune_llm(
|
125
|
+
model_name="google/gemma-2-4b-it",
|
126
|
+
dataset_path="my_training_data.json",
|
127
|
+
training_type="sft",
|
128
|
+
use_lora=True,
|
129
|
+
num_epochs=3,
|
130
|
+
batch_size=4
|
131
|
+
)
|
132
|
+
```
|
133
|
+
"""
|
134
|
+
if not output_dir:
|
135
|
+
output_dir = self._get_output_dir(model_name, training_type)
|
136
|
+
|
137
|
+
# Convert format string to enum
|
138
|
+
format_map = {
|
139
|
+
"alpaca": DatasetFormat.ALPACA,
|
140
|
+
"sharegpt": DatasetFormat.SHAREGPT,
|
141
|
+
"custom": DatasetFormat.CUSTOM
|
142
|
+
}
|
143
|
+
dataset_format_enum = format_map.get(dataset_format, DatasetFormat.ALPACA)
|
144
|
+
|
145
|
+
if training_type.lower() == "sft":
|
146
|
+
return self.llm_factory.finetune(
|
147
|
+
model_path=model_name,
|
148
|
+
train_data=dataset_path,
|
149
|
+
val_data=val_dataset_path,
|
150
|
+
output_dir=output_dir,
|
151
|
+
dataset_format=dataset_format_enum,
|
152
|
+
use_lora=use_lora,
|
153
|
+
batch_size=batch_size,
|
154
|
+
num_epochs=num_epochs,
|
155
|
+
learning_rate=learning_rate,
|
156
|
+
max_length=max_length,
|
157
|
+
lora_rank=lora_rank,
|
158
|
+
lora_alpha=lora_alpha,
|
159
|
+
**kwargs
|
160
|
+
)
|
161
|
+
else:
|
162
|
+
raise ValueError(f"Training type '{training_type}' not supported yet. Use 'sft' for now.")
|
163
|
+
|
164
|
+
def train_with_preferences(
|
165
|
+
self,
|
166
|
+
model_path: str,
|
167
|
+
preference_data: str,
|
168
|
+
output_dir: Optional[str] = None,
|
169
|
+
reference_model: Optional[str] = None,
|
170
|
+
beta: float = 0.1,
|
171
|
+
use_lora: bool = True,
|
172
|
+
batch_size: int = 4,
|
173
|
+
num_epochs: int = 3,
|
174
|
+
learning_rate: float = 5e-6,
|
175
|
+
val_data: Optional[str] = None,
|
176
|
+
**kwargs
|
177
|
+
) -> str:
|
178
|
+
"""
|
179
|
+
Train model with preference data using DPO.
|
180
|
+
|
181
|
+
Args:
|
182
|
+
model_path: Path to the base model
|
183
|
+
preference_data: Path to preference dataset
|
184
|
+
output_dir: Custom output directory
|
185
|
+
reference_model: Reference model for DPO (optional)
|
186
|
+
beta: DPO beta parameter
|
187
|
+
use_lora: Whether to use LoRA
|
188
|
+
batch_size: Training batch size
|
189
|
+
num_epochs: Number of epochs
|
190
|
+
learning_rate: Learning rate
|
191
|
+
val_data: Validation data path
|
192
|
+
**kwargs: Additional parameters
|
193
|
+
|
194
|
+
Returns:
|
195
|
+
Path to the trained model
|
196
|
+
"""
|
197
|
+
if not output_dir:
|
198
|
+
model_name = os.path.basename(model_path)
|
199
|
+
output_dir = self._get_output_dir(model_name, "dpo")
|
200
|
+
|
201
|
+
return self.llm_factory.dpo(
|
202
|
+
model_path=model_path,
|
203
|
+
train_data=preference_data,
|
204
|
+
val_data=val_data,
|
205
|
+
reference_model=reference_model,
|
206
|
+
output_dir=output_dir,
|
207
|
+
use_lora=use_lora,
|
208
|
+
batch_size=batch_size,
|
209
|
+
num_epochs=num_epochs,
|
210
|
+
learning_rate=learning_rate,
|
211
|
+
beta=beta,
|
212
|
+
**kwargs
|
213
|
+
)
|
214
|
+
|
215
|
+
def train_reward_model(
|
216
|
+
self,
|
217
|
+
model_path: str,
|
218
|
+
reward_data: str,
|
219
|
+
output_dir: Optional[str] = None,
|
220
|
+
use_lora: bool = True,
|
221
|
+
batch_size: int = 8,
|
222
|
+
num_epochs: int = 3,
|
223
|
+
learning_rate: float = 1e-5,
|
224
|
+
val_data: Optional[str] = None,
|
225
|
+
**kwargs
|
226
|
+
) -> str:
|
227
|
+
"""
|
228
|
+
Train a reward model for RLHF.
|
229
|
+
|
230
|
+
Args:
|
231
|
+
model_path: Base model path
|
232
|
+
reward_data: Reward training data
|
233
|
+
output_dir: Output directory
|
234
|
+
use_lora: Whether to use LoRA
|
235
|
+
batch_size: Batch size
|
236
|
+
num_epochs: Number of epochs
|
237
|
+
learning_rate: Learning rate
|
238
|
+
val_data: Validation data
|
239
|
+
**kwargs: Additional parameters
|
240
|
+
|
241
|
+
Returns:
|
242
|
+
Path to trained reward model
|
243
|
+
"""
|
244
|
+
if not output_dir:
|
245
|
+
model_name = os.path.basename(model_path)
|
246
|
+
output_dir = self._get_output_dir(model_name, "reward")
|
247
|
+
|
248
|
+
return self.llm_factory.train_reward_model(
|
249
|
+
model_path=model_path,
|
250
|
+
train_data=reward_data,
|
251
|
+
val_data=val_data,
|
252
|
+
output_dir=output_dir,
|
253
|
+
use_lora=use_lora,
|
254
|
+
batch_size=batch_size,
|
255
|
+
num_epochs=num_epochs,
|
256
|
+
learning_rate=learning_rate,
|
257
|
+
**kwargs
|
258
|
+
)
|
259
|
+
|
260
|
+
# =================
|
261
|
+
# Image Model Training Methods
|
262
|
+
# =================
|
263
|
+
|
264
|
+
def train_image_model(
|
265
|
+
self,
|
266
|
+
model_type: str = "flux",
|
267
|
+
training_images_dir: str = "",
|
268
|
+
output_dir: Optional[str] = None,
|
269
|
+
use_lora: bool = True,
|
270
|
+
num_epochs: int = 1000,
|
271
|
+
batch_size: int = 1,
|
272
|
+
learning_rate: float = 1e-4,
|
273
|
+
**kwargs
|
274
|
+
) -> str:
|
275
|
+
"""
|
276
|
+
Train an image generation model.
|
277
|
+
|
278
|
+
Args:
|
279
|
+
model_type: Type of model ("flux", "lora")
|
280
|
+
training_images_dir: Directory containing training images
|
281
|
+
output_dir: Output directory
|
282
|
+
use_lora: Whether to use LoRA
|
283
|
+
num_epochs: Training epochs
|
284
|
+
batch_size: Batch size
|
285
|
+
learning_rate: Learning rate
|
286
|
+
**kwargs: Additional parameters
|
287
|
+
|
288
|
+
Returns:
|
289
|
+
Path to trained model
|
290
|
+
"""
|
291
|
+
if not output_dir:
|
292
|
+
output_dir = self._get_output_dir("image_model", model_type)
|
293
|
+
|
294
|
+
# TODO: Implement image model training
|
295
|
+
logger.warning("Image model training not fully implemented yet")
|
296
|
+
return output_dir
|
297
|
+
|
298
|
+
# =================
|
299
|
+
# Utility Methods
|
300
|
+
# =================
|
301
|
+
|
302
|
+
def get_training_status(self, output_dir: str) -> Dict[str, Any]:
|
303
|
+
"""
|
304
|
+
Get training status from output directory.
|
305
|
+
|
306
|
+
Args:
|
307
|
+
output_dir: Training output directory
|
308
|
+
|
309
|
+
Returns:
|
310
|
+
Dictionary with training status information
|
311
|
+
"""
|
312
|
+
status = {
|
313
|
+
"output_dir": output_dir,
|
314
|
+
"exists": os.path.exists(output_dir),
|
315
|
+
"files": []
|
316
|
+
}
|
317
|
+
|
318
|
+
if status["exists"]:
|
319
|
+
status["files"] = os.listdir(output_dir)
|
320
|
+
|
321
|
+
return status
|
322
|
+
|
323
|
+
def list_trained_models(self) -> List[Dict[str, Any]]:
|
324
|
+
"""
|
325
|
+
List all trained models in the output directory.
|
326
|
+
|
327
|
+
Returns:
|
328
|
+
List of model information dictionaries
|
329
|
+
"""
|
330
|
+
models = []
|
331
|
+
|
332
|
+
if os.path.exists(self.base_output_dir):
|
333
|
+
for item in os.listdir(self.base_output_dir):
|
334
|
+
item_path = os.path.join(self.base_output_dir, item)
|
335
|
+
if os.path.isdir(item_path):
|
336
|
+
models.append({
|
337
|
+
"name": item,
|
338
|
+
"path": item_path,
|
339
|
+
"created": datetime.datetime.fromtimestamp(
|
340
|
+
os.path.getctime(item_path)
|
341
|
+
).isoformat()
|
342
|
+
})
|
343
|
+
|
344
|
+
return sorted(models, key=lambda x: x["created"], reverse=True)
|
345
|
+
|
346
|
+
|
347
|
+
# Convenience functions for quick access
|
348
|
+
def finetune_gemma(
|
349
|
+
dataset_path: str,
|
350
|
+
model_size: str = "4b",
|
351
|
+
output_dir: Optional[str] = None,
|
352
|
+
**kwargs
|
353
|
+
) -> str:
|
354
|
+
"""
|
355
|
+
Quick function to fine-tune Gemma models.
|
356
|
+
|
357
|
+
Args:
|
358
|
+
dataset_path: Path to training dataset
|
359
|
+
model_size: Model size ("2b", "4b", "7b")
|
360
|
+
output_dir: Output directory
|
361
|
+
**kwargs: Additional training parameters
|
362
|
+
|
363
|
+
Returns:
|
364
|
+
Path to fine-tuned model
|
365
|
+
|
366
|
+
Example:
|
367
|
+
```python
|
368
|
+
from isa_model.training import finetune_gemma
|
369
|
+
|
370
|
+
model_path = finetune_gemma(
|
371
|
+
dataset_path="my_data.json",
|
372
|
+
model_size="4b",
|
373
|
+
num_epochs=3,
|
374
|
+
batch_size=4
|
375
|
+
)
|
376
|
+
```
|
377
|
+
"""
|
378
|
+
factory = TrainingFactory()
|
379
|
+
|
380
|
+
model_map = {
|
381
|
+
"2b": "google/gemma-2-2b-it",
|
382
|
+
"4b": "google/gemma-2-4b-it",
|
383
|
+
"7b": "google/gemma-2-7b-it"
|
384
|
+
}
|
385
|
+
|
386
|
+
model_name = model_map.get(model_size, "google/gemma-2-4b-it")
|
387
|
+
|
388
|
+
return factory.finetune_llm(
|
389
|
+
model_name=model_name,
|
390
|
+
dataset_path=dataset_path,
|
391
|
+
output_dir=output_dir,
|
392
|
+
**kwargs
|
393
|
+
)
|
@@ -13,6 +13,10 @@ isa_model/deployment/gpu_fp16_ds8/scripts/download_model.py,sha256=E6iSAgBu3OGfK
|
|
13
13
|
isa_model/deployment/gpu_int8_ds8/app/server.py,sha256=lwWxdnR2DNEd0vIGQyfabKtDSUzSHVQsy3Z_AJejpVg,2102
|
14
14
|
isa_model/deployment/gpu_int8_ds8/scripts/test_client.py,sha256=aCULgRYzEQj_ELUK1bmPgN99yvFgNR5C0O3gc8S32pg,1421
|
15
15
|
isa_model/deployment/gpu_int8_ds8/scripts/test_client_os.py,sha256=XXrneTCHUeh1LNRcu-YtZQ5B4pNawlrxC-cTWmJU2A8,936
|
16
|
+
isa_model/eval/__init__.py,sha256=3sM7qLSIL_RMKcsmkCYcjOjv9ozuk16r7pnl4F-XeNA,1197
|
17
|
+
isa_model/eval/benchmarks.py,sha256=_L4Vwj2hwf2yhqoleIASO9z5e3LRCClCVEVCQbGt0I8,16885
|
18
|
+
isa_model/eval/factory.py,sha256=JmGRLTYPtZLqKwy8o1Z9NHUHX6mkRDw13ac_Qin_VrE,19529
|
19
|
+
isa_model/eval/metrics.py,sha256=mYeGwSa9PkgY0p-vadAscvak-pLrVfCSrsmAodVpgNQ,22584
|
16
20
|
isa_model/inference/__init__.py,sha256=usfuQJ4zYY2RRtHkE-V6LuJ5aN7WJogtPUj9Qmy4Wvw,318
|
17
21
|
isa_model/inference/ai_factory.py,sha256=mF-Pj8FUCsOvSG0IIg1OVMhDwXnpxIbZQJWVYWfjv2s,14660
|
18
22
|
isa_model/inference/base.py,sha256=qwOddnSGI0GUdD6qIdGBPQpkW7UjU3Y-zaZvu70B4WA,1278
|
@@ -50,6 +54,8 @@ isa_model/scripts/mlflow_manager.py,sha256=7xMN0_wELr1jcALuTW9WeWirRkPZPlE2LlFfZ
|
|
50
54
|
isa_model/scripts/model_registry.py,sha256=7rycPkVk8WHUO3LJaHfdyy5Yq8qmd_4WkGk4wKan-2w,14279
|
51
55
|
isa_model/scripts/start_mlflow.py,sha256=3AGKBzByjzbZ56I8w0IOfYnp3V6EU2Lv9NtX9maSqL8,2571
|
52
56
|
isa_model/scripts/training_tracker.py,sha256=cnXPi8ip2OK76-aWAOgC-dKx90PqZLEnP6UbHso7Fwc,8080
|
57
|
+
isa_model/training/__init__.py,sha256=RQDQ0m8p9YXBXgyyFxADeRA_-m18-wgNyWpjJhJfudU,950
|
58
|
+
isa_model/training/factory.py,sha256=iX-OkKRud09F2VfGXKKKj3w9d26kh9Po4FKvxTqmFiI,12581
|
53
59
|
isa_model/training/engine/llama_factory/__init__.py,sha256=WCqmUHTidASN4owGDOPSnKeLdG1gbK1MXQrRAzjP0z4,969
|
54
60
|
isa_model/training/engine/llama_factory/config.py,sha256=3OvjuXs9IyfcY52pB1SpXSOe0VwmKZvsmy8VK9Ig6Ss,3178
|
55
61
|
isa_model/training/engine/llama_factory/data_adapter.py,sha256=krqLp6Jy-IFQ6_M8O3FCtU-qqzUFJ65aNHpVq9C4Zyk,8865
|
@@ -79,8 +85,8 @@ isa_model/training/llm_model/annotation/tests/test_annotation_flow.py,sha256=DXY
|
|
79
85
|
isa_model/training/llm_model/annotation/tests/test_minio copy.py,sha256=EI-PlH5xttAZF14Z_xn6LjgIJBkvP2qjLcvbX2hc0RM,3946
|
80
86
|
isa_model/training/llm_model/annotation/tests/test_minio_upload.py,sha256=fL1eMubwR6L9lYc3zEwlWU9yjJuTsIYi93i0l9QUjm0,1109
|
81
87
|
isa_model/training/llm_model/annotation/views/annotation_controller.py,sha256=3VzJ52yI-YIpcaAAXy2qac7sr4hTnFdtn-ZEKTt4IkM,5792
|
82
|
-
isa_model-0.0.
|
83
|
-
isa_model-0.0.
|
84
|
-
isa_model-0.0.
|
85
|
-
isa_model-0.0.
|
86
|
-
isa_model-0.0.
|
88
|
+
isa_model-0.0.2.dist-info/licenses/LICENSE,sha256=nNPdMBBVrQz3f7AgKFZuyQgdar9d90Vdw51es-P72Dw,1084
|
89
|
+
isa_model-0.0.2.dist-info/METADATA,sha256=b-uD5aD32VND8BtZp0BaVUheGI_JhbyZLp57VNBjQuQ,8105
|
90
|
+
isa_model-0.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
91
|
+
isa_model-0.0.2.dist-info/top_level.txt,sha256=eHSy_Xb3kNkh2kK11mi1mZh0Wz91AQ5b8k2KFYO-rE8,10
|
92
|
+
isa_model-0.0.2.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|