Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
328 lines (254 loc) · 14.1 KB
/
Copy pathclient.py
File metadata and controls
328 lines (254 loc) · 14.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
from pathlib import Path
from typing import Optional
import typer
from web3 import Web3
from dincli.cli.utils import CACHE_DIR, build_and_send_tx, require_custom_manifest_service, resolve_manifest_path, _confirm_or_exit
from dincli.cli.worker import (
ensure_worker_image,
ensure_worker_packages_installed,
get_worker_packages_dir,
get_worker_requirements_path,
read_worker_result,
run_worker_container,
write_worker_job,
)
from dincli.services.cid_utils import get_bytes32_from_cid, get_cid_from_bytes32
from dincli.services.ipfs import upload_to_ipfs
app = typer.Typer(help="Commands for DIN clients in DIN.")
lms_app = typer.Typer(help="LMS related commands")
app.add_typer(lms_app, name="lms")
DISABLED_DP_MODES = {"disabled", "none", "off", "false"}
DP_MECHANISM_ALIASES = {
"gaussian": "post_training_gaussian",
"post-training-gaussian": "post_training_gaussian",
"laplace": "post_training_laplace",
"post-training-laplace": "post_training_laplace",
"update": "update_gaussian",
"update-gaussian": "update_gaussian",
}
def _normalize_dp_mechanism(dp_mechanism: str) -> str:
normalized_mechanism = str(dp_mechanism).strip().lower()
return DP_MECHANISM_ALIASES.get(normalized_mechanism, normalized_mechanism)
def resolve_dp_config(manifest: dict) -> dict:
"""Small CLI-side DP resolver used only to locate the local LMS artifact."""
dp_block = manifest.get("dp") or {}
if dp_block:
enabled = dp_block.get("enabled")
mode = str(dp_block.get("mode", "disabled")).strip().lower()
if enabled is False or (enabled is not True and mode in DISABLED_DP_MODES):
return {"enabled": False, "mechanism": "none"}
if enabled is True or mode not in DISABLED_DP_MODES:
return {
"enabled": True,
"mechanism": _normalize_dp_mechanism(
dp_block.get("mechanism", "post_training_gaussian")
),
}
legacy_mode = str(manifest.get("dp_mode", "disabled")).strip().lower()
if legacy_mode in DISABLED_DP_MODES:
return {"enabled": False, "mechanism": "none"}
return {
"enabled": True,
"mechanism": _normalize_dp_mechanism(
manifest.get("dp_mechanism", "post_training_gaussian")
),
}
def get_local_model_path(model_base_dir: Path, account_address: str, gi: int, dp_config: dict) -> Path:
client_model_dir = model_base_dir / "models" / "clients" / account_address
if dp_config["enabled"]:
return client_model_dir / f"lm_{gi}_{dp_config['mechanism']}.pth"
return client_model_dir / f"lm_{gi}.pth"
@app.command("create-client-dataset-dir")
def create_client_dataset_dir(
ctx: typer.Context,
model_id: int = typer.Argument(..., help="Model index"),
):
effective_network, w3, account, console = ctx.obj.get_en_w3_account_console(model_id)
model_base_dir = ctx.obj.get_model_base_dir(model_id)
client_dataset_dir = model_base_dir / "dataset" / "clients" / account.address
if client_dataset_dir.exists():
console.print(f"[bold yellow]Client dataset directory already exists: [/bold yellow]{client_dataset_dir}")
else:
client_dataset_dir.mkdir(parents=True, exist_ok=True)
console.print(f"[bold green]Created client dataset directory: [/bold green]{client_dataset_dir}")
@app.command("train-lms")
def train_lms(
ctx: typer.Context,
model_id: int = typer.Argument(..., help="Model index"),
gi: Optional[int] = typer.Option(None, "--gi", help="Global iteration to use"),
packages_dir: Optional[str] = typer.Option(None, "--packages-dir", help="Packages directory"),
no_cache: bool = typer.Option(False, "--no-cache", help="Do not use cached packages"),
):
effective_network, w3, account, console = ctx.obj.get_en_w3_account_console(model_id)
taskCoordinator_contract, taskAuditor_contract = ctx.obj.get_deployed_din_task_coordinator_contract(True, model_id), ctx.obj.get_deployed_din_task_auditor_contract(True, model_id)
runtime = ctx.obj.build_service_runtime(role="client", model_id=model_id)
current_GI, current_GIstate = ctx.obj.get_current_gi_and_state(taskCoordinator_contract)
ctx.obj.validate_gi_ET_curr_GI(gi, current_GI)
console.print("Training local model ...")
genesis_model_ipfs_hash_raw = taskCoordinator_contract.functions.genesisModelIpfsHash().call()
genesis_model_ipfs_hash = get_cid_from_bytes32(genesis_model_ipfs_hash_raw.hex())
console.print("Using Genesis Model IPFS Hash: ", genesis_model_ipfs_hash)
initial_model_ipfs_hash = None
if current_GI > 1:
(_, _, _, cid_raw) = taskCoordinator_contract.functions.getTier2Batch(current_GI - 1, 0).call()
initial_model_ipfs_hash = get_cid_from_bytes32(cid_raw.hex()) if cid_raw and cid_raw != bytes(32) else None
console.print("Using Latest Global Model IPFS Hash: ", initial_model_ipfs_hash)
model_base_dir = ctx.obj.get_model_base_dir(model_id)
manifest = runtime.require_manifest_key("train_client_model")
model_manifest = runtime.require_manifest_key("ModelArchitecture")
client_service_path = resolve_manifest_path(model_base_dir, manifest["path"], what="train_client_model path")
model_service_path = resolve_manifest_path(model_base_dir, model_manifest["path"], what="ModelArchitecture path")
require_custom_manifest_service(manifest, "train_client_model")
ctx.obj.ensure_file_exists(client_service_path, manifest["ipfs"], "client service", base_dir=model_base_dir)
ctx.obj.ensure_file_exists(model_service_path, model_manifest["ipfs"], "model architecture service", base_dir=model_base_dir)
client_requirements_cid = runtime.get_manifest_key("requirements.txt", {}).get("clients")
requirements_path = get_worker_requirements_path(model_base_dir, "clients")
packages_dir = Path(packages_dir) if packages_dir else None
if client_requirements_cid:
ctx.obj.ensure_file_exists(requirements_path, client_requirements_cid, "client requirements", base_dir=model_base_dir)
try:
ensure_worker_image(console)
if not no_cache and packages_dir is None:
packages_dir = ensure_worker_packages_installed(
requirements_path,
get_worker_packages_dir(effective_network, model_id),
console,
)
except RuntimeError as e:
console.print(f"[bold red]{e}[/bold red]")
raise typer.Exit(1)
# Always refresh the genesis model file locally from IPFS so the client has
# the base model object needed for deserialization and potential fallback.
ctx.obj.ensure_file_exists(
model_base_dir / "models" / "genesis_model.pth",
genesis_model_ipfs_hash,
"genesis model",
base_dir=model_base_dir,
)
if initial_model_ipfs_hash:
ctx.obj.ensure_file_exists(
model_base_dir / "models" / f"gm_{current_GI-1}.pt",
initial_model_ipfs_hash,
"latest global model",
base_dir=model_base_dir,
)
client_dataset_path = model_base_dir / "dataset" / "clients" / account.address / "data.pt"
_confirm_or_exit("Have you placed your client dataset at" + str(client_dataset_path.resolve()) + "?", "Please ensure that the client dataset is placed at" + str(client_dataset_path.resolve()) + ".", console=console)
if not client_dataset_path.exists():
console.print(f"[bold red]Error:[/bold red] No client dataset found at {client_dataset_path}")
console.print(f"[bold green]Creating client dataset directory at {client_dataset_path.parent}...[/bold green]")
try:
client_dataset_path.parent.mkdir(parents=True, exist_ok=True)
console.print(f"[bold yellow]Please place your client dataset at {client_dataset_path}[/bold yellow]")
except Exception as e:
console.print(f"[bold red]Error:[/bold red] {e}")
raise typer.Exit(0)
_confirm_or_exit(f"Creating a job to train a local model using containerized service. Are you sure that {client_dataset_path} is updated with your latest dataset? This may take a while. Do you want to continue?", "Aborted by user.", console=console)
jobs_dir = model_base_dir / "jobs" / "clients" / account.address
job_path, output_dir = write_worker_job(
jobs_dir,
f"client_lms_gi_{current_GI}",
{
"network": effective_network,
"model_base_dir": "/din/model",
"manifest_path": "/din/model/manifest.json",
"role": "client",
"service_path": manifest["path"],
"function_name": "train_client_model",
"args": [genesis_model_ipfs_hash, account.address, effective_network],
"kwargs": {
"initial_model_ipfs_hash": initial_model_ipfs_hash,
"model_base_dir": "/din/model",
"gi": current_GI,
},
},
)
console.print(f"Created client training job at {job_path}")
_confirm_or_exit("Starting training a local model using containerized service. This may take a while. Do you want to continue?", "Aborted by user.", console=console)
docker_result = run_worker_container(
container_name=f"din-worker-client-model-{model_id}-gi-{current_GI}",
model_base_dir=model_base_dir,
job_path=job_path,
output_dir=output_dir,
writable_subdirs=[model_base_dir / "models" / "clients" / account.address],
packages_dir=packages_dir,
)
if docker_result.stdout:
console.print(docker_result.stdout)
if docker_result.returncode != 0:
if docker_result.stderr:
console.print(docker_result.stderr)
console.print("[bold red]Client worker container failed.[/bold red]")
raise typer.Exit(docker_result.returncode)
worker_result = read_worker_result(output_dir)
if worker_result.get("status") != "ok":
console.print(f"[bold red]Client worker failed:[/bold red] {worker_result.get('error')}")
traceback = worker_result.get("traceback")
if traceback:
console.print(traceback)
raise typer.Exit(1)
container_model_path = Path(worker_result["result"])
local_model_path = model_base_dir / container_model_path.relative_to("/din/model")
console.print(f"[bold green]Local model generated at {local_model_path}[/bold green]")
@app.command("submit-lm")
def submit_lms(
ctx: typer.Context,
model_id: int = typer.Argument(..., help="Model index"),
gi: Optional[int] = typer.Option(None, "--gi", help="Global iteration to use"),
):
effective_network, w3, account, console = ctx.obj.get_en_w3_account_console(model_id)
taskCoordinator_contract, taskAuditor_contract = ctx.obj.get_deployed_din_task_coordinator_contract(True, model_id), ctx.obj.get_deployed_din_task_auditor_contract(True, model_id)
runtime = ctx.obj.build_service_runtime(role="client", model_id=model_id)
current_GI, current_GIstate = ctx.obj.get_current_gi_and_state(taskCoordinator_contract)
ref_gi = ctx.obj.validate_gi_LTE_curr_GI(gi, current_GI)
console.print("Submitting local model ...")
model_base_dir = ctx.obj.get_model_base_dir(model_id)
dp_config = resolve_dp_config(runtime.manifest)
local_model_path = get_local_model_path(model_base_dir, account.address, ref_gi, dp_config)
if not local_model_path.exists():
console.print(f"[bold red]Error:[/bold red] No local model found at {local_model_path}")
raise typer.Exit(1)
if dp_config["enabled"]:
console.print(f"DP mode is enabled with {dp_config['mechanism']} mechanism")
console.print(f"Model file: {local_model_path}")
else:
console.print("DP mode is disabled")
console.print(f"Model file: {local_model_path}")
client_model_ipfs_hash = upload_to_ipfs(
local_model_path,
f"Client {account.address} model uploaded to IPFS",
)
try:
client_model_ipfs_hash_bytes32 = Web3.to_bytes(hexstr=get_bytes32_from_cid(client_model_ipfs_hash))
build_and_send_tx(
ctx,
taskAuditor_contract.functions.submitLocalModel(client_model_ipfs_hash_bytes32, ref_gi),
f"Submitting local model with IPFS hash: {client_model_ipfs_hash} to task auditor",
f"Local model submitted to task auditor with IPFS hash: {client_model_ipfs_hash}",
f"Local model submission failed to task auditor with IPFS hash: {client_model_ipfs_hash}",
exit_on_failure=False
)
except Exception as e:
console.print(f"[bold red] Error submitting local model to task auditor: {e}[/bold red]")
@lms_app.command()
def show_models(
ctx: typer.Context,
model_id: int = typer.Argument(..., help="Model index"),
gi: int = typer.Option(None, "--gi", help="Global iteration to use"),
):
effective_network, w3, account, console = ctx.obj.get_en_w3_account_console(model_id)
taskCoordinator_contract, taskAuditor_contract = ctx.obj.get_deployed_din_task_coordinator_contract(True, model_id), ctx.obj.get_deployed_din_task_auditor_contract(True, model_id)
curr_GI, curr_GIstate = ctx.obj.get_current_gi_and_state(taskCoordinator_contract)
ref_gi = ctx.obj.validate_gi_LTE_curr_GI(gi, curr_GI)
console.print(f"[bold green]Showing local model submissions for global iteration {ref_gi}![/bold green]")
ctx.obj.validate_GIstate_LTE_given_GIstate(ref_gi, curr_GI, curr_GIstate, "LMSstarted", "Its not time for model submissions yet.")
has_submitted = taskAuditor_contract.functions.clientHasSubmitted(ref_gi, account.address).call()
if has_submitted:
console.print(f"[green]✓ Client {account.address} has submitted ![/green]")
else:
console.print(f"[red]Error:[/red] No local model submission found")
raise typer.Exit(1)
has_index = taskAuditor_contract.functions.clientSubmissionIndex(ref_gi, account.address).call()
lm_submission = taskAuditor_contract.functions.lmSubmissions(ref_gi, has_index).call()
console.print(f"[green]✓ Client {lm_submission[0]} submitted model {get_cid_from_bytes32(lm_submission[1].hex())}![/green]")
console.print(f"[bold green]✓ Local model submissions shown![/bold green]")