candle-graph 0.10.0

TensorFlow Profiler-style execution graphs for candle-rs (trace-only)
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
//! Canonical capture-to-bundle publication with idempotent crash reconciliation.
//!
//! [`CaptureRun`] owns the whole path from "start capturing one planned profile
//! run" to "a deeply verified, atomically published evidence bundle exists at
//! the destination": staging-trace placement, session lifetime, evidence and
//! viewer derivation, manifest hashing, fsync, atomic rename, and a typed
//! [`PublicationReceipt`]. Applications never write evidence files or rename
//! directories themselves.
//!
//! Publication is idempotent across crashes: retrying the same planned capture
//! against a destination that already holds a verified bundle for the same run
//! coordinates returns [`PublicationStatus::AlreadyPublished`] with a fresh
//! verification receipt, while any other pre-existing destination fails closed
//! as a conflict.

use std::fs;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{bail, ensure, Context, Result};
use serde::{Deserialize, Serialize};

use crate::artifact::{publish_bundle, verify_bundle, BundleVerificationReceipt};
use crate::instrument::{ProfileRun, TraceSession};
use crate::trace::{parse_trace, TraceRunMeta};

pub const PUBLICATION_SCHEMA: &str = "candle-graph/publication/1";

/// Whether this receipt covers a fresh publication or a reconciled retry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PublicationStatus {
    Published,
    AlreadyPublished,
}

/// Typed proof that one planned capture is durably published and verified.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublicationReceipt {
    pub schema: String,
    pub status: PublicationStatus,
    pub bundle_path: PathBuf,
    pub run_id: String,
    pub verification: BundleVerificationReceipt,
}

/// Outcome of [`CaptureRun::begin`]: either the planned capture is already
/// durably published (crash-retry reconciliation) or an active capture run.
#[allow(clippy::large_enum_variant)]
pub enum CaptureBegin {
    AlreadyPublished(Box<PublicationReceipt>),
    Active(CaptureRun),
}

impl std::fmt::Debug for CaptureBegin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::AlreadyPublished(receipt) => {
                f.debug_tuple("AlreadyPublished").field(receipt).finish()
            }
            Self::Active(run) => f
                .debug_struct("Active")
                .field("destination", &run.destination)
                .field("staging_trace", &run.staging_trace)
                .finish(),
        }
    }
}

/// One planned capture from session open through atomic bundle publication.
pub struct CaptureRun {
    session: Option<TraceSession>,
    run: ProfileRun,
    destination: PathBuf,
    staging_trace: PathBuf,
    nsight_dir: Option<PathBuf>,
}

impl CaptureRun {
    /// Reconcile the destination and, when the planned capture is not yet
    /// published, open a staged trace session next to it.
    pub fn begin(destination: impl Into<PathBuf>, run: ProfileRun) -> Result<CaptureBegin> {
        let destination = destination.into();
        for ancestor in destination.ancestors().skip(1) {
            ensure!(
                !ancestor.join("bundle.json").is_file(),
                "refusing to publish bundle {} inside existing bundle {}",
                destination.display(),
                ancestor.display()
            );
        }
        if let Some(receipt) = reconcile_published_bundle(&destination, &run)? {
            return Ok(CaptureBegin::AlreadyPublished(Box::new(receipt)));
        }
        let parent = destination
            .parent()
            .filter(|parent| !parent.as_os_str().is_empty())
            .unwrap_or_else(|| Path::new("."));
        fs::create_dir_all(parent)
            .with_context(|| format!("create bundle parent {}", parent.display()))?;
        let name = destination
            .file_name()
            .and_then(|name| name.to_str())
            .context("bundle destination needs a file name")?;
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let staging_trace = parent.join(format!(
            ".{name}.trace-{}-{nonce}.jsonl",
            std::process::id()
        ));
        let session = TraceSession::open(&staging_trace, run.clone())?;
        Ok(CaptureBegin::Active(Self {
            session: Some(session),
            run,
            destination,
            staging_trace,
            nsight_dir: None,
        }))
    }

    /// The live trace session for spans, ops, stats, scalars, and gradients.
    pub fn session(&self) -> &TraceSession {
        self.session
            .as_ref()
            .expect("an active capture run owns its trace session")
    }

    /// Retain a directory of official Nsight artifacts inside the bundle.
    pub fn with_nsight_dir(mut self, nsight_dir: impl Into<PathBuf>) -> Self {
        self.nsight_dir = Some(nsight_dir.into());
        self
    }

    pub fn destination(&self) -> &Path {
        &self.destination
    }

    /// The staged trace path. It exists until publication succeeds; on failure
    /// it is retained for diagnosis.
    pub fn staging_trace(&self) -> &Path {
        &self.staging_trace
    }

    /// Finish the session as complete and atomically publish the bundle.
    pub fn publish(mut self) -> Result<PublicationReceipt> {
        let session = self
            .session
            .take()
            .expect("an active capture run owns its trace session");
        let trace = session.finish()?;
        self.finalize(&trace)
    }

    /// Finish the session as an explicit failed capture and still publish the
    /// diagnosable bundle, so crashed campaign steps stay queryable.
    pub fn publish_failed(mut self, reason: impl Into<String>) -> Result<PublicationReceipt> {
        let session = self
            .session
            .take()
            .expect("an active capture run owns its trace session");
        let trace = session.finish_failed(reason)?;
        self.finalize(&trace)
    }

    fn finalize(&mut self, trace: &Path) -> Result<PublicationReceipt> {
        // The destination may have appeared since `begin` (concurrent retry).
        if let Some(receipt) = reconcile_published_bundle(&self.destination, &self.run)? {
            let _ = fs::remove_file(trace);
            return Ok(receipt);
        }
        publish_bundle(&self.destination, trace, self.nsight_dir.as_deref()).with_context(
            || {
                format!(
                    "publish capture bundle {} (staged trace retained at {})",
                    self.destination.display(),
                    trace.display()
                )
            },
        )?;
        let verification = verify_bundle(&self.destination).with_context(|| {
            format!(
                "verify published capture bundle {}",
                self.destination.display()
            )
        })?;
        let _ = fs::remove_file(trace);
        Ok(PublicationReceipt {
            schema: PUBLICATION_SCHEMA.into(),
            status: PublicationStatus::Published,
            bundle_path: self.destination.clone(),
            run_id: verification.run_id.clone(),
            verification,
        })
    }
}

/// If the destination already holds a deeply verified bundle for the same
/// planned capture, return its receipt; a missing destination returns `None`;
/// anything else fails closed as a conflict.
///
/// Retry identity is the planned-run coordinates — entrypoint, correlation ID,
/// phase, capture step, and device — not byte equality, because a retried
/// capture legitimately re-records timings under a new run ID.
pub fn reconcile_published_bundle(
    destination: &Path,
    run: &ProfileRun,
) -> Result<Option<PublicationReceipt>> {
    if !destination.exists() {
        return Ok(None);
    }
    let verification = verify_bundle(destination).with_context(|| {
        format!(
            "existing destination {} is not a verifiable evidence bundle; refusing to reuse or overwrite it",
            destination.display()
        )
    })?;
    let trace_path = destination.join("trace.jsonl");
    let document = parse_trace(&trace_path)
        .with_context(|| format!("parse published bundle trace {}", trace_path.display()))?;
    ensure!(
        verification.run_id == document.run.run_id,
        "published bundle manifest run ID {:?} does not match its trace run ID {:?}",
        verification.run_id,
        document.run.run_id
    );
    ensure_same_planned_capture(&document.run, run, destination)?;
    Ok(Some(PublicationReceipt {
        schema: PUBLICATION_SCHEMA.into(),
        status: PublicationStatus::AlreadyPublished,
        bundle_path: destination.to_path_buf(),
        run_id: verification.run_id.clone(),
        verification,
    }))
}

fn ensure_same_planned_capture(
    published: &TraceRunMeta,
    run: &ProfileRun,
    destination: &Path,
) -> Result<()> {
    let mismatches = [
        ("entrypoint", published.entrypoint != run.entrypoint),
        (
            "correlation_id",
            published.correlation_id != run.correlation_id,
        ),
        ("phase", published.phase != run.phase),
        ("capture_step", published.capture_step != run.capture_step),
        ("device", published.device != run.device),
    ]
    .into_iter()
    .filter_map(|(field, differs)| differs.then_some(field))
    .collect::<Vec<_>>();
    if !mismatches.is_empty() {
        bail!(
            "bundle destination {} already holds a different planned capture (conflicting {})",
            destination.display(),
            mismatches.join(", ")
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::trace_cli::load_evidence;
    use crate::trace::RunOutcome;

    fn temp_root(name: &str) -> PathBuf {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = std::env::temp_dir().join(format!(
            "candle-graph-publication-{name}-{}-{nonce}",
            std::process::id()
        ));
        fs::create_dir_all(&root).unwrap();
        root
    }

    fn active(begin: CaptureBegin) -> CaptureRun {
        match begin {
            CaptureBegin::Active(run) => run,
            CaptureBegin::AlreadyPublished(receipt) => {
                panic!("expected an active capture run, got {:?}", receipt.status)
            }
        }
    }

    #[test]
    fn capture_run_publishes_verified_bundle_and_cleans_staging() {
        let root = temp_root("publish");
        let destination = root.join("profiles/update-2");
        let run = ProfileRun::training("train::update", 2, "cpu");
        let capture = active(CaptureRun::begin(&destination, run.clone()).unwrap());
        let staging = capture.staging_trace().to_path_buf();
        {
            let session = capture.session();
            let measured = session.begin_measurement("update");
            session
                .record_scalar(measured.id(), "loss/total", 0.75)
                .unwrap();
        }
        let receipt = capture.publish().unwrap();
        assert_eq!(receipt.status, PublicationStatus::Published);
        assert_eq!(receipt.schema, PUBLICATION_SCHEMA);
        assert_eq!(receipt.run_id, receipt.verification.run_id);
        assert!(destination.join("bundle.json").is_file());
        assert!(!staging.exists(), "staging trace must be removed");

        let evidence = load_evidence(&destination).unwrap();
        assert_eq!(evidence.provenance.run_id, receipt.run_id);
        assert_eq!(evidence.tensor_stats.len(), 1);

        // Crash-retry: the same planned capture reconciles without recapturing.
        let retry = CaptureRun::begin(&destination, run).unwrap();
        match retry {
            CaptureBegin::AlreadyPublished(reconciled) => {
                assert_eq!(reconciled.status, PublicationStatus::AlreadyPublished);
                assert_eq!(reconciled.run_id, receipt.run_id);
                assert_eq!(
                    reconciled.verification.manifest_sha256,
                    receipt.verification.manifest_sha256
                );
            }
            CaptureBegin::Active(_) => panic!("expected reconciliation"),
        }

        // A different planned capture at the same destination is a conflict.
        let conflict = CaptureRun::begin(
            &destination,
            ProfileRun::training("train::update", 3, "cpu"),
        )
        .unwrap_err();
        assert!(conflict.to_string().contains("conflicting"));
        assert!(conflict.to_string().contains("capture_step"));

        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn non_bundle_destination_fails_closed() {
        let root = temp_root("conflict");
        let destination = root.join("profiles/update-2");
        fs::create_dir_all(&destination).unwrap();
        fs::write(destination.join("application.jsonl"), b"not a bundle").unwrap();
        let error = CaptureRun::begin(
            &destination,
            ProfileRun::training("train::update", 2, "cpu"),
        )
        .unwrap_err();
        assert!(error
            .to_string()
            .contains("not a verifiable evidence bundle"));
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn failed_captures_publish_diagnosable_verified_bundles() {
        let root = temp_root("failed");
        let destination = root.join("update-7");
        let run = ProfileRun::training("train::update", 7, "cpu");
        let capture = active(CaptureRun::begin(&destination, run.clone()).unwrap());
        let receipt = capture.publish_failed("loss became non-finite").unwrap();
        assert_eq!(receipt.status, PublicationStatus::Published);
        verify_bundle(&destination).unwrap();
        let document = parse_trace(destination.join("trace.jsonl")).unwrap();
        assert_eq!(document.terminal.outcome, RunOutcome::Failed);
        assert_eq!(
            document.terminal.reason.as_deref(),
            Some("loss became non-finite")
        );

        // Failed publications also reconcile instead of recapturing.
        match CaptureRun::begin(&destination, run).unwrap() {
            CaptureBegin::AlreadyPublished(reconciled) => {
                assert_eq!(reconciled.run_id, receipt.run_id);
            }
            CaptureBegin::Active(_) => panic!("expected reconciliation"),
        }
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn nested_bundle_destinations_are_rejected() {
        let root = temp_root("nested");
        let outer = root.join("outer");
        let capture = active(
            CaptureRun::begin(&outer, ProfileRun::training("train::update", 1, "cpu")).unwrap(),
        );
        drop(capture.session().begin_measurement("update"));
        capture.publish().unwrap();
        let error = CaptureRun::begin(
            outer.join("inner"),
            ProfileRun::training("train::update", 2, "cpu"),
        )
        .unwrap_err();
        assert!(error.to_string().contains("inside existing bundle"));
        fs::remove_dir_all(root).unwrap();
    }
}