wspx 0.1.0.dev0__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.
- wspx/__init__.py +24 -0
- wspx/_version.py +1 -0
- wspx/addressing.py +415 -0
- wspx/auth.py +77 -0
- wspx/cli/__init__.py +1 -0
- wspx/cli/client.py +124 -0
- wspx/cli/config.py +89 -0
- wspx/cli/connect.py +88 -0
- wspx/cli/main.py +848 -0
- wspx/cloud/__init__.py +20 -0
- wspx/cloud/app.py +191 -0
- wspx/cloud/backend.py +141 -0
- wspx/cloud/compose.py +85 -0
- wspx/cloud/deploy.py +102 -0
- wspx/config.py +189 -0
- wspx/control/__init__.py +5 -0
- wspx/control/app.py +488 -0
- wspx/control/backend.py +244 -0
- wspx/control/gateway_client.py +79 -0
- wspx/control/mcp_server.py +71 -0
- wspx/control/sessions.py +424 -0
- wspx/control/tracedb.py +183 -0
- wspx/control/ui/assets/index-BJFweZAn.css +1 -0
- wspx/control/ui/assets/index-DhUVhWIK.js +10 -0
- wspx/control/ui/index.html +13 -0
- wspx/control/views.py +180 -0
- wspx/diagnostics.py +146 -0
- wspx/driver/__init__.py +7 -0
- wspx/driver/loop.py +255 -0
- wspx/driver/messages.py +62 -0
- wspx/driver/providers/__init__.py +84 -0
- wspx/driver/providers/anthropic.py +149 -0
- wspx/driver/providers/compat.py +115 -0
- wspx/driver/providers/google.py +138 -0
- wspx/driver/providers/openai.py +125 -0
- wspx/driver/run.py +134 -0
- wspx/errors.py +69 -0
- wspx/gateway/__init__.py +8 -0
- wspx/gateway/app.py +626 -0
- wspx/gateway/ledger.py +536 -0
- wspx/gateway/prices.py +75 -0
- wspx/gateway/usage.py +284 -0
- wspx/handles.py +117 -0
- wspx/integrations/AGENTS.snippet.md +8 -0
- wspx/integrations/SKILL.md +52 -0
- wspx/integrations/__init__.py +17 -0
- wspx/integrations/claude-plugin/.claude-plugin/plugin.json +30 -0
- wspx/integrations/claude-plugin/skills/wspx/SKILL.md +52 -0
- wspx/integrations/codex-hooks.toml +9 -0
- wspx/lab/__init__.py +5 -0
- wspx/lab/analysis.py +485 -0
- wspx/lab/check.py +489 -0
- wspx/lab/evaluate.py +108 -0
- wspx/lab/export.py +275 -0
- wspx/lab/grep.py +754 -0
- wspx/lab/index.py +239 -0
- wspx/lab/ir/__init__.py +1 -0
- wspx/lab/ir/capture.py +299 -0
- wspx/lab/ir/graph.py +148 -0
- wspx/lab/ir/model_ir.py +522 -0
- wspx/lab/ir/semantics.py +1255 -0
- wspx/lab/ir/tape.py +853 -0
- wspx/lab/loader.py +204 -0
- wspx/lab/merge.py +200 -0
- wspx/lab/ops/__init__.py +76 -0
- wspx/lab/ops/builtin.py +379 -0
- wspx/lab/patch.py +320 -0
- wspx/lab/probe.py +206 -0
- wspx/lab/results.py +114 -0
- wspx/lab/sdk.py +120 -0
- wspx/lab/server.py +236 -0
- wspx/lab/session.py +1574 -0
- wspx/lab/tensors.py +119 -0
- wspx/lab/text.py +69 -0
- wspx/lab/tree.py +302 -0
- wspx/loops.py +127 -0
- wspx/project.py +143 -0
- wspx/py.typed +0 -0
- wspx/safety/__init__.py +15 -0
- wspx/safety/kill.py +47 -0
- wspx/safety/sweeper.py +68 -0
- wspx/store/__init__.py +64 -0
- wspx/store/objects.py +130 -0
- wspx/store/overlay.py +209 -0
- wspx/store/refs.py +150 -0
- wspx/testing/__init__.py +21 -0
- wspx/testing/api.py +155 -0
- wspx/testing/builtin.py +139 -0
- wspx/testing/runner.py +257 -0
- wspx/tools/__init__.py +17 -0
- wspx/tools/dispatch.py +129 -0
- wspx/tools/schemas.py +313 -0
- wspx/vcs/__init__.py +17 -0
- wspx/vcs/repo.py +431 -0
- wspx-0.1.0.dev0.dist-info/METADATA +278 -0
- wspx-0.1.0.dev0.dist-info/RECORD +99 -0
- wspx-0.1.0.dev0.dist-info/WHEEL +4 -0
- wspx-0.1.0.dev0.dist-info/entry_points.txt +10 -0
- wspx-0.1.0.dev0.dist-info/licenses/LICENSE +201 -0
wspx/lab/export.py
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Export (§5.5.3): merged checkpoints, PEFT adapters, GGUF and portable bundles.
|
|
2
|
+
|
|
3
|
+
``merged`` writes the live tensors, so the checkpoint is bit-identical to
|
|
4
|
+
what was tested. ``peft`` is exact for low-rank deltas and reports the error
|
|
5
|
+
of a truncated SVD otherwise.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import shutil
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
import tarfile
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
import torch
|
|
20
|
+
from safetensors.torch import save_file
|
|
21
|
+
|
|
22
|
+
from wspx.errors import WspxError
|
|
23
|
+
from wspx.lab.ir.model_ir import ModelIR
|
|
24
|
+
from wspx.lab.patch import TensorDelta
|
|
25
|
+
from wspx.lab.tensors import tensor_hash
|
|
26
|
+
from wspx.lab.tree import WorkingTree
|
|
27
|
+
|
|
28
|
+
SHARD_BYTES = 5 * 1024**3
|
|
29
|
+
CONFIG_FILES = (
|
|
30
|
+
"config.json",
|
|
31
|
+
"generation_config.json",
|
|
32
|
+
"tokenizer.json",
|
|
33
|
+
"tokenizer_config.json",
|
|
34
|
+
"special_tokens_map.json",
|
|
35
|
+
"tokenizer.model",
|
|
36
|
+
"vocab.json",
|
|
37
|
+
"merges.txt",
|
|
38
|
+
"chat_template.jinja",
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def export_merged(
|
|
43
|
+
model: torch.nn.Module,
|
|
44
|
+
ir: ModelIR,
|
|
45
|
+
out: Path,
|
|
46
|
+
*,
|
|
47
|
+
source: Path | None = None,
|
|
48
|
+
shard_bytes: int = SHARD_BYTES,
|
|
49
|
+
) -> dict[str, Any]:
|
|
50
|
+
"""Sharded safetensors of the live weights, plus config and tokenizer files."""
|
|
51
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
52
|
+
tensors = {name: p.detach().contiguous().cpu() for name, p in model.named_parameters()}
|
|
53
|
+
tensors.update(
|
|
54
|
+
{
|
|
55
|
+
name: b.detach().contiguous().cpu()
|
|
56
|
+
for name, b in model.named_buffers()
|
|
57
|
+
if name in _persistent_buffers(model)
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
shards: list[dict[str, torch.Tensor]] = [{}]
|
|
61
|
+
size = 0
|
|
62
|
+
for name in sorted(tensors):
|
|
63
|
+
t = tensors[name]
|
|
64
|
+
nbytes = t.numel() * t.element_size()
|
|
65
|
+
if shards[-1] and size + nbytes > shard_bytes:
|
|
66
|
+
shards.append({})
|
|
67
|
+
size = 0
|
|
68
|
+
shards[-1][name] = t
|
|
69
|
+
size += nbytes
|
|
70
|
+
weight_map: dict[str, str] = {}
|
|
71
|
+
files = []
|
|
72
|
+
for i, shard in enumerate(shards):
|
|
73
|
+
fname = (
|
|
74
|
+
"model.safetensors"
|
|
75
|
+
if len(shards) == 1
|
|
76
|
+
else f"model-{i + 1:05d}-of-{len(shards):05d}.safetensors"
|
|
77
|
+
)
|
|
78
|
+
save_file(shard, str(out / fname), metadata={"format": "pt", "wspx": ir.fingerprint})
|
|
79
|
+
files.append(fname)
|
|
80
|
+
weight_map.update(dict.fromkeys(shard, fname))
|
|
81
|
+
if len(shards) > 1:
|
|
82
|
+
total = sum(t.numel() * t.element_size() for t in tensors.values())
|
|
83
|
+
(out / "model.safetensors.index.json").write_text(
|
|
84
|
+
json.dumps({"metadata": {"total_size": total}, "weight_map": weight_map}, indent=2)
|
|
85
|
+
)
|
|
86
|
+
_copy_configs(model, out, source)
|
|
87
|
+
promoted = [
|
|
88
|
+
n
|
|
89
|
+
for n, t in tensors.items()
|
|
90
|
+
if t.dtype == torch.float32 and ir.params.get(n) and ir.params[n].dtype != "float32"
|
|
91
|
+
]
|
|
92
|
+
warnings = []
|
|
93
|
+
if promoted:
|
|
94
|
+
warnings.append(
|
|
95
|
+
f"{len(promoted)} tensor(s) stored in fp32 (promote mode); runtimes that cast the "
|
|
96
|
+
f"whole model to one dtype will round them: {', '.join(promoted[:4])}"
|
|
97
|
+
)
|
|
98
|
+
hashes = {name: tensor_hash(t) for name, t in tensors.items() if name in ir.params}
|
|
99
|
+
return {
|
|
100
|
+
"path": str(out),
|
|
101
|
+
"files": files,
|
|
102
|
+
"tensors": len(tensors),
|
|
103
|
+
"warnings": warnings,
|
|
104
|
+
"hashes": hashes,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _persistent_buffers(model: torch.nn.Module) -> set[str]:
|
|
109
|
+
out = set()
|
|
110
|
+
for mod_name, module in model.named_modules():
|
|
111
|
+
for name, _ in module.named_buffers(recurse=False):
|
|
112
|
+
if name not in getattr(module, "_non_persistent_buffers_set", set()):
|
|
113
|
+
out.add(f"{mod_name}.{name}" if mod_name else name)
|
|
114
|
+
return out
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _copy_configs(model: torch.nn.Module, out: Path, source: Path | None) -> None:
|
|
118
|
+
copied = False
|
|
119
|
+
if source is not None:
|
|
120
|
+
for name in CONFIG_FILES:
|
|
121
|
+
if (source / name).is_file():
|
|
122
|
+
shutil.copy2(source / name, out / name)
|
|
123
|
+
copied = True
|
|
124
|
+
config = getattr(model, "config", None)
|
|
125
|
+
if config is not None and not (out / "config.json").exists():
|
|
126
|
+
config.save_pretrained(str(out))
|
|
127
|
+
copied = True
|
|
128
|
+
del copied
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def export_peft(
|
|
132
|
+
model: torch.nn.Module,
|
|
133
|
+
ir: ModelIR,
|
|
134
|
+
tree: WorkingTree,
|
|
135
|
+
out: Path,
|
|
136
|
+
*,
|
|
137
|
+
base_model: str | None = None,
|
|
138
|
+
max_rank: int = 64,
|
|
139
|
+
) -> dict[str, Any]:
|
|
140
|
+
"""A PEFT LoRA adapter equal to the working tree's edits (exact for low-rank deltas)."""
|
|
141
|
+
tensors: dict[str, torch.Tensor] = {}
|
|
142
|
+
ranks: dict[str, int] = {}
|
|
143
|
+
errors: dict[str, float] = {}
|
|
144
|
+
fan_in_fan_out: set[bool] = set()
|
|
145
|
+
state = tree.state
|
|
146
|
+
names = sorted(set(state.merged) | set(state.adapter))
|
|
147
|
+
if not names:
|
|
148
|
+
raise WspxError("nothing to export: the working tree has no edits")
|
|
149
|
+
for name in names:
|
|
150
|
+
role = ir.primary_role(name)
|
|
151
|
+
info = ir.params[name]
|
|
152
|
+
module = model.get_submodule(info.module)
|
|
153
|
+
if (
|
|
154
|
+
len(info.shape) != 2
|
|
155
|
+
or role is None
|
|
156
|
+
or role.in_dim is None
|
|
157
|
+
or not hasattr(module, "weight")
|
|
158
|
+
or name.rpartition(".")[2] != "weight"
|
|
159
|
+
):
|
|
160
|
+
raise WspxError(
|
|
161
|
+
f"{name}: PEFT LoRA covers 2-D linear weights only; export --merged instead"
|
|
162
|
+
)
|
|
163
|
+
deltas = [
|
|
164
|
+
tree.delta(o) for o in (*state.merged.get(name, ()), *state.adapter.get(name, ()))
|
|
165
|
+
]
|
|
166
|
+
b, a, err = _lora_factors(deltas, info.shape, role.in_dim, max_rank)
|
|
167
|
+
flip = role.in_dim == 0
|
|
168
|
+
fan_in_fan_out.add(flip)
|
|
169
|
+
key = f"base_model.model.{info.module}"
|
|
170
|
+
tensors[f"{key}.lora_A.weight"] = a.contiguous()
|
|
171
|
+
tensors[f"{key}.lora_B.weight"] = b.contiguous()
|
|
172
|
+
ranks[info.module] = a.shape[0]
|
|
173
|
+
if err > 0:
|
|
174
|
+
errors[name] = err
|
|
175
|
+
if len(fan_in_fan_out) > 1:
|
|
176
|
+
raise WspxError(
|
|
177
|
+
"edits mix nn.Linear and Conv1D layouts; PEFT needs one fan_in_fan_out setting"
|
|
178
|
+
)
|
|
179
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
180
|
+
save_file(tensors, str(out / "adapter_model.safetensors"), metadata={"format": "pt"})
|
|
181
|
+
r = max(ranks.values())
|
|
182
|
+
config = {
|
|
183
|
+
"peft_type": "LORA",
|
|
184
|
+
"base_model_name_or_path": base_model,
|
|
185
|
+
"r": r,
|
|
186
|
+
"lora_alpha": r,
|
|
187
|
+
"rank_pattern": ranks,
|
|
188
|
+
"alpha_pattern": ranks,
|
|
189
|
+
"target_modules": sorted(ranks),
|
|
190
|
+
"lora_dropout": 0.0,
|
|
191
|
+
"bias": "none",
|
|
192
|
+
"fan_in_fan_out": fan_in_fan_out.pop(),
|
|
193
|
+
"inference_mode": True,
|
|
194
|
+
"task_type": "CAUSAL_LM" if ir.is_lm else None,
|
|
195
|
+
"init_lora_weights": True,
|
|
196
|
+
}
|
|
197
|
+
(out / "adapter_config.json").write_text(json.dumps(config, indent=2))
|
|
198
|
+
return {"path": str(out), "modules": sorted(ranks), "ranks": ranks, "svd_error": errors}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _lora_factors(
|
|
202
|
+
deltas: list[TensorDelta], shape: tuple[int, ...], in_dim: int, max_rank: int
|
|
203
|
+
) -> tuple[torch.Tensor, torch.Tensor, float]:
|
|
204
|
+
"""``ΔW = B A`` in PEFT's convention (B [out, r], A [r, in]); exact when every delta is low-rank."""
|
|
205
|
+
if all(d.kind == "lowrank" and d.index is None for d in deltas):
|
|
206
|
+
us, vs = [], []
|
|
207
|
+
for d in deltas:
|
|
208
|
+
assert d.u is not None
|
|
209
|
+
assert d.v is not None
|
|
210
|
+
us.append(d.u * d.scale)
|
|
211
|
+
vs.append(d.v)
|
|
212
|
+
u, v = torch.cat(us, dim=1), torch.cat(vs, dim=1)
|
|
213
|
+
# Stored blocks are u (dim 0) ⊗ v (dim 1). PEFT wants B [out, r] and A [r, in] with
|
|
214
|
+
# ΔW = B A for nn.Linear ([out, in]) and ΔW = (B A)ᵀ for Conv1D ([in, out]).
|
|
215
|
+
b, a = (u, v.T) if in_dim == 1 else (v, u.T)
|
|
216
|
+
return b.float(), a.float(), 0.0
|
|
217
|
+
full = torch.zeros(shape)
|
|
218
|
+
for d in deltas:
|
|
219
|
+
d.add_into(full)
|
|
220
|
+
mat = full if in_dim == 1 else full.T # always factor the [out, in] matrix
|
|
221
|
+
u, s, vh = torch.linalg.svd(mat, full_matrices=False)
|
|
222
|
+
r = int(min(max_rank, int((s > 1e-7 * s.max()).sum()) or 1))
|
|
223
|
+
b = u[:, :r] * s[:r]
|
|
224
|
+
a = vh[:r]
|
|
225
|
+
err = float((mat - b @ a).norm() / mat.norm().clamp_min(1e-30))
|
|
226
|
+
return b, a, err
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def export_gguf(merged_dir: Path, out_file: Path, *, outtype: str = "auto") -> dict[str, Any]:
|
|
230
|
+
"""Convert a merged export with llama.cpp's ``convert_hf_to_gguf.py``."""
|
|
231
|
+
script = _find_converter()
|
|
232
|
+
cmd = [
|
|
233
|
+
sys.executable,
|
|
234
|
+
str(script),
|
|
235
|
+
str(merged_dir),
|
|
236
|
+
"--outfile",
|
|
237
|
+
str(out_file),
|
|
238
|
+
"--outtype",
|
|
239
|
+
outtype,
|
|
240
|
+
]
|
|
241
|
+
proc = subprocess.run(cmd, capture_output=True, text=True, check=False)
|
|
242
|
+
if proc.returncode != 0:
|
|
243
|
+
raise WspxError(f"GGUF conversion failed:\n{proc.stderr[-2000:]}")
|
|
244
|
+
return {"path": str(out_file)}
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _find_converter() -> Path:
|
|
248
|
+
root = os.environ.get("LLAMA_CPP_DIR")
|
|
249
|
+
candidates = [Path(root) / "convert_hf_to_gguf.py"] if root else []
|
|
250
|
+
found = shutil.which("convert_hf_to_gguf.py")
|
|
251
|
+
if found:
|
|
252
|
+
candidates.append(Path(found))
|
|
253
|
+
for path in candidates:
|
|
254
|
+
if path.is_file():
|
|
255
|
+
return path
|
|
256
|
+
raise WspxError("GGUF export needs llama.cpp: set LLAMA_CPP_DIR to a llama.cpp checkout")
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def export_bundle(
|
|
260
|
+
objects: dict[str, bytes], manifest: dict[str, Any], out_file: Path
|
|
261
|
+
) -> dict[str, Any]:
|
|
262
|
+
"""A portable tarball: manifest plus every object needed to rebuild the commit on its base."""
|
|
263
|
+
out_file.parent.mkdir(parents=True, exist_ok=True)
|
|
264
|
+
import io
|
|
265
|
+
|
|
266
|
+
with tarfile.open(out_file, "w:gz") as tar:
|
|
267
|
+
data = json.dumps(manifest, indent=2, sort_keys=True).encode()
|
|
268
|
+
info = tarfile.TarInfo("manifest.json")
|
|
269
|
+
info.size = len(data)
|
|
270
|
+
tar.addfile(info, io.BytesIO(data))
|
|
271
|
+
for oid, blob in sorted(objects.items()):
|
|
272
|
+
info = tarfile.TarInfo(f"objects/{oid.removeprefix('sha256:')}")
|
|
273
|
+
info.size = len(blob)
|
|
274
|
+
tar.addfile(info, io.BytesIO(blob))
|
|
275
|
+
return {"path": str(out_file), "objects": len(objects)}
|