burn_p2p_python 0.21.0-pre.22

Python/Torch workload bridge for burn_p2p.
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::{
    collections::BTreeMap,
    ffi::OsString,
    io::{Read, Write},
    net::{TcpListener, TcpStream},
    path::PathBuf,
    process::{Child, Command, Stdio},
    sync::{Arc, Mutex},
    thread,
    time::{Duration, Instant},
};

use anyhow::{Context, bail};
use burn_p2p_core::{CapabilityEstimate, MergePolicy, MetricValue};
use burn_p2p_experiment::RuntimePatch;
use burn_p2p_workload::{EvalSplit, PatchOutcome, TrainerCanonicalReconcileStrategy};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::Value;

use crate::{PythonBatchRef, PythonTorchRuntimeConfig};

#[derive(Clone, Debug)]
pub(crate) struct PythonWorkerClient {
    inner: Arc<Mutex<PythonWorkerTransport>>,
}

impl PythonWorkerClient {
    pub(crate) fn spawn(config: &PythonTorchRuntimeConfig) -> anyhow::Result<Self> {
        let runtime_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("python");
        let mut pythonpath_entries = vec![runtime_root];
        pythonpath_entries.extend(config.module_search_roots.iter().cloned());
        let pythonpath = join_pythonpath(&pythonpath_entries)?;
        let config_json = serde_json::to_string(&config.workload_config)
            .context("serialize python workload config")?;
        let listener =
            TcpListener::bind(("127.0.0.1", 0)).context("bind local python worker listener")?;
        let listener_addr = listener
            .local_addr()
            .context("resolve local python worker listener address")?;

        let mut command = Command::new(&config.python_executable);
        command
            .arg("-m")
            .arg("burn_p2p_python_runtime.worker")
            .arg("--factory")
            .arg(&config.workload_factory)
            .arg("--config-json")
            .arg(config_json)
            .arg("--connect-host")
            .arg(listener_addr.ip().to_string())
            .arg("--connect-port")
            .arg(listener_addr.port().to_string())
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::inherit())
            .env("PYTHONUNBUFFERED", "1");

        if let Some(path) = pythonpath {
            command.env("PYTHONPATH", path);
        }
        for (key, value) in &config.env {
            command.env(key, value);
        }

        let mut child = command
            .spawn()
            .with_context(|| format!("spawn python worker {:?}", config.python_executable))?;
        let stream = accept_worker_connection(&listener, &mut child, Duration::from_secs(10))?;
        Ok(Self {
            inner: Arc::new(Mutex::new(PythonWorkerTransport {
                child,
                stream,
                next_request_id: 1,
            })),
        })
    }

    pub(crate) fn hello(&self) -> anyhow::Result<HelloResponse> {
        self.call::<_, HelloResponse>("hello", Value::Null)
    }

    pub(crate) fn capability_probe(&self) -> anyhow::Result<CapabilityProbeResponse> {
        self.call::<_, CapabilityProbeResponse>("capability_probe", Value::Null)
    }

    pub(crate) fn init_model(&self, device: &str) -> anyhow::Result<String> {
        let response = self.call::<_, ModelHandleResponse>(
            "init_model",
            serde_json::json!({ "device": device }),
        )?;
        Ok(response.model_id)
    }

    pub(crate) fn train_window(
        &self,
        model_id: &str,
        batches: &[PythonBatchRef],
    ) -> anyhow::Result<BTreeMap<String, MetricValue>> {
        let response = self.call::<_, MetricsResponse>(
            "train_window",
            serde_json::json!({
                "model_id": model_id,
                "batches": batches,
            }),
        )?;
        Ok(response.metrics)
    }

    pub(crate) fn evaluate(
        &self,
        model_id: &str,
        split: EvalSplit,
    ) -> anyhow::Result<BTreeMap<String, MetricValue>> {
        let response = self.call::<_, MetricsResponse>(
            "evaluate",
            serde_json::json!({
                "model_id": model_id,
                "split": split,
            }),
        )?;
        Ok(response.metrics)
    }

    pub(crate) fn apply_patch(&self, patch: &RuntimePatch) -> anyhow::Result<PatchOutcome> {
        self.call("apply_patch", patch)
    }

    pub(crate) fn load_model_artifact_path(
        &self,
        model_id: &str,
        artifact_path: &std::path::Path,
    ) -> anyhow::Result<()> {
        let mut transport = self
            .inner
            .lock()
            .map_err(|_| anyhow::anyhow!("python worker mutex poisoned"))?;
        let _: AckResponse = transport.call::<_, AckResponse>(
            "load_model_artifact",
            serde_json::json!({
                "model_id": model_id,
                "artifact_path": artifact_path.to_string_lossy(),
            }),
        )?;
        Ok(())
    }

    pub(crate) fn materialize_model_artifact_path(
        &self,
        model_id: &str,
        artifact_path: &std::path::Path,
    ) -> anyhow::Result<()> {
        let mut transport = self
            .inner
            .lock()
            .map_err(|_| anyhow::anyhow!("python worker mutex poisoned"))?;
        let _: AckResponse = transport.call(
            "materialize_model_artifact",
            serde_json::json!({
                "model_id": model_id,
                "artifact_path": artifact_path.to_string_lossy(),
            }),
        )?;
        Ok(())
    }

    pub(crate) fn merge_candidate_models(
        &self,
        base_model_id: &str,
        candidates: &[PythonMergeCandidateRef],
        policy: MergePolicy,
    ) -> anyhow::Result<Option<String>> {
        let response = self.call::<_, OptionalModelHandleResponse>(
            "merge_candidate_models",
            serde_json::json!({
                "base_model_id": base_model_id,
                "candidates": candidates,
                "policy": policy,
            }),
        )?;
        Ok(response.model_id)
    }

    pub(crate) fn apply_single_root_ema(
        &self,
        base_model_id: &str,
        merged_model_id: &str,
        policy: MergePolicy,
    ) -> anyhow::Result<String> {
        let response = self.call::<_, ModelHandleResponse>(
            "apply_single_root_ema",
            serde_json::json!({
                "base_model_id": base_model_id,
                "merged_model_id": merged_model_id,
                "policy": policy,
            }),
        )?;
        Ok(response.model_id)
    }

    pub(crate) fn reconcile_canonical_model(
        &self,
        local_model_id: &str,
        canonical_model_id: &str,
        strategy: TrainerCanonicalReconcileStrategy,
    ) -> anyhow::Result<String> {
        let response = self.call::<_, ModelHandleResponse>(
            "reconcile_canonical_model",
            serde_json::json!({
                "local_model_id": local_model_id,
                "canonical_model_id": canonical_model_id,
                "strategy": strategy,
            }),
        )?;
        Ok(response.model_id)
    }

    pub(crate) fn release_model(&self, model_id: &str) {
        let _ = self
            .call::<_, AckResponse>("release_model", serde_json::json!({ "model_id": model_id }));
    }

    fn call<P, R>(&self, method: &str, params: P) -> anyhow::Result<R>
    where
        P: Serialize,
        R: DeserializeOwned,
    {
        let mut transport = self
            .inner
            .lock()
            .map_err(|_| anyhow::anyhow!("python worker mutex poisoned"))?;
        transport.call(method, params)
    }
}

#[derive(Debug)]
struct PythonWorkerTransport {
    child: Child,
    stream: TcpStream,
    next_request_id: u64,
}

impl PythonWorkerTransport {
    fn call<P, R>(&mut self, method: &str, params: P) -> anyhow::Result<R>
    where
        P: Serialize,
        R: DeserializeOwned,
    {
        let request_id = self.next_request_id;
        self.next_request_id += 1;
        let request = RpcRequest {
            id: request_id,
            method: method.to_owned(),
            params,
        };
        let bytes = serde_json::to_vec(&request).context("serialize python rpc request")?;
        write_frame(&mut self.stream, &bytes)
            .with_context(|| format!("send python rpc request {method}"))?;

        let response_bytes = match read_frame(&mut self.stream) {
            Ok(bytes) => bytes,
            Err(error) => {
                let status = self
                    .child
                    .try_wait()
                    .context("poll python worker status after transport failure")?;
                return Err(error).with_context(|| {
                    format!(
                        "read python rpc response {method} (worker status: {:?})",
                        status
                    )
                });
            }
        };
        if response_bytes.is_empty() {
            let status = self
                .child
                .try_wait()
                .context("poll python worker status after empty response")?;
            bail!(
                "python worker returned empty response for {method} (status: {:?})",
                status
            );
        }
        let response: RpcResponse =
            serde_json::from_slice(&response_bytes).context("decode python rpc response")?;
        if response.id != request_id {
            bail!(
                "python rpc response id mismatch for {method}: expected {}, got {}",
                request_id,
                response.id
            );
        }
        if !response.ok {
            bail!(
                "python rpc {method} failed: {}",
                response
                    .error
                    .unwrap_or_else(|| "unknown python worker error".to_owned())
            );
        }
        let result = response.result.unwrap_or(Value::Null);
        match serde_json::from_value(result.clone()) {
            Ok(value) => Ok(value),
            Err(error) => bail!("decode result for python rpc {method}: {error}; payload={result}"),
        }
    }
}

impl Drop for PythonWorkerTransport {
    fn drop(&mut self) {
        let _ = self.call::<_, AckResponse>("shutdown", Value::Null);
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

fn accept_worker_connection(
    listener: &TcpListener,
    child: &mut Child,
    timeout: Duration,
) -> anyhow::Result<TcpStream> {
    listener
        .set_nonblocking(true)
        .context("configure python worker listener as nonblocking")?;
    let deadline = Instant::now() + timeout;
    loop {
        match listener.accept() {
            Ok((stream, _)) => {
                stream
                    .set_nodelay(true)
                    .context("configure python worker tcp nodelay")?;
                return Ok(stream);
            }
            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                if let Some(status) = child
                    .try_wait()
                    .context("poll python worker while waiting for connection")?
                {
                    bail!("python worker exited before establishing rpc connection: {status}");
                }
                if Instant::now() >= deadline {
                    bail!("timed out waiting for python worker rpc connection");
                }
                thread::sleep(Duration::from_millis(25));
            }
            Err(error) => return Err(error).context("accept python worker rpc connection"),
        }
    }
}

fn write_frame(stream: &mut TcpStream, payload: &[u8]) -> anyhow::Result<()> {
    let length = u64::try_from(payload.len()).context("frame payload exceeds u64")?;
    stream
        .write_all(&length.to_be_bytes())
        .and_then(|_| stream.write_all(payload))
        .and_then(|_| stream.flush())
        .context("write framed python rpc message")
}

fn read_frame(stream: &mut TcpStream) -> anyhow::Result<Vec<u8>> {
    let mut length_bytes = [0_u8; 8];
    stream
        .read_exact(&mut length_bytes)
        .context("read python rpc frame length")?;
    let length = u64::from_be_bytes(length_bytes);
    let mut payload = vec![0_u8; usize::try_from(length).context("python rpc frame too large")?];
    stream
        .read_exact(&mut payload)
        .context("read python rpc frame payload")?;
    Ok(payload)
}

fn join_pythonpath(entries: &[PathBuf]) -> anyhow::Result<Option<OsString>> {
    let mut all_entries = entries.to_vec();
    if let Some(existing) = std::env::var_os("PYTHONPATH") {
        all_entries.extend(std::env::split_paths(&existing));
    }
    if all_entries.is_empty() {
        return Ok(None);
    }
    Ok(Some(
        std::env::join_paths(all_entries).context("build python path for python worker")?,
    ))
}

#[derive(Clone, Debug, Serialize)]
struct RpcRequest<P> {
    id: u64,
    method: String,
    params: P,
}

#[derive(Clone, Debug, Deserialize)]
struct RpcResponse {
    id: u64,
    ok: bool,
    #[serde(default)]
    result: Option<Value>,
    #[serde(default)]
    error: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub(crate) struct HelloResponse {
    pub protocol_version: u32,
    pub workload_name: String,
}

#[derive(Clone, Debug, Deserialize)]
pub(crate) struct CapabilityProbeResponse {
    pub runtime_device: String,
    pub capability: CapabilityEstimate,
}

#[derive(Clone, Debug, Deserialize)]
struct ModelHandleResponse {
    model_id: String,
}

#[derive(Clone, Debug, Deserialize)]
struct OptionalModelHandleResponse {
    model_id: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
struct MetricsResponse {
    metrics: BTreeMap<String, MetricValue>,
}

#[derive(Clone, Debug, Deserialize)]
struct AckResponse {}

#[derive(Clone, Debug, Serialize)]
pub(crate) struct PythonMergeCandidateRef<'a> {
    pub peer_id: &'a str,
    pub head_id: &'a str,
    pub artifact_id: &'a str,
    pub model_id: &'a str,
    pub sample_weight: f64,
    pub quality_weight: f64,
}