@swapai/core 0.1.0 → 0.2.1
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.
- package/README.md +29 -0
- package/dist/effect.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1126 -193
- package/dist/index.js.map +1 -1
- package/dist/{types-DTbU1MEi.d.ts → types-DkvGbscw.d.ts} +1 -0
- package/package.json +1 -1
- package/python/swapai_worker.py +105 -43
|
@@ -24,6 +24,7 @@ type ReferenceClassifier<Result extends ResultValue> = (input: string) => Result
|
|
|
24
24
|
interface Classifier<Result extends ResultValue> {
|
|
25
25
|
isTrained(): boolean;
|
|
26
26
|
logClassification(input: string, result: Result): void;
|
|
27
|
+
clearTrainingData(): void;
|
|
27
28
|
classify(input: string, referenceClassifier?: ReferenceClassifier<Result>): Promise<Result>;
|
|
28
29
|
flush(): Promise<void>;
|
|
29
30
|
close(): Promise<void>;
|
package/package.json
CHANGED
package/python/swapai_worker.py
CHANGED
|
@@ -3,9 +3,12 @@ from __future__ import annotations
|
|
|
3
3
|
|
|
4
4
|
import argparse
|
|
5
5
|
import json
|
|
6
|
+
import math
|
|
6
7
|
import os
|
|
8
|
+
import sqlite3
|
|
7
9
|
import sys
|
|
8
10
|
import types
|
|
11
|
+
from contextlib import contextmanager
|
|
9
12
|
from pathlib import Path
|
|
10
13
|
from typing import Any
|
|
11
14
|
|
|
@@ -28,52 +31,106 @@ def require_version(needle: Any) -> None:
|
|
|
28
31
|
)
|
|
29
32
|
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
@contextmanager
|
|
35
|
+
def artifact_write_lock(args: argparse.Namespace):
|
|
36
|
+
lock_database = sqlite3.connect(args.artifact_lock_database, timeout=30)
|
|
37
|
+
main_database: sqlite3.Connection | None = None
|
|
38
|
+
try:
|
|
39
|
+
lock_database.execute("BEGIN IMMEDIATE")
|
|
40
|
+
main_database = sqlite3.connect(f"file:{args.main_database}?mode=ro", uri=True)
|
|
41
|
+
row = main_database.execute(
|
|
42
|
+
"SELECT data_epoch, clear_pending FROM classifiers WHERE name = ?",
|
|
43
|
+
(args.classifier_name,),
|
|
44
|
+
).fetchone()
|
|
45
|
+
if row is None or row[0] != args.expected_epoch or row[1] != 0:
|
|
46
|
+
raise RuntimeError("Classifier training was superseded by data erasure")
|
|
47
|
+
yield
|
|
48
|
+
finally:
|
|
49
|
+
if main_database is not None:
|
|
50
|
+
main_database.close()
|
|
51
|
+
try:
|
|
52
|
+
lock_database.rollback()
|
|
53
|
+
finally:
|
|
54
|
+
lock_database.close()
|
|
34
55
|
|
|
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
|
-
|
|
56
|
+
|
|
57
|
+
def train(args: argparse.Namespace) -> None:
|
|
58
|
+
with artifact_write_lock(args):
|
|
59
|
+
import needle
|
|
60
|
+
from needle.model.finetune import build_main, finetune_local
|
|
61
|
+
|
|
62
|
+
require_version(needle)
|
|
63
|
+
checkpoint_dir = Path(args.checkpoint_dir).resolve()
|
|
64
|
+
checkpoint_dir.mkdir(parents=True, exist_ok=True)
|
|
65
|
+
checkpoint = checkpoint_dir / "needle2.pkl"
|
|
66
|
+
output = Path(args.output).resolve()
|
|
67
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
68
|
+
training_input = sys.stdin.read()
|
|
69
|
+
number_labels: dict[str, Any] | None = None
|
|
70
|
+
if args.numeric_labels_from_stdin:
|
|
71
|
+
labels_json, separator, training_input = training_input.partition("\n")
|
|
72
|
+
if not separator:
|
|
73
|
+
raise RuntimeError("Numeric label map is missing")
|
|
74
|
+
labels = json.loads(labels_json)
|
|
75
|
+
values = labels.get("values") if isinstance(labels, dict) else None
|
|
76
|
+
if (
|
|
77
|
+
not isinstance(labels, dict)
|
|
78
|
+
or labels.get("format") != 1
|
|
79
|
+
or not isinstance(values, list)
|
|
80
|
+
or not values
|
|
81
|
+
or any(
|
|
82
|
+
not isinstance(value, (int, float))
|
|
83
|
+
or isinstance(value, bool)
|
|
84
|
+
or not math.isfinite(value)
|
|
85
|
+
for value in values
|
|
86
|
+
)
|
|
87
|
+
or values != sorted(set(values))
|
|
88
|
+
):
|
|
89
|
+
raise RuntimeError("Numeric label map has an invalid format")
|
|
90
|
+
number_labels = labels
|
|
91
|
+
training_data = Path(args.training_data).resolve()
|
|
92
|
+
training_data.write_text(training_input, encoding="utf-8")
|
|
93
|
+
adapter = output.parent / "swapai-lora.pkl"
|
|
94
|
+
with training_data.open("r", encoding="utf-8") as handle:
|
|
95
|
+
example_count = sum(1 for line in handle if line.strip())
|
|
96
|
+
batch_size = min(16, max(1, (example_count + 9) // 10))
|
|
97
|
+
|
|
98
|
+
finetune_local(
|
|
99
|
+
types.SimpleNamespace(
|
|
100
|
+
jsonl_path=str(training_data),
|
|
101
|
+
checkpoint=str(checkpoint),
|
|
102
|
+
epochs=args.epochs,
|
|
103
|
+
batch_size=batch_size,
|
|
104
|
+
lr=1e-4,
|
|
105
|
+
lora_rank=16,
|
|
106
|
+
lora_alpha=32.0,
|
|
107
|
+
max_len=1024,
|
|
108
|
+
val_split=0.0,
|
|
109
|
+
seed=0,
|
|
110
|
+
generate=0,
|
|
111
|
+
model="deepseek/deepseek-v4-flash",
|
|
112
|
+
workers=1,
|
|
113
|
+
checkpoint_dir=str(checkpoint_dir),
|
|
114
|
+
out=str(adapter),
|
|
115
|
+
qat_bits="auto",
|
|
116
|
+
)
|
|
64
117
|
)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
118
|
+
build_main(
|
|
119
|
+
types.SimpleNamespace(
|
|
120
|
+
checkpoint=str(checkpoint),
|
|
121
|
+
lora=str(adapter),
|
|
122
|
+
out=str(output),
|
|
123
|
+
upload=False,
|
|
124
|
+
bits=None,
|
|
125
|
+
)
|
|
73
126
|
)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
127
|
+
if not output.is_file():
|
|
128
|
+
raise RuntimeError("Needle did not create the requested .cact model")
|
|
129
|
+
if number_labels is not None:
|
|
130
|
+
Path(f"{output}.numbers.json").write_text(
|
|
131
|
+
json.dumps(number_labels, separators=(",", ":")),
|
|
132
|
+
encoding="utf-8",
|
|
133
|
+
)
|
|
77
134
|
|
|
78
135
|
|
|
79
136
|
def classification_result(response: Any) -> Any:
|
|
@@ -147,6 +204,11 @@ def parser() -> argparse.ArgumentParser:
|
|
|
147
204
|
train_parser.add_argument("--output", required=True)
|
|
148
205
|
train_parser.add_argument("--checkpoint-dir", required=True)
|
|
149
206
|
train_parser.add_argument("--epochs", type=int, default=10)
|
|
207
|
+
train_parser.add_argument("--artifact-lock-database", required=True)
|
|
208
|
+
train_parser.add_argument("--main-database", required=True)
|
|
209
|
+
train_parser.add_argument("--classifier-name", required=True)
|
|
210
|
+
train_parser.add_argument("--expected-epoch", type=int, required=True)
|
|
211
|
+
train_parser.add_argument("--numeric-labels-from-stdin", action="store_true")
|
|
150
212
|
|
|
151
213
|
serve_parser = commands.add_parser("serve")
|
|
152
214
|
serve_parser.add_argument("--model", required=True)
|